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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #if HAVE_PTHREAD_H
27 # include <pthread.h>
28 #endif
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
37 static data_source_t data_source_current[1] =
38 {
39 {"value", DS_TYPE_GAUGE, NAN, NAN}
40 };
42 static data_set_t ds_current =
43 {
44 "current", 1, data_source_current
45 };
47 static data_source_t data_source_humidity[1] =
48 {
49 {"value", DS_TYPE_GAUGE, 0.0, 100.0}
50 };
52 static data_set_t ds_humidity =
53 {
54 "humidity", 1, data_source_humidity
55 };
57 static data_source_t data_source_power[1] =
58 {
59 {"value", DS_TYPE_GAUGE, 0.0, NAN}
60 };
62 static data_set_t ds_power =
63 {
64 "power", 1, data_source_power
65 };
67 static data_source_t data_source_voltage[1] =
68 {
69 {"value", DS_TYPE_GAUGE, NAN, NAN}
70 };
72 static data_set_t ds_voltage =
73 {
74 "voltage", 1, data_source_voltage
75 };
77 static data_source_t data_source_percent[1] =
78 {
79 {"percent", DS_TYPE_GAUGE, 0, 100.1}
80 };
82 static data_set_t ds_percent =
83 {
84 "percent", 1, data_source_percent
85 };
87 static data_source_t data_source_timeleft[1] =
88 {
89 {"timeleft", DS_TYPE_GAUGE, 0, 100.0}
90 };
92 static data_set_t ds_timeleft =
93 {
94 "timeleft", 1, data_source_timeleft
95 };
97 static data_source_t data_source_temperature[1] =
98 {
99 {"value", DS_TYPE_GAUGE, -273.15, NAN}
100 };
102 static data_set_t ds_temperature =
103 {
104 "temperature", 1, data_source_temperature
105 };
107 static data_source_t data_source_frequency[1] =
108 {
109 {"frequency", DS_TYPE_GAUGE, 0, NAN}
110 };
112 static data_set_t ds_frequency =
113 {
114 "frequency", 1, data_source_frequency
115 };
117 #if NUT_HAVE_READ
118 struct nut_ups_s;
119 typedef struct nut_ups_s nut_ups_t;
120 struct nut_ups_s
121 {
122 UPSCONN conn;
123 char *upsname;
124 char *hostname;
125 int port;
126 nut_ups_t *next;
127 };
129 static nut_ups_t *upslist_head = NULL;
131 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
132 static int read_busy = 0;
134 static const char *config_keys[] =
135 {
136 "UPS"
137 };
138 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
140 static void free_nut_ups_t (nut_ups_t *ups)
141 {
142 upscli_disconnect (&ups->conn);
143 sfree (ups->hostname);
144 sfree (ups->upsname);
145 sfree (ups);
146 } /* void free_nut_ups_t */
148 static int nut_add_ups (const char *name)
149 {
150 nut_ups_t *ups;
151 int status;
153 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
155 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
156 if (ups == NULL)
157 {
158 ERROR ("nut plugin: nut_add_ups: malloc failed.");
159 return (1);
160 }
161 memset (ups, '\0', sizeof (nut_ups_t));
163 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
164 &ups->port);
165 if (status != 0)
166 {
167 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
168 free_nut_ups_t (ups);
169 return (1);
170 }
172 status = upscli_connect (&ups->conn, ups->hostname, ups->port,
173 UPSCLI_CONN_TRYSSL);
174 if (status != 0)
175 {
176 ERROR ("nut plugin: nut_add_ups: upscli_connect (%s, %i) failed: %s",
177 ups->hostname, ups->port, upscli_strerror (&ups->conn));
178 free_nut_ups_t (ups);
179 return (1);
180 }
182 if (upslist_head == NULL)
183 upslist_head = ups;
184 else
185 {
186 nut_ups_t *last = upslist_head;
187 while (last->next != NULL)
188 last = last->next;
189 last->next = ups;
190 }
192 return (0);
193 } /* int nut_add_ups */
195 static int nut_config (const char *key, const char *value)
196 {
197 if (strcasecmp (key, "UPS") == 0)
198 return (nut_add_ups (value));
199 else
200 return (-1);
201 } /* int nut_config */
203 static void nut_submit (nut_ups_t *ups, const char *type,
204 const char *type_instance, gauge_t value)
205 {
206 value_t values[1];
207 value_list_t vl = VALUE_LIST_INIT;
209 values[0].gauge = value;
211 vl.values = values;
212 vl.values_len = STATIC_ARRAY_SIZE (values);
213 vl.time = time (NULL);
214 strncpy (vl.host,
215 (strcasecmp (ups->hostname, "localhost") == 0)
216 ? hostname_g
217 : ups->hostname,
218 sizeof (vl.host));
219 strcpy (vl.plugin, "nut");
220 strncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
221 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
223 vl.host[sizeof (vl.host) - 1] = '\0';
224 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
225 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
227 plugin_dispatch_values (type, &vl);
228 } /* void nut_submit */
230 static int nut_read_one (nut_ups_t *ups)
231 {
232 const char *query[3] = {"VAR", ups->upsname, NULL};
233 unsigned int query_num = 2;
234 char **answer;
235 unsigned int answer_num;
236 int status;
238 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
239 * error */
240 status = upscli_list_start (&ups->conn, query_num, query);
241 if (status != 0)
242 {
243 ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
244 ups->upsname, upscli_strerror (&ups->conn));
245 return (-1);
246 }
248 while ((status = upscli_list_next (&ups->conn, query_num, query,
249 &answer_num, &answer)) == 1)
250 {
251 char *key;
252 double value;
254 if (answer_num < 4)
255 continue;
257 key = answer[2];
258 value = atof (answer[3]);
260 if (strncmp ("ambient.", key, 8) == 0)
261 {
262 if (strcmp ("ambient.humidity", key) == 0)
263 nut_submit (ups, "humidity", "ambient", value);
264 else if (strcmp ("ambient.temperature", key) == 0)
265 nut_submit (ups, "temperature", "ambient", value);
266 }
267 else if (strncmp ("battery.", key, 8) == 0)
268 {
269 if (strcmp ("battery.charge", key) == 0)
270 nut_submit (ups, "percent", "charge", value);
271 else if (strcmp ("battery.current", key) == 0)
272 nut_submit (ups, "current", "battery", value);
273 else if (strcmp ("battery.runtime", key) == 0)
274 nut_submit (ups, "timeleft", "battery", value);
275 else if (strcmp ("battery.temperature", key) == 0)
276 nut_submit (ups, "temperature", "battery", value);
277 else if (strcmp ("battery.voltage", key) == 0)
278 nut_submit (ups, "voltage", "battery", value);
279 }
280 else if (strncmp ("input.", key, 6) == 0)
281 {
282 if (strcmp ("input.frequency", key) == 0)
283 nut_submit (ups, "frequency", "input", value);
284 else if (strcmp ("input.voltage", key) == 0)
285 nut_submit (ups, "voltage", "input", value);
286 }
287 else if (strncmp ("output.", key, 7) == 0)
288 {
289 if (strcmp ("output.current", key) == 0)
290 nut_submit (ups, "current", "output", value);
291 else if (strcmp ("output.frequency", key) == 0)
292 nut_submit (ups, "frequency", "output", value);
293 else if (strcmp ("output.voltage", key) == 0)
294 nut_submit (ups, "voltage", "output", value);
295 }
296 else if (strncmp ("ups.", key, 4) == 0)
297 {
298 if (strcmp ("ups.load", key) == 0)
299 nut_submit (ups, "percent", "load", value);
300 else if (strcmp ("ups.power", key) == 0)
301 nut_submit (ups, "power", "ups", value);
302 else if (strcmp ("ups.temperature", key) == 0)
303 nut_submit (ups, "temperature", "ups", value);
304 }
305 } /* while (upscli_list_next) */
307 return (0);
308 } /* int nut_read_one */
310 static int nut_read (void)
311 {
312 nut_ups_t *ups;
313 int success = 0;
315 pthread_mutex_lock (&read_lock);
316 success = read_busy;
317 read_busy = 1;
318 pthread_mutex_unlock (&read_lock);
320 if (success != 0)
321 return (0);
323 for (ups = upslist_head; ups != NULL; ups = ups->next)
324 if (nut_read_one (ups) == 0)
325 success++;
327 pthread_mutex_lock (&read_lock);
328 read_busy = 0;
329 pthread_mutex_unlock (&read_lock);
331 return ((success != 0) ? 0 : -1);
332 } /* int nut_read */
334 static int nut_shutdown (void)
335 {
336 nut_ups_t *this;
337 nut_ups_t *next;
339 this = upslist_head;
340 while (this != NULL)
341 {
342 next = this->next;
343 free_nut_ups_t (this);
344 this = next;
345 }
347 return (0);
348 } /* int nut_shutdown */
349 #endif /* NUT_HAVE_READ */
351 void module_register (modreg_e load)
352 {
353 if (load & MR_DATASETS)
354 {
355 plugin_register_data_set (&ds_current);
356 plugin_register_data_set (&ds_humidity);
357 plugin_register_data_set (&ds_power);
358 plugin_register_data_set (&ds_voltage);
359 plugin_register_data_set (&ds_percent);
360 plugin_register_data_set (&ds_timeleft);
361 plugin_register_data_set (&ds_temperature);
362 plugin_register_data_set (&ds_frequency);
363 }
365 #if NUT_HAVE_READ
366 if (load & MR_READ)
367 {
368 plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
369 plugin_register_read ("nut", nut_read);
370 plugin_register_shutdown ("nut", nut_shutdown);
371 }
372 #endif
373 } /* void module_register */
375 /* vim: set sw=2 ts=8 sts=2 tw=78 : */