#define _(String) (String)
#define N_(String) (String)
#define MAXSTRING 256
+<<<<<<< .mine
+=======
#define Error_abort0(fmd) generic_error_out(__FILE__, __LINE__, fmd)
+>>>>>>> .r733
#define MODULE_NAME "apcups"
/* Prototypes */
+<<<<<<< .mine
+=======
static void generic_error_out(const char *, int , const char *, ...);
+>>>>>>> .r733
/* Default values for contacting daemon */
static char *host = "localhost";
static char *net_errmsg = NULL; /* pointer to error message */
static char net_errbuf[256]; /* error message buffer for messages */
+<<<<<<< .mine
+struct sockaddr_in tcp_serv_addr; /* socket information */
+int net_errno = 0; /* error number -- not yet implemented */
+char *net_errmsg = NULL; /* pointer to error message */
+char net_errbuf[256]; /* error message buffer for messages */
+
+void (*error_cleanup) (void) = NULL;
+
+=======
+>>>>>>> .r733
/*
* The following are only if not compiled to test the module with its own main.
*/
#endif /* ifndef APCMAIN */
+<<<<<<< .mine
+struct apc_detail_s {
+ int connected;
+ float linev;
+ float loadpct;
+ float bcharge;
+ float timeleft;
+ float outputv;
+ float itemp;
+ float battv;
+ float linefreq;
+=======
struct apc_detail_s
{
float linev;
float itemp;
float battv;
float linefreq;
+>>>>>>> .r733
};
#define BIG_BUF 4096
+<<<<<<< .mine
+
+#define BIG_BUF 5000
+
+/* Implement snprintf */
+int asnprintf(char *str, size_t size, const char *fmt, ...)
+{
+#ifdef HAVE_VSNPRINTF
+ va_list arg_ptr;
+ int len;
+
+ va_start(arg_ptr, fmt);
+ len = vsnprintf(str, size, fmt, arg_ptr);
+ va_end(arg_ptr);
+
+ str[size - 1] = 0;
+ return len;
+
+#else
+
+ va_list arg_ptr;
+ int len;
+ char *buf;
+
+ buf = (char *)malloc(BIG_BUF);
+
+ va_start(arg_ptr, fmt);
+ len = vsprintf(buf, fmt, arg_ptr);
+ va_end(arg_ptr);
+
+ if (len >= BIG_BUF){
+ syslog(LOG_ERR, "apcups: asnprintf(): Buffer overflow");
+ return(0);
+ }
+
+ memcpy(str, buf, size);
+ str[size - 1] = 0;
+
+ free(buf);
+ return len;
+#endif
+}
+
+/* Implement vsnprintf() */
+int avsnprintf(char *str, size_t size, const char *format, va_list ap)
+{
+#ifdef HAVE_VSNPRINTF
+ int len;
+
+ len = vsnprintf(str, size, format, ap);
+ str[size - 1] = 0;
+
+ return len;
+
+#else
+
+ int len;
+ char *buf;
+
+ buf = (char *)malloc(BIG_BUF);
+
+ len = vsprintf(buf, format, ap);
+ if (len >= BIG_BUF){
+ syslog(LOG_ERR, "apcups: avsnprintf(): Buffer overflow");
+ return(0);
+ }
+
+ memcpy(str, buf, size);
+ str[size - 1] = 0;
+
+ free(buf);
+ return len;
+#endif
+}
+
+=======
+>>>>>>> .r733
/*
+<<<<<<< .mine
+=======
* Subroutine normally called by macro error_abort() to print
* FATAL ERROR message and supplied error message
*/
}
/*
+>>>>>>> .r733
* Read nbytes from the network.
* It is possible that the total bytes require in several
* read requests
}
/* Close the network connection */
-void net_close (int sockfd)
+static void net_close (int sockfd)
{
short pktsiz = 0;
* Returns -1 on error
* Returns socket file descriptor otherwise
*/
-int net_open(char *host, char *service, int port)
+static int net_open(char *host, char *service, int port)
{
int sockfd;
struct hostent *hp;
* Returns -1 on hard end of file (i.e. network connection close)
* Returns -2 on error
*/
-int net_recv(int sockfd, char *buff, int maxlen)
+static int net_recv(int sockfd, char *buff, int maxlen)
{
- int nbytes;
- short pktsiz;
+ int nbytes;
+ short pktsiz;
- /* get data size -- in short */
- if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
- /* probably pipe broken because client died */
- return -1; /* assume hard EOF received */
- }
- if (nbytes != sizeof(short))
- return -2;
+ /* get data size -- in short */
+ if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
+ /* probably pipe broken because client died */
+ return -1; /* assume hard EOF received */
+ }
+ if (nbytes != sizeof(short))
+ return -2;
- pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
- if (pktsiz > maxlen) {
- net_errmsg = "net_recv: record length too large\n";
- return -2;
- }
- if (pktsiz == 0)
- return 0; /* soft EOF */
+ pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
+ if (pktsiz > maxlen) {
+ net_errmsg = "net_recv: record length too large\n";
+ return -2;
+ }
+ if (pktsiz == 0)
+ return 0; /* soft EOF */
- /* now read the actual data */
- if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
- net_errmsg = "net_recv: read_nbytes error\n";
- return -2;
- }
- if (nbytes != pktsiz) {
- net_errmsg = "net_recv: error in read_nbytes\n";
- return -2;
- }
+ /* now read the actual data */
+ if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
+ net_errmsg = "net_recv: read_nbytes error\n";
+ return -2;
+ }
+ if (nbytes != pktsiz) {
+ net_errmsg = "net_recv: error in read_nbytes\n";
+ return -2;
+ }
- return (nbytes); /* return actual length of message */
+ return (nbytes); /* return actual length of message */
}
/*
* Returns number of bytes sent
* Returns -1 on error
*/
-int net_send(int sockfd, char *buff, int len)
+static int net_send(int sockfd, char *buff, int len)
{
- int rc;
- short pktsiz;
+ int rc;
+ short pktsiz;
- /* send short containing size of data packet */
- pktsiz = htons((short)len);
- rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
- if (rc != sizeof(short)) {
- net_errmsg = "net_send: write_nbytes error of length prefix\n";
- return -1;
- }
+ /* send short containing size of data packet */
+ pktsiz = htons((short)len);
+ rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
+ if (rc != sizeof(short)) {
+ net_errmsg = "net_send: write_nbytes error of length prefix\n";
+ return -1;
+ }
- /* send data packet */
- rc = write_nbytes(sockfd, buff, len);
- if (rc != len) {
- net_errmsg = "net_send: write_nbytes error\n";
- return -1;
- }
+ /* send data packet */
+ rc = write_nbytes(sockfd, buff, len);
+ if (rc != len) {
+ net_errmsg = "net_send: write_nbytes error\n";
+ return -1;
+ }
- return rc;
+ return rc;
}
/* Get and print status from apcupsd NIS server */
-static void do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
+static int do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
{
+<<<<<<< .mine
int sockfd, n;
char recvline[MAXSTRING + 1];
char *tokptr;
- if ((sockfd = net_open(host, NULL, port)) < 0)
- Error_abort0(net_errmsg);
+ if ((sockfd = net_open(host, NULL, port)) < 0){
+ syslog(LOG_ERR, "apcups: Cannot open connection: %s",
+ net_errmsg);
+ net_errmsg = NULL;
+ return;
+ }
net_send(sockfd, "status", 6);
recvline[n] = 0;
#ifdef APCMAIN
fputs(recvline, stdout);
- int printit = 1;
-#else
- int printit = 0;
#endif /* ifdef APCMAIN */
+=======
+ int sockfd;
+ int n;
+ char recvline[MAXSTRING + 1];
+ char *tokptr;
+ char *key;
+ double value;
+#if APCMAIN
+# define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
+#else
+# define PRINT_VALUE(name, val) /**/
+#endif
+>>>>>>> .r733
+<<<<<<< .mine
tokptr = strtok(recvline,":");
while(tokptr != NULL) {
/* Look for Limit_Add */
if(strncmp("LINEV",tokptr,5) == 0) {
- if(printit) fprintf(stdout," Found LINEV.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found LINEV.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->linev = atof (tokptr);
}else if(strncmp("BATTV",tokptr,5) == 0) {
- if(printit) fprintf(stdout," Found BATTV.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found BATTV.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->battv = atof (tokptr);
}else if(strncmp("ITEMP",tokptr,5) == 0) {
- if(printit) fprintf(stdout," Found ITEMP.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found ITEMP.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->itemp = atof (tokptr);
}else if(strncmp("LOADPCT",tokptr,7) == 0) {
- if(printit) fprintf(stdout," Found LOADPCT.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found LOADPCT.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->loadpct = atof (tokptr);
}else if(strncmp("BCHARGE",tokptr,7) == 0) {
- if(printit) fprintf(stdout," Found BCHARGE.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found BCHARGE.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->bcharge = atof (tokptr);
}else if(strncmp("OUTPUTV",tokptr,7) == 0) {
- if(printit) fprintf(stdout," Found OUTPUTV.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found OUTPUTV.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->outputv = atof (tokptr);
}else if(strncmp("LINEFREQ",tokptr,8) == 0) {
- if(printit) fprintf(stdout," Found LINEFREQ.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found LINEFREQ.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->linefreq = atof (tokptr);
}else if(strncmp("TIMELEFT",tokptr,8) == 0) {
- if(printit) fprintf(stdout," Found TIMELEFT.\n");
+#ifdef APCMAIN
+ fprintf(stdout," Found TIMELEFT.\n");
+#endif /* ifdef APCMAIN */
tokptr = strtok(NULL," \t");
if(tokptr == NULL) continue;
- if(printit) fprintf(stdout," Value %s.\n",tokptr);
apcups_detail->timeleft = atof (tokptr);
} /* */
tokptr = strtok(NULL,":");
}
}
+=======
+ /* TODO: Keep the socket open, if possible */
+ if ((sockfd = net_open (host, NULL, port)) < 0)
+ {
+ syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
+ return (-1);
+ }
+>>>>>>> .r733
- if (n < 0)
- Error_abort0(net_errmsg);
+<<<<<<< .mine
+ if (n < 0) {
+ syslog(LOG_ERR, "apcups: Error recieving data: %s",
+ net_errmsg);
+ net_errmsg = NULL;
+ return;
+ }
+ /* signal that we did in fact connect. */
+ apcups_detail->connected = 1;
net_close(sockfd);
+=======
+ net_send (sockfd, "status", 6);
+
+ while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
+ {
+ recvline[n] = '\0';
+#if APCMAIN
+ printf ("net_recv = `%s';\n", recvline);
+#endif /* if APCMAIN */
+
+ tokptr = strtok (recvline, ":");
+ while (tokptr != NULL)
+ {
+ key = tokptr;
+ if ((tokptr = strtok (NULL, " \t")) == NULL)
+ continue;
+ value = atof (tokptr);
+ PRINT_VALUE (key, value);
+
+ if (strcmp ("LINEV", key) == 0)
+ apcups_detail->linev = value;
+ else if (strcmp ("BATTV", tokptr) == 0)
+ apcups_detail->battv = value;
+ else if (strcmp ("ITEMP", tokptr) == 0)
+ apcups_detail->itemp = value;
+ else if (strcmp ("LOADPCT", tokptr) == 0)
+ apcups_detail->loadpct = value;
+ else if (strcmp ("BCHARGE", tokptr) == 0)
+ apcups_detail->bcharge = value;
+ else if (strcmp ("OUTPUTV", tokptr) == 0)
+ apcups_detail->outputv = value;
+ else if (strcmp ("LINEFREQ", tokptr) == 0)
+ apcups_detail->linefreq = value;
+ else if (strcmp ("TIMELEFT", tokptr) == 0)
+ apcups_detail->timeleft = value;
+ else
+ {
+ syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
+ key, value);
+ }
+
+ tokptr = strtok (NULL, ":");
+ } /* while (tokptr != NULL) */
+ }
+
+ if (n < 0)
+ Error_abort0(net_errmsg);
+
+ net_close(sockfd);
+
+ return (0);
+>>>>>>> .r733
}
#ifdef APCMAIN
int main(int argc, char **argv)
{
- /* we are not really going to use this */
- struct apc_detail_s apcups_detail;
+ /* we are not really going to use this */
+ struct apc_detail_s apcups_detail;
+
+<<<<<<< .mine
+ openlog("apcups",LOG_PID | LOG_NDELAY | LOG_LOCAL1);
if (!*host || strcmp(host, "0.0.0.0") == 0)
host = "localhost";
do_pthreads_status(host, port, &apcups_detail);
return 0;
+=======
+ if (!*host || strcmp(host, "0.0.0.0") == 0)
+ host = "localhost";
+
+ do_pthreads_status(host, port, &apcups_detail);
+
+ return 0;
+>>>>>>> .r733
}
#else
static void apcups_init (void)
apcups_detail.itemp = 0.0;
apcups_detail.battv = 0.0;
apcups_detail.linefreq = 0.0;
+ apcups_detail.connected = 0;
if (!*host || strcmp(host, "0.0.0.0") == 0)
do_pthreads_status(host, port, &apcups_detail);
+ /*
+ * if we did not connect then do not bother submitting
+ * zeros. We want rrd files to have NAN.
+ */
+ if(!apcups_detail.connected) return;
+
apcups_submit (host, &apcups_detail);
apc_bvolt_submit (host, &apcups_detail);
apc_load_submit (host, &apcups_detail);