1 /*
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>
6 *
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.
10 *
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.
15 *
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,
19 * MA 02111-1307, USA.
20 *
21 * Authors:
22 * Anthony Gialluca <tonyabg at charter.net>
23 **/
25 #include "collectd.h"
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! */
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <strings.h>
35 #include <signal.h>
36 #include <ctype.h>
37 #include <syslog.h>
38 #include <limits.h>
39 #include <pwd.h>
40 #include <time.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <setjmp.h>
44 #include <termios.h>
45 #include <netdb.h>
46 #include <sys/ioctl.h>
47 #include <sys/ipc.h>
48 #include <sys/sem.h>
49 #include <sys/shm.h>
50 #include <sys/socket.h>
51 #include <sys/types.h>
52 #include <sys/time.h>
53 #include <time.h>
54 #include <sys/wait.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
58 #define NISPORT 3551
59 #define _(String) (String)
60 #define N_(String) (String)
61 #define MAXSTRING 256
62 <<<<<<< .mine
63 =======
64 #define Error_abort0(fmd) generic_error_out(__FILE__, __LINE__, fmd)
65 >>>>>>> .r733
66 #define MODULE_NAME "apcups"
69 /* Prototypes */
70 <<<<<<< .mine
71 =======
72 static void generic_error_out(const char *, int , const char *, ...);
73 >>>>>>> .r733
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 */
84 <<<<<<< .mine
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;
92 =======
93 >>>>>>> .r733
94 /*
95 * The following are only if not compiled to test the module with its own main.
96 */
97 /* FIXME: Rename DSes to be more generic and follow established conventions. */
98 #ifndef APCMAIN
99 static char *volt_file_template = "apcups_volt-%s.rrd";
100 static char *volt_ds_def[] =
101 {
102 "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
103 "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
104 NULL
105 };
106 static int volt_ds_num = 2;
108 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
109 static char *bvolt_ds_def[] =
110 {
111 "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
112 };
113 static int bvolt_ds_num = 1;
115 static char *load_file_template = "apcups_load-%s.rrd";
116 static char *load_ds_def[] =
117 {
118 "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
119 };
120 static int load_ds_num = 1;
122 static char *charge_file_template = "apcups_charge-%s.rrd";
123 static char *charge_ds_def[] =
124 {
125 "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
126 };
127 static int charge_ds_num = 1;
129 static char *time_file_template = "apcups_time-%s.rrd";
130 static char *time_ds_def[] =
131 {
132 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
133 };
134 static int time_ds_num = 1;
136 static char *temp_file_template = "apcups_temp-%s.rrd";
137 static char *temp_ds_def[] =
138 {
139 "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
140 };
141 static int temp_ds_num = 1;
143 static char *freq_file_template = "apcups_freq-%s.rrd";
144 static char *freq_ds_def[] =
145 {
146 "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
147 };
148 static int freq_ds_num = 1;
150 static char *config_keys[] =
151 {
152 "Host",
153 "Port",
154 NULL
155 };
156 static int config_keys_num = 2;
158 #endif /* ifndef APCMAIN */
160 <<<<<<< .mine
161 struct apc_detail_s {
162 int connected;
163 float linev;
164 float loadpct;
165 float bcharge;
166 float timeleft;
167 float outputv;
168 float itemp;
169 float battv;
170 float linefreq;
171 =======
172 struct apc_detail_s
173 {
174 float linev;
175 float loadpct;
176 float bcharge;
177 float timeleft;
178 float outputv;
179 float itemp;
180 float battv;
181 float linefreq;
182 >>>>>>> .r733
183 };
185 #define BIG_BUF 4096
187 <<<<<<< .mine
189 #define BIG_BUF 5000
191 /* Implement snprintf */
192 int asnprintf(char *str, size_t size, const char *fmt, ...)
193 {
194 #ifdef HAVE_VSNPRINTF
195 va_list arg_ptr;
196 int len;
198 va_start(arg_ptr, fmt);
199 len = vsnprintf(str, size, fmt, arg_ptr);
200 va_end(arg_ptr);
202 str[size - 1] = 0;
203 return len;
205 #else
207 va_list arg_ptr;
208 int len;
209 char *buf;
211 buf = (char *)malloc(BIG_BUF);
213 va_start(arg_ptr, fmt);
214 len = vsprintf(buf, fmt, arg_ptr);
215 va_end(arg_ptr);
217 if (len >= BIG_BUF){
218 syslog(LOG_ERR, "apcups: asnprintf(): Buffer overflow");
219 return(0);
220 }
222 memcpy(str, buf, size);
223 str[size - 1] = 0;
225 free(buf);
226 return len;
227 #endif
228 }
230 /* Implement vsnprintf() */
231 int avsnprintf(char *str, size_t size, const char *format, va_list ap)
232 {
233 #ifdef HAVE_VSNPRINTF
234 int len;
236 len = vsnprintf(str, size, format, ap);
237 str[size - 1] = 0;
239 return len;
241 #else
243 int len;
244 char *buf;
246 buf = (char *)malloc(BIG_BUF);
248 len = vsprintf(buf, format, ap);
249 if (len >= BIG_BUF){
250 syslog(LOG_ERR, "apcups: avsnprintf(): Buffer overflow");
251 return(0);
252 }
254 memcpy(str, buf, size);
255 str[size - 1] = 0;
257 free(buf);
258 return len;
259 #endif
260 }
262 =======
263 >>>>>>> .r733
264 /*
265 <<<<<<< .mine
266 =======
267 * Subroutine normally called by macro error_abort() to print
268 * FATAL ERROR message and supplied error message
269 */
270 static void generic_error_out(const char *file, int line, const char *fmt, ...)
271 {
272 char buf[256];
273 va_list arg_ptr;
274 int i;
276 snprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line);
277 i = strlen(buf);
278 va_start(arg_ptr, fmt);
279 vsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr);
280 va_end(arg_ptr);
281 fprintf(stdout, "%s", buf);
283 exit(1);
284 }
286 /*
287 >>>>>>> .r733
288 * Read nbytes from the network.
289 * It is possible that the total bytes require in several
290 * read requests
291 */
292 static int read_nbytes(int fd, char *ptr, int nbytes)
293 {
294 int nleft, nread;
296 nleft = nbytes;
298 while (nleft > 0) {
299 do {
300 nread = read(fd, ptr, nleft);
301 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
303 if (nread <= 0) {
304 net_errno = errno;
305 return (nread); /* error, or EOF */
306 }
308 nleft -= nread;
309 ptr += nread;
310 }
312 return (nbytes - nleft); /* return >= 0 */
313 }
315 /*
316 * Write nbytes to the network.
317 * It may require several writes.
318 */
319 static int write_nbytes(int fd, char *ptr, int nbytes)
320 {
321 int nleft, nwritten;
323 nleft = nbytes;
324 while (nleft > 0) {
325 nwritten = write(fd, ptr, nleft);
327 if (nwritten <= 0) {
328 net_errno = errno;
329 return (nwritten); /* error */
330 }
332 nleft -= nwritten;
333 ptr += nwritten;
334 }
336 return (nbytes - nleft);
337 }
339 /* Close the network connection */
340 static void net_close (int sockfd)
341 {
342 short pktsiz = 0;
344 /* send EOF sentinel */
345 write_nbytes(sockfd, (char *) &pktsiz, sizeof(short));
346 close (sockfd);
347 }
350 /*
351 * Open a TCP connection to the UPS network server
352 * Returns -1 on error
353 * Returns socket file descriptor otherwise
354 */
355 static int net_open(char *host, char *service, int port)
356 {
357 int sockfd;
358 struct hostent *hp;
359 unsigned int inaddr; /* Careful here to use unsigned int for */
360 /* compatibility with Alpha */
362 /*
363 * Fill in the structure serv_addr with the address of the server that
364 * we want to connect with.
365 */
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;
372 } else {
373 if ((hp = gethostbyname(host)) == NULL) {
374 net_errmsg = "tcp_open: hostname error\n";
375 return -1;
376 }
378 if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
379 net_errmsg = "tcp_open: funny gethostbyname value\n";
380 return -1;
381 }
383 tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
384 }
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";
389 return -1;
390 }
392 /* connect to server */
393 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
394 /*
395 * Work around a bug in OpenBSD & FreeBSD userspace pthreads
396 * implementations. Rationale is the same as described above.
397 */
398 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
399 #endif
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;
406 close(sockfd);
407 return -1;
408 }
410 return sockfd;
411 } /* int net_open(char *host, char *service, int port) */
413 /*
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
421 */
422 static int net_recv(int sockfd, char *buff, int maxlen)
423 {
424 int nbytes;
425 short pktsiz;
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 */
431 }
432 if (nbytes != sizeof(short))
433 return -2;
435 pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
436 if (pktsiz > maxlen) {
437 net_errmsg = "net_recv: record length too large\n";
438 return -2;
439 }
440 if (pktsiz == 0)
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";
446 return -2;
447 }
448 if (nbytes != pktsiz) {
449 net_errmsg = "net_recv: error in read_nbytes\n";
450 return -2;
451 }
453 return (nbytes); /* return actual length of message */
454 }
456 /*
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
462 */
463 static int net_send(int sockfd, char *buff, int len)
464 {
465 int rc;
466 short pktsiz;
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";
473 return -1;
474 }
476 /* send data packet */
477 rc = write_nbytes(sockfd, buff, len);
478 if (rc != len) {
479 net_errmsg = "net_send: write_nbytes error\n";
480 return -1;
481 }
483 return rc;
484 }
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)
489 {
490 <<<<<<< .mine
491 int sockfd, n;
492 char recvline[MAXSTRING + 1];
493 char *tokptr;
495 if ((sockfd = net_open(host, NULL, port)) < 0){
496 syslog(LOG_ERR, "apcups: Cannot open connection: %s",
497 net_errmsg);
498 net_errmsg = NULL;
499 return;
500 }
502 net_send(sockfd, "status", 6);
504 while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
505 recvline[n] = 0;
506 #ifdef APCMAIN
507 fputs(recvline, stdout);
508 #endif /* ifdef APCMAIN */
509 =======
510 int sockfd;
511 int n;
512 char recvline[MAXSTRING + 1];
513 char *tokptr;
514 char *key;
515 double value;
516 #if APCMAIN
517 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
518 #else
519 # define PRINT_VALUE(name, val) /**/
520 #endif
521 >>>>>>> .r733
523 <<<<<<< .mine
524 tokptr = strtok(recvline,":");
525 while(tokptr != NULL) {
526 /* Look for Limit_Add */
527 if(strncmp("LINEV",tokptr,5) == 0) {
528 #ifdef APCMAIN
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) {
535 #ifdef APCMAIN
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) {
542 #ifdef APCMAIN
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) {
549 #ifdef APCMAIN
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) {
556 #ifdef APCMAIN
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) {
563 #ifdef APCMAIN
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) {
570 #ifdef APCMAIN
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) {
577 #ifdef APCMAIN
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);
583 } /* */
584 tokptr = strtok(NULL,":");
585 }
586 }
587 =======
588 /* TODO: Keep the socket open, if possible */
589 if ((sockfd = net_open (host, NULL, port)) < 0)
590 {
591 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
592 return (-1);
593 }
594 >>>>>>> .r733
596 <<<<<<< .mine
597 if (n < 0) {
598 syslog(LOG_ERR, "apcups: Error recieving data: %s",
599 net_errmsg);
600 net_errmsg = NULL;
601 return;
602 }
603 /* signal that we did in fact connect. */
604 apcups_detail->connected = 1;
606 net_close(sockfd);
607 =======
608 net_send (sockfd, "status", 6);
610 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
611 {
612 recvline[n] = '\0';
613 #if APCMAIN
614 printf ("net_recv = `%s';\n", recvline);
615 #endif /* if APCMAIN */
617 tokptr = strtok (recvline, ":");
618 while (tokptr != NULL)
619 {
620 key = tokptr;
621 if ((tokptr = strtok (NULL, " \t")) == NULL)
622 continue;
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;
642 else
643 {
644 syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
645 key, value);
646 }
648 tokptr = strtok (NULL, ":");
649 } /* while (tokptr != NULL) */
650 }
652 if (n < 0)
653 Error_abort0(net_errmsg);
655 net_close(sockfd);
657 return (0);
658 >>>>>>> .r733
659 }
661 #ifdef APCMAIN
662 int main(int argc, char **argv)
663 {
664 /* we are not really going to use this */
665 struct apc_detail_s apcups_detail;
667 <<<<<<< .mine
668 openlog("apcups",LOG_PID | LOG_NDELAY | LOG_LOCAL1);
670 if (!*host || strcmp(host, "0.0.0.0") == 0)
671 host = "localhost";
673 do_pthreads_status(host, port, &apcups_detail);
675 return 0;
676 =======
677 if (!*host || strcmp(host, "0.0.0.0") == 0)
678 host = "localhost";
680 do_pthreads_status(host, port, &apcups_detail);
682 return 0;
683 >>>>>>> .r733
684 }
685 #else
686 static void apcups_init (void)
687 {
688 return;
689 }
691 static int apcups_config (char *key, char *value)
692 {
693 static char lhost[126];
695 if (strcasecmp (key, "host") == 0)
696 {
697 lhost[0] = '\0';
698 strcpy(lhost,key);
699 host = lhost;
700 }
701 else if (strcasecmp (key, "Port") == 0)
702 {
703 int port_tmp = atoi (value);
704 if(port_tmp < 1 || port_tmp > 65535) {
705 syslog (LOG_WARNING, "apcups: `port' failed: %s",
706 value);
707 return (1);
708 } else {
709 port = port_tmp;
710 }
711 }
712 else
713 {
714 return (-1);
715 }
716 return(0);
717 }
719 #define BUFSIZE 256
720 static void apcups_submit (char *host,
721 struct apc_detail_s *apcups_detail)
722 {
723 char buf[BUFSIZE];
725 if (snprintf (buf, BUFSIZE, "%u:%f:%f",
726 (unsigned int) curtime,
727 apcups_detail->linev,
728 apcups_detail->outputv) >= BUFSIZE)
729 return;
731 plugin_submit (MODULE_NAME, host, buf);
732 }
734 static void apc_bvolt_submit (char *host,
735 struct apc_detail_s *apcups_detail)
736 {
737 char buf[BUFSIZE];
739 if (snprintf (buf, BUFSIZE, "%u:%f",
740 (unsigned int) curtime,
741 apcups_detail->battv) >= BUFSIZE)
742 return;
744 plugin_submit ("apcups_bvolt", host, buf);
745 }
747 static void apc_load_submit (char *host,
748 struct apc_detail_s *apcups_detail)
749 {
750 char buf[BUFSIZE];
752 if (snprintf (buf, BUFSIZE, "%u:%f",
753 (unsigned int) curtime,
754 apcups_detail->loadpct) >= BUFSIZE)
755 return;
757 plugin_submit ("apcups_load", host, buf);
758 }
760 static void apc_charge_submit (char *host,
761 struct apc_detail_s *apcups_detail)
762 {
763 char buf[BUFSIZE];
765 if (snprintf (buf, BUFSIZE, "%u:%f",
766 (unsigned int) curtime,
767 apcups_detail->bcharge) >= BUFSIZE)
768 return;
770 plugin_submit ("apcups_charge", host, buf);
771 }
773 static void apc_temp_submit (char *host,
774 struct apc_detail_s *apcups_detail)
775 {
776 char buf[BUFSIZE];
778 if (snprintf (buf, BUFSIZE, "%u:%f",
779 (unsigned int) curtime,
780 apcups_detail->itemp) >= BUFSIZE)
781 return;
783 plugin_submit ("apcups_temp", host, buf);
784 }
786 static void apc_time_submit (char *host,
787 struct apc_detail_s *apcups_detail)
788 {
789 char buf[BUFSIZE];
791 if (snprintf (buf, BUFSIZE, "%u:%f",
792 (unsigned int) curtime,
793 apcups_detail->timeleft) >= BUFSIZE)
794 return;
796 plugin_submit ("apcups_time", host, buf);
797 }
799 static void apc_freq_submit (char *host,
800 struct apc_detail_s *apcups_detail)
801 {
802 char buf[BUFSIZE];
804 if (snprintf (buf, BUFSIZE, "%u:%f",
805 (unsigned int) curtime,
806 apcups_detail->linefreq) >= BUFSIZE)
807 return;
809 plugin_submit ("apcups_freq", host, buf);
810 }
811 #undef BUFSIZE
813 static void apcups_read (void)
814 {
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)
829 host = "localhost";
831 do_pthreads_status(host, port, &apcups_detail);
833 /*
834 * if we did not connect then do not bother submitting
835 * zeros. We want rrd files to have NAN.
836 */
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);
846 }
849 static void apcups_write (char *host, char *inst, char *val)
850 {
851 char file[512];
852 int status;
854 status = snprintf (file, 512, volt_file_template, inst);
855 if (status < 1)
856 return;
857 else if (status >= 512)
858 return;
860 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
861 }
863 static void apc_bvolt_write (char *host, char *inst, char *val)
864 {
865 char file[512];
866 int status;
868 status = snprintf (file, 512, bvolt_file_template, inst);
869 if (status < 1)
870 return;
871 else if (status >= 512)
872 return;
874 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
875 }
877 static void apc_load_write (char *host, char *inst, char *val)
878 {
879 char file[512];
880 int status;
882 status = snprintf (file, 512, load_file_template, inst);
883 if (status < 1)
884 return;
885 else if (status >= 512)
886 return;
888 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
889 }
891 static void apc_charge_write (char *host, char *inst, char *val)
892 {
893 char file[512];
894 int status;
896 status = snprintf (file, 512, charge_file_template, inst);
897 if (status < 1)
898 return;
899 else if (status >= 512)
900 return;
902 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
903 }
905 static void apc_temp_write (char *host, char *inst, char *val)
906 {
907 char file[512];
908 int status;
910 status = snprintf (file, 512, temp_file_template, inst);
911 if (status < 1)
912 return;
913 else if (status >= 512)
914 return;
916 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
917 }
919 static void apc_time_write (char *host, char *inst, char *val)
920 {
921 char file[512];
922 int status;
924 status = snprintf (file, 512, time_file_template, inst);
925 if (status < 1)
926 return;
927 else if (status >= 512)
928 return;
930 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
931 }
933 static void apc_freq_write (char *host, char *inst, char *val)
934 {
935 char file[512];
936 int status;
938 status = snprintf (file, 512, freq_file_template, inst);
939 if (status < 1)
940 return;
941 else if (status >= 512)
942 return;
944 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
945 }
947 void module_register (void)
948 {
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);
957 }
959 #endif /* ifdef APCMAIN */
960 #undef MODULE_NAME