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 */
38 # include <sys/types.h>
41 # include <sys/socket.h>
48 # include <netinet/in.h>
53 #define MODULE_NAME "apcups"
55 #define APCUPS_DEFAULT_HOST "localhost"
75 /* Default values for contacting daemon */
76 static char *conf_host = NULL;
77 static int conf_port = NISPORT;
79 static int global_sockfd = -1;
81 static const char *config_keys[] =
87 static int config_keys_num = 2;
89 /* Close the network connection */
90 static int apcups_shutdown (void)
92 uint16_t packet_size = 0;
94 if (global_sockfd < 0)
97 DEBUG ("Gracefully shutting down socket %i.", global_sockfd);
99 /* send EOF sentinel */
100 swrite (global_sockfd, (void *) &packet_size, sizeof (packet_size));
102 close (global_sockfd);
106 } /* int apcups_shutdown */
109 * Open a TCP connection to the UPS network server
110 * Returns -1 on error
111 * Returns socket file descriptor otherwise
113 static int net_open (char *host, char *service, int port)
118 struct addrinfo ai_hints;
119 struct addrinfo *ai_return;
120 struct addrinfo *ai_list;
122 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
124 /* Convert the port to a string */
125 snprintf (port_str, 8, "%i", port);
129 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
130 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
131 ai_hints.ai_socktype = SOCK_STREAM;
133 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
137 DEBUG ("getaddrinfo failed: %s",
138 (status == EAI_SYSTEM)
139 ? sstrerror (errno, errbuf, sizeof (errbuf))
140 : gai_strerror (status));
146 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
148 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
152 /* `ai_list' still holds the current description of the socket.. */
156 DEBUG ("Unable to open a socket");
157 freeaddrinfo (ai_return);
161 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
163 freeaddrinfo (ai_return);
165 if (status != 0) /* `connect(2)' failed */
168 DEBUG ("connect failed: %s",
169 sstrerror (errno, errbuf, sizeof (errbuf)));
174 DEBUG ("Done opening a socket %i", sd);
177 } /* int net_open (char *host, char *service, int port) */
180 * Receive a message from the other end. Each message consists of
181 * two packets. The first is a header that contains the size
182 * of the data that follows in the second packet.
183 * Returns number of bytes read
184 * Returns 0 on end of file
185 * Returns -1 on hard end of file (i.e. network connection close)
186 * Returns -2 on error
188 static int net_recv (int *sockfd, char *buf, int buflen)
190 uint16_t packet_size;
192 /* get data size -- in short */
193 if (sread (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
199 packet_size = ntohs (packet_size);
200 if (packet_size > buflen)
202 DEBUG ("record length too large");
206 if (packet_size == 0)
209 /* now read the actual data */
210 if (sread (*sockfd, (void *) buf, packet_size) != 0)
216 return ((int) packet_size);
217 } /* static int net_recv (int *sockfd, char *buf, int buflen) */
220 * Send a message over the network. The send consists of
221 * two network packets. The first is sends a short containing
222 * the length of the data packet which follows.
223 * Returns zero on success
224 * Returns non-zero on error
226 static int net_send (int *sockfd, char *buff, int len)
228 uint16_t packet_size;
231 assert (*sockfd >= 0);
233 /* send short containing size of data packet */
234 packet_size = htons ((uint16_t) len);
236 if (swrite (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
242 /* send data packet */
243 if (swrite (*sockfd, (void *) buff, len) != 0)
252 /* Get and print status from apcupsd NIS server */
253 static int apc_query_server (char *host, int port,
254 struct apc_detail_s *apcups_detail)
263 static complain_t compl;
266 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
268 # define PRINT_VALUE(name, val) /**/
271 if (global_sockfd < 0)
273 if ((global_sockfd = net_open (host, NULL, port)) < 0)
275 plugin_complain (LOG_ERR, &compl, "apcups plugin: "
276 "Connecting to the apcupsd failed.");
281 plugin_relief (LOG_NOTICE, &compl, "apcups plugin: "
282 "Connection re-established to the apcupsd.");
286 if (net_send (&global_sockfd, "status", 6) < 0)
288 ERROR ("apcups plugin: Writing to the socket failed.");
292 while ((n = net_recv (&global_sockfd, recvline, sizeof (recvline) - 1)) > 0)
294 assert (n < sizeof (recvline));
297 printf ("net_recv = `%s';\n", recvline);
298 #endif /* if APCMAIN */
301 tokptr = strtok_r (recvline, " :\t", &toksaveptr);
302 while (tokptr != NULL)
305 if ((tokptr = strtok_r (NULL, " :\t", &toksaveptr)) == NULL)
307 value = atof (tokptr);
309 PRINT_VALUE (key, value);
311 if (strcmp ("LINEV", key) == 0)
312 apcups_detail->linev = value;
313 else if (strcmp ("BATTV", key) == 0)
314 apcups_detail->battv = value;
315 else if (strcmp ("ITEMP", key) == 0)
316 apcups_detail->itemp = value;
317 else if (strcmp ("LOADPCT", key) == 0)
318 apcups_detail->loadpct = value;
319 else if (strcmp ("BCHARGE", key) == 0)
320 apcups_detail->bcharge = value;
321 else if (strcmp ("OUTPUTV", key) == 0)
322 apcups_detail->outputv = value;
323 else if (strcmp ("LINEFREQ", key) == 0)
324 apcups_detail->linefreq = value;
325 else if (strcmp ("TIMELEFT", key) == 0)
326 apcups_detail->timeleft = value;
328 tokptr = strtok_r (NULL, ":", &toksaveptr);
329 } /* while (tokptr != NULL) */
334 WARNING ("apcups plugin: Error reading from socket");
341 static int apcups_config (const char *key, const char *value)
343 if (strcasecmp (key, "host") == 0)
345 if (conf_host != NULL)
350 if ((conf_host = strdup (value)) == NULL)
353 else if (strcasecmp (key, "Port") == 0)
355 int port_tmp = atoi (value);
356 if (port_tmp < 1 || port_tmp > 65535)
358 WARNING ("apcups plugin: Invalid port: %i", port_tmp);
361 conf_port = port_tmp;
370 static void apc_submit_generic (char *type, char *type_inst, double value)
373 value_list_t vl = VALUE_LIST_INIT;
375 values[0].gauge = value;
379 vl.time = time (NULL);
380 strcpy (vl.host, hostname_g);
381 strcpy (vl.plugin, "apcups");
382 strcpy (vl.plugin_instance, "");
383 strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
385 plugin_dispatch_values (type, &vl);
388 static void apc_submit (struct apc_detail_s *apcups_detail)
390 apc_submit_generic ("apcups_voltage", "input", apcups_detail->linev);
391 apc_submit_generic ("apcups_voltage", "output", apcups_detail->outputv);
392 apc_submit_generic ("apcups_voltage", "battery", apcups_detail->battv);
393 apc_submit_generic ("apcups_charge", "", apcups_detail->bcharge);
394 apc_submit_generic ("apcups_charge_pct", "", apcups_detail->loadpct);
395 apc_submit_generic ("apcups_timeleft", "", apcups_detail->timeleft);
396 apc_submit_generic ("apcups_temp", "", apcups_detail->itemp);
397 apc_submit_generic ("apcups_frequency", "input", apcups_detail->linefreq);
400 static int apcups_read (void)
402 struct apc_detail_s apcups_detail;
405 apcups_detail.linev = -1.0;
406 apcups_detail.outputv = -1.0;
407 apcups_detail.battv = -1.0;
408 apcups_detail.loadpct = -1.0;
409 apcups_detail.bcharge = -1.0;
410 apcups_detail.timeleft = -1.0;
411 apcups_detail.itemp = -300.0;
412 apcups_detail.linefreq = -1.0;
414 status = apc_query_server (conf_host == NULL
415 ? APCUPS_DEFAULT_HOST
417 conf_port, &apcups_detail);
420 * if we did not connect then do not bother submitting
421 * zeros. We want rrd files to have NAN.
425 DEBUG ("apc_query_server (%s, %i) = %i",
427 ? APCUPS_DEFAULT_HOST
433 apc_submit (&apcups_detail);
438 void module_register (void)
440 plugin_register_config ("apcups", apcups_config, config_keys,
442 plugin_register_read ("apcups", apcups_read);
443 plugin_register_shutdown ("apcups", apcups_shutdown);
444 } /* void module_register */