2 * Copyright (C) 2000-2004 Kern Sibbald
3 * Copyright (C) 1996-99 Andre M. Hedrick <andre@suse.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General
7 * Public License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the Free
16 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 #include <sys/ioctl.h>
41 # include <sys/socket.h>
42 # include <sys/types.h>
43 # include <sys/time.h>
45 # include <sys/wait.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include "common.h" /* rrd_update_file */
51 #include "plugin.h" /* plugin_register, plugin_submit */
52 #include "configfile.h" /* cf_register */
55 #define _(String) (String)
56 #define N_(String) (String)
58 #define Error_abort0(fmd) error_out(__FILE__, __LINE__, fmd)
59 #define MODULE_NAME "apcups"
63 void generic_error_out(const char *, int , const char *, ...);
65 /* Default values for contacting daemon */
66 static char *host = "localhost";
67 static int port = NISPORT;
69 char argvalue[MAXSTRING];
71 struct sockaddr_in tcp_serv_addr; /* socket information */
72 int net_errno = 0; /* error number -- not yet implemented */
73 char *net_errmsg = NULL; /* pointer to error message */
74 char net_errbuf[256]; /* error message buffer for messages */
76 void (*error_out) (const char *file, int line, const char *fmt, ...) = generic_error_out;
77 void (*error_cleanup) (void) = NULL;
80 * The following are only if not compiled to test the module with
84 static char *volt_file_template = "apcups_volt-%s.rrd";
85 static char *volt_ds_def[] =
87 "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
88 "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
91 static int volt_ds_num = 2;
93 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
94 static char *bvolt_ds_def[] =
96 "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
98 static int bvolt_ds_num = 1;
100 static char *load_file_template = "apcups_load-%s.rrd";
101 static char *load_ds_def[] =
103 "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
105 static int load_ds_num = 1;
107 static char *charge_file_template = "apcups_charge-%s.rrd";
108 static char *charge_ds_def[] =
110 "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
112 static int charge_ds_num = 1;
114 static char *time_file_template = "apcups_time-%s.rrd";
115 static char *time_ds_def[] =
117 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
119 static int time_ds_num = 1;
121 static char *temp_file_template = "apcups_temp-%s.rrd";
122 static char *temp_ds_def[] =
124 "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
126 static int temp_ds_num = 1;
128 static char *freq_file_template = "apcups_freq-%s.rrd";
129 static char *freq_ds_def[] =
131 "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
133 static int freq_ds_num = 1;
135 static char *config_keys[] =
141 static int config_keys_num = 2;
143 #endif /* ifndef APCMAIN */
145 struct apc_detail_s {
156 /* Guarantee that the string is properly terminated */
157 char *astrncpy(char *dest, const char *src, int maxlen)
159 strncpy(dest, src, maxlen - 1);
160 dest[maxlen - 1] = 0;
167 /* Implement snprintf */
168 int asnprintf(char *str, size_t size, const char *fmt, ...)
170 #ifdef HAVE_VSNPRINTF
174 va_start(arg_ptr, fmt);
175 len = vsnprintf(str, size, fmt, arg_ptr);
187 buf = (char *)malloc(BIG_BUF);
189 va_start(arg_ptr, fmt);
190 len = vsprintf(buf, fmt, arg_ptr);
194 Error_abort0(_("Buffer overflow.\n"));
196 memcpy(str, buf, size);
204 /* Implement vsnprintf() */
205 int avsnprintf(char *str, size_t size, const char *format, va_list ap)
207 #ifdef HAVE_VSNPRINTF
210 len = vsnprintf(str, size, format, ap);
220 buf = (char *)malloc(BIG_BUF);
222 len = vsprintf(buf, format, ap);
224 Error_abort0(_("Buffer overflow.\n"));
226 memcpy(str, buf, size);
235 * Subroutine normally called by macro error_abort() to print
236 * FATAL ERROR message and supplied error message
238 void generic_error_out(const char *file, int line, const char *fmt, ...)
244 asnprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line);
246 va_start(arg_ptr, fmt);
247 avsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr);
249 fprintf(stdout, "%s", buf);
258 * Read nbytes from the network.
259 * It is possible that the total bytes require in several
262 static int read_nbytes(int fd, char *ptr, int nbytes)
271 nread = read(fd, ptr, nleft);
272 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
276 return (nread); /* error, or EOF */
283 return (nbytes - nleft); /* return >= 0 */
288 * Write nbytes to the network.
289 * It may require several writes.
291 static int write_nbytes(int fd, char *ptr, int nbytes)
297 nwritten = write(fd, ptr, nleft);
301 return (nwritten); /* error */
308 return (nbytes - nleft);
312 /* Close the network connection */
313 void net_close(int sockfd)
317 /* send EOF sentinel */
318 write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
324 * Open a TCP connection to the UPS network server
325 * Returns -1 on error
326 * Returns socket file descriptor otherwise
328 int net_open(char *host, char *service, int port)
332 unsigned int inaddr; /* Careful here to use unsigned int for */
333 /* compatibility with Alpha */
336 * Fill in the structure serv_addr with the address of
337 * the server that we want to connect with.
339 memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
340 tcp_serv_addr.sin_family = AF_INET;
341 tcp_serv_addr.sin_port = htons(port);
343 if ((inaddr = inet_addr(host)) != INADDR_NONE) {
344 tcp_serv_addr.sin_addr.s_addr = inaddr;
346 if ((hp = gethostbyname(host)) == NULL) {
347 net_errmsg = "tcp_open: hostname error\n";
351 if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
352 net_errmsg = "tcp_open: funny gethostbyname value\n";
356 tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
360 /* Open a TCP socket */
361 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
362 net_errmsg = "tcp_open: cannot open stream socket\n";
366 /* connect to server */
367 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
369 * Work around a bug in OpenBSD & FreeBSD userspace pthreads
370 * implementations. Rationale is the same as described above.
372 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
375 if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
376 asnprintf(net_errbuf, sizeof(net_errbuf),
377 _("tcp_open: cannot connect to server %s on port %d.\n"
378 "ERR=%s\n"), host, port, strerror(errno));
379 net_errmsg = net_errbuf;
390 * Receive a message from the other end. Each message consists of
391 * two packets. The first is a header that contains the size
392 * of the data that follows in the second packet.
393 * Returns number of bytes read
394 * Returns 0 on end of file
395 * Returns -1 on hard end of file (i.e. network connection close)
396 * Returns -2 on error
398 int net_recv(int sockfd, char *buff, int maxlen)
403 /* get data size -- in short */
404 if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
405 /* probably pipe broken because client died */
406 return -1; /* assume hard EOF received */
408 if (nbytes != sizeof(short))
411 pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
412 if (pktsiz > maxlen) {
413 net_errmsg = "net_recv: record length too large\n";
417 return 0; /* soft EOF */
419 /* now read the actual data */
420 if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
421 net_errmsg = "net_recv: read_nbytes error\n";
424 if (nbytes != pktsiz) {
425 net_errmsg = "net_recv: error in read_nbytes\n";
429 return (nbytes); /* return actual length of message */
433 * Send a message over the network. The send consists of
434 * two network packets. The first is sends a short containing
435 * the length of the data packet which follows.
436 * Returns number of bytes sent
437 * Returns -1 on error
439 int net_send(int sockfd, char *buff, int len)
444 /* send short containing size of data packet */
445 pktsiz = htons((short)len);
446 rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
447 if (rc != sizeof(short)) {
448 net_errmsg = "net_send: write_nbytes error of length prefix\n";
452 /* send data packet */
453 rc = write_nbytes(sockfd, buff, len);
455 net_errmsg = "net_send: write_nbytes error\n";
463 /* Get and print status from apcupsd NIS server */
464 static void do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
467 char recvline[MAXSTRING + 1];
470 if ((sockfd = net_open(host, NULL, port)) < 0)
471 Error_abort0(net_errmsg);
473 net_send(sockfd, "status", 6);
475 while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
478 fputs(recvline, stdout);
482 #endif /* ifdef APCMAIN */
484 tokptr = strtok(recvline,":");
485 while(tokptr != NULL) {
486 /* Look for Limit_Add */
487 if(strncmp("LINEV",tokptr,5) == 0) {
488 if(printit) fprintf(stdout," Found LINEV.\n");
489 tokptr = strtok(NULL," \t");
490 if(tokptr == NULL) continue;
491 if(printit) fprintf(stdout," Value %s.\n",tokptr);
492 apcups_detail->linev = atof (tokptr);
493 }else if(strncmp("BATTV",tokptr,5) == 0) {
494 if(printit) fprintf(stdout," Found BATTV.\n");
495 tokptr = strtok(NULL," \t");
496 if(tokptr == NULL) continue;
497 if(printit) fprintf(stdout," Value %s.\n",tokptr);
498 apcups_detail->battv = atof (tokptr);
499 }else if(strncmp("ITEMP",tokptr,5) == 0) {
500 if(printit) fprintf(stdout," Found ITEMP.\n");
501 tokptr = strtok(NULL," \t");
502 if(tokptr == NULL) continue;
503 if(printit) fprintf(stdout," Value %s.\n",tokptr);
504 apcups_detail->itemp = atof (tokptr);
505 }else if(strncmp("LOADPCT",tokptr,7) == 0) {
506 if(printit) fprintf(stdout," Found LOADPCT.\n");
507 tokptr = strtok(NULL," \t");
508 if(tokptr == NULL) continue;
509 if(printit) fprintf(stdout," Value %s.\n",tokptr);
510 apcups_detail->loadpct = atof (tokptr);
511 }else if(strncmp("BCHARGE",tokptr,7) == 0) {
512 if(printit) fprintf(stdout," Found BCHARGE.\n");
513 tokptr = strtok(NULL," \t");
514 if(tokptr == NULL) continue;
515 if(printit) fprintf(stdout," Value %s.\n",tokptr);
516 apcups_detail->bcharge = atof (tokptr);
517 }else if(strncmp("OUTPUTV",tokptr,7) == 0) {
518 if(printit) fprintf(stdout," Found OUTPUTV.\n");
519 tokptr = strtok(NULL," \t");
520 if(tokptr == NULL) continue;
521 if(printit) fprintf(stdout," Value %s.\n",tokptr);
522 apcups_detail->outputv = atof (tokptr);
523 }else if(strncmp("LINEFREQ",tokptr,8) == 0) {
524 if(printit) fprintf(stdout," Found LINEFREQ.\n");
525 tokptr = strtok(NULL," \t");
526 if(tokptr == NULL) continue;
527 if(printit) fprintf(stdout," Value %s.\n",tokptr);
528 apcups_detail->linefreq = atof (tokptr);
529 }else if(strncmp("TIMELEFT",tokptr,8) == 0) {
530 if(printit) fprintf(stdout," Found TIMELEFT.\n");
531 tokptr = strtok(NULL," \t");
532 if(tokptr == NULL) continue;
533 if(printit) fprintf(stdout," Value %s.\n",tokptr);
534 apcups_detail->timeleft = atof (tokptr);
536 tokptr = strtok(NULL,":");
541 Error_abort0(net_errmsg);
547 int main(int argc, char **argv)
549 /* we are not really going to use this */
550 struct apc_detail_s apcups_detail;
552 if (!*host || strcmp(host, "0.0.0.0") == 0)
555 do_pthreads_status(host, port, &apcups_detail);
560 static void apcups_init (void)
565 static int apcups_config (char *key, char *value)
567 static char lhost[126];
569 if (strcasecmp (key, "host") == 0)
575 else if (strcasecmp (key, "Port") == 0)
577 int port_tmp = atoi (value);
578 if(port_tmp < 1 || port_tmp > 65535) {
579 syslog (LOG_WARNING, "apcups: `port' failed: %s",
594 static void apcups_submit (char *host,
595 struct apc_detail_s *apcups_detail)
599 if (snprintf (buf, BUFSIZE, "%u:%f:%f",
600 (unsigned int) curtime,
601 apcups_detail->linev,
602 apcups_detail->outputv) >= BUFSIZE)
605 plugin_submit (MODULE_NAME, host, buf);
608 static void apc_bvolt_submit (char *host,
609 struct apc_detail_s *apcups_detail)
613 if (snprintf (buf, BUFSIZE, "%u:%f",
614 (unsigned int) curtime,
615 apcups_detail->battv) >= BUFSIZE)
618 plugin_submit ("apcups_bvolt", host, buf);
621 static void apc_load_submit (char *host,
622 struct apc_detail_s *apcups_detail)
626 if (snprintf (buf, BUFSIZE, "%u:%f",
627 (unsigned int) curtime,
628 apcups_detail->loadpct) >= BUFSIZE)
631 plugin_submit ("apcups_load", host, buf);
634 static void apc_charge_submit (char *host,
635 struct apc_detail_s *apcups_detail)
639 if (snprintf (buf, BUFSIZE, "%u:%f",
640 (unsigned int) curtime,
641 apcups_detail->bcharge) >= BUFSIZE)
644 plugin_submit ("apcups_charge", host, buf);
647 static void apc_temp_submit (char *host,
648 struct apc_detail_s *apcups_detail)
652 if (snprintf (buf, BUFSIZE, "%u:%f",
653 (unsigned int) curtime,
654 apcups_detail->itemp) >= BUFSIZE)
657 plugin_submit ("apcups_temp", host, buf);
660 static void apc_time_submit (char *host,
661 struct apc_detail_s *apcups_detail)
665 if (snprintf (buf, BUFSIZE, "%u:%f",
666 (unsigned int) curtime,
667 apcups_detail->timeleft) >= BUFSIZE)
670 plugin_submit ("apcups_time", host, buf);
673 static void apc_freq_submit (char *host,
674 struct apc_detail_s *apcups_detail)
678 if (snprintf (buf, BUFSIZE, "%u:%f",
679 (unsigned int) curtime,
680 apcups_detail->linefreq) >= BUFSIZE)
683 plugin_submit ("apcups_freq", host, buf);
687 static void apcups_read (void)
689 struct apc_detail_s apcups_detail;
691 apcups_detail.linev = 0.0;
692 apcups_detail.loadpct = 0.0;
693 apcups_detail.bcharge = 0.0;
694 apcups_detail.timeleft = 0.0;
695 apcups_detail.outputv = 0.0;
696 apcups_detail.itemp = 0.0;
697 apcups_detail.battv = 0.0;
698 apcups_detail.linefreq = 0.0;
701 if (!*host || strcmp(host, "0.0.0.0") == 0)
704 do_pthreads_status(host, port, &apcups_detail);
706 apcups_submit (host, &apcups_detail);
707 apc_bvolt_submit (host, &apcups_detail);
708 apc_load_submit (host, &apcups_detail);
709 apc_charge_submit (host, &apcups_detail);
710 apc_temp_submit (host, &apcups_detail);
711 apc_time_submit (host, &apcups_detail);
712 apc_freq_submit (host, &apcups_detail);
716 static void apcups_write (char *host, char *inst, char *val)
721 status = snprintf (file, 512, volt_file_template, inst);
724 else if (status >= 512)
727 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
730 static void apc_bvolt_write (char *host, char *inst, char *val)
735 status = snprintf (file, 512, bvolt_file_template, inst);
738 else if (status >= 512)
741 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
744 static void apc_load_write (char *host, char *inst, char *val)
749 status = snprintf (file, 512, load_file_template, inst);
752 else if (status >= 512)
755 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
758 static void apc_charge_write (char *host, char *inst, char *val)
763 status = snprintf (file, 512, charge_file_template, inst);
766 else if (status >= 512)
769 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
772 static void apc_temp_write (char *host, char *inst, char *val)
777 status = snprintf (file, 512, temp_file_template, inst);
780 else if (status >= 512)
783 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
786 static void apc_time_write (char *host, char *inst, char *val)
791 status = snprintf (file, 512, time_file_template, inst);
794 else if (status >= 512)
797 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
800 static void apc_freq_write (char *host, char *inst, char *val)
805 status = snprintf (file, 512, freq_file_template, inst);
808 else if (status >= 512)
811 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
814 void module_register (void)
816 plugin_register (MODULE_NAME, apcups_init, apcups_read, apcups_write);
817 plugin_register ("apcups_bvolt", NULL, NULL, apc_bvolt_write);
818 plugin_register ("apcups_load", NULL, NULL, apc_load_write);
819 plugin_register ("apcups_charge", NULL, NULL, apc_charge_write);
820 plugin_register ("apcups_temp", NULL, NULL, apc_temp_write);
821 plugin_register ("apcups_time", NULL, NULL, apc_time_write);
822 plugin_register ("apcups_freq", NULL, NULL, apc_freq_write);
823 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
826 #endif /* ifdef APCMAIN */