2 * collectd - src/apcups.c
3 * Copyright (C) 2007 Florian octo Forster
4 * Copyright (C) 2006 Anthony Gialluca <tonyabg at charter.net>
5 * Copyright (C) 2000-2004 Kern Sibbald
6 * Copyright (C) 1996-99 Andre M. Hedrick <andre at suse.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General
11 * Public License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 * Anthony Gialluca <tonyabg at charter.net>
28 * FIXME: Don't know why but without this here atof() was not returning
29 * correct values for me. This is behavior that I don't understand and
30 * should be examined in closer detail.
35 #include "common.h" /* rrd_update_file */
36 #include "plugin.h" /* plugin_register, plugin_submit */
37 #include "configfile.h" /* cf_register */
40 # include <sys/types.h>
43 # include <sys/socket.h>
50 # include <netinet/in.h>
55 #define MODULE_NAME "apcups"
57 #define APCUPS_DEFAULT_HOST "localhost"
77 /* Default values for contacting daemon */
78 static char *conf_host = NULL;
79 static int conf_port = NISPORT;
81 static int global_sockfd = -1;
83 static const char *config_keys[] =
89 static int config_keys_num = 2;
91 /* Close the network connection */
92 static int apcups_shutdown (void)
94 uint16_t packet_size = 0;
96 if (global_sockfd < 0)
99 DEBUG ("Gracefully shutting down socket %i.", global_sockfd);
101 /* send EOF sentinel */
102 swrite (global_sockfd, (void *) &packet_size, sizeof (packet_size));
104 close (global_sockfd);
108 } /* int apcups_shutdown */
111 * Open a TCP connection to the UPS network server
112 * Returns -1 on error
113 * Returns socket file descriptor otherwise
115 static int net_open (char *host, int port)
120 struct addrinfo ai_hints;
121 struct addrinfo *ai_return;
122 struct addrinfo *ai_list;
124 assert ((port > 0x00000000) && (port <= 0x0000FFFF));
126 /* Convert the port to a string */
127 ssnprintf (port_str, sizeof (port_str), "%i", port);
130 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
131 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
132 ai_hints.ai_socktype = SOCK_STREAM;
134 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
138 INFO ("getaddrinfo failed: %s",
139 (status == EAI_SYSTEM)
140 ? sstrerror (errno, errbuf, sizeof (errbuf))
141 : gai_strerror (status));
147 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
149 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
153 /* `ai_list' still holds the current description of the socket.. */
157 DEBUG ("Unable to open a socket");
158 freeaddrinfo (ai_return);
162 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
164 freeaddrinfo (ai_return);
166 if (status != 0) /* `connect(2)' failed */
169 INFO ("connect failed: %s",
170 sstrerror (errno, errbuf, sizeof (errbuf)));
175 DEBUG ("Done opening a socket %i", sd);
178 } /* int net_open (char *host, char *service, int port) */
181 * Receive a message from the other end. Each message consists of
182 * two packets. The first is a header that contains the size
183 * of the data that follows in the second packet.
184 * Returns number of bytes read
185 * Returns 0 on end of file
186 * Returns -1 on hard end of file (i.e. network connection close)
187 * Returns -2 on error
189 static int net_recv (int *sockfd, char *buf, int buflen)
191 uint16_t packet_size;
193 /* get data size -- in short */
194 if (sread (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
200 packet_size = ntohs (packet_size);
201 if (packet_size > buflen)
203 DEBUG ("record length too large");
207 if (packet_size == 0)
210 /* now read the actual data */
211 if (sread (*sockfd, (void *) buf, packet_size) != 0)
217 return ((int) packet_size);
218 } /* static int net_recv (int *sockfd, char *buf, int buflen) */
221 * Send a message over the network. The send consists of
222 * two network packets. The first is sends a short containing
223 * the length of the data packet which follows.
224 * Returns zero on success
225 * Returns non-zero on error
227 static int net_send (int *sockfd, char *buff, int len)
229 uint16_t packet_size;
232 assert (*sockfd >= 0);
234 /* send short containing size of data packet */
235 packet_size = htons ((uint16_t) len);
237 if (swrite (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
243 /* send data packet */
244 if (swrite (*sockfd, (void *) buff, len) != 0)
253 /* Get and print status from apcupsd NIS server */
254 static int apc_query_server (char *host, int port,
255 struct apc_detail_s *apcups_detail)
265 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
267 # define PRINT_VALUE(name, val) /**/
270 if (global_sockfd < 0)
272 global_sockfd = net_open (host, port);
273 if (global_sockfd < 0)
275 ERROR ("apcups plugin: Connecting to the "
281 if (net_send (&global_sockfd, "status", 6) < 0)
283 ERROR ("apcups plugin: Writing to the socket failed.");
287 while ((n = net_recv (&global_sockfd, recvline, sizeof (recvline) - 1)) > 0)
289 assert ((unsigned int)n < sizeof (recvline));
292 printf ("net_recv = `%s';\n", recvline);
293 #endif /* if APCMAIN */
296 tokptr = strtok_r (recvline, " :\t", &toksaveptr);
297 while (tokptr != NULL)
300 if ((tokptr = strtok_r (NULL, " :\t", &toksaveptr)) == NULL)
302 value = atof (tokptr);
304 PRINT_VALUE (key, value);
306 if (strcmp ("LINEV", key) == 0)
307 apcups_detail->linev = value;
308 else if (strcmp ("BATTV", key) == 0)
309 apcups_detail->battv = value;
310 else if (strcmp ("ITEMP", key) == 0)
311 apcups_detail->itemp = value;
312 else if (strcmp ("LOADPCT", key) == 0)
313 apcups_detail->loadpct = value;
314 else if (strcmp ("BCHARGE", key) == 0)
315 apcups_detail->bcharge = value;
316 else if (strcmp ("OUTPUTV", key) == 0)
317 apcups_detail->outputv = value;
318 else if (strcmp ("LINEFREQ", key) == 0)
319 apcups_detail->linefreq = value;
320 else if (strcmp ("TIMELEFT", key) == 0)
321 apcups_detail->timeleft = value;
323 tokptr = strtok_r (NULL, ":", &toksaveptr);
324 } /* while (tokptr != NULL) */
329 WARNING ("apcups plugin: Error reading from socket");
336 static int apcups_config (const char *key, const char *value)
338 if (strcasecmp (key, "host") == 0)
340 if (conf_host != NULL)
345 if ((conf_host = strdup (value)) == NULL)
348 else if (strcasecmp (key, "Port") == 0)
350 int port_tmp = atoi (value);
351 if (port_tmp < 1 || port_tmp > 65535)
353 WARNING ("apcups plugin: Invalid port: %i", port_tmp);
356 conf_port = port_tmp;
365 static void apc_submit_generic (char *type, char *type_inst, double value)
368 value_list_t vl = VALUE_LIST_INIT;
370 values[0].gauge = value;
374 vl.time = time (NULL);
375 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
376 sstrncpy (vl.plugin, "apcups", sizeof (vl.plugin));
377 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
378 sstrncpy (vl.type, type, sizeof (vl.type));
379 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
381 plugin_dispatch_values (&vl);
384 static void apc_submit (struct apc_detail_s *apcups_detail)
386 apc_submit_generic ("voltage", "input", apcups_detail->linev);
387 apc_submit_generic ("voltage", "output", apcups_detail->outputv);
388 apc_submit_generic ("voltage", "battery", apcups_detail->battv);
389 apc_submit_generic ("charge", "", apcups_detail->bcharge);
390 apc_submit_generic ("percent", "load", apcups_detail->loadpct);
391 apc_submit_generic ("timeleft", "", apcups_detail->timeleft);
392 apc_submit_generic ("temperature", "", apcups_detail->itemp);
393 apc_submit_generic ("frequency", "input", apcups_detail->linefreq);
396 static int apcups_read (void)
398 struct apc_detail_s apcups_detail;
401 apcups_detail.linev = -1.0;
402 apcups_detail.outputv = -1.0;
403 apcups_detail.battv = -1.0;
404 apcups_detail.loadpct = -1.0;
405 apcups_detail.bcharge = -1.0;
406 apcups_detail.timeleft = -1.0;
407 apcups_detail.itemp = -300.0;
408 apcups_detail.linefreq = -1.0;
410 status = apc_query_server (conf_host == NULL
411 ? APCUPS_DEFAULT_HOST
413 conf_port, &apcups_detail);
416 * if we did not connect then do not bother submitting
417 * zeros. We want rrd files to have NAN.
421 DEBUG ("apc_query_server (%s, %i) = %i",
423 ? APCUPS_DEFAULT_HOST
429 apc_submit (&apcups_detail);
434 void module_register (void)
436 plugin_register_config ("apcups", apcups_config, config_keys,
438 plugin_register_read ("apcups", apcups_read);
439 plugin_register_shutdown ("apcups", apcups_shutdown);
440 } /* void module_register */