3 * Copyright (C) 2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
31 # include <upsclient.h>
32 # define NUT_HAVE_READ 1
34 # define NUT_HAVE_READ 0
37 static data_source_t data_source_current[1] =
39 {"value", DS_TYPE_GAUGE, NAN, NAN}
42 static data_set_t ds_current =
44 "current", 1, data_source_current
47 static data_source_t data_source_humidity[1] =
49 {"value", DS_TYPE_GAUGE, 0.0, 100.0}
52 static data_set_t ds_humidity =
54 "humidity", 1, data_source_humidity
57 static data_source_t data_source_power[1] =
59 {"value", DS_TYPE_GAUGE, 0.0, NAN}
62 static data_set_t ds_power =
64 "power", 1, data_source_power
67 static data_source_t data_source_voltage[1] =
69 {"value", DS_TYPE_GAUGE, NAN, NAN}
72 static data_set_t ds_voltage =
74 "voltage", 1, data_source_voltage
77 static data_source_t data_source_percent[1] =
79 {"percent", DS_TYPE_GAUGE, 0, 100.1}
82 static data_set_t ds_percent =
84 "percent", 1, data_source_percent
87 static data_source_t data_source_timeleft[1] =
89 {"timeleft", DS_TYPE_GAUGE, 0, 100.0}
92 static data_set_t ds_timeleft =
94 "timeleft", 1, data_source_timeleft
97 static data_source_t data_source_temperature[1] =
99 {"value", DS_TYPE_GAUGE, -273.15, NAN}
102 static data_set_t ds_temperature =
104 "temperature", 1, data_source_temperature
107 static data_source_t data_source_frequency[1] =
109 {"frequency", DS_TYPE_GAUGE, 0, NAN}
112 static data_set_t ds_frequency =
114 "frequency", 1, data_source_frequency
119 typedef struct nut_ups_s nut_ups_t;
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[] =
138 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
140 static void free_nut_ups_t (nut_ups_t *ups)
142 upscli_disconnect (&ups->conn);
143 sfree (ups->hostname);
144 sfree (ups->upsname);
146 } /* void free_nut_ups_t */
148 static int nut_add_ups (const char *name)
153 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
155 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
158 ERROR ("nut plugin: nut_add_ups: malloc failed.");
161 memset (ups, '\0', sizeof (nut_ups_t));
163 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
167 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
168 free_nut_ups_t (ups);
172 status = upscli_connect (&ups->conn, ups->hostname, ups->port,
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);
182 if (upslist_head == NULL)
186 nut_ups_t *last = upslist_head;
187 while (last->next != NULL)
193 } /* int nut_add_ups */
195 static int nut_config (const char *key, const char *value)
197 if (strcasecmp (key, "UPS") == 0)
198 return (nut_add_ups (value));
201 } /* int nut_config */
203 static void nut_submit (nut_ups_t *ups, const char *type,
204 const char *type_instance, gauge_t value)
207 value_list_t vl = VALUE_LIST_INIT;
209 values[0].gauge = value;
212 vl.values_len = STATIC_ARRAY_SIZE (values);
213 vl.time = time (NULL);
215 (strcasecmp (ups->hostname, "localhost") == 0)
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)
232 const char *query[3] = {"VAR", ups->upsname, NULL};
233 unsigned int query_num = 2;
235 unsigned int answer_num;
238 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
240 status = upscli_list_start (&ups->conn, query_num, query);
243 ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
244 ups->upsname, upscli_strerror (&ups->conn));
248 while ((status = upscli_list_next (&ups->conn, query_num, query,
249 &answer_num, &answer)) == 1)
258 value = atof (answer[3]);
260 if (strncmp ("ambient.", key, 8) == 0)
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);
267 else if (strncmp ("battery.", key, 8) == 0)
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);
280 else if (strncmp ("input.", key, 6) == 0)
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);
287 else if (strncmp ("output.", key, 7) == 0)
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);
296 else if (strncmp ("ups.", key, 4) == 0)
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);
305 } /* while (upscli_list_next) */
308 } /* int nut_read_one */
310 static int nut_read (void)
315 pthread_mutex_lock (&read_lock);
318 pthread_mutex_unlock (&read_lock);
323 for (ups = upslist_head; ups != NULL; ups = ups->next)
324 if (nut_read_one (ups) == 0)
327 pthread_mutex_lock (&read_lock);
329 pthread_mutex_unlock (&read_lock);
331 return ((success != 0) ? 0 : -1);
334 static int nut_shutdown (void)
343 free_nut_ups_t (this);
348 } /* int nut_shutdown */
349 #endif /* NUT_HAVE_READ */
351 void module_register (modreg_e load)
353 if (load & MR_DATASETS)
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);
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);
373 } /* void module_register */
375 /* vim: set sw=2 ts=8 sts=2 tw=78 : */