2 * collectd - src/apcups.c
3 * Copyright (C) 2006-2015 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 collectd.org>
29 #include "common.h" /* rrd_update_file */
30 #include "plugin.h" /* plugin_register, plugin_submit */
33 #include <sys/types.h>
40 #include <netinet/in.h>
43 #ifndef APCUPS_SERVER_TIMEOUT
44 #define APCUPS_SERVER_TIMEOUT 15.0
47 #ifndef APCUPS_DEFAULT_NODE
48 #define APCUPS_DEFAULT_NODE "localhost"
51 #ifndef APCUPS_DEFAULT_SERVICE
52 #define APCUPS_DEFAULT_SERVICE "3551"
72 /* Default values for contacting daemon */
73 static char *conf_node = NULL;
74 static char *conf_service = NULL;
75 /* Defaults to false for backwards compatibility. */
76 static _Bool conf_report_seconds = 0;
77 static _Bool conf_persistent_conn = 1;
79 static int global_sockfd = -1;
81 static int count_retries = 0;
82 static int count_iterations = 0;
84 static int net_shutdown(int *fd) {
85 uint16_t packet_size = 0;
87 if ((fd == NULL) || (*fd < 0))
90 (void)swrite(*fd, (void *)&packet_size, sizeof(packet_size));
95 } /* int net_shutdown */
97 /* Close the network connection */
98 static int apcups_shutdown(void) {
99 if (global_sockfd < 0)
102 net_shutdown(&global_sockfd);
104 } /* int apcups_shutdown */
107 * Open a TCP connection to the UPS network server
108 * Returns -1 on error
109 * Returns socket file descriptor otherwise
111 static int net_open(char const *node, char const *service) {
114 struct addrinfo *ai_return;
115 struct addrinfo *ai_list;
117 /* TODO: Change this to `AF_UNSPEC' if apcupsd can handle IPv6 */
118 struct addrinfo ai_hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM};
120 status = getaddrinfo(node, service, &ai_hints, &ai_return);
123 INFO("apcups plugin: getaddrinfo failed: %s",
124 (status == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
125 : gai_strerror(status));
131 for (ai_list = ai_return; ai_list != NULL; ai_list = ai_list->ai_next) {
132 sd = socket(ai_list->ai_family, ai_list->ai_socktype, ai_list->ai_protocol);
136 /* `ai_list' still holds the current description of the socket.. */
139 DEBUG("apcups plugin: Unable to open a socket");
140 freeaddrinfo(ai_return);
144 status = connect(sd, ai_list->ai_addr, ai_list->ai_addrlen);
146 freeaddrinfo(ai_return);
148 if (status != 0) /* `connect(2)' failed */
151 INFO("apcups plugin: connect failed: %s",
152 sstrerror(errno, errbuf, sizeof(errbuf)));
157 DEBUG("apcups plugin: Done opening a socket %i", sd);
163 * Receive a message from the other end. Each message consists of
164 * two packets. The first is a header that contains the size
165 * of the data that follows in the second packet.
166 * Returns number of bytes read
167 * Returns 0 on end of file
168 * Returns -1 on hard end of file (i.e. network connection close)
169 * Returns -2 on error
171 static int net_recv(int *sockfd, char *buf, int buflen) {
172 uint16_t packet_size;
174 /* get data size -- in short */
175 if (sread(*sockfd, (void *)&packet_size, sizeof(packet_size)) != 0) {
181 packet_size = ntohs(packet_size);
182 if (packet_size > buflen) {
183 ERROR("apcups plugin: Received %" PRIu16 " bytes of payload "
184 "but have only %i bytes of buffer available.",
185 packet_size, buflen);
191 if (packet_size == 0)
194 /* now read the actual data */
195 if (sread(*sockfd, (void *)buf, packet_size) != 0) {
201 return ((int)packet_size);
202 } /* static int net_recv (int *sockfd, char *buf, int buflen) */
205 * Send a message over the network. The send consists of
206 * two network packets. The first is sends a short containing
207 * the length of the data packet which follows.
208 * Returns zero on success
209 * Returns non-zero on error
211 static int net_send(int *sockfd, const char *buff, int len) {
212 uint16_t packet_size;
215 assert(*sockfd >= 0);
217 /* send short containing size of data packet */
218 packet_size = htons((uint16_t)len);
220 if (swrite(*sockfd, (void *)&packet_size, sizeof(packet_size)) != 0) {
226 /* send data packet */
227 if (swrite(*sockfd, (void *)buff, len) != 0) {
236 /* Get and print status from apcupsd NIS server */
237 static int apc_query_server(char const *node, char const *service,
238 apc_detail_t *apcups_detail) {
247 #define PRINT_VALUE(name, val) \
248 printf(" Found property: name = %s; value = %f;\n", name, val)
250 #define PRINT_VALUE(name, val) /**/
254 if (global_sockfd < 0) {
255 global_sockfd = net_open(node, service);
256 if (global_sockfd < 0) {
257 ERROR("apcups plugin: Connecting to the "
263 status = net_send(&global_sockfd, "status", strlen("status"));
265 /* net_send is closing the socket on error. */
266 assert(global_sockfd < 0);
273 ERROR("apcups plugin: Writing to the socket failed.");
278 } /* while (retry) */
280 /* When collectd's collection interval is larger than apcupsd's
281 * timeout, we would have to retry / re-connect each iteration. Try to
282 * detect this situation and shut down the socket gracefully in that
283 * case. Otherwise, keep the socket open to avoid overhead. */
285 if ((count_iterations == 10) && (count_retries > 2)) {
286 NOTICE("apcups plugin: There have been %i retries in the "
287 "first %i iterations. Will close the socket "
288 "in future iterations.",
289 count_retries, count_iterations);
290 conf_persistent_conn = 0;
293 while ((n = net_recv(&global_sockfd, recvline, sizeof(recvline) - 1)) > 0) {
294 assert((size_t)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) {
304 if ((tokptr = strtok_r(NULL, " :\t", &toksaveptr)) == NULL)
308 if (strtogauge(tokptr, &value) != 0)
311 PRINT_VALUE(key, value);
313 if (strcmp("LINEV", key) == 0)
314 apcups_detail->linev = value;
315 else if (strcmp("BATTV", key) == 0)
316 apcups_detail->battv = value;
317 else if (strcmp("ITEMP", key) == 0)
318 apcups_detail->itemp = value;
319 else if (strcmp("LOADPCT", key) == 0)
320 apcups_detail->loadpct = value;
321 else if (strcmp("BCHARGE", key) == 0)
322 apcups_detail->bcharge = value;
323 else if (strcmp("OUTPUTV", key) == 0)
324 apcups_detail->outputv = value;
325 else if (strcmp("LINEFREQ", key) == 0)
326 apcups_detail->linefreq = value;
327 else if (strcmp("TIMELEFT", key) == 0) {
328 /* Convert minutes to seconds if requested by
330 if (conf_report_seconds)
332 apcups_detail->timeleft = value;
335 tokptr = strtok_r(NULL, ":", &toksaveptr);
336 } /* while (tokptr != NULL) */
338 status = errno; /* save errno, net_shutdown() may re-set it. */
340 if (!conf_persistent_conn)
341 net_shutdown(&global_sockfd);
345 ERROR("apcups plugin: Reading from socket failed: %s",
346 sstrerror(status, errbuf, sizeof(errbuf)));
353 static int apcups_config(oconfig_item_t *ci) {
354 _Bool persistent_conn_set = 0;
356 for (int i = 0; i < ci->children_num; i++) {
357 oconfig_item_t *child = ci->children + i;
359 if (strcasecmp(child->key, "Host") == 0)
360 cf_util_get_string(child, &conf_node);
361 else if (strcasecmp(child->key, "Port") == 0)
362 cf_util_get_service(child, &conf_service);
363 else if (strcasecmp(child->key, "ReportSeconds") == 0)
364 cf_util_get_boolean(child, &conf_report_seconds);
365 else if (strcasecmp(child->key, "PersistentConnection") == 0) {
366 cf_util_get_boolean(child, &conf_persistent_conn);
367 persistent_conn_set = 1;
369 ERROR("apcups plugin: Unknown config option \"%s\".", child->key);
372 if (!persistent_conn_set) {
373 double interval = CDTIME_T_TO_DOUBLE(plugin_get_interval());
374 if (interval > APCUPS_SERVER_TIMEOUT) {
375 NOTICE("apcups plugin: Plugin poll interval set to %.3f seconds. "
376 "Apcupsd NIS socket timeout is %.3f seconds, "
377 "PersistentConnection disabled by default.",
378 interval, APCUPS_SERVER_TIMEOUT);
379 conf_persistent_conn = 0;
384 } /* int apcups_config */
386 static void apc_submit_generic(const char *type, const char *type_inst,
391 value_list_t vl = VALUE_LIST_INIT;
392 vl.values = &(value_t){.gauge = value};
394 sstrncpy(vl.plugin, "apcups", sizeof(vl.plugin));
395 sstrncpy(vl.type, type, sizeof(vl.type));
396 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
398 plugin_dispatch_values(&vl);
401 static void apc_submit(apc_detail_t const *apcups_detail) {
402 apc_submit_generic("voltage", "input", apcups_detail->linev);
403 apc_submit_generic("voltage", "output", apcups_detail->outputv);
404 apc_submit_generic("voltage", "battery", apcups_detail->battv);
405 apc_submit_generic("charge", "", apcups_detail->bcharge);
406 apc_submit_generic("percent", "load", apcups_detail->loadpct);
407 apc_submit_generic("timeleft", "", apcups_detail->timeleft);
408 apc_submit_generic("temperature", "", apcups_detail->itemp);
409 apc_submit_generic("frequency", "input", apcups_detail->linefreq);
412 static int apcups_read(void) {
413 apc_detail_t apcups_detail = {
425 apc_query_server(conf_node == NULL ? APCUPS_DEFAULT_NODE : conf_node,
426 conf_service, &apcups_detail);
428 DEBUG("apcups plugin: apc_query_server (\"%s\", \"%s\") = %d",
429 conf_node == NULL ? APCUPS_DEFAULT_NODE : conf_node, conf_service,
434 apc_submit(&apcups_detail);
439 void module_register(void) {
440 plugin_register_complex_config("apcups", apcups_config);
441 plugin_register_read("apcups", apcups_read);
442 plugin_register_shutdown("apcups", apcups_shutdown);
443 } /* void module_register */