2 * collectd - src/apcups.c
3 * Copyright (C) 2006-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-1999 Andre M. Hedrick <andre at suse.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General
10 * Public License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 * Anthony Gialluca <tonyabg at charter.net>
24 * Florian octo Forster <octo at verplant.org>
28 #include "common.h" /* rrd_update_file */
29 #include "plugin.h" /* plugin_register, plugin_submit */
30 #include "configfile.h" /* cf_register */
33 # include <sys/types.h>
36 # include <sys/socket.h>
43 # include <netinet/in.h>
48 #define MODULE_NAME "apcups"
50 #define APCUPS_DEFAULT_HOST "localhost"
70 /* Default values for contacting daemon */
71 static char *conf_host = NULL;
72 static int conf_port = NISPORT;
74 static int global_sockfd = -1;
76 static const char *config_keys[] =
82 static int config_keys_num = 2;
84 static int net_shutdown (int *fd)
86 uint16_t packet_size = 0;
88 if ((fd == NULL) || (*fd < 0))
91 swrite (*fd, (void *) &packet_size, sizeof (packet_size));
96 } /* int net_shutdown */
98 /* Close the network connection */
99 static int apcups_shutdown (void)
101 if (global_sockfd < 0)
104 net_shutdown (&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, 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 ssnprintf (port_str, sizeof (port_str), "%i", port);
128 memset ((void *) &ai_hints, '\0', sizeof (ai_hints));
129 ai_hints.ai_family = AF_INET; /* XXX: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
130 ai_hints.ai_socktype = SOCK_STREAM;
132 status = getaddrinfo (host, port_str, &ai_hints, &ai_return);
136 INFO ("getaddrinfo failed: %s",
137 (status == EAI_SYSTEM)
138 ? sstrerror (errno, errbuf, sizeof (errbuf))
139 : gai_strerror (status));
145 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next)
147 sd = socket (ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
151 /* `ai_list' still holds the current description of the socket.. */
155 DEBUG ("Unable to open a socket");
156 freeaddrinfo (ai_return);
160 status = connect (sd, ai_list->ai_addr, ai_list->ai_addrlen);
162 freeaddrinfo (ai_return);
164 if (status != 0) /* `connect(2)' failed */
167 INFO ("connect failed: %s",
168 sstrerror (errno, errbuf, sizeof (errbuf)));
173 DEBUG ("Done opening a socket %i", sd);
176 } /* int net_open (char *host, char *service, int port) */
179 * Receive a message from the other end. Each message consists of
180 * two packets. The first is a header that contains the size
181 * of the data that follows in the second packet.
182 * Returns number of bytes read
183 * Returns 0 on end of file
184 * Returns -1 on hard end of file (i.e. network connection close)
185 * Returns -2 on error
187 static int net_recv (int *sockfd, char *buf, int buflen)
189 uint16_t packet_size;
191 /* get data size -- in short */
192 if (sread (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
199 packet_size = ntohs (packet_size);
200 if (packet_size > buflen)
202 ERROR ("apcups plugin: Received %"PRIu16" bytes of payload "
203 "but have only %i bytes of buffer available.",
204 packet_size, buflen);
210 if (packet_size == 0)
213 /* now read the actual data */
214 if (sread (*sockfd, (void *) buf, packet_size) != 0)
221 return ((int) packet_size);
222 } /* static int net_recv (int *sockfd, char *buf, int buflen) */
225 * Send a message over the network. The send consists of
226 * two network packets. The first is sends a short containing
227 * the length of the data packet which follows.
228 * Returns zero on success
229 * Returns non-zero on error
231 static int net_send (int *sockfd, char *buff, int len)
233 uint16_t packet_size;
236 assert (*sockfd >= 0);
238 /* send short containing size of data packet */
239 packet_size = htons ((uint16_t) len);
241 if (swrite (*sockfd, (void *) &packet_size, sizeof (packet_size)) != 0)
248 /* send data packet */
249 if (swrite (*sockfd, (void *) buff, len) != 0)
259 /* Get and print status from apcupsd NIS server */
260 static int apc_query_server (char *host, int port,
261 struct apc_detail_s *apcups_detail)
271 # define PRINT_VALUE(name, val) printf(" Found property: name = %s; value = %f;\n", name, val)
273 # define PRINT_VALUE(name, val) /**/
276 if (global_sockfd < 0)
278 global_sockfd = net_open (host, port);
279 if (global_sockfd < 0)
281 ERROR ("apcups plugin: Connecting to the "
287 if (net_send (&global_sockfd, "status", 6) < 0)
289 ERROR ("apcups plugin: Writing to the socket failed.");
293 while ((n = net_recv (&global_sockfd, recvline, sizeof (recvline) - 1)) > 0)
295 assert ((unsigned int)n < sizeof (recvline));
298 printf ("net_recv = `%s';\n", recvline);
299 #endif /* if APCMAIN */
302 tokptr = strtok_r (recvline, " :\t", &toksaveptr);
303 while (tokptr != NULL)
306 if ((tokptr = strtok_r (NULL, " :\t", &toksaveptr)) == NULL)
308 value = atof (tokptr);
310 PRINT_VALUE (key, value);
312 if (strcmp ("LINEV", key) == 0)
313 apcups_detail->linev = value;
314 else if (strcmp ("BATTV", key) == 0)
315 apcups_detail->battv = value;
316 else if (strcmp ("ITEMP", key) == 0)
317 apcups_detail->itemp = value;
318 else if (strcmp ("LOADPCT", key) == 0)
319 apcups_detail->loadpct = value;
320 else if (strcmp ("BCHARGE", key) == 0)
321 apcups_detail->bcharge = value;
322 else if (strcmp ("OUTPUTV", key) == 0)
323 apcups_detail->outputv = value;
324 else if (strcmp ("LINEFREQ", key) == 0)
325 apcups_detail->linefreq = value;
326 else if (strcmp ("TIMELEFT", key) == 0)
327 apcups_detail->timeleft = value;
329 tokptr = strtok_r (NULL, ":", &toksaveptr);
330 } /* while (tokptr != NULL) */
335 WARNING ("apcups plugin: Error reading from socket");
342 static int apcups_config (const char *key, const char *value)
344 if (strcasecmp (key, "host") == 0)
346 if (conf_host != NULL)
351 if ((conf_host = strdup (value)) == NULL)
354 else if (strcasecmp (key, "Port") == 0)
356 int port_tmp = atoi (value);
357 if (port_tmp < 1 || port_tmp > 65535)
359 WARNING ("apcups plugin: Invalid port: %i", port_tmp);
362 conf_port = port_tmp;
371 static void apc_submit_generic (char *type, char *type_inst, double value)
374 value_list_t vl = VALUE_LIST_INIT;
376 values[0].gauge = value;
380 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
381 sstrncpy (vl.plugin, "apcups", sizeof (vl.plugin));
382 sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
383 sstrncpy (vl.type, type, sizeof (vl.type));
384 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
386 plugin_dispatch_values (&vl);
389 static void apc_submit (struct apc_detail_s *apcups_detail)
391 apc_submit_generic ("voltage", "input", apcups_detail->linev);
392 apc_submit_generic ("voltage", "output", apcups_detail->outputv);
393 apc_submit_generic ("voltage", "battery", apcups_detail->battv);
394 apc_submit_generic ("charge", "", apcups_detail->bcharge);
395 apc_submit_generic ("percent", "load", apcups_detail->loadpct);
396 apc_submit_generic ("timeleft", "", apcups_detail->timeleft);
397 apc_submit_generic ("temperature", "", apcups_detail->itemp);
398 apc_submit_generic ("frequency", "input", apcups_detail->linefreq);
401 static int apcups_read (void)
403 struct apc_detail_s apcups_detail;
406 apcups_detail.linev = -1.0;
407 apcups_detail.outputv = -1.0;
408 apcups_detail.battv = -1.0;
409 apcups_detail.loadpct = -1.0;
410 apcups_detail.bcharge = -1.0;
411 apcups_detail.timeleft = -1.0;
412 apcups_detail.itemp = -300.0;
413 apcups_detail.linefreq = -1.0;
415 status = apc_query_server (conf_host == NULL
416 ? APCUPS_DEFAULT_HOST
418 conf_port, &apcups_detail);
421 * if we did not connect then do not bother submitting
422 * zeros. We want rrd files to have NAN.
426 DEBUG ("apc_query_server (%s, %i) = %i",
428 ? APCUPS_DEFAULT_HOST
434 apc_submit (&apcups_detail);
439 void module_register (void)
441 plugin_register_config ("apcups", apcups_config, config_keys,
443 plugin_register_read ("apcups", apcups_read);
444 plugin_register_shutdown ("apcups", apcups_shutdown);
445 } /* void module_register */