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