3 * Copyright (C) 2007 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
32 #include <upsclient.h>
35 typedef UPSCONN_t collectd_upsconn_t;
37 typedef UPSCONN collectd_upsconn_t;
39 #error "Unable to determine the UPS connection type."
43 typedef struct nut_ups_s nut_ups_t;
45 collectd_upsconn_t *conn;
52 static nut_ups_t *upslist_head = NULL;
54 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
55 static int read_busy = 0;
57 static const char *config_keys[] = {"UPS", "FORCESSL", "VERIFYPEER", "CAPATH"};
58 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
59 static int force_ssl = 0; // Initialized to default of 0 (false)
60 static int verify_peer = 0; // Initialized to default of 0 (false)
61 static char *ca_path = NULL;
63 static void free_nut_ups_t(nut_ups_t *ups) {
64 if (ups->conn != NULL) {
65 upscli_disconnect(ups->conn);
71 } /* void free_nut_ups_t */
73 static int nut_add_ups(const char *name) {
77 DEBUG("nut plugin: nut_add_ups (name = %s);", name);
79 ups = calloc(1, sizeof(*ups));
81 ERROR("nut plugin: nut_add_ups: calloc failed.");
85 status = upscli_splitname(name, &ups->upsname, &ups->hostname, &ups->port);
87 ERROR("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
92 if (upslist_head == NULL)
95 nut_ups_t *last = upslist_head;
96 while (last->next != NULL)
102 } /* int nut_add_ups */
104 static int nut_force_ssl(const char *value) {
105 if (strcasecmp(value, "true") == 0)
107 else if (strcasecmp(value, "false") == 0)
108 force_ssl = 0; // Should already be set to 0 from initialization
111 WARNING("nut plugin: nut_force_ssl: invalid FORCESSL value "
112 "found. Defaulting to false.");
115 } /* int nut_parse_force_ssl */
117 static int nut_verify_peer(const char *value) {
118 if (strcasecmp(value, "true") == 0)
120 else if (strcasecmp(value, "false") == 0)
121 verify_peer = 0; // Should already be set to 0 from initialization
124 WARNING("nut plugin: nut_verify_peer: invalid VERIFYPEER value "
125 "found. Defaulting to false.");
128 } /* int nut_verify_peer */
130 static int nut_ca_path(const char *value) {
131 if (value != NULL && strcmp(value, "") != 0) {
132 ca_path = malloc(strlen(value) + 1);
133 strncpy(ca_path, value, (strlen(value) + 1));
135 ca_path = NULL; // Should alread be set to NULL from initialization
138 } /* int nut_ca_path */
140 static int nut_config(const char *key, const char *value) {
141 if (strcasecmp(key, "UPS") == 0)
142 return (nut_add_ups(value));
143 else if (strcasecmp(key, "FORCESSL") == 0)
144 return (nut_force_ssl(value));
145 else if (strcasecmp(key, "VERIFYPEER") == 0)
146 return (nut_verify_peer(value));
147 else if (strcasecmp(key, "CAPATH") == 0)
148 return (nut_ca_path(value));
151 } /* int nut_config */
153 static void nut_submit(nut_ups_t *ups, const char *type,
154 const char *type_instance, gauge_t value) {
155 value_list_t vl = VALUE_LIST_INIT;
157 vl.values = &(value_t){.gauge = value};
160 (strcasecmp(ups->hostname, "localhost") == 0) ? hostname_g
163 sstrncpy(vl.plugin, "nut", sizeof(vl.plugin));
164 sstrncpy(vl.plugin_instance, ups->upsname, sizeof(vl.plugin_instance));
165 sstrncpy(vl.type, type, sizeof(vl.type));
166 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
168 plugin_dispatch_values(&vl);
169 } /* void nut_submit */
171 static int nut_connect(nut_ups_t *ups) {
172 #ifdef WITH_UPSCLIENT_27
177 if (verify_peer == 1 && force_ssl == 0) {
178 WARNING("nut plugin: nut_connect: VerifyPeer true but ForceSSL "
179 "false. Setting ForceSSL to true.");
183 if (verify_peer == 1 && ca_path == NULL) {
184 ERROR("nut plugin: nut_connect: VerifyPeer true but missing "
189 if (verify_peer == 1) {
190 status = upscli_init(verify_peer, ca_path, NULL, NULL);
193 ERROR("nut plugin: nut_connect: upscli_init (%i, %s) failed: %s",
194 verify_peer, ca_path, upscli_strerror(ups->conn));
198 } /* if (verify_peer == 1) */
200 if (verify_peer == 1)
201 ssl_flags = (UPSCLI_CONN_REQSSL | UPSCLI_CONN_CERTVERIF);
202 else if (force_ssl == 1)
203 ssl_flags = UPSCLI_CONN_REQSSL;
205 ssl_flags = UPSCLI_CONN_TRYSSL;
207 status = upscli_connect(ups->conn, ups->hostname, ups->port, ssl_flags);
210 ERROR("nut plugin: nut_connect: upscli_connect (%s, %i) failed: %s",
211 ups->hostname, ups->port, upscli_strerror(ups->conn));
215 } /* if (status != 0) */
217 INFO("nut plugin: Connection to (%s, %i) established.", ups->hostname,
220 // Output INFO or WARNING based on SSL and VERIFICATION
221 ssl_status = upscli_ssl(ups->conn); // 1 for SSL, 0 for not, -1 for error
222 if (ssl_status == 1 && verify_peer == 1) {
223 INFO("nut plugin: Connection is secured with SSL and certificate "
224 "has been verified.");
225 } else if (ssl_status == 1) {
226 INFO("nut plugin: Connection is secured with SSL with no verification "
227 "of server SSL certificate.");
228 } else if (ssl_status == 0) {
229 WARNING("nut plugin: Connection is unsecured (no SSL).");
231 ERROR("nut plugin: nut_connect: upscli_ssl failed: %s",
232 upscli_strerror(ups->conn));
236 } /* if (ssl_status == 1 && verify_peer == 1) */
239 #else /* #ifdef WITH_UPSCLIENT_27 */
244 if (verify_peer == 1 || ca_path != NULL) {
245 WARNING("nut plugin: nut_connect: Dependency libupsclient version "
246 "insufficient (<2.7) for VerifyPeer support. Ignoring VerifyPeer "
251 ssl_flags = UPSCLI_CONN_REQSSL;
253 ssl_flags = UPSCLI_CONN_TRYSSL;
255 status = upscli_connect(ups->conn, ups->hostname, ups->port, ssl_flags);
258 ERROR("nut plugin: nut_connect: upscli_connect (%s, %i) failed: %s",
259 ups->hostname, ups->port, upscli_strerror(ups->conn));
262 } /* if (status != 0) */
264 INFO("nut plugin: Connection to (%s, %i) established.", ups->hostname,
267 // Output INFO or WARNING based on SSL
268 ssl_status = upscli_ssl(ups->conn); // 1 for SSL, 0 for not, -1 for error
269 if (ssl_status == 1) {
270 INFO("nut plugin: Connection is secured with SSL with no verification "
271 "of server SSL certificate.");
272 } else if (ssl_status == 0) {
273 WARNING("nut plugin: Connection is unsecured (no SSL).");
275 ERROR("nut plugin: nut_connect: upscli_ssl failed: %s",
276 upscli_strerror(ups->conn));
279 } /* if (ssl_status == 1 && verify_peer == 1) */
284 static int nut_read_one(nut_ups_t *ups) {
285 const char *query[3] = {"VAR", ups->upsname, NULL};
286 unsigned int query_num = 2;
288 unsigned int answer_num;
291 /* (Re-)Connect if we have no connection */
292 if (ups->conn == NULL) {
293 ups->conn = malloc(sizeof(*ups->conn));
294 if (ups->conn == NULL) {
295 ERROR("nut plugin: malloc failed.");
299 status = nut_connect(ups);
303 } /* if (ups->conn == NULL) */
305 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
307 status = upscli_list_start(ups->conn, query_num, query);
309 ERROR("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
310 ups->upsname, upscli_strerror(ups->conn));
311 upscli_disconnect(ups->conn);
313 #ifdef WITH_UPSCLIENT_27
319 while ((status = upscli_list_next(ups->conn, query_num, query, &answer_num,
328 value = atof(answer[3]);
330 if (strncmp("ambient.", key, 8) == 0) {
331 if (strcmp("ambient.humidity", key) == 0)
332 nut_submit(ups, "humidity", "ambient", value);
333 else if (strcmp("ambient.temperature", key) == 0)
334 nut_submit(ups, "temperature", "ambient", value);
335 } else if (strncmp("battery.", key, 8) == 0) {
336 if (strcmp("battery.charge", key) == 0)
337 nut_submit(ups, "percent", "charge", value);
338 else if (strcmp("battery.current", key) == 0)
339 nut_submit(ups, "current", "battery", value);
340 else if (strcmp("battery.runtime", key) == 0)
341 nut_submit(ups, "timeleft", "battery", value);
342 else if (strcmp("battery.temperature", key) == 0)
343 nut_submit(ups, "temperature", "battery", value);
344 else if (strcmp("battery.voltage", key) == 0)
345 nut_submit(ups, "voltage", "battery", value);
346 } else if (strncmp("input.", key, 6) == 0) {
347 if (strcmp("input.frequency", key) == 0)
348 nut_submit(ups, "frequency", "input", value);
349 else if (strcmp("input.voltage", key) == 0)
350 nut_submit(ups, "voltage", "input", value);
351 } else if (strncmp("output.", key, 7) == 0) {
352 if (strcmp("output.current", key) == 0)
353 nut_submit(ups, "current", "output", value);
354 else if (strcmp("output.frequency", key) == 0)
355 nut_submit(ups, "frequency", "output", value);
356 else if (strcmp("output.voltage", key) == 0)
357 nut_submit(ups, "voltage", "output", value);
358 } else if (strncmp("ups.", key, 4) == 0) {
359 if (strcmp("ups.load", key) == 0)
360 nut_submit(ups, "percent", "load", value);
361 else if (strcmp("ups.power", key) == 0)
362 nut_submit(ups, "power", "ups", value);
363 else if (strcmp("ups.temperature", key) == 0)
364 nut_submit(ups, "temperature", "ups", value);
366 } /* while (upscli_list_next) */
369 } /* int nut_read_one */
371 static int nut_read(void) {
374 pthread_mutex_lock(&read_lock);
377 pthread_mutex_unlock(&read_lock);
382 for (nut_ups_t *ups = upslist_head; ups != NULL; ups = ups->next)
383 if (nut_read_one(ups) == 0)
386 pthread_mutex_lock(&read_lock);
388 pthread_mutex_unlock(&read_lock);
390 return ((success != 0) ? 0 : -1);
393 static int nut_shutdown(void) {
398 while (this != NULL) {
400 free_nut_ups_t(this);
403 #ifdef WITH_UPSCLIENT_27
408 } /* int nut_shutdown */
410 void module_register(void) {
411 plugin_register_config("nut", nut_config, config_keys, config_keys_num);
412 plugin_register_read("nut", nut_read);
413 plugin_register_shutdown("nut", nut_shutdown);
414 } /* void module_register */