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>
49 # include <netinet/in.h>
54 #define MODULE_NAME "apcups"
56 #define APCUPS_DEFAULT_HOST "localhost"
76 /* Default values for contacting daemon */
77 static char *conf_host = NULL;
78 static int conf_port = NISPORT;
80 static int global_sockfd = -1;
83 * The following are only if not compiled to test the module with its own main.
85 static data_source_t data_source_voltage[1] =
87 {"value", DS_TYPE_GAUGE, NAN, NAN}
90 static data_set_t ds_voltage =
92 "voltage", 1, data_source_voltage
95 static data_source_t data_source_percent[1] =
97 {"percent", DS_TYPE_GAUGE, 0, 100.1}
100 static data_set_t ds_percent =
102 "percent", 1, data_source_percent
105 static data_source_t data_source_timeleft[1] =
107 {"timeleft", DS_TYPE_GAUGE, 0, 100.0}
110 static data_set_t ds_timeleft =
112 "timeleft", 1, data_source_timeleft
115 static data_source_t data_source_temperature[1] =
117 {"value", DS_TYPE_GAUGE, -273.15, NAN}
120 static data_set_t ds_temperature =
122 "temperature", 1, data_source_temperature
125 static data_source_t data_source_frequency[1] =
127 {"frequency", DS_TYPE_GAUGE, 0, NAN}
130 static data_set_t ds_frequency =
132 "frequency", 1, data_source_frequency
135 static const char *config_keys[] =
141 static int config_keys_num = 2;
143 /* Close the network connection */
144 static int apcups_shutdown (void)
146 uint16_t packet_size = 0;
148 if (global_sockfd < 0)
151 DBG ("Gracefully shutting down socket %i.", global_sockfd);
153 /* send EOF sentinel */
154 swrite (global_sockfd, (void *) &packet_size, sizeof (packet_size));
156 close (global_sockfd);
160 } /* int apcups_shutdown */
163 * Open a TCP connection to the UPS network server
164 * Returns -1 on error
165 * Returns socket file descriptor otherwise
167 static int net_open (char *host, char *service, int port)
172 struct addrinfo ai_hints;
173 struct addrinfo *ai_return;
174 struct addrinfo *ai_list;
176 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
178 /* Convert the port to a string */
179 snprintf (port_str, 8, "%i", port);
183 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
184 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
185 ai_hints.ai_socktype = SOCK_STREAM;
187 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
190 DBG ("getaddrinfo failed: %s", status == EAI_SYSTEM ? strerror (errno) : gai_strerror (status));
196 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
198 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
202 /* `ai_list' still holds the current description of the socket.. */
206 DBG ("Unable to open a socket");
207 freeaddrinfo (ai_return);
211 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
213 freeaddrinfo (ai_return);
215 if (status != 0) /* `connect(2)' failed */
217 DBG ("connect failed: %s", strerror (errno));
222 DBG ("Done opening a socket %i", sd);
225 } /* int net_open (char *host, char *service, int port) */
228 * Receive a message from the other end. Each message consists of
229 * two packets. The first is a header that contains the size
230 * of the data that follows in the second packet.
231 * Returns number of bytes read
232 * Returns 0 on end of file
233 * Returns -1 on hard end of file (i.e. network connection close)
234 * Returns -2 on error
236 static int net_recv (int *sockfd, char *buf, int buflen)
238 uint16_t packet_size;
240 /* get data size -- in short */
241 if (sread (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
247 packet_size = ntohs (packet_size);
248 if (packet_size > buflen)
250 DBG ("record length too large");
254 if (packet_size == 0)
257 /* now read the actual data */
258 if (sread (*sockfd, (void *) buf, packet_size) != 0)
264 return ((int) packet_size);
265 } /* static int net_recv (int *sockfd, char *buf, int buflen) */
268 * Send a message over the network. The send consists of
269 * two network packets. The first is sends a short containing
270 * the length of the data packet which follows.
271 * Returns zero on success
272 * Returns non-zero on error
274 static int net_send (int *sockfd, char *buff, int len)
276 uint16_t packet_size;
279 assert (*sockfd >= 0);
281 /* send short containing size of data packet */
282 packet_size = htons ((uint16_t) len);
284 if (swrite (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
290 /* send data packet */
291 if (swrite (*sockfd, (void *) buff, len) != 0)
300 /* Get and print status from apcupsd NIS server */
301 static int apc_query_server (char *host, int port,
302 struct apc_detail_s *apcups_detail)
310 static complain_t compl;
313 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
315 # define PRINT_VALUE(name, val) /**/
318 if (global_sockfd < 0)
320 if ((global_sockfd = net_open (host, NULL, port)) < 0)
322 plugin_complain (LOG_ERR, &compl, "apcups plugin: "
323 "Connecting to the apcupsd failed.");
328 plugin_relief (LOG_NOTICE, &compl, "apcups plugin: "
329 "Connection re-established to the apcupsd.");
333 if (net_send (&global_sockfd, "status", 6) < 0)
335 syslog (LOG_ERR, "apcups plugin: Writing to the socket failed.");
339 while ((n = net_recv (&global_sockfd, recvline, sizeof (recvline) - 1)) > 0)
341 assert (n < sizeof (recvline));
344 printf ("net_recv = `%s';\n", recvline);
345 #endif /* if APCMAIN */
347 tokptr = strtok (recvline, " :\t");
348 while (tokptr != NULL)
351 if ((tokptr = strtok (NULL, " :\t")) == NULL)
353 value = atof (tokptr);
355 PRINT_VALUE (key, value);
357 if (strcmp ("LINEV", key) == 0)
358 apcups_detail->linev = value;
359 else if (strcmp ("BATTV", key) == 0)
360 apcups_detail->battv = value;
361 else if (strcmp ("ITEMP", key) == 0)
362 apcups_detail->itemp = value;
363 else if (strcmp ("LOADPCT", key) == 0)
364 apcups_detail->loadpct = value;
365 else if (strcmp ("BCHARGE", key) == 0)
366 apcups_detail->bcharge = value;
367 else if (strcmp ("OUTPUTV", key) == 0)
368 apcups_detail->outputv = value;
369 else if (strcmp ("LINEFREQ", key) == 0)
370 apcups_detail->linefreq = value;
371 else if (strcmp ("TIMELEFT", key) == 0)
372 apcups_detail->timeleft = value;
374 tokptr = strtok (NULL, ":");
375 } /* while (tokptr != NULL) */
380 syslog (LOG_WARNING, "apcups plugin: Error reading from socket");
387 static int apcups_config (const char *key, const char *value)
389 if (strcasecmp (key, "host") == 0)
391 if (conf_host != NULL)
396 if ((conf_host = strdup (value)) == NULL)
399 else if (strcasecmp (key, "Port") == 0)
401 int port_tmp = atoi (value);
402 if (port_tmp < 1 || port_tmp > 65535)
404 syslog (LOG_WARNING, "apcups plugin: Invalid port: %i", port_tmp);
407 conf_port = port_tmp;
416 static void apc_submit_generic (char *type, char *type_inst, double value)
419 value_list_t vl = VALUE_LIST_INIT;
421 values[0].gauge = value;
425 vl.time = time (NULL);
426 strcpy (vl.host, hostname);
427 strcpy (vl.plugin, "apcups");
428 strcpy (vl.plugin_instance, "");
429 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
431 plugin_dispatch_values (type, &vl);
434 static void apc_submit (struct apc_detail_s *apcups_detail)
436 apc_submit_generic ("apcups_voltage", "input", apcups_detail->linev);
437 apc_submit_generic ("apcups_voltage", "output", apcups_detail->outputv);
438 apc_submit_generic ("apcups_voltage", "battery", apcups_detail->battv);
439 apc_submit_generic ("apcups_charge", "", apcups_detail->bcharge);
440 apc_submit_generic ("apcups_charge_pct", "", apcups_detail->loadpct);
441 apc_submit_generic ("apcups_timeleft", "", apcups_detail->timeleft);
442 apc_submit_generic ("apcups_temp", "", apcups_detail->itemp);
443 apc_submit_generic ("apcups_frequency", "input", apcups_detail->linefreq);
446 static int apcups_read (void)
448 struct apc_detail_s apcups_detail;
451 apcups_detail.linev = -1.0;
452 apcups_detail.outputv = -1.0;
453 apcups_detail.battv = -1.0;
454 apcups_detail.loadpct = -1.0;
455 apcups_detail.bcharge = -1.0;
456 apcups_detail.timeleft = -1.0;
457 apcups_detail.itemp = -300.0;
458 apcups_detail.linefreq = -1.0;
460 status = apc_query_server (conf_host == NULL
461 ? APCUPS_DEFAULT_HOST
463 conf_port, &apcups_detail);
466 * if we did not connect then do not bother submitting
467 * zeros. We want rrd files to have NAN.
471 DBG ("apc_query_server (%s, %i) = %i",
473 ? APCUPS_DEFAULT_HOST
479 apc_submit (&apcups_detail);
484 void module_register (void)
486 plugin_register_data_set (&ds_voltage);
487 plugin_register_data_set (&ds_percent);
488 plugin_register_data_set (&ds_timeleft);
489 plugin_register_data_set (&ds_temperature);
490 plugin_register_data_set (&ds_frequency);
492 plugin_register_config ("apcups", apcups_config, config_keys, config_keys_num);
494 plugin_register_read ("apcups", apcups_read);
495 plugin_register_shutdown ("apcups", apcups_shutdown);