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 if (ups->conn != NULL)
143 {
144 upscli_disconnect (ups->conn);
145 sfree (ups->conn);
146 }
147 sfree (ups->hostname);
148 sfree (ups->upsname);
149 sfree (ups);
150 } /* void free_nut_ups_t */
152 static int nut_add_ups (const char *name)
153 {
154 nut_ups_t *ups;
155 int status;
157 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
159 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
160 if (ups == NULL)
161 {
162 ERROR ("nut plugin: nut_add_ups: malloc failed.");
163 return (1);
164 }
165 memset (ups, '\0', sizeof (nut_ups_t));
167 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
168 &ups->port);
169 if (status != 0)
170 {
171 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
172 free_nut_ups_t (ups);
173 return (1);
174 }
176 if (upslist_head == NULL)
177 upslist_head = ups;
178 else
179 {
180 nut_ups_t *last = upslist_head;
181 while (last->next != NULL)
182 last = last->next;
183 last->next = ups;
184 }
186 return (0);
187 } /* int nut_add_ups */
189 static int nut_config (const char *key, const char *value)
190 {
191 if (strcasecmp (key, "UPS") == 0)
192 return (nut_add_ups (value));
193 else
194 return (-1);
195 } /* int nut_config */
197 static void nut_submit (nut_ups_t *ups, const char *type,
198 const char *type_instance, gauge_t value)
199 {
200 value_t values[1];
201 value_list_t vl = VALUE_LIST_INIT;
203 values[0].gauge = value;
205 vl.values = values;
206 vl.values_len = STATIC_ARRAY_SIZE (values);
207 vl.time = time (NULL);
208 strncpy (vl.host,
209 (strcasecmp (ups->hostname, "localhost") == 0)
210 ? hostname_g
211 : ups->hostname,
212 sizeof (vl.host));
213 strcpy (vl.plugin, "nut");
214 strncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
215 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
217 vl.host[sizeof (vl.host) - 1] = '\0';
218 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
219 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
221 plugin_dispatch_values (type, &vl);
222 } /* void nut_submit */
224 static int nut_read_one (nut_ups_t *ups)
225 {
226 const char *query[3] = {"VAR", ups->upsname, NULL};
227 unsigned int query_num = 2;
228 char **answer;
229 unsigned int answer_num;
230 int status;
232 /* (Re-)Connect if we have no connection */
233 if (ups->conn == NULL)
234 {
235 ups->conn = (UPSCONN *) malloc (sizeof (UPSCONN));
236 if (ups->conn == NULL)
237 {
238 ERROR ("nut plugin: malloc failed.");
239 return (-1);
240 }
242 status = upscli_connect (ups->conn, ups->hostname, ups->port,
243 UPSCLI_CONN_TRYSSL);
244 if (status != 0)
245 {
246 ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
247 ups->hostname, ups->port, upscli_strerror (ups->conn));
248 sfree (ups->conn);
249 return (-1);
250 }
252 INFO ("nut plugin: Connection to (%s, %i) established.",
253 ups->hostname, ups->port);
254 } /* if (ups->conn == NULL) */
256 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
257 * error */
258 status = upscli_list_start (ups->conn, query_num, query);
259 if (status != 0)
260 {
261 ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
262 ups->upsname, upscli_strerror (ups->conn));
263 upscli_disconnect (ups->conn);
264 sfree (ups->conn);
265 return (-1);
266 }
268 while ((status = upscli_list_next (ups->conn, query_num, query,
269 &answer_num, &answer)) == 1)
270 {
271 char *key;
272 double value;
274 if (answer_num < 4)
275 continue;
277 key = answer[2];
278 value = atof (answer[3]);
280 if (strncmp ("ambient.", key, 8) == 0)
281 {
282 if (strcmp ("ambient.humidity", key) == 0)
283 nut_submit (ups, "humidity", "ambient", value);
284 else if (strcmp ("ambient.temperature", key) == 0)
285 nut_submit (ups, "temperature", "ambient", value);
286 }
287 else if (strncmp ("battery.", key, 8) == 0)
288 {
289 if (strcmp ("battery.charge", key) == 0)
290 nut_submit (ups, "percent", "charge", value);
291 else if (strcmp ("battery.current", key) == 0)
292 nut_submit (ups, "current", "battery", value);
293 else if (strcmp ("battery.runtime", key) == 0)
294 nut_submit (ups, "timeleft", "battery", value);
295 else if (strcmp ("battery.temperature", key) == 0)
296 nut_submit (ups, "temperature", "battery", value);
297 else if (strcmp ("battery.voltage", key) == 0)
298 nut_submit (ups, "voltage", "battery", value);
299 }
300 else if (strncmp ("input.", key, 6) == 0)
301 {
302 if (strcmp ("input.frequency", key) == 0)
303 nut_submit (ups, "frequency", "input", value);
304 else if (strcmp ("input.voltage", key) == 0)
305 nut_submit (ups, "voltage", "input", value);
306 }
307 else if (strncmp ("output.", key, 7) == 0)
308 {
309 if (strcmp ("output.current", key) == 0)
310 nut_submit (ups, "current", "output", value);
311 else if (strcmp ("output.frequency", key) == 0)
312 nut_submit (ups, "frequency", "output", value);
313 else if (strcmp ("output.voltage", key) == 0)
314 nut_submit (ups, "voltage", "output", value);
315 }
316 else if (strncmp ("ups.", key, 4) == 0)
317 {
318 if (strcmp ("ups.load", key) == 0)
319 nut_submit (ups, "percent", "load", value);
320 else if (strcmp ("ups.power", key) == 0)
321 nut_submit (ups, "power", "ups", value);
322 else if (strcmp ("ups.temperature", key) == 0)
323 nut_submit (ups, "temperature", "ups", value);
324 }
325 } /* while (upscli_list_next) */
327 return (0);
328 } /* int nut_read_one */
330 static int nut_read (void)
331 {
332 nut_ups_t *ups;
333 int success = 0;
335 pthread_mutex_lock (&read_lock);
336 success = read_busy;
337 read_busy = 1;
338 pthread_mutex_unlock (&read_lock);
340 if (success != 0)
341 return (0);
343 for (ups = upslist_head; ups != NULL; ups = ups->next)
344 if (nut_read_one (ups) == 0)
345 success++;
347 pthread_mutex_lock (&read_lock);
348 read_busy = 0;
349 pthread_mutex_unlock (&read_lock);
351 return ((success != 0) ? 0 : -1);
352 } /* int nut_read */
354 static int nut_shutdown (void)
355 {
356 nut_ups_t *this;
357 nut_ups_t *next;
359 this = upslist_head;
360 while (this != NULL)
361 {
362 next = this->next;
363 free_nut_ups_t (this);
364 this = next;
365 }
367 return (0);
368 } /* int nut_shutdown */
369 #endif /* NUT_HAVE_READ */
371 void module_register (modreg_e load)
372 {
373 if (load & MR_DATASETS)
374 {
375 plugin_register_data_set (&ds_current);
376 plugin_register_data_set (&ds_humidity);
377 plugin_register_data_set (&ds_power);
378 plugin_register_data_set (&ds_voltage);
379 plugin_register_data_set (&ds_percent);
380 plugin_register_data_set (&ds_timeleft);
381 plugin_register_data_set (&ds_temperature);
382 plugin_register_data_set (&ds_frequency);
383 }
385 #if NUT_HAVE_READ
386 if (load & MR_READ)
387 {
388 plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
389 plugin_register_read ("nut", nut_read);
390 plugin_register_shutdown ("nut", nut_shutdown);
391 }
392 #endif
393 } /* void module_register */
395 /* vim: set sw=2 ts=8 sts=2 tw=78 : */