Merge branch 'collectd-3.11' into collectd-4.0
[collectd.git] / src / nut.c
1 /**
2  * collectd - src/nut.c
3  * Copyright (C) 2007  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if HAVE_PTHREAD_H
27 # include <pthread.h>
28 #endif
29
30 #if HAVE_UPSCLIENT_H
31 # include <upsclient.h>
32 # define NUT_HAVE_READ 1
33 #else
34 # define NUT_HAVE_READ 0
35 #endif
36
37 #if HAVE_UPSCONN_T
38 typedef UPSCONN_t collectd_upsconn_t;
39 #elif HAVE_UPSCONN
40 typedef UPSCONN collectd_upsconn_t;
41 #else
42 # error "Unable to determine the UPS connection type."
43 #endif
44
45
46 #if NUT_HAVE_READ
47 struct nut_ups_s;
48 typedef struct nut_ups_s nut_ups_t;
49 struct nut_ups_s
50 {
51   collectd_upsconn_t *conn;
52   char      *upsname;
53   char      *hostname;
54   int        port;
55   nut_ups_t *next;
56 };
57
58 static nut_ups_t *upslist_head = NULL;
59
60 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
61 static int read_busy = 0;
62
63 static const char *config_keys[] =
64 {
65   "UPS"
66 };
67 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
68
69 static void free_nut_ups_t (nut_ups_t *ups)
70 {
71   if (ups->conn != NULL)
72   {
73     upscli_disconnect (ups->conn);
74     sfree (ups->conn);
75   }
76   sfree (ups->hostname);
77   sfree (ups->upsname);
78   sfree (ups);
79 } /* void free_nut_ups_t */
80
81 static int nut_add_ups (const char *name)
82 {
83   nut_ups_t *ups;
84   int status;
85
86   DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
87
88   ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
89   if (ups == NULL)
90   {
91     ERROR ("nut plugin: nut_add_ups: malloc failed.");
92     return (1);
93   }
94   memset (ups, '\0', sizeof (nut_ups_t));
95
96   status = upscli_splitname (name, &ups->upsname, &ups->hostname,
97       &ups->port);
98   if (status != 0)
99   {
100     ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
101     free_nut_ups_t (ups);
102     return (1);
103   }
104
105   if (upslist_head == NULL)
106     upslist_head = ups;
107   else
108   {
109     nut_ups_t *last = upslist_head;
110     while (last->next != NULL)
111       last = last->next;
112     last->next = ups;
113   }
114
115   return (0);
116 } /* int nut_add_ups */
117
118 static int nut_config (const char *key, const char *value)
119 {
120   if (strcasecmp (key, "UPS") == 0)
121     return (nut_add_ups (value));
122   else
123     return (-1);
124 } /* int nut_config */
125
126 static void nut_submit (nut_ups_t *ups, const char *type,
127     const char *type_instance, gauge_t value)
128 {
129   value_t values[1];
130   value_list_t vl = VALUE_LIST_INIT;
131
132   values[0].gauge = value;
133
134   vl.values = values;
135   vl.values_len = STATIC_ARRAY_SIZE (values);
136   vl.time = time (NULL);
137   strncpy (vl.host,
138       (strcasecmp (ups->hostname, "localhost") == 0)
139       ? hostname_g
140       : ups->hostname,
141       sizeof (vl.host));
142   strcpy (vl.plugin, "nut");
143   strncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
144   strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
145
146   vl.host[sizeof (vl.host) - 1] = '\0';
147   vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
148   vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
149
150   plugin_dispatch_values (type, &vl);
151 } /* void nut_submit */
152
153 static int nut_read_one (nut_ups_t *ups)
154 {
155   const char *query[3] = {"VAR", ups->upsname, NULL};
156   unsigned int query_num = 2;
157   char **answer;
158   unsigned int answer_num;
159   int status;
160
161   /* (Re-)Connect if we have no connection */
162   if (ups->conn == NULL)
163   {
164     ups->conn = (collectd_upsconn_t *) malloc (sizeof (collectd_upsconn_t));
165     if (ups->conn == NULL)
166     {
167       ERROR ("nut plugin: malloc failed.");
168       return (-1);
169     }
170
171     status = upscli_connect (ups->conn, ups->hostname, ups->port,
172         UPSCLI_CONN_TRYSSL);
173     if (status != 0)
174     {
175       ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
176           ups->hostname, ups->port, upscli_strerror (ups->conn));
177       sfree (ups->conn);
178       return (-1);
179     }
180
181     INFO ("nut plugin: Connection to (%s, %i) established.",
182         ups->hostname, ups->port);
183   } /* if (ups->conn == NULL) */
184
185   /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
186    * error */
187   status = upscli_list_start (ups->conn, query_num, query);
188   if (status != 0)
189   {
190     ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
191         ups->upsname, upscli_strerror (ups->conn));
192     upscli_disconnect (ups->conn);
193     sfree (ups->conn);
194     return (-1);
195   }
196
197   while ((status = upscli_list_next (ups->conn, query_num, query,
198           &answer_num, &answer)) == 1)
199   {
200     char  *key;
201     double value;
202
203     if (answer_num < 4)
204       continue;
205
206     key = answer[2];
207     value = atof (answer[3]);
208
209     if (strncmp ("ambient.", key, 8) == 0)
210     {
211       if (strcmp ("ambient.humidity", key) == 0)
212         nut_submit (ups, "humidity", "ambient", value);
213       else if (strcmp ("ambient.temperature", key) == 0)
214         nut_submit (ups, "temperature", "ambient", value);
215     }
216     else if (strncmp ("battery.", key, 8) == 0)
217     {
218       if (strcmp ("battery.charge", key) == 0)
219         nut_submit (ups, "percent", "charge", value);
220       else if (strcmp ("battery.current", key) == 0)
221         nut_submit (ups, "current", "battery", value);
222       else if (strcmp ("battery.runtime", key) == 0)
223         nut_submit (ups, "timeleft", "battery", value);
224       else if (strcmp ("battery.temperature", key) == 0)
225         nut_submit (ups, "temperature", "battery", value);
226       else if (strcmp ("battery.voltage", key) == 0)
227         nut_submit (ups, "voltage", "battery", value);
228     }
229     else if (strncmp ("input.", key, 6) == 0)
230     {
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     }
236     else if (strncmp ("output.", key, 7) == 0)
237     {
238       if (strcmp ("output.current", key) == 0)
239         nut_submit (ups, "current", "output", value);
240       else if (strcmp ("output.frequency", key) == 0)
241         nut_submit (ups, "frequency", "output", value);
242       else if (strcmp ("output.voltage", key) == 0)
243         nut_submit (ups, "voltage", "output", value);
244     }
245     else if (strncmp ("ups.", key, 4) == 0)
246     {
247       if (strcmp ("ups.load", key) == 0)
248         nut_submit (ups, "percent", "load", value);
249       else if (strcmp ("ups.power", key) == 0)
250         nut_submit (ups, "power", "ups", value);
251       else if (strcmp ("ups.temperature", key) == 0)
252         nut_submit (ups, "temperature", "ups", value);
253     }
254   } /* while (upscli_list_next) */
255
256   return (0);
257 } /* int nut_read_one */
258
259 static int nut_read (void)
260 {
261   nut_ups_t *ups;
262   int success = 0;
263
264   pthread_mutex_lock (&read_lock);
265   success = read_busy;
266   read_busy = 1;
267   pthread_mutex_unlock (&read_lock);
268
269   if (success != 0)
270     return (0);
271
272   for (ups = upslist_head; ups != NULL; ups = ups->next)
273     if (nut_read_one (ups) == 0)
274       success++;
275
276   pthread_mutex_lock (&read_lock);
277   read_busy = 0;
278   pthread_mutex_unlock (&read_lock);
279
280   return ((success != 0) ? 0 : -1);
281 } /* int nut_read */
282
283 static int nut_shutdown (void)
284 {
285   nut_ups_t *this;
286   nut_ups_t *next;
287
288   this = upslist_head;
289   while (this != NULL)
290   {
291     next = this->next;
292     free_nut_ups_t (this);
293     this = next;
294   }
295
296   return (0);
297 } /* int nut_shutdown */
298 #endif /* NUT_HAVE_READ */
299
300 void module_register (void)
301 {
302 #if NUT_HAVE_READ
303   plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
304   plugin_register_read ("nut", nut_read);
305   plugin_register_shutdown ("nut", nut_shutdown);
306 #endif
307 } /* void module_register */
308
309 /* vim: set sw=2 ts=8 sts=2 tw=78 : */