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 * FIXME: Don't know why but without this here atof() was not returning
27 * correct values for me. This is behavior that I don't understand and
28 * should be examined in closer detail.
33 #include "common.h" /* rrd_update_file */
34 #include "plugin.h" /* plugin_register, plugin_submit */
35 #include "configfile.h" /* cf_register */
36 #include "utils_debug.h"
42 #include <sys/socket.h> /* Used for socket connections */
43 #include <netinet/in.h> /* Used for socket connections */
44 #include <arpa/inet.h> /* Used for socket connections */
47 # include <sys/types.h>
50 # include <sys/socket.h>
56 # include <netinet/in.h>
61 # include <arpa/inet.h> /* inet_addr */
64 #include <setjmp.h> /* FIXME: Is this really neccessary? */
65 #include <termios.h> /* FIXME: Is this really neccessary? */
66 #include <sys/ioctl.h> /* FIXME: Is this really neccessary? */
67 #include <sys/ipc.h> /* FIXME: Is this really neccessary? */
68 #include <sys/sem.h> /* FIXME: Is this really neccessary? */
69 #include <sys/shm.h> /* FIXME: Is this really neccessary? */
76 #define _(String) (String)
77 #define N_(String) (String)
79 #define MODULE_NAME "apcups"
85 /* Default values for contacting daemon */
87 static char *host = "localhost"; /* the default host to connect to */
88 static int port = NISPORT; /* the default port to connect to */
90 static char *global_host = NULL;
91 static int global_port = NISPORT;
96 * This is used in do_apc_status() to track the last connection state.
97 * We do not want the read function spitting out an error every "step"
98 * seconds (usually 10 secs).
100 static int apcConnStatus = 0;
102 static struct sockaddr_in tcp_serv_addr; /* socket information */
103 static char *net_errmsg = NULL; /* pointer to error message */
104 static char net_errbuf[256]; /* error message buffer for messages */
109 * The following are normally compiled, when the module is compiled with its
110 * own main for testing these are ifdef'd out.
113 * FIXME: Rename DSes to be more generic and follow established conventions.
114 * However, they currently match the values put out by apcupsd.
117 static char *bvolt_file_template = "apcups/voltage-%s.rrd";
118 static char *bvolt_ds_def[] =
120 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":0:U",
122 static int bvolt_ds_num = 1;
124 static char *load_file_template = "apcups/charge_percent.rrd";
125 static char *load_ds_def[] =
127 "DS:percent:GAUGE:"COLLECTD_HEARTBEAT":0:100",
129 static int load_ds_num = 1;
131 static char *charge_file_template = "apcups/charge.rrd";
132 static char *charge_ds_def[] =
134 "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
136 static int charge_ds_num = 1;
138 static char *time_file_template = "apcups/time.rrd";
139 static char *time_ds_def[] =
141 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
143 static int time_ds_num = 1;
145 static char *temp_file_template = "apcups/temperature.rrd";
146 static char *temp_ds_def[] =
148 /* -273.15 is absolute zero */
149 "DS:temperature:GAUGE:"COLLECTD_HEARTBEAT":-274:U",
151 static int temp_ds_num = 1;
153 static char *freq_file_template = "apcups/frequency-%s.rrd";
154 static char *freq_ds_def[] =
156 "DS:frequency:GAUGE:"COLLECTD_HEARTBEAT":0:U",
158 static int freq_ds_num = 1;
160 static char *config_keys[] =
166 static int config_keys_num = 2;
168 #endif /* ifndef APCMAIN */
200 * Read nbytes from the network.
201 * It is possible that the total bytes require in several
204 static int read_nbytes (int fd, char *ptr, int nbytes)
212 nread = read (fd, ptr, nleft);
213 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
216 return (nread); /* error, or EOF */
223 return (nbytes - nleft); /* return >= 0 */
227 * Write nbytes to the network.
228 * It may require several writes.
230 static int write_nbytes (int fd, void *buf, int buflen)
240 return (nwritten); /* error */
245 nwritten = write (fd, ptr, nleft);
249 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
258 /* If we get here, (nleft <= 0) is true */
262 /* Close the network connection */
263 static void net_close (int sockfd)
267 /* send EOF sentinel */
268 write_nbytes (sockfd, &pktsiz, sizeof (short));
274 * Open a TCP connection to the UPS network server
275 * Returns -1 on error
276 * Returns socket file descriptor otherwise
278 static int net_open (char *host, char *service, int port)
283 struct addrinfo ai_hints;
284 struct addrinfo *ai_return;
285 struct addrinfo *ai_list;
287 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
289 /* Convert the port to a string */
290 snprintf (port_str, 8, "%i", port);
294 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
295 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
296 ai_hints.ai_socktype = SOCK_STREAM;
298 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
301 DBG ("getaddrinfo failed: %s", status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
307 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
309 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
313 /* `ai_list' still holds the current description of the socket.. */
317 DBG ("Unable to open a socket");
318 freeaddrinfo (ai_return);
322 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
324 freeaddrinfo (ai_return);
326 if (status != 0) /* `connect(2)' failed */
328 DBG ("connect failed: %s", strerror (errno));
333 } /* int net_open (char *host, char *service, int port) */
336 * Receive a message from the other end. Each message consists of
337 * two packets. The first is a header that contains the size
338 * of the data that follows in the second packet.
339 * Returns number of bytes read
340 * Returns 0 on end of file
341 * Returns -1 on hard end of file (i.e. network connection close)
342 * Returns -2 on error
344 static int net_recv (int sockfd, char *buf, int buflen)
349 /* get data size -- in short */
350 if ((nbytes = read_nbytes (sockfd, (char *) &pktsiz, sizeof (short))) <= 0)
353 if (nbytes != sizeof (short))
356 pktsiz = ntohs (pktsiz);
359 DBG ("record length too large");
366 /* now read the actual data */
367 if ((nbytes = read_nbytes (sockfd, buf, pktsiz)) <= 0)
370 if (nbytes != pktsiz)
374 } /* static int net_recv (int sockfd, char *buf, int buflen) */
377 * Send a message over the network. The send consists of
378 * two network packets. The first is sends a short containing
379 * the length of the data packet which follows.
380 * Returns zero on success
381 * Returns non-zero on error
383 static int net_send (int sockfd, char *buff, int len)
388 /* send short containing size of data packet */
389 packet_size = htons ((short) len);
391 rc = write_nbytes (sockfd, &packet_size, sizeof (packet_size));
392 if (rc != sizeof (packet_size))
395 /* send data packet */
396 rc = write_nbytes (sockfd, buff, len);
405 * Get and print status from apcupsd NIS server if APCMAIN is defined.
406 * Poplate apcups_detail structure for plugin submit().
408 static int do_apc_status(char *host, int port, struct apc_detail_s *apcups_detail)
410 /* Get and print status from apcupsd NIS server */
411 static int apc_query_server (char *host, int port,
412 struct apc_detail_s *apcups_detail)
417 char recvline[MAXSTRING + 1];
422 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
424 # define PRINT_VALUE(name, val) /**/
429 * TODO: Keep the socket open, if possible.
430 * Can open socket in module init, but without a corresponding module
431 * uninit there is no place to gracefully close the socket.
434 /* TODO: Keep the socket open, if possible */
436 if ((sockfd = net_open (host, NULL, port)) < 0)
439 * When the integer apcConnStatus rolls over it will print out
440 * again, if we haven't connected by then.
442 if (apcConnStatus++ == 0)
443 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed: %s",
446 } else apcConnStatus = 0;
448 if (net_send (sockfd, "status", 6) < 0)
450 syslog (LOG_ERR, "apcups plugin: sending to the apcupsd failed: %s",
457 net_send (sockfd, "status", 6);
460 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
462 recvline[n-1] = '\0';
464 printf ("net_recv = \"%s\"\n", recvline);
465 #endif /* if APCMAIN */
467 tokptr = strtok (recvline, ":");
468 while (tokptr != NULL)
471 if ((tokptr = strtok (NULL, " \t")) == NULL)
474 if (strncmp ("LINEV", key,5) == 0) {
475 value = atof (tokptr);
476 PRINT_VALUE (key, value);
477 apcups_detail->linev = value;
478 } else if (strncmp ("BATTV", key,5) == 0) {
479 value = atof (tokptr);
480 PRINT_VALUE (key, value);
481 apcups_detail->battv = value;
482 } else if (strncmp ("ITEMP", key,5) == 0) {
483 value = atof (tokptr);
484 PRINT_VALUE (key, value);
485 apcups_detail->itemp = value;
486 } else if (strncmp ("LOADPCT", key,7) == 0) {
487 value = atof (tokptr);
488 PRINT_VALUE (key, value);
489 apcups_detail->loadpct = value;
490 } else if (strncmp ("BCHARGE", key,7) == 0) {
491 value = atof (tokptr);
492 PRINT_VALUE (key, value);
493 apcups_detail->bcharge = value;
494 } else if (strncmp ("OUTPUTV", key,7) == 0) {
495 value = atof (tokptr);
496 PRINT_VALUE (key, value);
497 apcups_detail->outputv = value;
498 } else if (strncmp ("LINEFREQ", key,8) == 0) {
499 value = atof (tokptr);
500 PRINT_VALUE (key, value);
501 apcups_detail->linefreq = value;
502 } else if (strncmp ("TIMELEFT", key,8) == 0) {
503 value = atof (tokptr);
504 PRINT_VALUE (key, value);
505 apcups_detail->timeleft = value;
508 tokptr = strtok (NULL, ":");
509 } /* while (tokptr != NULL) */
514 syslog(LOG_ERR, "apcups plugin: Error recieving data: %s",
524 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
530 /* signal that we did in fact connect. */
531 apcups_detail->connected = 1;
543 * This is used for testing apcups in a standalone mode.
544 * Usefull for debugging.
546 int main(int argc, char **argv)
548 int main (int argc, char **argv)
551 /* we are not really going to use this */
552 struct apc_detail_s apcups_detail;
555 openlog("apcups",LOG_PID | LOG_NDELAY | LOG_LOCAL1);
557 if (!*host || strcmp(host, "0.0.0.0") == 0)
559 if (!*host || strcmp (host, "0.0.0.0") == 0)
564 if(do_apc_status(host, port, &apcups_detail) < 0) {
565 printf("apcups: Failed...\n");
569 apc_query_server (global_host, global_port, &apcups_detail);
575 static int apcups_config (char *key, char *value)
578 static char lhost[126];
580 if (strcasecmp (key, "host") == 0)
586 else if (strcasecmp (key, "Port") == 0)
588 int port_tmp = atoi (value);
589 if(port_tmp < 1 || port_tmp > 65535) {
590 syslog (LOG_WARNING, "apcups: `port' failed: %s",
602 if (strcmp(host, "0.0.0.0") == 0)
607 if (strcasecmp (key, "host") == 0)
609 if (global_host != NULL)
614 if ((global_host = strdup (value)) == NULL)
617 else if (strcasecmp (key, "Port") == 0)
619 int port_tmp = atoi (value);
620 if (port_tmp < 1 || port_tmp > 65535)
622 syslog (LOG_WARNING, "apcups plugin: Invalid port: %i", port_tmp);
625 global_port = port_tmp;
635 static void apcups_init (void)
640 static void apc_write_voltage (char *host, char *inst, char *val)
645 status = snprintf (file, 512, bvolt_file_template, inst);
646 if ((status < 1) || (status >= 512))
649 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
652 static void apc_write_charge (char *host, char *inst, char *val)
654 rrd_update_file (host, charge_file_template, val, charge_ds_def, charge_ds_num);
657 static void apc_write_percent (char *host, char *inst, char *val)
659 rrd_update_file (host, load_file_template, val, load_ds_def, load_ds_num);
662 static void apc_write_timeleft (char *host, char *inst, char *val)
664 rrd_update_file (host, time_file_template, val, time_ds_def, time_ds_num);
667 static void apc_write_temperature (char *host, char *inst, char *val)
669 rrd_update_file (host, temp_file_template, val, temp_ds_def, temp_ds_num);
672 static void apc_write_frequency (char *host, char *inst, char *val)
675 struct apc_detail_s apcups_detail;
677 apcups_detail.linev = 0.0;
678 apcups_detail.loadpct = 0.0;
679 apcups_detail.bcharge = 0.0;
680 apcups_detail.timeleft = 0.0;
681 apcups_detail.outputv = 0.0;
682 apcups_detail.itemp = 0.0;
683 apcups_detail.battv = 0.0;
684 apcups_detail.linefreq = 0.0;
685 apcups_detail.connected = 0;
693 if (!*host || strcmp(host, "0.0.0.0") == 0)
696 if(do_apc_status(host, port, &apcups_detail) < 0) return;
699 * if we did not connect then do not bother submitting
700 * zeros. We want rrd files to have NAN.
702 if(!apcups_detail.connected) return;
704 status = snprintf (file, 512, freq_file_template, inst);
705 if ((status < 1) || (status >= 512))
710 apcups_submit (host, &apcups_detail);
711 apc_bvolt_submit (host, &apcups_detail);
712 apc_load_submit (host, &apcups_detail);
713 apc_charge_submit (host, &apcups_detail);
714 apc_temp_submit (host, &apcups_detail);
715 apc_time_submit (host, &apcups_detail);
716 apc_freq_submit (host, &apcups_detail);
718 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
722 static void apc_submit_generic (char *type, char *inst,
729 status = snprintf (file, 512, volt_file_template, inst);
732 else if (status >= 512)
735 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
743 static void apc_bvolt_write (char *host, char *inst, char *val)
748 status = snprintf (file, 512, bvolt_file_template, inst);
751 else if (status >= 512)
754 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
757 status = snprintf (buf, 512, "%u:%f",
758 (unsigned int) curtime, value);
759 if ((status < 1) || (status >= 512))
764 static void apc_load_write (char *host, char *inst, char *val)
769 status = snprintf (file, 512, load_file_template, inst);
772 else if (status >= 512)
775 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
777 plugin_submit (type, inst, buf);
781 static void apc_submit (struct apc_detail_s *apcups_detail)
787 status = snprintf (file, 512, charge_file_template, inst);
790 else if (status >= 512)
793 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
795 apc_submit_generic ("apcups_voltage", "input", apcups_detail->linev);
796 apc_submit_generic ("apcups_voltage", "output", apcups_detail->outputv);
797 apc_submit_generic ("apcups_voltage", "battery", apcups_detail->battv);
798 apc_submit_generic ("apcups_charge", "-", apcups_detail->bcharge);
799 apc_submit_generic ("apcups_charge_pct", "-", apcups_detail->loadpct);
800 apc_submit_generic ("apcups_timeleft", "-", apcups_detail->timeleft);
801 apc_submit_generic ("apcups_temp", "-", apcups_detail->itemp);
802 apc_submit_generic ("apcups_frequency", "input", apcups_detail->linefreq);
806 static void apcups_read (void)
812 status = snprintf (file, 512, temp_file_template, inst);
815 else if (status >= 512)
818 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
821 struct apc_detail_s apcups_detail;
826 static void apc_time_write (char *host, char *inst, char *val)
831 if (global_host == NULL)
834 apcups_detail.linev = -1.0;
835 apcups_detail.outputv = -1.0;
836 apcups_detail.battv = -1.0;
837 apcups_detail.loadpct = -1.0;
838 apcups_detail.bcharge = -1.0;
839 apcups_detail.timeleft = -1.0;
840 apcups_detail.itemp = -300.0;
841 apcups_detail.linefreq = -1.0;
845 status = snprintf (file, 512, time_file_template, inst);
848 else if (status >= 512)
851 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
854 status = apc_query_server (global_host, global_port, &apcups_detail);
857 * if we did not connect then do not bother submitting
858 * zeros. We want rrd files to have NAN.
865 static void apc_freq_write (char *host, char *inst, char *val)
870 status = snprintf (file, 512, freq_file_template, inst);
873 else if (status >= 512)
876 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
879 apc_submit (&apcups_detail);
883 void module_register (void)
885 plugin_register (MODULE_NAME, apcups_init, apcups_read, NULL);
886 plugin_register ("apcups_voltage", NULL, NULL, apc_write_voltage);
887 plugin_register ("apcups_charge", NULL, NULL, apc_write_charge);
888 plugin_register ("apcups_charge_pct", NULL, NULL, apc_write_percent);
889 plugin_register ("apcups_timeleft", NULL, NULL, apc_write_timeleft);
890 plugin_register ("apcups_temp", NULL, NULL, apc_write_temperature);
891 plugin_register ("apcups_frequency", NULL, NULL, apc_write_frequency);
892 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
895 #endif /* ifdef APCMAIN */