Added ForceSSL feature. Updated documentation and added my contributor information.
[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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #include <upsclient.h>
33
34 #if HAVE_UPSCONN_T
35 typedef UPSCONN_t collectd_upsconn_t;
36 #elif HAVE_UPSCONN
37 typedef UPSCONN collectd_upsconn_t;
38 #else
39 #error "Unable to determine the UPS connection type."
40 #endif
41
42 struct nut_ups_s;
43 typedef struct nut_ups_s nut_ups_t;
44 struct nut_ups_s {
45   collectd_upsconn_t *conn;
46   char *upsname;
47   char *hostname;
48   int port;
49   nut_ups_t *next;
50 };
51
52 static nut_ups_t *upslist_head = NULL;
53
54 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
55 static int read_busy = 0;
56
57 static const char *config_keys[] = {"UPS", "FORCESSL"};
58 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
59 static int force_ssl = 0;   // Initialized to default of 0 (false)
60
61 static void free_nut_ups_t(nut_ups_t *ups) {
62   if (ups->conn != NULL) {
63     upscli_disconnect(ups->conn);
64     sfree(ups->conn);
65   }
66   sfree(ups->hostname);
67   sfree(ups->upsname);
68   sfree(ups);
69 } /* void free_nut_ups_t */
70
71 static int nut_add_ups(const char *name) {
72   nut_ups_t *ups;
73   int status;
74
75   DEBUG("nut plugin: nut_add_ups (name = %s);", name);
76
77   ups = calloc(1, sizeof(*ups));
78   if (ups == NULL) {
79     ERROR("nut plugin: nut_add_ups: calloc failed.");
80     return (1);
81   }
82
83   status = upscli_splitname(name, &ups->upsname, &ups->hostname, &ups->port);
84   if (status != 0) {
85     ERROR("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
86     free_nut_ups_t(ups);
87     return (1);
88   }
89
90   if (upslist_head == NULL)
91     upslist_head = ups;
92   else {
93     nut_ups_t *last = upslist_head;
94     while (last->next != NULL)
95       last = last->next;
96     last->next = ups;
97   }
98
99   return (0);
100 } /* int nut_add_ups */
101
102 static int nut_force_ssl(const char *value) {
103   if (strcasecmp(value, "true") == 0)
104     force_ssl = 1;
105   else if (strcasecmp(value, "false") == 0)
106     force_ssl = 0; // Should already be set to 0 from initialization
107   else {
108     force_ssl = 0;
109     WARNING("nut plugin: nut_force_ssl: invalid FORCESSL value "
110             "found. Defaulting to false.");
111   }
112   return (0);
113 } /* int nut_parse_force_ssl */
114
115 static int nut_config(const char *key, const char *value) {
116   if (strcasecmp(key, "UPS") == 0)
117     return (nut_add_ups(value));
118   else if (strcasecmp(key, "FORCESSL") == 0)
119     return (nut_force_ssl(value));
120   else
121     return (-1);
122 } /* int nut_config */
123
124 static void nut_submit(nut_ups_t *ups, const char *type,
125                        const char *type_instance, gauge_t value) {
126   value_list_t vl = VALUE_LIST_INIT;
127
128   vl.values = &(value_t){.gauge = value};
129   vl.values_len = 1;
130   sstrncpy(vl.host,
131            (strcasecmp(ups->hostname, "localhost") == 0) ? hostname_g
132                                                          : ups->hostname,
133            sizeof(vl.host));
134   sstrncpy(vl.plugin, "nut", sizeof(vl.plugin));
135   sstrncpy(vl.plugin_instance, ups->upsname, sizeof(vl.plugin_instance));
136   sstrncpy(vl.type, type, sizeof(vl.type));
137   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
138
139   plugin_dispatch_values(&vl);
140 } /* void nut_submit */
141
142 static int nut_read_one(nut_ups_t *ups) {
143   const char *query[3] = {"VAR", ups->upsname, NULL};
144   unsigned int query_num = 2;
145   char **answer;
146   unsigned int answer_num;
147   int status;
148   int ssl_status;
149   int ssl_flags;
150
151   /* (Re-)Connect if we have no connection */
152   if (ups->conn == NULL) {
153     ups->conn = malloc(sizeof(*ups->conn));
154     if (ups->conn == NULL) {
155       ERROR("nut plugin: malloc failed.");
156       return (-1);
157     }
158
159     if (force_ssl == 1)
160       ssl_flags = UPSCLI_CONN_REQSSL;
161     else
162       ssl_flags = UPSCLI_CONN_TRYSSL;
163
164     status = upscli_connect(ups->conn, ups->hostname, ups->port, ssl_flags);
165
166     if (status != 0) {
167       ERROR("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
168             ups->hostname, ups->port, upscli_strerror(ups->conn));
169       sfree(ups->conn);
170       return (-1);
171     } /* if (status != 0) */
172
173     INFO("nut plugin: Connection to (%s, %i) established.", ups->hostname,
174          ups->port);
175
176     // Output INFO or WARNING based on SSL and VERIFICATION
177     ssl_status =  upscli_ssl(ups->conn); // 1 for SSL, 0 for not, -1 for error
178     if (ssl_status == 1){
179       INFO("nut plugin: Connection is secured with SSL.");
180     }
181     else if (ssl_status == 0){
182       WARNING("nut plugin: Connection is unsecured (no SSL).");
183     }else{
184       ERROR("nut plugin: nut_read_one: upscli_ssl failed: %s",
185             upscli_strerror(ups->conn));
186       sfree(ups->conn);
187       return (-1);
188     } /* if (ssl_status == 1 && verify_peer == 1) */
189
190   } /* if (ups->conn == NULL) */
191
192   /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
193    * error */
194   status = upscli_list_start(ups->conn, query_num, query);
195   if (status != 0) {
196     ERROR("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
197           ups->upsname, upscli_strerror(ups->conn));
198     upscli_disconnect(ups->conn);
199     sfree(ups->conn);
200     return (-1);
201   }
202
203   while ((status = upscli_list_next(ups->conn, query_num, query, &answer_num,
204                                     &answer)) == 1) {
205     char *key;
206     double value;
207
208     if (answer_num < 4)
209       continue;
210
211     key = answer[2];
212     value = atof(answer[3]);
213
214     if (strncmp("ambient.", key, 8) == 0) {
215       if (strcmp("ambient.humidity", key) == 0)
216         nut_submit(ups, "humidity", "ambient", value);
217       else if (strcmp("ambient.temperature", key) == 0)
218         nut_submit(ups, "temperature", "ambient", value);
219     } else if (strncmp("battery.", key, 8) == 0) {
220       if (strcmp("battery.charge", key) == 0)
221         nut_submit(ups, "percent", "charge", value);
222       else if (strcmp("battery.current", key) == 0)
223         nut_submit(ups, "current", "battery", value);
224       else if (strcmp("battery.runtime", key) == 0)
225         nut_submit(ups, "timeleft", "battery", value);
226       else if (strcmp("battery.temperature", key) == 0)
227         nut_submit(ups, "temperature", "battery", value);
228       else if (strcmp("battery.voltage", key) == 0)
229         nut_submit(ups, "voltage", "battery", value);
230     } else if (strncmp("input.", key, 6) == 0) {
231       if (strcmp("input.frequency", key) == 0)
232         nut_submit(ups, "frequency", "input", value);
233       else if (strcmp("input.voltage", key) == 0)
234         nut_submit(ups, "voltage", "input", value);
235     } else if (strncmp("output.", key, 7) == 0) {
236       if (strcmp("output.current", key) == 0)
237         nut_submit(ups, "current", "output", value);
238       else if (strcmp("output.frequency", key) == 0)
239         nut_submit(ups, "frequency", "output", value);
240       else if (strcmp("output.voltage", key) == 0)
241         nut_submit(ups, "voltage", "output", value);
242     } else if (strncmp("ups.", key, 4) == 0) {
243       if (strcmp("ups.load", key) == 0)
244         nut_submit(ups, "percent", "load", value);
245       else if (strcmp("ups.power", key) == 0)
246         nut_submit(ups, "power", "ups", value);
247       else if (strcmp("ups.temperature", key) == 0)
248         nut_submit(ups, "temperature", "ups", value);
249     }
250   } /* while (upscli_list_next) */
251
252   return (0);
253 } /* int nut_read_one */
254
255 static int nut_read(void) {
256   int success = 0;
257
258   pthread_mutex_lock(&read_lock);
259   success = read_busy;
260   read_busy = 1;
261   pthread_mutex_unlock(&read_lock);
262
263   if (success != 0)
264     return (0);
265
266   for (nut_ups_t *ups = upslist_head; ups != NULL; ups = ups->next)
267     if (nut_read_one(ups) == 0)
268       success++;
269
270   pthread_mutex_lock(&read_lock);
271   read_busy = 0;
272   pthread_mutex_unlock(&read_lock);
273
274   return ((success != 0) ? 0 : -1);
275 } /* int nut_read */
276
277 static int nut_shutdown(void) {
278   nut_ups_t *this;
279   nut_ups_t *next;
280
281   this = upslist_head;
282   while (this != NULL) {
283     next = this->next;
284     free_nut_ups_t(this);
285     this = next;
286   }
287
288   return (0);
289 } /* int nut_shutdown */
290
291 void module_register(void) {
292   plugin_register_config("nut", nut_config, config_keys, config_keys_num);
293   plugin_register_read("nut", nut_read);
294   plugin_register_shutdown("nut", nut_shutdown);
295 } /* void module_register */