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)
62 #define Error_abort0(fmd) generic_error_out(__FILE__, __LINE__, fmd)
63 #define MODULE_NAME "apcups"
67 static void generic_error_out(const char *, int , const char *, ...);
69 /* Default values for contacting daemon */
70 static char *host = "localhost";
71 static int port = NISPORT;
73 static struct sockaddr_in tcp_serv_addr; /* socket information */
74 static int net_errno = 0; /* error number -- not yet implemented */
75 static char *net_errmsg = NULL; /* pointer to error message */
76 static char net_errbuf[256]; /* error message buffer for messages */
79 * The following are only if not compiled to test the module with its own main.
81 /* FIXME: Rename DSes to be more generic and follow established conventions. */
83 static char *volt_file_template = "apcups_volt-%s.rrd";
84 static char *volt_ds_def[] =
86 "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
87 "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
90 static int volt_ds_num = 2;
92 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
93 static char *bvolt_ds_def[] =
95 "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
97 static int bvolt_ds_num = 1;
99 static char *load_file_template = "apcups_load-%s.rrd";
100 static char *load_ds_def[] =
102 "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
104 static int load_ds_num = 1;
106 static char *charge_file_template = "apcups_charge-%s.rrd";
107 static char *charge_ds_def[] =
109 "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
111 static int charge_ds_num = 1;
113 static char *time_file_template = "apcups_time-%s.rrd";
114 static char *time_ds_def[] =
116 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
118 static int time_ds_num = 1;
120 static char *temp_file_template = "apcups_temp-%s.rrd";
121 static char *temp_ds_def[] =
123 "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
125 static int temp_ds_num = 1;
127 static char *freq_file_template = "apcups_freq-%s.rrd";
128 static char *freq_ds_def[] =
130 "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
132 static int freq_ds_num = 1;
134 static char *config_keys[] =
140 static int config_keys_num = 2;
142 #endif /* ifndef APCMAIN */
159 * Subroutine normally called by macro error_abort() to print
160 * FATAL ERROR message and supplied error message
162 static void generic_error_out(const char *file, int line, const char *fmt, ...)
168 snprintf(buf, sizeof(buf), _("FATAL ERROR in %s at line %d\n"), file, line);
170 va_start(arg_ptr, fmt);
171 vsnprintf((char *)&buf[i], sizeof(buf) - i, (char *)fmt, arg_ptr);
173 fprintf(stdout, "%s", buf);
179 * Read nbytes from the network.
180 * It is possible that the total bytes require in several
183 static int read_nbytes(int fd, char *ptr, int nbytes)
191 nread = read(fd, ptr, nleft);
192 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
196 return (nread); /* error, or EOF */
203 return (nbytes - nleft); /* return >= 0 */
207 * Write nbytes to the network.
208 * It may require several writes.
210 static int write_nbytes(int fd, char *ptr, int nbytes)
216 nwritten = write(fd, ptr, nleft);
220 return (nwritten); /* error */
227 return (nbytes - nleft);
230 /* Close the network connection */
231 void net_close (int sockfd)
235 /* send EOF sentinel */
236 write_nbytes(sockfd, (char *) &pktsiz, sizeof(short));
242 * Open a TCP connection to the UPS network server
243 * Returns -1 on error
244 * Returns socket file descriptor otherwise
246 int net_open(char *host, char *service, int port)
250 unsigned int inaddr; /* Careful here to use unsigned int for */
251 /* compatibility with Alpha */
254 * Fill in the structure serv_addr with the address of the server that
255 * we want to connect with.
257 memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
258 tcp_serv_addr.sin_family = AF_INET;
259 tcp_serv_addr.sin_port = htons(port);
261 if ((inaddr = inet_addr(host)) != INADDR_NONE) {
262 tcp_serv_addr.sin_addr.s_addr = inaddr;
264 if ((hp = gethostbyname(host)) == NULL) {
265 net_errmsg = "tcp_open: hostname error\n";
269 if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
270 net_errmsg = "tcp_open: funny gethostbyname value\n";
274 tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
277 /* Open a TCP socket */
278 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
279 net_errmsg = "tcp_open: cannot open stream socket\n";
283 /* connect to server */
284 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
286 * Work around a bug in OpenBSD & FreeBSD userspace pthreads
287 * implementations. Rationale is the same as described above.
289 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
292 if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
293 snprintf(net_errbuf, sizeof(net_errbuf),
294 _("tcp_open: cannot connect to server %s on port %d.\n"
295 "ERR=%s\n"), host, port, strerror(errno));
296 net_errmsg = net_errbuf;
302 } /* int net_open(char *host, char *service, int port) */
305 * Receive a message from the other end. Each message consists of
306 * two packets. The first is a header that contains the size
307 * of the data that follows in the second packet.
308 * Returns number of bytes read
309 * Returns 0 on end of file
310 * Returns -1 on hard end of file (i.e. network connection close)
311 * Returns -2 on error
313 int net_recv(int sockfd, char *buff, int maxlen)
318 /* get data size -- in short */
319 if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
320 /* probably pipe broken because client died */
321 return -1; /* assume hard EOF received */
323 if (nbytes != sizeof(short))
326 pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
327 if (pktsiz > maxlen) {
328 net_errmsg = "net_recv: record length too large\n";
332 return 0; /* soft EOF */
334 /* now read the actual data */
335 if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
336 net_errmsg = "net_recv: read_nbytes error\n";
339 if (nbytes != pktsiz) {
340 net_errmsg = "net_recv: error in read_nbytes\n";
344 return (nbytes); /* return actual length of message */
348 * Send a message over the network. The send consists of
349 * two network packets. The first is sends a short containing
350 * the length of the data packet which follows.
351 * Returns number of bytes sent
352 * Returns -1 on error
354 int net_send(int sockfd, char *buff, int len)
359 /* send short containing size of data packet */
360 pktsiz = htons((short)len);
361 rc = write_nbytes(sockfd, (char *)&pktsiz, sizeof(short));
362 if (rc != sizeof(short)) {
363 net_errmsg = "net_send: write_nbytes error of length prefix\n";
367 /* send data packet */
368 rc = write_nbytes(sockfd, buff, len);
370 net_errmsg = "net_send: write_nbytes error\n";
378 /* Get and print status from apcupsd NIS server */
379 static void do_pthreads_status(char *host, int port, struct apc_detail_s *apcups_detail)
382 char recvline[MAXSTRING + 1];
385 if ((sockfd = net_open(host, NULL, port)) < 0)
386 Error_abort0(net_errmsg);
388 net_send(sockfd, "status", 6);
390 while ((n = net_recv(sockfd, recvline, sizeof(recvline))) > 0) {
393 fputs(recvline, stdout);
397 #endif /* ifdef APCMAIN */
399 tokptr = strtok(recvline,":");
400 while(tokptr != NULL) {
401 /* Look for Limit_Add */
402 if(strncmp("LINEV",tokptr,5) == 0) {
403 if(printit) fprintf(stdout," Found LINEV.\n");
404 tokptr = strtok(NULL," \t");
405 if(tokptr == NULL) continue;
406 if(printit) fprintf(stdout," Value %s.\n",tokptr);
407 apcups_detail->linev = atof (tokptr);
408 }else if(strncmp("BATTV",tokptr,5) == 0) {
409 if(printit) fprintf(stdout," Found BATTV.\n");
410 tokptr = strtok(NULL," \t");
411 if(tokptr == NULL) continue;
412 if(printit) fprintf(stdout," Value %s.\n",tokptr);
413 apcups_detail->battv = atof (tokptr);
414 }else if(strncmp("ITEMP",tokptr,5) == 0) {
415 if(printit) fprintf(stdout," Found ITEMP.\n");
416 tokptr = strtok(NULL," \t");
417 if(tokptr == NULL) continue;
418 if(printit) fprintf(stdout," Value %s.\n",tokptr);
419 apcups_detail->itemp = atof (tokptr);
420 }else if(strncmp("LOADPCT",tokptr,7) == 0) {
421 if(printit) fprintf(stdout," Found LOADPCT.\n");
422 tokptr = strtok(NULL," \t");
423 if(tokptr == NULL) continue;
424 if(printit) fprintf(stdout," Value %s.\n",tokptr);
425 apcups_detail->loadpct = atof (tokptr);
426 }else if(strncmp("BCHARGE",tokptr,7) == 0) {
427 if(printit) fprintf(stdout," Found BCHARGE.\n");
428 tokptr = strtok(NULL," \t");
429 if(tokptr == NULL) continue;
430 if(printit) fprintf(stdout," Value %s.\n",tokptr);
431 apcups_detail->bcharge = atof (tokptr);
432 }else if(strncmp("OUTPUTV",tokptr,7) == 0) {
433 if(printit) fprintf(stdout," Found OUTPUTV.\n");
434 tokptr = strtok(NULL," \t");
435 if(tokptr == NULL) continue;
436 if(printit) fprintf(stdout," Value %s.\n",tokptr);
437 apcups_detail->outputv = atof (tokptr);
438 }else if(strncmp("LINEFREQ",tokptr,8) == 0) {
439 if(printit) fprintf(stdout," Found LINEFREQ.\n");
440 tokptr = strtok(NULL," \t");
441 if(tokptr == NULL) continue;
442 if(printit) fprintf(stdout," Value %s.\n",tokptr);
443 apcups_detail->linefreq = atof (tokptr);
444 }else if(strncmp("TIMELEFT",tokptr,8) == 0) {
445 if(printit) fprintf(stdout," Found TIMELEFT.\n");
446 tokptr = strtok(NULL," \t");
447 if(tokptr == NULL) continue;
448 if(printit) fprintf(stdout," Value %s.\n",tokptr);
449 apcups_detail->timeleft = atof (tokptr);
451 tokptr = strtok(NULL,":");
456 Error_abort0(net_errmsg);
462 int main(int argc, char **argv)
464 /* we are not really going to use this */
465 struct apc_detail_s apcups_detail;
467 if (!*host || strcmp(host, "0.0.0.0") == 0)
470 do_pthreads_status(host, port, &apcups_detail);
475 static void apcups_init (void)
480 static int apcups_config (char *key, char *value)
482 static char lhost[126];
484 if (strcasecmp (key, "host") == 0)
490 else if (strcasecmp (key, "Port") == 0)
492 int port_tmp = atoi (value);
493 if(port_tmp < 1 || port_tmp > 65535) {
494 syslog (LOG_WARNING, "apcups: `port' failed: %s",
509 static void apcups_submit (char *host,
510 struct apc_detail_s *apcups_detail)
514 if (snprintf (buf, BUFSIZE, "%u:%f:%f",
515 (unsigned int) curtime,
516 apcups_detail->linev,
517 apcups_detail->outputv) >= BUFSIZE)
520 plugin_submit (MODULE_NAME, host, buf);
523 static void apc_bvolt_submit (char *host,
524 struct apc_detail_s *apcups_detail)
528 if (snprintf (buf, BUFSIZE, "%u:%f",
529 (unsigned int) curtime,
530 apcups_detail->battv) >= BUFSIZE)
533 plugin_submit ("apcups_bvolt", host, buf);
536 static void apc_load_submit (char *host,
537 struct apc_detail_s *apcups_detail)
541 if (snprintf (buf, BUFSIZE, "%u:%f",
542 (unsigned int) curtime,
543 apcups_detail->loadpct) >= BUFSIZE)
546 plugin_submit ("apcups_load", host, buf);
549 static void apc_charge_submit (char *host,
550 struct apc_detail_s *apcups_detail)
554 if (snprintf (buf, BUFSIZE, "%u:%f",
555 (unsigned int) curtime,
556 apcups_detail->bcharge) >= BUFSIZE)
559 plugin_submit ("apcups_charge", host, buf);
562 static void apc_temp_submit (char *host,
563 struct apc_detail_s *apcups_detail)
567 if (snprintf (buf, BUFSIZE, "%u:%f",
568 (unsigned int) curtime,
569 apcups_detail->itemp) >= BUFSIZE)
572 plugin_submit ("apcups_temp", host, buf);
575 static void apc_time_submit (char *host,
576 struct apc_detail_s *apcups_detail)
580 if (snprintf (buf, BUFSIZE, "%u:%f",
581 (unsigned int) curtime,
582 apcups_detail->timeleft) >= BUFSIZE)
585 plugin_submit ("apcups_time", host, buf);
588 static void apc_freq_submit (char *host,
589 struct apc_detail_s *apcups_detail)
593 if (snprintf (buf, BUFSIZE, "%u:%f",
594 (unsigned int) curtime,
595 apcups_detail->linefreq) >= BUFSIZE)
598 plugin_submit ("apcups_freq", host, buf);
602 static void apcups_read (void)
604 struct apc_detail_s apcups_detail;
606 apcups_detail.linev = 0.0;
607 apcups_detail.loadpct = 0.0;
608 apcups_detail.bcharge = 0.0;
609 apcups_detail.timeleft = 0.0;
610 apcups_detail.outputv = 0.0;
611 apcups_detail.itemp = 0.0;
612 apcups_detail.battv = 0.0;
613 apcups_detail.linefreq = 0.0;
616 if (!*host || strcmp(host, "0.0.0.0") == 0)
619 do_pthreads_status(host, port, &apcups_detail);
621 apcups_submit (host, &apcups_detail);
622 apc_bvolt_submit (host, &apcups_detail);
623 apc_load_submit (host, &apcups_detail);
624 apc_charge_submit (host, &apcups_detail);
625 apc_temp_submit (host, &apcups_detail);
626 apc_time_submit (host, &apcups_detail);
627 apc_freq_submit (host, &apcups_detail);
631 static void apcups_write (char *host, char *inst, char *val)
636 status = snprintf (file, 512, volt_file_template, inst);
639 else if (status >= 512)
642 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
645 static void apc_bvolt_write (char *host, char *inst, char *val)
650 status = snprintf (file, 512, bvolt_file_template, inst);
653 else if (status >= 512)
656 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
659 static void apc_load_write (char *host, char *inst, char *val)
664 status = snprintf (file, 512, load_file_template, inst);
667 else if (status >= 512)
670 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
673 static void apc_charge_write (char *host, char *inst, char *val)
678 status = snprintf (file, 512, charge_file_template, inst);
681 else if (status >= 512)
684 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
687 static void apc_temp_write (char *host, char *inst, char *val)
692 status = snprintf (file, 512, temp_file_template, inst);
695 else if (status >= 512)
698 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
701 static void apc_time_write (char *host, char *inst, char *val)
706 status = snprintf (file, 512, time_file_template, inst);
709 else if (status >= 512)
712 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
715 static void apc_freq_write (char *host, char *inst, char *val)
720 status = snprintf (file, 512, freq_file_template, inst);
723 else if (status >= 512)
726 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
729 void module_register (void)
731 plugin_register (MODULE_NAME, apcups_init, apcups_read, apcups_write);
732 plugin_register ("apcups_bvolt", NULL, NULL, apc_bvolt_write);
733 plugin_register ("apcups_load", NULL, NULL, apc_load_write);
734 plugin_register ("apcups_charge", NULL, NULL, apc_charge_write);
735 plugin_register ("apcups_temp", NULL, NULL, apc_temp_write);
736 plugin_register ("apcups_time", NULL, NULL, apc_time_write);
737 plugin_register ("apcups_freq", NULL, NULL, apc_freq_write);
738 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
741 #endif /* ifdef APCMAIN */