Merge pull request #2618 from ajssmith/amqp1_dev1_branch
[collectd.git] / src / nut.c
1 /**
2  * collectd - src/nut.c
3  * Copyright (C) 2007       Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  *   Pavel Rochnyak <pavel2000 ngs.ru>
26  **/
27
28 #include "collectd.h"
29
30 #include "common.h"
31 #include "plugin.h"
32
33 #include <upsclient.h>
34
35 #if HAVE_UPSCONN_T
36 typedef UPSCONN_t collectd_upsconn_t;
37 #elif HAVE_UPSCONN
38 typedef UPSCONN collectd_upsconn_t;
39 #else
40 #error "Unable to determine the UPS connection type."
41 #endif
42
43 struct nut_ups_s;
44 typedef struct nut_ups_s nut_ups_t;
45 struct nut_ups_s {
46   collectd_upsconn_t *conn;
47   char *upsname;
48   char *hostname;
49   int port;
50   nut_ups_t *next;
51 };
52
53 static const char *config_keys[] = {"UPS", "FORCESSL", "VERIFYPEER", "CAPATH",
54                                     "CONNECTTIMEOUT"};
55 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
56 static int force_ssl;   // Initialized to default of 0 (false)
57 static int verify_peer; // Initialized to default of 0 (false)
58 static int ssl_flags = UPSCLI_CONN_TRYSSL;
59 static int connect_timeout = -1;
60 static char *ca_path;
61
62 static int nut_read(user_data_t *user_data);
63
64 static void free_nut_ups_t(void *arg) {
65   nut_ups_t *ups = arg;
66
67   if (ups->conn != NULL) {
68     upscli_disconnect(ups->conn);
69     sfree(ups->conn);
70   }
71   sfree(ups->hostname);
72   sfree(ups->upsname);
73   sfree(ups);
74 } /* void free_nut_ups_t */
75
76 static int nut_add_ups(const char *name) {
77   nut_ups_t *ups;
78   int status;
79   char *cb_name;
80
81   DEBUG("nut plugin: nut_add_ups (name = %s);", name);
82
83   ups = calloc(1, sizeof(*ups));
84   if (ups == NULL) {
85     ERROR("nut plugin: nut_add_ups: calloc failed.");
86     return 1;
87   }
88
89   status = upscli_splitname(name, &ups->upsname, &ups->hostname, &ups->port);
90   if (status != 0) {
91     ERROR("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
92     free_nut_ups_t(ups);
93     return 1;
94   }
95
96   cb_name = ssnprintf_alloc("nut/%s", name);
97
98   status = plugin_register_complex_read(
99       /* group     = */ "nut",
100       /* name      = */ cb_name,
101       /* callback  = */ nut_read,
102       /* interval  = */ 0,
103       /* user_data = */ &(user_data_t){
104           .data = ups, .free_func = free_nut_ups_t,
105       });
106
107   sfree(cb_name);
108
109   if (status == EINVAL) {
110     WARNING("nut plugin: UPS \"%s\" already added. "
111             "Please check your configuration.",
112             name);
113     return -1;
114   }
115
116   return 0;
117 } /* int nut_add_ups */
118
119 static int nut_force_ssl(const char *value) {
120   if (strcasecmp(value, "true") == 0)
121     force_ssl = 1;
122   else if (strcasecmp(value, "false") == 0)
123     force_ssl = 0; // Should already be set to 0 from initialization
124   else {
125     force_ssl = 0;
126     WARNING("nut plugin: nut_force_ssl: invalid FORCESSL value "
127             "found. Defaulting to false.");
128   }
129   return 0;
130 } /* int nut_parse_force_ssl */
131
132 static int nut_verify_peer(const char *value) {
133   if (strcasecmp(value, "true") == 0)
134     verify_peer = 1;
135   else if (strcasecmp(value, "false") == 0)
136     verify_peer = 0; // Should already be set to 0 from initialization
137   else {
138     verify_peer = 0;
139     WARNING("nut plugin: nut_verify_peer: invalid VERIFYPEER value "
140             "found. Defaulting to false.");
141   }
142   return 0;
143 } /* int nut_verify_peer */
144
145 static int nut_ca_path(const char *value) {
146   if (value != NULL && strcmp(value, "") != 0) {
147     ca_path = strdup(value);
148   } else {
149     ca_path = NULL; // Should alread be set to NULL from initialization
150   }
151   return 0;
152 } /* int nut_ca_path */
153
154 static int nut_set_connect_timeout(const char *value) {
155 #if HAVE_UPSCLI_TRYCONNECT
156   long ret;
157
158   errno = 0;
159   ret = strtol(value, /* endptr = */ NULL, /* base = */ 10);
160   if (errno == 0)
161     connect_timeout = ret;
162   else
163     WARNING("nut plugin: The ConnectTimeout option requires numeric argument. "
164             "Setting ignored.");
165 #else /* #if HAVE_UPSCLI_TRYCONNECT */
166   WARNING("nut plugin: Dependency libupsclient version insufficient (<2.6.2) "
167           "for ConnectTimeout option support. Setting ignored.");
168 #endif
169   return 0;
170 } /* int nut_set_connect_timeout */
171
172 static int nut_config(const char *key, const char *value) {
173   if (strcasecmp(key, "UPS") == 0)
174     return nut_add_ups(value);
175   else if (strcasecmp(key, "FORCESSL") == 0)
176     return nut_force_ssl(value);
177   else if (strcasecmp(key, "VERIFYPEER") == 0)
178     return nut_verify_peer(value);
179   else if (strcasecmp(key, "CAPATH") == 0)
180     return nut_ca_path(value);
181   else if (strcasecmp(key, "CONNECTTIMEOUT") == 0)
182     return nut_set_connect_timeout(value);
183   else
184     return -1;
185 } /* int nut_config */
186
187 static void nut_submit(nut_ups_t *ups, const char *type,
188                        const char *type_instance, gauge_t value) {
189   value_list_t vl = VALUE_LIST_INIT;
190
191   vl.values = &(value_t){.gauge = value};
192   vl.values_len = 1;
193   if (strcasecmp(ups->hostname, "localhost") != 0)
194     sstrncpy(vl.host, ups->hostname, sizeof(vl.host));
195   sstrncpy(vl.plugin, "nut", sizeof(vl.plugin));
196   sstrncpy(vl.plugin_instance, ups->upsname, sizeof(vl.plugin_instance));
197   sstrncpy(vl.type, type, sizeof(vl.type));
198   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
199
200   plugin_dispatch_values(&vl);
201 } /* void nut_submit */
202
203 static int nut_connect(nut_ups_t *ups) {
204   int status, ssl_status;
205
206 #if HAVE_UPSCLI_TRYCONNECT
207   struct timeval tv;
208   tv.tv_sec = connect_timeout / 1000;
209   tv.tv_usec = connect_timeout % 1000;
210
211   status =
212       upscli_tryconnect(ups->conn, ups->hostname, ups->port, ssl_flags, &tv);
213 #else /* #if HAVE_UPSCLI_TRYCONNECT */
214   status = upscli_connect(ups->conn, ups->hostname, ups->port, ssl_flags);
215 #endif
216
217   if (status != 0) {
218     ERROR("nut plugin: nut_connect: upscli_connect (%s, %i) failed: %s",
219           ups->hostname, ups->port, upscli_strerror(ups->conn));
220     sfree(ups->conn);
221     return -1;
222   } /* if (status != 0) */
223
224   INFO("nut plugin: Connection to (%s, %i) established.", ups->hostname,
225        ups->port);
226
227   // Output INFO or WARNING based on SSL and VERIFICATION
228   ssl_status = upscli_ssl(ups->conn); // 1 for SSL, 0 for not, -1 for error
229   if (ssl_status == 1 && verify_peer == 1) {
230     INFO("nut plugin: Connection is secured with SSL and certificate "
231          "has been verified.");
232   } else if (ssl_status == 1) {
233     INFO("nut plugin: Connection is secured with SSL with no verification "
234          "of server SSL certificate.");
235   } else if (ssl_status == 0) {
236     WARNING("nut plugin: Connection is unsecured (no SSL).");
237   } else {
238     ERROR("nut plugin: nut_connect: upscli_ssl failed: %s",
239           upscli_strerror(ups->conn));
240     sfree(ups->conn);
241     return -1;
242   } /* if (ssl_status == 1 && verify_peer == 1) */
243   return 0;
244 }
245
246 static int nut_read(user_data_t *user_data) {
247   nut_ups_t *ups = user_data->data;
248   const char *query[3] = {"VAR", ups->upsname, NULL};
249   unsigned int query_num = 2;
250   char **answer;
251   unsigned int answer_num;
252   int status;
253
254   /* (Re-)Connect if we have no connection */
255   if (ups->conn == NULL) {
256     ups->conn = malloc(sizeof(*ups->conn));
257     if (ups->conn == NULL) {
258       ERROR("nut plugin: malloc failed.");
259       return -1;
260     }
261
262     status = nut_connect(ups);
263     if (status == -1)
264       return -1;
265
266   } /* if (ups->conn == NULL) */
267
268   /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
269    * error */
270   status = upscli_list_start(ups->conn, query_num, query);
271   if (status != 0) {
272     ERROR("nut plugin: nut_read: upscli_list_start (%s) failed: %s",
273           ups->upsname, upscli_strerror(ups->conn));
274     upscli_disconnect(ups->conn);
275     sfree(ups->conn);
276     return -1;
277   }
278
279   while ((status = upscli_list_next(ups->conn, query_num, query, &answer_num,
280                                     &answer)) == 1) {
281     char *key;
282     double value;
283
284     if (answer_num < 4)
285       continue;
286
287     key = answer[2];
288     value = atof(answer[3]);
289
290     if (strncmp("ambient.", key, 8) == 0) {
291       if (strcmp("ambient.humidity", key) == 0)
292         nut_submit(ups, "humidity", "ambient", value);
293       else if (strcmp("ambient.temperature", key) == 0)
294         nut_submit(ups, "temperature", "ambient", value);
295     } else if (strncmp("battery.", key, 8) == 0) {
296       if (strcmp("battery.charge", key) == 0)
297         nut_submit(ups, "percent", "charge", value);
298       else if (strcmp("battery.current", key) == 0)
299         nut_submit(ups, "current", "battery", value);
300       else if (strcmp("battery.runtime", key) == 0)
301         nut_submit(ups, "timeleft", "battery", value);
302       else if (strcmp("battery.temperature", key) == 0)
303         nut_submit(ups, "temperature", "battery", value);
304       else if (strcmp("battery.voltage", key) == 0)
305         nut_submit(ups, "voltage", "battery", value);
306     } else if (strncmp("input.", key, 6) == 0) {
307       if (strcmp("input.frequency", key) == 0)
308         nut_submit(ups, "frequency", "input", value);
309       else if (strcmp("input.voltage", key) == 0)
310         nut_submit(ups, "voltage", "input", value);
311     } else if (strncmp("output.", key, 7) == 0) {
312       if (strcmp("output.current", key) == 0)
313         nut_submit(ups, "current", "output", value);
314       else if (strcmp("output.frequency", key) == 0)
315         nut_submit(ups, "frequency", "output", value);
316       else if (strcmp("output.voltage", key) == 0)
317         nut_submit(ups, "voltage", "output", value);
318     } else if (strncmp("ups.", key, 4) == 0) {
319       if (strcmp("ups.load", key) == 0)
320         nut_submit(ups, "percent", "load", value);
321       else if (strcmp("ups.power", key) == 0)
322         nut_submit(ups, "power", "ups", value);
323       else if (strcmp("ups.temperature", key) == 0)
324         nut_submit(ups, "temperature", "ups", value);
325     }
326   } /* while (upscli_list_next) */
327
328   return 0;
329 } /* int nut_read */
330
331 static int nut_init(void) {
332 #if HAVE_UPSCLI_INIT
333   if (verify_peer == 1 && force_ssl == 0) {
334     WARNING("nut plugin: nut_connect: VerifyPeer true but ForceSSL "
335             "false. Setting ForceSSL to true.");
336     force_ssl = 1;
337   }
338
339   if (verify_peer == 1 && ca_path == NULL) {
340     ERROR("nut plugin: nut_connect: VerifyPeer true but missing "
341           "CAPath value.");
342     plugin_unregister_read_group("nut");
343     return -1;
344   }
345
346   if (verify_peer == 1 || force_ssl == 1) {
347     int status = upscli_init(verify_peer, ca_path, NULL, NULL);
348
349     if (status != 1) {
350       ERROR("nut plugin: upscli_init (%i, %s) failed", verify_peer, ca_path);
351       upscli_cleanup();
352       plugin_unregister_read_group("nut");
353       return -1;
354     }
355   } /* if (verify_peer == 1) */
356
357   if (verify_peer == 1)
358     ssl_flags = (UPSCLI_CONN_REQSSL | UPSCLI_CONN_CERTVERIF);
359   else if (force_ssl == 1)
360     ssl_flags = UPSCLI_CONN_REQSSL;
361
362 #else /* #if HAVE_UPSCLI_INIT */
363   if (verify_peer == 1 || ca_path != NULL) {
364     WARNING("nut plugin: nut_connect: Dependency libupsclient version "
365             "insufficient (<2.7) for VerifyPeer support. Ignoring VerifyPeer "
366             "and CAPath.");
367     verify_peer = 0;
368   }
369
370   if (force_ssl == 1)
371     ssl_flags = UPSCLI_CONN_REQSSL;
372 #endif
373
374   if (connect_timeout <= 0)
375     connect_timeout = (long)CDTIME_T_TO_MS(plugin_get_interval());
376
377   return 0;
378 } /* int nut_init */
379
380 static int nut_shutdown(void) {
381 #if HAVE_UPSCLI_INIT
382   upscli_cleanup();
383 #endif
384
385   return 0;
386 } /* int nut_shutdown */
387
388 void module_register(void) {
389   plugin_register_config("nut", nut_config, config_keys, config_keys_num);
390   plugin_register_init("nut", nut_init);
391   plugin_register_shutdown("nut", nut_shutdown);
392 } /* void module_register */