2 * collectd - src/apcups.c
3 * Copyright (C) 2006 Anthony Gialluca <tonyabg at charter.net>
4 * Copyright (C) 2000-2004 Kern Sibbald
5 * Copyright (C) 1996-99 Andre M. Hedrick <andre at suse.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of version 2 of the GNU General
9 * Public License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * Anthony Gialluca <tonyabg at charter.net>
26 #include "common.h" /* rrd_update_file */
27 #include "plugin.h" /* plugin_register, plugin_submit */
28 #include "configfile.h" /* cf_register */
30 /* FIXME: Check defines before including anything! */
46 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
59 #define _(String) (String)
60 #define N_(String) (String)
64 #define Error_abort0(fmd) generic_error_out(__FILE__, __LINE__, fmd)
66 #define MODULE_NAME "apcups"
72 static void generic_error_out(const char *, int , const char *, ...);
75 /* Default values for contacting daemon */
76 static char *host = "localhost";
77 static int port = NISPORT;
79 static struct sockaddr_in tcp_serv_addr; /* socket information */
80 static int net_errno = 0; /* error number -- not yet implemented */
81 static char *net_errmsg = NULL; /* pointer to error message */
82 static char net_errbuf[256]; /* error message buffer for messages */
85 struct sockaddr_in tcp_serv_addr; /* socket information */
86 int net_errno = 0; /* error number -- not yet implemented */
87 char *net_errmsg = NULL; /* pointer to error message */
88 char net_errbuf[256]; /* error message buffer for messages */
90 void (*error_cleanup) (void) = NULL;
95 * The following are only if not compiled to test the module with its own main.
97 /* FIXME: Rename DSes to be more generic and follow established conventions. */
99 static char *volt_file_template = "apcups_volt-%s.rrd";
100 static char *volt_ds_def[] =
102 "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
103 "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
106 static int volt_ds_num = 2;
108 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
109 static char *bvolt_ds_def[] =
111 "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
113 static int bvolt_ds_num = 1;
115 static char *load_file_template = "apcups_load-%s.rrd";
116 static char *load_ds_def[] =
118 "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
120 static int load_ds_num = 1;
122 static char *charge_file_template = "apcups_charge-%s.rrd";
123 static char *charge_ds_def[] =
125 "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
127 static int charge_ds_num = 1;
129 static char *time_file_template = "apcups_time-%s.rrd";
130 static char *time_ds_def[] =
132 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
134 static int time_ds_num = 1;
136 static char *temp_file_template = "apcups_temp-%s.rrd";
137 static char *temp_ds_def[] =
139 "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
141 static int temp_ds_num = 1;
143 static char *freq_file_template = "apcups_freq-%s.rrd";
144 static char *freq_ds_def[] =
146 "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
148 static int freq_ds_num = 1;
150 static char *config_keys[] =
156 static int config_keys_num = 2;
158 #endif /* ifndef APCMAIN */
161 struct apc_detail_s {
191 /* Implement snprintf */
192 int asnprintf(char *str, size_t size, const char *fmt, ...)
194 #ifdef HAVE_VSNPRINTF
198 va_start(arg_ptr, fmt);
199 len = vsnprintf(str, size, fmt, arg_ptr);
211 buf = (char *)malloc(BIG_BUF);
213 va_start(arg_ptr, fmt);
214 len = vsprintf(buf, fmt, arg_ptr);
218 syslog(LOG_ERR, "apcups: asnprintf(): Buffer overflow");
222 memcpy(str, buf, size);
230 /* Implement vsnprintf() */
231 int avsnprintf(char *str, size_t size, const char *format, va_list ap)
233 #ifdef HAVE_VSNPRINTF
236 len = vsnprintf(str, size, format, ap);
246 buf = (char *)malloc(BIG_BUF);
248 len = vsprintf(buf, format, ap);
250 syslog(LOG_ERR, "apcups: avsnprintf(): Buffer overflow");
254 memcpy(str, buf, size);
267 * Subroutine normally called by macro error_abort() to print
268 * FATAL ERROR message and supplied error message
270 static void generic_error_out(const char *file, int line, const char *fmt, ...)
276 snprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line);
278 va_start(arg_ptr, fmt);
279 vsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr);
281 fprintf(stdout, "%s", buf);
288 * Read nbytes from the network.
289 * It is possible that the total bytes require in several
292 static int read_nbytes(int fd, char *ptr, int nbytes)
300 nread = read(fd, ptr, nleft);
301 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
305 return (nread); /* error, or EOF */
312 return (nbytes - nleft); /* return >= 0 */
316 * Write nbytes to the network.
317 * It may require several writes.
319 static int write_nbytes(int fd, char *ptr, int nbytes)
325 nwritten = write(fd, ptr, nleft);
329 return (nwritten); /* error */
336 return (nbytes - nleft);
339 /* Close the network connection */
340 static void net_close (int sockfd)
344 /* send EOF sentinel */
345 write_nbytes(sockfd, (char *) &pktsiz, sizeof(short));
351 * Open a TCP connection to the UPS network server
352 * Returns -1 on error
353 * Returns socket file descriptor otherwise
355 static int net_open(char *host, char *service, int port)
359 unsigned int inaddr; /* Careful here to use unsigned int for */
360 /* compatibility with Alpha */
363 * Fill in the structure serv_addr with the address of the server that
364 * we want to connect with.
366 memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
367 tcp_serv_addr.sin_family = AF_INET;
368 tcp_serv_addr.sin_port = htons(port);
370 if ((inaddr = inet_addr(host)) != INADDR_NONE) {
371 tcp_serv_addr.sin_addr.s_addr = inaddr;
373 if ((hp = gethostbyname(host)) == NULL) {
374 net_errmsg = "tcp_open: hostname error\n";
378 if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
379 net_errmsg = "tcp_open: funny gethostbyname value\n";
383 tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
386 /* Open a TCP socket */
387 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
388 net_errmsg = "tcp_open: cannot open stream socket\n";
392 /* connect to server */
393 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
395 * Work around a bug in OpenBSD & FreeBSD userspace pthreads
396 * implementations. Rationale is the same as described above.
398 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
401 if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
402 snprintf(net_errbuf, sizeof(net_errbuf),
403 _("tcp_open: cannot connect to server %s on port %d.\n"
404 "ERR=%s\n"), host, port, strerror(errno));
405 net_errmsg = net_errbuf;
411 } /* int net_open(char *host, char *service, int port) */
414 * Receive a message from the other end. Each message consists of
415 * two packets. The first is a header that contains the size
416 * of the data that follows in the second packet.
417 * Returns number of bytes read
418 * Returns 0 on end of file
419 * Returns -1 on hard end of file (i.e. network connection close)
420 * Returns -2 on error
422 static int net_recv(int sockfd, char *buff, int maxlen)
427 /* get data size -- in short */
428 if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
429 /* probably pipe broken because client died */
430 return -1; /* assume hard EOF received */
432 if (nbytes != sizeof(short))
435 pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
436 if (pktsiz > maxlen) {
437 net_errmsg = "net_recv: record length too large\n";
441 return 0; /* soft EOF */
443 /* now read the actual data */
444 if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
445 net_errmsg = "net_recv: read_nbytes error\n";
448 if (nbytes != pktsiz) {
449 net_errmsg = "net_recv: error in read_nbytes\n";
453 return (nbytes); /* return actual length of message */
457 * Send a message over the network. The send consists of
458 * two network packets. The first is sends a short containing
459 * the length of the data packet which follows.
460 * Returns number of bytes sent
461 * Returns -1 on error
463 static int net_send(int sockfd, char *buff, int len)
468 /* send short containing size of data packet */
469 pktsiz = htons((short)len);
470 rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
471 if (rc != sizeof(short)) {
472 net_errmsg = "net_send: write_nbytes error of length prefix\n";
476 /* send data packet */
477 rc = write_nbytes(sockfd, buff, len);
479 net_errmsg = "net_send: write_nbytes error\n";
487 /* Get and print status from apcupsd NIS server */
488 static int do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
492 char recvline[MAXSTRING + 1];
495 if ((sockfd = net_open(host, NULL, port)) < 0){
496 syslog(LOG_ERR, "apcups: Cannot open connection: %s",
502 net_send(sockfd, "status", 6);
504 while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
507 fputs(recvline, stdout);
508 #endif /* ifdef APCMAIN */
512 char recvline[MAXSTRING + 1];
517 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
519 # define PRINT_VALUE(name, val) /**/
524 tokptr = strtok(recvline,":");
525 while(tokptr != NULL) {
526 /* Look for Limit_Add */
527 if(strncmp("LINEV",tokptr,5) == 0) {
529 fprintf(stdout," Found LINEV.\n");
530 #endif /* ifdef APCMAIN */
531 tokptr = strtok(NULL," \t");
532 if(tokptr == NULL) continue;
533 apcups_detail->linev = atof (tokptr);
534 }else if(strncmp("BATTV",tokptr,5) == 0) {
536 fprintf(stdout," Found BATTV.\n");
537 #endif /* ifdef APCMAIN */
538 tokptr = strtok(NULL," \t");
539 if(tokptr == NULL) continue;
540 apcups_detail->battv = atof (tokptr);
541 }else if(strncmp("ITEMP",tokptr,5) == 0) {
543 fprintf(stdout," Found ITEMP.\n");
544 #endif /* ifdef APCMAIN */
545 tokptr = strtok(NULL," \t");
546 if(tokptr == NULL) continue;
547 apcups_detail->itemp = atof (tokptr);
548 }else if(strncmp("LOADPCT",tokptr,7) == 0) {
550 fprintf(stdout," Found LOADPCT.\n");
551 #endif /* ifdef APCMAIN */
552 tokptr = strtok(NULL," \t");
553 if(tokptr == NULL) continue;
554 apcups_detail->loadpct = atof (tokptr);
555 }else if(strncmp("BCHARGE",tokptr,7) == 0) {
557 fprintf(stdout," Found BCHARGE.\n");
558 #endif /* ifdef APCMAIN */
559 tokptr = strtok(NULL," \t");
560 if(tokptr == NULL) continue;
561 apcups_detail->bcharge = atof (tokptr);
562 }else if(strncmp("OUTPUTV",tokptr,7) == 0) {
564 fprintf(stdout," Found OUTPUTV.\n");
565 #endif /* ifdef APCMAIN */
566 tokptr = strtok(NULL," \t");
567 if(tokptr == NULL) continue;
568 apcups_detail->outputv = atof (tokptr);
569 }else if(strncmp("LINEFREQ",tokptr,8) == 0) {
571 fprintf(stdout," Found LINEFREQ.\n");
572 #endif /* ifdef APCMAIN */
573 tokptr = strtok(NULL," \t");
574 if(tokptr == NULL) continue;
575 apcups_detail->linefreq = atof (tokptr);
576 }else if(strncmp("TIMELEFT",tokptr,8) == 0) {
578 fprintf(stdout," Found TIMELEFT.\n");
579 #endif /* ifdef APCMAIN */
580 tokptr = strtok(NULL," \t");
581 if(tokptr == NULL) continue;
582 apcups_detail->timeleft = atof (tokptr);
584 tokptr = strtok(NULL,":");
588 /* TODO: Keep the socket open, if possible */
589 if ((sockfd = net_open (host, NULL, port)) < 0)
591 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
598 syslog(LOG_ERR, "apcups: Error recieving data: %s",
603 /* signal that we did in fact connect. */
604 apcups_detail->connected = 1;
608 net_send (sockfd, "status", 6);
610 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
614 printf ("net_recv = `%s';\n", recvline);
615 #endif /* if APCMAIN */
617 tokptr = strtok (recvline, ":");
618 while (tokptr != NULL)
621 if ((tokptr = strtok (NULL, " \t")) == NULL)
623 value = atof (tokptr);
624 PRINT_VALUE (key, value);
626 if (strcmp ("LINEV", key) == 0)
627 apcups_detail->linev = value;
628 else if (strcmp ("BATTV", tokptr) == 0)
629 apcups_detail->battv = value;
630 else if (strcmp ("ITEMP", tokptr) == 0)
631 apcups_detail->itemp = value;
632 else if (strcmp ("LOADPCT", tokptr) == 0)
633 apcups_detail->loadpct = value;
634 else if (strcmp ("BCHARGE", tokptr) == 0)
635 apcups_detail->bcharge = value;
636 else if (strcmp ("OUTPUTV", tokptr) == 0)
637 apcups_detail->outputv = value;
638 else if (strcmp ("LINEFREQ", tokptr) == 0)
639 apcups_detail->linefreq = value;
640 else if (strcmp ("TIMELEFT", tokptr) == 0)
641 apcups_detail->timeleft = value;
644 syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
648 tokptr = strtok (NULL, ":");
649 } /* while (tokptr != NULL) */
653 Error_abort0(net_errmsg);
662 int main(int argc, char **argv)
664 /* we are not really going to use this */
665 struct apc_detail_s apcups_detail;
668 openlog("apcups",LOG_PID | LOG_NDELAY | LOG_LOCAL1);
670 if (!*host || strcmp(host, "0.0.0.0") == 0)
673 do_pthreads_status(host, port, &apcups_detail);
677 if (!*host || strcmp(host, "0.0.0.0") == 0)
680 do_pthreads_status(host, port, &apcups_detail);
686 static void apcups_init (void)
691 static int apcups_config (char *key, char *value)
693 static char lhost[126];
695 if (strcasecmp (key, "host") == 0)
701 else if (strcasecmp (key, "Port") == 0)
703 int port_tmp = atoi (value);
704 if(port_tmp < 1 || port_tmp > 65535) {
705 syslog (LOG_WARNING, "apcups: `port' failed: %s",
720 static void apcups_submit (char *host,
721 struct apc_detail_s *apcups_detail)
725 if (snprintf (buf, BUFSIZE, "%u:%f:%f",
726 (unsigned int) curtime,
727 apcups_detail->linev,
728 apcups_detail->outputv) >= BUFSIZE)
731 plugin_submit (MODULE_NAME, host, buf);
734 static void apc_bvolt_submit (char *host,
735 struct apc_detail_s *apcups_detail)
739 if (snprintf (buf, BUFSIZE, "%u:%f",
740 (unsigned int) curtime,
741 apcups_detail->battv) >= BUFSIZE)
744 plugin_submit ("apcups_bvolt", host, buf);
747 static void apc_load_submit (char *host,
748 struct apc_detail_s *apcups_detail)
752 if (snprintf (buf, BUFSIZE, "%u:%f",
753 (unsigned int) curtime,
754 apcups_detail->loadpct) >= BUFSIZE)
757 plugin_submit ("apcups_load", host, buf);
760 static void apc_charge_submit (char *host,
761 struct apc_detail_s *apcups_detail)
765 if (snprintf (buf, BUFSIZE, "%u:%f",
766 (unsigned int) curtime,
767 apcups_detail->bcharge) >= BUFSIZE)
770 plugin_submit ("apcups_charge", host, buf);
773 static void apc_temp_submit (char *host,
774 struct apc_detail_s *apcups_detail)
778 if (snprintf (buf, BUFSIZE, "%u:%f",
779 (unsigned int) curtime,
780 apcups_detail->itemp) >= BUFSIZE)
783 plugin_submit ("apcups_temp", host, buf);
786 static void apc_time_submit (char *host,
787 struct apc_detail_s *apcups_detail)
791 if (snprintf (buf, BUFSIZE, "%u:%f",
792 (unsigned int) curtime,
793 apcups_detail->timeleft) >= BUFSIZE)
796 plugin_submit ("apcups_time", host, buf);
799 static void apc_freq_submit (char *host,
800 struct apc_detail_s *apcups_detail)
804 if (snprintf (buf, BUFSIZE, "%u:%f",
805 (unsigned int) curtime,
806 apcups_detail->linefreq) >= BUFSIZE)
809 plugin_submit ("apcups_freq", host, buf);
813 static void apcups_read (void)
815 struct apc_detail_s apcups_detail;
817 apcups_detail.linev = 0.0;
818 apcups_detail.loadpct = 0.0;
819 apcups_detail.bcharge = 0.0;
820 apcups_detail.timeleft = 0.0;
821 apcups_detail.outputv = 0.0;
822 apcups_detail.itemp = 0.0;
823 apcups_detail.battv = 0.0;
824 apcups_detail.linefreq = 0.0;
825 apcups_detail.connected = 0;
828 if (!*host || strcmp(host, "0.0.0.0") == 0)
831 do_pthreads_status(host, port, &apcups_detail);
834 * if we did not connect then do not bother submitting
835 * zeros. We want rrd files to have NAN.
837 if(!apcups_detail.connected) return;
839 apcups_submit (host, &apcups_detail);
840 apc_bvolt_submit (host, &apcups_detail);
841 apc_load_submit (host, &apcups_detail);
842 apc_charge_submit (host, &apcups_detail);
843 apc_temp_submit (host, &apcups_detail);
844 apc_time_submit (host, &apcups_detail);
845 apc_freq_submit (host, &apcups_detail);
849 static void apcups_write (char *host, char *inst, char *val)
854 status = snprintf (file, 512, volt_file_template, inst);
857 else if (status >= 512)
860 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
863 static void apc_bvolt_write (char *host, char *inst, char *val)
868 status = snprintf (file, 512, bvolt_file_template, inst);
871 else if (status >= 512)
874 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
877 static void apc_load_write (char *host, char *inst, char *val)
882 status = snprintf (file, 512, load_file_template, inst);
885 else if (status >= 512)
888 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
891 static void apc_charge_write (char *host, char *inst, char *val)
896 status = snprintf (file, 512, charge_file_template, inst);
899 else if (status >= 512)
902 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
905 static void apc_temp_write (char *host, char *inst, char *val)
910 status = snprintf (file, 512, temp_file_template, inst);
913 else if (status >= 512)
916 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
919 static void apc_time_write (char *host, char *inst, char *val)
924 status = snprintf (file, 512, time_file_template, inst);
927 else if (status >= 512)
930 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
933 static void apc_freq_write (char *host, char *inst, char *val)
938 status = snprintf (file, 512, freq_file_template, inst);
941 else if (status >= 512)
944 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
947 void module_register (void)
949 plugin_register (MODULE_NAME, apcups_init, apcups_read, apcups_write);
950 plugin_register ("apcups_bvolt", NULL, NULL, apc_bvolt_write);
951 plugin_register ("apcups_load", NULL, NULL, apc_load_write);
952 plugin_register ("apcups_charge", NULL, NULL, apc_charge_write);
953 plugin_register ("apcups_temp", NULL, NULL, apc_temp_write);
954 plugin_register ("apcups_time", NULL, NULL, apc_time_write);
955 plugin_register ("apcups_freq", NULL, NULL, apc_freq_write);
956 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
959 #endif /* ifdef APCMAIN */