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"
39 # include <sys/types.h>
42 # include <sys/socket.h>
48 # include <netinet/in.h>
53 #define MODULE_NAME "apcups"
55 /* Default values for contacting daemon */
56 static char *global_host = NULL;
57 static int global_port = NISPORT;
60 * The following are only if not compiled to test the module with its own main.
62 /* FIXME: Rename DSes to be more generic and follow established conventions. */
64 static char *bvolt_file_template = "apcups/voltage-%s.rrd";
65 static char *bvolt_ds_def[] =
67 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":0:U",
69 static int bvolt_ds_num = 1;
71 static char *load_file_template = "apcups/charge_percent.rrd";
72 static char *load_ds_def[] =
74 "DS:percent:GAUGE:"COLLECTD_HEARTBEAT":0:100",
76 static int load_ds_num = 1;
78 static char *charge_file_template = "apcups/charge.rrd";
79 static char *charge_ds_def[] =
81 "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
83 static int charge_ds_num = 1;
85 static char *time_file_template = "apcups/time.rrd";
86 static char *time_ds_def[] =
88 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
90 static int time_ds_num = 1;
92 static char *temp_file_template = "apcups/temperature.rrd";
93 static char *temp_ds_def[] =
95 /* -273.15 is absolute zero */
96 "DS:temperature:GAUGE:"COLLECTD_HEARTBEAT":-274:U",
98 static int temp_ds_num = 1;
100 static char *freq_file_template = "apcups/frequency-%s.rrd";
101 static char *freq_ds_def[] =
103 "DS:frequency:GAUGE:"COLLECTD_HEARTBEAT":0:U",
105 static int freq_ds_num = 1;
107 static char *config_keys[] =
113 static int config_keys_num = 2;
115 #endif /* ifndef APCMAIN */
132 * Read nbytes from the network.
133 * It is possible that the total bytes require in several
136 static int read_nbytes (int fd, char *ptr, int nbytes)
144 nread = read (fd, ptr, nleft);
145 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
148 return (nread); /* error, or EOF */
155 return (nbytes - nleft); /* return >= 0 */
159 * Write nbytes to the network.
160 * It may require several writes.
162 static int write_nbytes (int fd, void *buf, int buflen)
173 nwritten = write (fd, ptr, nleft);
177 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
185 /* If we get here, (nleft <= 0) is true */
189 /* Close the network connection */
190 static void net_close (int sockfd)
194 /* send EOF sentinel */
195 write_nbytes (sockfd, &pktsiz, sizeof (short));
201 * Open a TCP connection to the UPS network server
202 * Returns -1 on error
203 * Returns socket file descriptor otherwise
205 static int net_open (char *host, char *service, int port)
210 struct addrinfo ai_hints;
211 struct addrinfo *ai_return;
212 struct addrinfo *ai_list;
214 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
216 /* Convert the port to a string */
217 snprintf (port_str, 8, "%i", port);
221 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
222 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
223 ai_hints.ai_socktype = SOCK_STREAM;
225 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
228 DBG ("getaddrinfo failed: %s", status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
234 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
236 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
240 /* `ai_list' still holds the current description of the socket.. */
244 DBG ("Unable to open a socket");
245 freeaddrinfo (ai_return);
249 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
251 freeaddrinfo (ai_return);
253 if (status != 0) /* `connect(2)' failed */
255 DBG ("connect failed: %s", strerror (errno));
260 } /* int net_open (char *host, char *service, int port) */
263 * Receive a message from the other end. Each message consists of
264 * two packets. The first is a header that contains the size
265 * of the data that follows in the second packet.
266 * Returns number of bytes read
267 * Returns 0 on end of file
268 * Returns -1 on hard end of file (i.e. network connection close)
269 * Returns -2 on error
271 static int net_recv (int sockfd, char *buf, int buflen)
276 /* get data size -- in short */
277 if ((nbytes = read_nbytes (sockfd, (char *) &pktsiz, sizeof (short))) <= 0)
280 if (nbytes != sizeof (short))
283 pktsiz = ntohs (pktsiz);
286 DBG ("record length too large");
293 /* now read the actual data */
294 if ((nbytes = read_nbytes (sockfd, buf, pktsiz)) <= 0)
297 if (nbytes != pktsiz)
301 } /* static int net_recv (int sockfd, char *buf, int buflen) */
304 * Send a message over the network. The send consists of
305 * two network packets. The first is sends a short containing
306 * the length of the data packet which follows.
307 * Returns zero on success
308 * Returns non-zero on error
310 static int net_send (int sockfd, char *buff, int len)
315 /* send short containing size of data packet */
316 packet_size = htons ((short) len);
318 rc = write_nbytes (sockfd, &packet_size, sizeof (packet_size));
319 if (rc != sizeof (packet_size))
322 /* send data packet */
323 rc = write_nbytes (sockfd, buff, len);
330 /* Get and print status from apcupsd NIS server */
331 static int apc_query_server (char *host, int port,
332 struct apc_detail_s *apcups_detail)
341 static int complain = 0;
344 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
346 # define PRINT_VALUE(name, val) /**/
349 /* TODO: Keep the socket open, if possible */
350 if ((sockfd = net_open (host, NULL, port)) < 0)
352 /* Complain once every six hours.
353 int complain_step = 21600 / atoi (COLLECTD_STEP);
355 if ((complain % complain_step) == 0)
356 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
363 if (net_send (sockfd, "status", 6) < 0)
365 syslog (LOG_ERR, "apcups plugin: Writing to the socket failed.");
369 /* XXX: Do we read `n' or `n-1' bytes? */
370 while ((n = net_recv (sockfd, recvline, sizeof (recvline) - 1)) > 0)
372 assert (n < sizeof (recvline));
375 printf ("net_recv = `%s';\n", recvline);
376 #endif /* if APCMAIN */
378 tokptr = strtok (recvline, ":");
379 while (tokptr != NULL)
382 if ((tokptr = strtok (NULL, " \t")) == NULL)
384 value = atof (tokptr);
385 PRINT_VALUE (key, value);
387 if (strcmp ("LINEV", key) == 0)
388 apcups_detail->linev = value;
389 else if (strcmp ("BATTV", tokptr) == 0)
390 apcups_detail->battv = value;
391 else if (strcmp ("ITEMP", tokptr) == 0)
392 apcups_detail->itemp = value;
393 else if (strcmp ("LOADPCT", tokptr) == 0)
394 apcups_detail->loadpct = value;
395 else if (strcmp ("BCHARGE", tokptr) == 0)
396 apcups_detail->bcharge = value;
397 else if (strcmp ("OUTPUTV", tokptr) == 0)
398 apcups_detail->outputv = value;
399 else if (strcmp ("LINEFREQ", tokptr) == 0)
400 apcups_detail->linefreq = value;
401 else if (strcmp ("TIMELEFT", tokptr) == 0)
402 apcups_detail->timeleft = value;
405 syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
409 tokptr = strtok (NULL, ":");
410 } /* while (tokptr != NULL) */
417 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
426 * This is used for testing apcups in a standalone mode.
427 * Usefull for debugging.
429 int main (int argc, char **argv)
431 /* we are not really going to use this */
432 struct apc_detail_s apcups_detail;
434 openlog ("apcups", LOG_PID | LOG_NDELAY | LOG_LOCAL1);
436 if (!*host || strcmp (host, "0.0.0.0") == 0)
439 if(do_apc_status (host, port, &apcups_detail) < 0)
441 printf("apcups: Failed...\n");
445 apc_query_server (global_host, global_port, &apcups_detail);
450 static int apcups_config (char *key, char *value)
452 if (strcasecmp (key, "host") == 0)
454 if (global_host != NULL)
459 if ((global_host = strdup (value)) == NULL)
462 else if (strcasecmp (key, "Port") == 0)
464 int port_tmp = atoi (value);
465 if (port_tmp < 1 || port_tmp > 65535)
467 syslog (LOG_WARNING, "apcups plugin: Invalid port: %i", port_tmp);
470 global_port = port_tmp;
479 static void apcups_init (void)
484 static void apc_write_voltage (char *host, char *inst, char *val)
489 status = snprintf (file, 512, bvolt_file_template, inst);
490 if ((status < 1) || (status >= 512))
493 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
496 static void apc_write_charge (char *host, char *inst, char *val)
498 rrd_update_file (host, charge_file_template, val, charge_ds_def, charge_ds_num);
501 static void apc_write_percent (char *host, char *inst, char *val)
503 rrd_update_file (host, load_file_template, val, load_ds_def, load_ds_num);
506 static void apc_write_timeleft (char *host, char *inst, char *val)
508 rrd_update_file (host, time_file_template, val, time_ds_def, time_ds_num);
511 static void apc_write_temperature (char *host, char *inst, char *val)
513 rrd_update_file (host, temp_file_template, val, temp_ds_def, temp_ds_num);
516 static void apc_write_frequency (char *host, char *inst, char *val)
521 status = snprintf (file, 512, freq_file_template, inst);
522 if ((status < 1) || (status >= 512))
525 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
528 static void apc_submit_generic (char *type, char *inst,
534 status = snprintf (buf, 512, "%u:%f",
535 (unsigned int) curtime, value);
536 if ((status < 1) || (status >= 512))
539 plugin_submit (type, inst, buf);
542 static void apc_submit (struct apc_detail_s *apcups_detail)
544 apc_submit_generic ("apcups_voltage", "input", apcups_detail->linev);
545 apc_submit_generic ("apcups_voltage", "output", apcups_detail->outputv);
546 apc_submit_generic ("apcups_voltage", "battery", apcups_detail->battv);
547 apc_submit_generic ("apcups_charge", "-", apcups_detail->bcharge);
548 apc_submit_generic ("apcups_charge_pct", "-", apcups_detail->loadpct);
549 apc_submit_generic ("apcups_timeleft", "-", apcups_detail->timeleft);
550 apc_submit_generic ("apcups_temp", "-", apcups_detail->itemp);
551 apc_submit_generic ("apcups_frequency", "input", apcups_detail->linefreq);
554 static void apcups_read (void)
556 struct apc_detail_s apcups_detail;
559 if (global_host == NULL)
562 apcups_detail.linev = -1.0;
563 apcups_detail.outputv = -1.0;
564 apcups_detail.battv = -1.0;
565 apcups_detail.loadpct = -1.0;
566 apcups_detail.bcharge = -1.0;
567 apcups_detail.timeleft = -1.0;
568 apcups_detail.itemp = -300.0;
569 apcups_detail.linefreq = -1.0;
571 status = apc_query_server (global_host, global_port, &apcups_detail);
574 * if we did not connect then do not bother submitting
575 * zeros. We want rrd files to have NAN.
580 apc_submit (&apcups_detail);
583 void module_register (void)
585 plugin_register (MODULE_NAME, apcups_init, apcups_read, NULL);
586 plugin_register ("apcups_voltage", NULL, NULL, apc_write_voltage);
587 plugin_register ("apcups_charge", NULL, NULL, apc_write_charge);
588 plugin_register ("apcups_charge_pct", NULL, NULL, apc_write_percent);
589 plugin_register ("apcups_timeleft", NULL, NULL, apc_write_timeleft);
590 plugin_register ("apcups_temp", NULL, NULL, apc_write_temperature);
591 plugin_register ("apcups_frequency", NULL, NULL, apc_write_frequency);
592 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
595 #endif /* ifdef APCMAIN */