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"
40 # include <sys/types.h>
43 # include <sys/socket.h>
50 # include <netinet/in.h>
59 #define MODULE_NAME "apcups"
61 /* Default values for contacting daemon */
62 static char *global_host = "localhost";
63 static int global_port = NISPORT;
66 * The following are only if not compiled to test the module with its own main.
69 static char *bvolt_file_template = "apcups/voltage-%s.rrd";
70 static char *bvolt_ds_def[] =
72 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":0:U",
74 static int bvolt_ds_num = 1;
76 static char *load_file_template = "apcups/charge_percent.rrd";
77 static char *load_ds_def[] =
79 "DS:percent:GAUGE:"COLLECTD_HEARTBEAT":0:110",
81 static int load_ds_num = 1;
83 static char *charge_file_template = "apcups/charge.rrd";
84 static char *charge_ds_def[] =
86 "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
88 static int charge_ds_num = 1;
90 static char *time_file_template = "apcups/time.rrd";
91 static char *time_ds_def[] =
93 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
95 static int time_ds_num = 1;
97 static char *temp_file_template = "apcups/temperature.rrd";
98 static char *temp_ds_def[] =
100 /* -273.15 is absolute zero */
101 "DS:temperature:GAUGE:"COLLECTD_HEARTBEAT":-274:U",
103 static int temp_ds_num = 1;
105 static char *freq_file_template = "apcups/frequency-%s.rrd";
106 static char *freq_ds_def[] =
108 "DS:frequency:GAUGE:"COLLECTD_HEARTBEAT":0:U",
110 static int freq_ds_num = 1;
112 static char *config_keys[] =
118 static int config_keys_num = 2;
120 #endif /* if APCMAIN */
137 * Read nbytes from the network.
138 * It is possible that the total bytes require in several
141 static int read_nbytes (int *fd, char *ptr, int nbytes)
151 while ((nleft > 0) && (nread != 0))
153 nread = read (*fd, ptr, nleft);
155 if (nread == -1 && (errno == EINTR || errno == EAGAIN))
161 syslog (LOG_ERR, "apcups plugin: write failed: %s", strerror (errno));
169 return (nbytes - nleft);
173 * Write nbytes to the network.
174 * It may require several writes.
176 static int write_nbytes (int *fd, void *buf, int buflen)
190 nwritten = write (*fd, ptr, nleft);
192 if ((nwritten == -1) && ((errno == EAGAIN) || (errno == EINTR)))
197 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
206 /* If we get here, (nleft <= 0) is true */
210 /* Close the network connection */
211 static void net_close (int *fd)
217 /* send EOF sentinel */
218 write_nbytes (fd, &pktsiz, sizeof (short));
226 * Open a TCP connection to the UPS network server
227 * Returns -1 on error
228 * Returns socket file descriptor otherwise
230 static int net_open (char *host, char *service, int port)
235 struct addrinfo ai_hints;
236 struct addrinfo *ai_return;
237 struct addrinfo *ai_list;
239 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
241 /* Convert the port to a string */
242 snprintf (port_str, 8, "%i", port);
246 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
247 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
248 ai_hints.ai_socktype = SOCK_STREAM;
250 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
253 DBG ("getaddrinfo failed: %s", status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
259 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
261 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
265 /* `ai_list' still holds the current description of the socket.. */
269 DBG ("Unable to open a socket");
270 freeaddrinfo (ai_return);
274 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
276 freeaddrinfo (ai_return);
278 if (status != 0) /* `connect(2)' failed */
280 DBG ("connect failed: %s", strerror (errno));
285 } /* int net_open (char *host, char *service, int port) */
288 * Receive a message from the other end. Each message consists of
289 * two packets. The first is a header that contains the size
290 * of the data that follows in the second packet.
291 * Returns number of bytes read
292 * Returns 0 on end of file
293 * Returns -1 on hard end of file (i.e. network connection close)
294 * Returns -2 on error
296 static int net_recv (int *sockfd, char *buf, int buflen)
301 /* get data size -- in short */
302 if ((nbytes = read_nbytes (sockfd, (char *) &pktsiz, sizeof (short))) <= 0)
305 if (nbytes != sizeof (short))
308 pktsiz = ntohs (pktsiz);
311 DBG ("record length too large");
318 /* now read the actual data */
319 if ((nbytes = read_nbytes (sockfd, buf, pktsiz)) <= 0)
322 if (nbytes != pktsiz)
326 } /* static int net_recv (int sockfd, char *buf, int buflen) */
329 * Send a message over the network. The send consists of
330 * two network packets. The first is sends a short containing
331 * the length of the data packet which follows.
332 * Returns zero on success
333 * Returns non-zero on error
335 static int net_send (int *sockfd, char *buff, int len)
342 /* send short containing size of data packet */
343 packet_size = htons ((short) len);
345 rc = write_nbytes (sockfd, &packet_size, sizeof (packet_size));
346 if (rc != sizeof (packet_size))
349 /* send data packet */
350 rc = write_nbytes (sockfd, buff, len);
357 /* Get and print status from apcupsd NIS server */
358 static int apc_query_server (char *host, int port,
359 struct apc_detail_s *apcups_detail)
367 static int sockfd = -1;
368 static unsigned int complain = 0;
371 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
373 # define PRINT_VALUE(name, val) /**/
378 if ((sockfd = net_open (host, NULL, port)) < 0)
380 /* Complain first time and once every six hours. */
381 int complain_step = 21600 / atoi (COLLECTD_STEP);
383 if (complain == 0 || (complain % complain_step) == 0)
384 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
390 syslog (LOG_ERR, "apcups plugin: Connection re-established to the apcupsd.");
395 if (net_send (&sockfd, "status", 6) < 0)
397 syslog (LOG_ERR, "apcups plugin: Writing to the socket failed.");
401 /* XXX: Do we read `n' or `n-1' bytes? */
402 while ((n = net_recv (&sockfd, recvline, sizeof (recvline) - 1)) > 0)
404 assert (n < sizeof (recvline));
407 printf ("net_recv = `%s';\n", recvline);
408 #endif /* if APCMAIN */
410 tokptr = strtok (recvline, ":");
411 while (tokptr != NULL)
414 if ((tokptr = strtok (NULL, " \t")) == NULL)
416 value = atof (tokptr);
417 PRINT_VALUE (key, value);
419 if (strcmp ("LINEV", key) == 0)
420 apcups_detail->linev = value;
421 else if (strcmp ("BATTV", tokptr) == 0)
422 apcups_detail->battv = value;
423 else if (strcmp ("ITEMP", tokptr) == 0)
424 apcups_detail->itemp = value;
425 else if (strcmp ("LOADPCT", tokptr) == 0)
426 apcups_detail->loadpct = value;
427 else if (strcmp ("BCHARGE", tokptr) == 0)
428 apcups_detail->bcharge = value;
429 else if (strcmp ("OUTPUTV", tokptr) == 0)
430 apcups_detail->outputv = value;
431 else if (strcmp ("LINEFREQ", tokptr) == 0)
432 apcups_detail->linefreq = value;
433 else if (strcmp ("TIMELEFT", tokptr) == 0)
434 apcups_detail->timeleft = value;
436 tokptr = strtok (NULL, ":");
437 } /* while (tokptr != NULL) */
442 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
445 /* close the opened socket */
455 * This is used for testing apcups in a standalone mode.
456 * Usefull for debugging.
458 int main (int argc, char **argv)
460 /* we are not really going to use this */
461 struct apc_detail_s apcups_detail;
463 openlog ("apcups", LOG_PID | LOG_NDELAY | LOG_LOCAL1, LOG_USER);
465 if (global_host == NULL || strcmp (global_host, "0.0.0.0") == 0)
466 global_host = "localhost";
468 if(apc_query_server (global_host, global_port, &apcups_detail) < 0)
470 printf("apcups: Failed...\n");
477 static int apcups_config (char *key, char *value)
479 if (strcasecmp (key, "host") == 0)
481 if (global_host != NULL)
486 if ((global_host = strdup (value)) == NULL)
489 else if (strcasecmp (key, "Port") == 0)
491 int port_tmp = atoi (value);
492 if (port_tmp < 1 || port_tmp > 65535)
494 syslog (LOG_WARNING, "apcups plugin: Invalid port: %i", port_tmp);
497 global_port = port_tmp;
506 static void apcups_init (void)
511 static void apc_write_voltage (char *host, char *inst, char *val)
516 status = snprintf (file, 512, bvolt_file_template, inst);
517 if ((status < 1) || (status >= 512))
520 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
523 static void apc_write_charge (char *host, char *inst, char *val)
525 rrd_update_file (host, charge_file_template, val, charge_ds_def, charge_ds_num);
528 static void apc_write_percent (char *host, char *inst, char *val)
530 rrd_update_file (host, load_file_template, val, load_ds_def, load_ds_num);
533 static void apc_write_timeleft (char *host, char *inst, char *val)
535 rrd_update_file (host, time_file_template, val, time_ds_def, time_ds_num);
538 static void apc_write_temperature (char *host, char *inst, char *val)
540 rrd_update_file (host, temp_file_template, val, temp_ds_def, temp_ds_num);
543 static void apc_write_frequency (char *host, char *inst, char *val)
548 status = snprintf (file, 512, freq_file_template, inst);
549 if ((status < 1) || (status >= 512))
552 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
555 static void apc_submit_generic (char *type, char *inst,
561 status = snprintf (buf, 512, "%u:%f",
562 (unsigned int) curtime, value);
563 if ((status < 1) || (status >= 512))
566 plugin_submit (type, inst, buf);
569 static void apc_submit (struct apc_detail_s *apcups_detail)
571 apc_submit_generic ("apcups_voltage", "input", apcups_detail->linev);
572 apc_submit_generic ("apcups_voltage", "output", apcups_detail->outputv);
573 apc_submit_generic ("apcups_voltage", "battery", apcups_detail->battv);
574 apc_submit_generic ("apcups_charge", "-", apcups_detail->bcharge);
575 apc_submit_generic ("apcups_charge_pct", "-", apcups_detail->loadpct);
576 apc_submit_generic ("apcups_timeleft", "-", apcups_detail->timeleft);
577 apc_submit_generic ("apcups_temp", "-", apcups_detail->itemp);
578 apc_submit_generic ("apcups_frequency", "input", apcups_detail->linefreq);
581 static void apcups_read (void)
583 struct apc_detail_s apcups_detail;
586 if (global_host == NULL)
589 apcups_detail.linev = -1.0;
590 apcups_detail.outputv = -1.0;
591 apcups_detail.battv = -1.0;
592 apcups_detail.loadpct = -1.0;
593 apcups_detail.bcharge = -1.0;
594 apcups_detail.timeleft = -1.0;
595 apcups_detail.itemp = -300.0;
596 apcups_detail.linefreq = -1.0;
598 status = apc_query_server (global_host, global_port, &apcups_detail);
601 * if we did not connect then do not bother submitting
602 * zeros. We want rrd files to have NAN.
607 apc_submit (&apcups_detail);
610 void module_register (void)
612 plugin_register (MODULE_NAME, apcups_init, apcups_read, NULL);
613 plugin_register ("apcups_voltage", NULL, NULL, apc_write_voltage);
614 plugin_register ("apcups_charge", NULL, NULL, apc_write_charge);
615 plugin_register ("apcups_charge_pct", NULL, NULL, apc_write_percent);
616 plugin_register ("apcups_timeleft", NULL, NULL, apc_write_timeleft);
617 plugin_register ("apcups_temp", NULL, NULL, apc_write_temperature);
618 plugin_register ("apcups_frequency", NULL, NULL, apc_write_frequency);
619 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
622 #endif /* if APCMAIN */