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 */
31 # include <sys/types.h>
34 # include <sys/socket.h>
40 # include <netinet/in.h>
43 # include <arpa/inet.h> /* inet_addr */
48 #include <setjmp.h> /* FIXME: Is this really neccessary? */
49 #include <termios.h> /* FIXME: Is this really neccessary? */
50 #include <sys/ioctl.h> /* FIXME: Is this really neccessary? */
51 #include <sys/ipc.h> /* FIXME: Is this really neccessary? */
52 #include <sys/sem.h> /* FIXME: Is this really neccessary? */
53 #include <sys/shm.h> /* FIXME: Is this really neccessary? */
57 #define _(String) (String)
58 #define N_(String) (String)
60 #define MODULE_NAME "apcups"
62 /* Default values for contacting daemon */
63 static char *host = "localhost";
64 static int port = NISPORT;
66 static struct sockaddr_in tcp_serv_addr; /* socket information */
67 static char *net_errmsg = NULL; /* pointer to error message */
68 static char net_errbuf[256]; /* error message buffer for messages */
71 * The following are only if not compiled to test the module with its own main.
73 /* FIXME: Rename DSes to be more generic and follow established conventions. */
75 static char *volt_file_template = "apcups_volt-%s.rrd";
76 static char *volt_ds_def[] =
78 "DS:linev:GAUGE:"COLLECTD_HEARTBEAT":0:250",
79 "DS:outputv:GAUGE:"COLLECTD_HEARTBEAT":0:250",
82 static int volt_ds_num = 2;
84 static char *bvolt_file_template = "apcups_bvolt-%s.rrd";
85 static char *bvolt_ds_def[] =
87 "DS:battv:GAUGE:"COLLECTD_HEARTBEAT":0:100",
89 static int bvolt_ds_num = 1;
91 static char *load_file_template = "apcups_load-%s.rrd";
92 static char *load_ds_def[] =
94 "DS:loadpct:GAUGE:"COLLECTD_HEARTBEAT":0:120",
96 static int load_ds_num = 1;
98 static char *charge_file_template = "apcups_charge-%s.rrd";
99 static char *charge_ds_def[] =
101 "DS:bcharge:GAUGE:"COLLECTD_HEARTBEAT":0:100",
103 static int charge_ds_num = 1;
105 static char *time_file_template = "apcups_time-%s.rrd";
106 static char *time_ds_def[] =
108 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
110 static int time_ds_num = 1;
112 static char *temp_file_template = "apcups_temp-%s.rrd";
113 static char *temp_ds_def[] =
115 "DS:itemp:GAUGE:"COLLECTD_HEARTBEAT":0:100",
117 static int temp_ds_num = 1;
119 static char *freq_file_template = "apcups_freq-%s.rrd";
120 static char *freq_ds_def[] =
122 "DS:linefreq:GAUGE:"COLLECTD_HEARTBEAT":0:65",
124 static int freq_ds_num = 1;
126 static char *config_keys[] =
132 static int config_keys_num = 2;
134 #endif /* ifndef APCMAIN */
151 * Read nbytes from the network.
152 * It is possible that the total bytes require in several
155 static int read_nbytes(int fd, char *ptr, int nbytes)
163 nread = read(fd, ptr, nleft);
164 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
167 return (nread); /* error, or EOF */
174 return (nbytes - nleft); /* return >= 0 */
178 * Write nbytes to the network.
179 * It may require several writes.
181 static int write_nbytes(int fd, void *buf, int buflen)
192 nwritten = write(fd, ptr, nleft);
196 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
204 /* If we get here, (nleft <= 0) is true */
208 /* Close the network connection */
209 static void net_close (int sockfd)
213 /* send EOF sentinel */
214 write_nbytes (sockfd, &pktsiz, sizeof (short));
220 * Open a TCP connection to the UPS network server
221 * Returns -1 on error
222 * Returns socket file descriptor otherwise
224 static int net_open(char *host, char *service, int port)
228 unsigned int inaddr; /* Careful here to use unsigned int for */
229 /* compatibility with Alpha */
232 * Fill in the structure serv_addr with the address of the server that
233 * we want to connect with.
235 memset((char *)&tcp_serv_addr, 0, sizeof(tcp_serv_addr));
236 tcp_serv_addr.sin_family = AF_INET;
237 tcp_serv_addr.sin_port = htons(port);
239 if ((inaddr = inet_addr(host)) != INADDR_NONE) {
240 tcp_serv_addr.sin_addr.s_addr = inaddr;
242 if ((hp = gethostbyname(host)) == NULL) {
243 net_errmsg = "tcp_open: hostname error\n";
247 if (hp->h_length != sizeof(inaddr) || hp->h_addrtype != AF_INET) {
248 net_errmsg = "tcp_open: funny gethostbyname value\n";
252 tcp_serv_addr.sin_addr.s_addr = *(unsigned int *)hp->h_addr;
255 /* Open a TCP socket */
256 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
257 net_errmsg = "tcp_open: cannot open stream socket\n";
261 /* connect to server */
262 #if defined HAVE_OPENBSD_OS || defined HAVE_FREEBSD_OS
264 * Work around a bug in OpenBSD & FreeBSD userspace pthreads
265 * implementations. Rationale is the same as described above.
267 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL));
270 if (connect(sockfd, (struct sockaddr *)&tcp_serv_addr, sizeof(tcp_serv_addr)) < 0) {
271 snprintf(net_errbuf, sizeof(net_errbuf),
272 _("tcp_open: cannot connect to server %s on port %d.\n"
273 "ERR=%s\n"), host, port, strerror(errno));
274 net_errmsg = net_errbuf;
280 } /* int net_open(char *host, char *service, int port) */
283 * Receive a message from the other end. Each message consists of
284 * two packets. The first is a header that contains the size
285 * of the data that follows in the second packet.
286 * Returns number of bytes read
287 * Returns 0 on end of file
288 * Returns -1 on hard end of file (i.e. network connection close)
289 * Returns -2 on error
291 static int net_recv(int sockfd, char *buff, int maxlen)
296 /* get data size -- in short */
297 if ((nbytes = read_nbytes(sockfd, (char *)&pktsiz, sizeof(short))) <= 0) {
298 /* probably pipe broken because client died */
299 return -1; /* assume hard EOF received */
301 if (nbytes != sizeof(short))
304 pktsiz = ntohs(pktsiz); /* decode no. of bytes that follow */
305 if (pktsiz > maxlen) {
306 net_errmsg = "net_recv: record length too large\n";
310 return 0; /* soft EOF */
312 /* now read the actual data */
313 if ((nbytes = read_nbytes(sockfd, buff, pktsiz)) <= 0) {
314 net_errmsg = "net_recv: read_nbytes error\n";
317 if (nbytes != pktsiz) {
318 net_errmsg = "net_recv: error in read_nbytes\n";
322 return (nbytes); /* return actual length of message */
326 * Send a message over the network. The send consists of
327 * two network packets. The first is sends a short containing
328 * the length of the data packet which follows.
329 * Returns zero on success
330 * Returns non-zero on error
332 static int net_send (int sockfd, char *buff, int len)
337 /* send short containing size of data packet */
338 packet_size = htons ((short) len);
340 rc = write_nbytes(sockfd, &packet_size, sizeof (packet_size));
341 if (rc != sizeof(packet_size))
344 /* send data packet */
345 rc = write_nbytes (sockfd, buff, len);
352 /* Get and print status from apcupsd NIS server */
353 static int do_pthreads_status (char *host, int port,
354 struct apc_detail_s *apcups_detail)
358 char recvline[MAXSTRING + 1];
363 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
365 # define PRINT_VALUE(name, val) /**/
368 /* TODO: Keep the socket open, if possible */
369 if ((sockfd = net_open (host, NULL, port)) < 0)
371 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
375 net_send (sockfd, "status", 6);
377 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
381 printf ("net_recv = `%s';\n", recvline);
382 #endif /* if APCMAIN */
384 tokptr = strtok (recvline, ":");
385 while (tokptr != NULL)
388 if ((tokptr = strtok (NULL, " \t")) == NULL)
390 value = atof (tokptr);
391 PRINT_VALUE (key, value);
393 if (strcmp ("LINEV", key) == 0)
394 apcups_detail->linev = value;
395 else if (strcmp ("BATTV", tokptr) == 0)
396 apcups_detail->battv = value;
397 else if (strcmp ("ITEMP", tokptr) == 0)
398 apcups_detail->itemp = value;
399 else if (strcmp ("LOADPCT", tokptr) == 0)
400 apcups_detail->loadpct = value;
401 else if (strcmp ("BCHARGE", tokptr) == 0)
402 apcups_detail->bcharge = value;
403 else if (strcmp ("OUTPUTV", tokptr) == 0)
404 apcups_detail->outputv = value;
405 else if (strcmp ("LINEFREQ", tokptr) == 0)
406 apcups_detail->linefreq = value;
407 else if (strcmp ("TIMELEFT", tokptr) == 0)
408 apcups_detail->timeleft = value;
411 syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
415 tokptr = strtok (NULL, ":");
416 } /* while (tokptr != NULL) */
423 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
431 int main(int argc, char **argv)
433 /* we are not really going to use this */
434 struct apc_detail_s apcups_detail;
436 if (!*host || strcmp(host, "0.0.0.0") == 0)
439 do_pthreads_status(host, port, &apcups_detail);
444 static void apcups_init (void)
449 static int apcups_config (char *key, char *value)
451 static char lhost[126];
453 if (strcasecmp (key, "host") == 0)
459 else if (strcasecmp (key, "Port") == 0)
461 int port_tmp = atoi (value);
462 if(port_tmp < 1 || port_tmp > 65535) {
463 syslog (LOG_WARNING, "apcups: `port' failed: %s",
478 static void apcups_submit (char *host,
479 struct apc_detail_s *apcups_detail)
483 if (snprintf (buf, BUFSIZE, "%u:%f:%f",
484 (unsigned int) curtime,
485 apcups_detail->linev,
486 apcups_detail->outputv) >= BUFSIZE)
489 plugin_submit (MODULE_NAME, host, buf);
492 static void apc_bvolt_submit (char *host,
493 struct apc_detail_s *apcups_detail)
497 if (snprintf (buf, BUFSIZE, "%u:%f",
498 (unsigned int) curtime,
499 apcups_detail->battv) >= BUFSIZE)
502 plugin_submit ("apcups_bvolt", host, buf);
505 static void apc_load_submit (char *host,
506 struct apc_detail_s *apcups_detail)
510 if (snprintf (buf, BUFSIZE, "%u:%f",
511 (unsigned int) curtime,
512 apcups_detail->loadpct) >= BUFSIZE)
515 plugin_submit ("apcups_load", host, buf);
518 static void apc_charge_submit (char *host,
519 struct apc_detail_s *apcups_detail)
523 if (snprintf (buf, BUFSIZE, "%u:%f",
524 (unsigned int) curtime,
525 apcups_detail->bcharge) >= BUFSIZE)
528 plugin_submit ("apcups_charge", host, buf);
531 static void apc_temp_submit (char *host,
532 struct apc_detail_s *apcups_detail)
536 if (snprintf (buf, BUFSIZE, "%u:%f",
537 (unsigned int) curtime,
538 apcups_detail->itemp) >= BUFSIZE)
541 plugin_submit ("apcups_temp", host, buf);
544 static void apc_time_submit (char *host,
545 struct apc_detail_s *apcups_detail)
549 if (snprintf (buf, BUFSIZE, "%u:%f",
550 (unsigned int) curtime,
551 apcups_detail->timeleft) >= BUFSIZE)
554 plugin_submit ("apcups_time", host, buf);
557 static void apc_freq_submit (char *host,
558 struct apc_detail_s *apcups_detail)
562 if (snprintf (buf, BUFSIZE, "%u:%f",
563 (unsigned int) curtime,
564 apcups_detail->linefreq) >= BUFSIZE)
567 plugin_submit ("apcups_freq", host, buf);
571 static void apcups_read (void)
573 struct apc_detail_s apcups_detail;
576 apcups_detail.linev = 0.0;
577 apcups_detail.loadpct = 0.0;
578 apcups_detail.bcharge = 0.0;
579 apcups_detail.timeleft = 0.0;
580 apcups_detail.outputv = 0.0;
581 apcups_detail.itemp = 0.0;
582 apcups_detail.battv = 0.0;
583 apcups_detail.linefreq = 0.0;
586 if (!*host || strcmp(host, "0.0.0.0") == 0)
589 status = do_pthreads_status(host, port, &apcups_detail);
592 * if we did not connect then do not bother submitting
593 * zeros. We want rrd files to have NAN.
598 apcups_submit (host, &apcups_detail);
599 apc_bvolt_submit (host, &apcups_detail);
600 apc_load_submit (host, &apcups_detail);
601 apc_charge_submit (host, &apcups_detail);
602 apc_temp_submit (host, &apcups_detail);
603 apc_time_submit (host, &apcups_detail);
604 apc_freq_submit (host, &apcups_detail);
608 static void apcups_write (char *host, char *inst, char *val)
613 status = snprintf (file, 512, volt_file_template, inst);
616 else if (status >= 512)
619 rrd_update_file (host, file, val, volt_ds_def, volt_ds_num);
622 static void apc_bvolt_write (char *host, char *inst, char *val)
627 status = snprintf (file, 512, bvolt_file_template, inst);
630 else if (status >= 512)
633 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
636 static void apc_load_write (char *host, char *inst, char *val)
641 status = snprintf (file, 512, load_file_template, inst);
644 else if (status >= 512)
647 rrd_update_file (host, file, val, load_ds_def, load_ds_num);
650 static void apc_charge_write (char *host, char *inst, char *val)
655 status = snprintf (file, 512, charge_file_template, inst);
658 else if (status >= 512)
661 rrd_update_file (host, file, val, charge_ds_def, charge_ds_num);
664 static void apc_temp_write (char *host, char *inst, char *val)
669 status = snprintf (file, 512, temp_file_template, inst);
672 else if (status >= 512)
675 rrd_update_file (host, file, val, temp_ds_def, temp_ds_num);
678 static void apc_time_write (char *host, char *inst, char *val)
683 status = snprintf (file, 512, time_file_template, inst);
686 else if (status >= 512)
689 rrd_update_file (host, file, val, time_ds_def, time_ds_num);
692 static void apc_freq_write (char *host, char *inst, char *val)
697 status = snprintf (file, 512, freq_file_template, inst);
700 else if (status >= 512)
703 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
706 void module_register (void)
708 plugin_register (MODULE_NAME, apcups_init, apcups_read, apcups_write);
709 plugin_register ("apcups_bvolt", NULL, NULL, apc_bvolt_write);
710 plugin_register ("apcups_load", NULL, NULL, apc_load_write);
711 plugin_register ("apcups_charge", NULL, NULL, apc_charge_write);
712 plugin_register ("apcups_temp", NULL, NULL, apc_temp_write);
713 plugin_register ("apcups_time", NULL, NULL, apc_time_write);
714 plugin_register ("apcups_freq", NULL, NULL, apc_freq_write);
715 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
718 #endif /* ifdef APCMAIN */