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 */
29 #include "utils_debug.h"
32 # include <sys/types.h>
35 # include <sys/socket.h>
41 # include <netinet/in.h>
46 # include <arpa/inet.h> /* inet_addr */
49 #include <setjmp.h> /* FIXME: Is this really neccessary? */
50 #include <termios.h> /* FIXME: Is this really neccessary? */
51 #include <sys/ioctl.h> /* FIXME: Is this really neccessary? */
52 #include <sys/ipc.h> /* FIXME: Is this really neccessary? */
53 #include <sys/sem.h> /* FIXME: Is this really neccessary? */
54 #include <sys/shm.h> /* FIXME: Is this really neccessary? */
58 #define _(String) (String)
59 #define N_(String) (String)
61 #define MODULE_NAME "apcups"
63 /* Default values for contacting daemon */
64 static char *host = "localhost";
65 static int port = NISPORT;
68 * The following are only if not compiled to test the module with its own main.
70 /* FIXME: Rename DSes to be more generic and follow established conventions. */
72 static char *bvolt_file_template = "apcups/voltage-%s.rrd";
73 static char *bvolt_ds_def[] =
75 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":0:U",
77 static int bvolt_ds_num = 1;
79 static char *load_file_template = "apcups/charge_percent.rrd";
80 static char *load_ds_def[] =
82 "DS:percent:GAUGE:"COLLECTD_HEARTBEAT":0:100",
84 static int load_ds_num = 1;
86 static char *charge_file_template = "apcups/charge.rrd";
87 static char *charge_ds_def[] =
89 "DS:charge:GAUGE:"COLLECTD_HEARTBEAT":0:U",
91 static int charge_ds_num = 1;
93 static char *time_file_template = "apcups/time.rrd";
94 static char *time_ds_def[] =
96 "DS:timeleft:GAUGE:"COLLECTD_HEARTBEAT":0:100",
98 static int time_ds_num = 1;
100 static char *temp_file_template = "apcups/temperature.rrd";
101 static char *temp_ds_def[] =
103 /* -273.15 is absolute zero */
104 "DS:temperature:GAUGE:"COLLECTD_HEARTBEAT":-274:U",
106 static int temp_ds_num = 1;
108 static char *freq_file_template = "apcups/frequency-%s.rrd";
109 static char *freq_ds_def[] =
111 "DS:frequency:GAUGE:"COLLECTD_HEARTBEAT":0:U",
113 static int freq_ds_num = 1;
115 static char *config_keys[] =
121 static int config_keys_num = 2;
123 #endif /* ifndef APCMAIN */
140 * Read nbytes from the network.
141 * It is possible that the total bytes require in several
144 static int read_nbytes(int fd, char *ptr, int nbytes)
152 nread = read(fd, ptr, nleft);
153 } while (nread == -1 && (errno == EINTR || errno == EAGAIN));
156 return (nread); /* error, or EOF */
163 return (nbytes - nleft); /* return >= 0 */
167 * Write nbytes to the network.
168 * It may require several writes.
170 static int write_nbytes(int fd, void *buf, int buflen)
181 nwritten = write(fd, ptr, nleft);
185 syslog (LOG_ERR, "Writing to socket failed: %s", strerror (errno));
193 /* If we get here, (nleft <= 0) is true */
197 /* Close the network connection */
198 static void net_close (int sockfd)
202 /* send EOF sentinel */
203 write_nbytes (sockfd, &pktsiz, sizeof (short));
209 * Open a TCP connection to the UPS network server
210 * Returns -1 on error
211 * Returns socket file descriptor otherwise
213 static int net_open (char *host, char *service, int port)
218 struct addrinfo ai_hints;
219 struct addrinfo *ai_return;
220 struct addrinfo *ai_list;
222 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
224 /* Convert the port to a string */
225 snprintf (port_str, 8, "%i", port);
229 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
230 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
231 ai_hints.ai_socktype = SOCK_STREAM;
233 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
236 DBG ("getaddrinfo failed: %s", status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
242 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
244 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
248 /* `ai_list' still holds the current description of the socket.. */
252 DBG ("Unable to open a socket");
253 freeaddrinfo (ai_return);
257 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
259 freeaddrinfo (ai_return);
261 if (status != 0) /* `connect(2)' failed */
263 DBG ("connect failed: %s", strerror (errno));
268 } /* int net_open(char *host, char *service, int port) */
271 * Receive a message from the other end. Each message consists of
272 * two packets. The first is a header that contains the size
273 * of the data that follows in the second packet.
274 * Returns number of bytes read
275 * Returns 0 on end of file
276 * Returns -1 on hard end of file (i.e. network connection close)
277 * Returns -2 on error
279 static int net_recv (int sockfd, char *buf, int buflen)
284 /* get data size -- in short */
285 if ((nbytes = read_nbytes (sockfd, (char *) &pktsiz, sizeof (short))) <= 0)
288 if (nbytes != sizeof (short))
291 pktsiz = ntohs(pktsiz);
294 DBG ("record length too large");
301 /* now read the actual data */
302 if ((nbytes = read_nbytes (sockfd, buf, pktsiz)) <= 0)
305 if (nbytes != pktsiz)
309 } /* static int net_recv (int sockfd, char *buf, int buflen) */
312 * Send a message over the network. The send consists of
313 * two network packets. The first is sends a short containing
314 * the length of the data packet which follows.
315 * Returns zero on success
316 * Returns non-zero on error
318 static int net_send (int sockfd, char *buff, int len)
323 /* send short containing size of data packet */
324 packet_size = htons ((short) len);
326 rc = write_nbytes(sockfd, &packet_size, sizeof (packet_size));
327 if (rc != sizeof(packet_size))
330 /* send data packet */
331 rc = write_nbytes (sockfd, buff, len);
338 /* Get and print status from apcupsd NIS server */
339 static int do_pthreads_status (char *host, int port,
340 struct apc_detail_s *apcups_detail)
344 char recvline[MAXSTRING + 1];
349 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
351 # define PRINT_VALUE(name, val) /**/
354 /* TODO: Keep the socket open, if possible */
355 if ((sockfd = net_open (host, NULL, port)) < 0)
357 syslog (LOG_ERR, "apcups plugin: Connecting to the apcupsd failed.");
361 net_send (sockfd, "status", 6);
363 while ((n = net_recv (sockfd, recvline, sizeof (recvline))) > 0)
367 printf ("net_recv = `%s';\n", recvline);
368 #endif /* if APCMAIN */
370 tokptr = strtok (recvline, ":");
371 while (tokptr != NULL)
374 if ((tokptr = strtok (NULL, " \t")) == NULL)
376 value = atof (tokptr);
377 PRINT_VALUE (key, value);
379 if (strcmp ("LINEV", key) == 0)
380 apcups_detail->linev = value;
381 else if (strcmp ("BATTV", tokptr) == 0)
382 apcups_detail->battv = value;
383 else if (strcmp ("ITEMP", tokptr) == 0)
384 apcups_detail->itemp = value;
385 else if (strcmp ("LOADPCT", tokptr) == 0)
386 apcups_detail->loadpct = value;
387 else if (strcmp ("BCHARGE", tokptr) == 0)
388 apcups_detail->bcharge = value;
389 else if (strcmp ("OUTPUTV", tokptr) == 0)
390 apcups_detail->outputv = value;
391 else if (strcmp ("LINEFREQ", tokptr) == 0)
392 apcups_detail->linefreq = value;
393 else if (strcmp ("TIMELEFT", tokptr) == 0)
394 apcups_detail->timeleft = value;
397 syslog (LOG_WARNING, "apcups plugin: Received unknown property from apcupsd: `%s' = %f",
401 tokptr = strtok (NULL, ":");
402 } /* while (tokptr != NULL) */
409 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
417 int main(int argc, char **argv)
419 /* we are not really going to use this */
420 struct apc_detail_s apcups_detail;
422 if (!*host || strcmp(host, "0.0.0.0") == 0)
425 do_pthreads_status(host, port, &apcups_detail);
430 static int apcups_config (char *key, char *value)
432 static char lhost[126];
434 if (strcasecmp (key, "host") == 0)
440 else if (strcasecmp (key, "Port") == 0)
442 int port_tmp = atoi (value);
443 if(port_tmp < 1 || port_tmp > 65535) {
444 syslog (LOG_WARNING, "apcups: `port' failed: %s",
458 static void apcups_init (void)
463 static void apc_write_voltage (char *host, char *inst, char *val)
468 status = snprintf (file, 512, bvolt_file_template, inst);
469 if ((status < 1) || (status >= 512))
472 rrd_update_file (host, file, val, bvolt_ds_def, bvolt_ds_num);
475 static void apc_write_charge (char *host, char *inst, char *val)
477 rrd_update_file (host, charge_file_template, val, charge_ds_def, charge_ds_num);
480 static void apc_write_percent (char *host, char *inst, char *val)
482 rrd_update_file (host, load_file_template, val, load_ds_def, load_ds_num);
485 static void apc_write_timeleft (char *host, char *inst, char *val)
487 rrd_update_file (host, time_file_template, val, time_ds_def, time_ds_num);
490 static void apc_write_temperature (char *host, char *inst, char *val)
492 rrd_update_file (host, temp_file_template, val, temp_ds_def, temp_ds_num);
495 static void apc_write_frequency (char *host, char *inst, char *val)
500 status = snprintf (file, 512, freq_file_template, inst);
501 if ((status < 1) || (status >= 512))
504 rrd_update_file (host, file, val, freq_ds_def, freq_ds_num);
507 static void apc_submit_generic (char *type, char *inst,
513 status = snprintf (buf, 512, "%u:%f",
514 (unsigned int) curtime, value);
515 if ((status < 1) || (status >= 512))
518 plugin_submit (type, inst, buf);
521 static void apc_submit (struct apc_detail_s *apcups_detail)
523 apc_submit_generic ("apcups_voltage", "input", apcups_detail->linev);
524 apc_submit_generic ("apcups_voltage", "output", apcups_detail->outputv);
525 apc_submit_generic ("apcups_voltage", "battery", apcups_detail->battv);
526 apc_submit_generic ("apcups_charge", "-", apcups_detail->bcharge);
527 apc_submit_generic ("apcups_charge_pct", "-", apcups_detail->loadpct);
528 apc_submit_generic ("apcups_timeleft", "-", apcups_detail->timeleft);
529 apc_submit_generic ("apcups_temp", "-", apcups_detail->itemp);
530 apc_submit_generic ("apcups_frequency", "input", apcups_detail->linefreq);
533 static void apcups_read (void)
535 struct apc_detail_s apcups_detail;
541 apcups_detail.linev = -1.0;
542 apcups_detail.outputv = -1.0;
543 apcups_detail.battv = -1.0;
544 apcups_detail.loadpct = -1.0;
545 apcups_detail.bcharge = -1.0;
546 apcups_detail.timeleft = -1.0;
547 apcups_detail.itemp = -300.0;
548 apcups_detail.linefreq = -1.0;
550 status = do_pthreads_status(host, port, &apcups_detail);
553 * if we did not connect then do not bother submitting
554 * zeros. We want rrd files to have NAN.
559 apc_submit (&apcups_detail);
562 void module_register (void)
564 plugin_register (MODULE_NAME, apcups_init, apcups_read, NULL);
565 plugin_register ("apcups_voltage", NULL, NULL, apc_write_voltage);
566 plugin_register ("apcups_charge", NULL, NULL, apc_write_charge);
567 plugin_register ("apcups_charge_pct", NULL, NULL, apc_write_percent);
568 plugin_register ("apcups_timeleft", NULL, NULL, apc_write_timeleft);
569 plugin_register ("apcups_temp", NULL, NULL, apc_write_temperature);
570 plugin_register ("apcups_frequency", NULL, NULL, apc_write_frequency);
571 cf_register (MODULE_NAME, apcups_config, config_keys, config_keys_num);
574 #endif /* ifdef APCMAIN */