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 if (ups->conn != NULL)
144 upscli_disconnect (ups->conn);
147 sfree (ups->hostname);
148 sfree (ups->upsname);
150 } /* void free_nut_ups_t */
152 static int nut_add_ups (const char *name)
157 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
159 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
162 ERROR ("nut plugin: nut_add_ups: malloc failed.");
165 memset (ups, '\0', sizeof (nut_ups_t));
167 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
171 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
172 free_nut_ups_t (ups);
176 if (upslist_head == NULL)
180 nut_ups_t *last = upslist_head;
181 while (last->next != NULL)
187 } /* int nut_add_ups */
189 static int nut_config (const char *key, const char *value)
191 if (strcasecmp (key, "UPS") == 0)
192 return (nut_add_ups (value));
195 } /* int nut_config */
197 static void nut_submit (nut_ups_t *ups, const char *type,
198 const char *type_instance, gauge_t value)
201 value_list_t vl = VALUE_LIST_INIT;
203 values[0].gauge = value;
206 vl.values_len = STATIC_ARRAY_SIZE (values);
207 vl.time = time (NULL);
209 (strcasecmp (ups->hostname, "localhost") == 0)
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)
226 const char *query[3] = {"VAR", ups->upsname, NULL};
227 unsigned int query_num = 2;
229 unsigned int answer_num;
232 /* (Re-)Connect if we have no connection */
233 if (ups->conn == NULL)
235 ups->conn = (UPSCONN *) malloc (sizeof (UPSCONN));
236 if (ups->conn == NULL)
238 ERROR ("nut plugin: malloc failed.");
242 status = upscli_connect (ups->conn, ups->hostname, ups->port,
246 ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
247 ups->hostname, ups->port, upscli_strerror (ups->conn));
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
258 status = upscli_list_start (ups->conn, query_num, query);
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);
268 while ((status = upscli_list_next (ups->conn, query_num, query,
269 &answer_num, &answer)) == 1)
278 value = atof (answer[3]);
280 if (strncmp ("ambient.", key, 8) == 0)
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);
287 else if (strncmp ("battery.", key, 8) == 0)
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);
300 else if (strncmp ("input.", key, 6) == 0)
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);
307 else if (strncmp ("output.", key, 7) == 0)
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);
316 else if (strncmp ("ups.", key, 4) == 0)
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);
325 } /* while (upscli_list_next) */
328 } /* int nut_read_one */
330 static int nut_read (void)
335 pthread_mutex_lock (&read_lock);
338 pthread_mutex_unlock (&read_lock);
343 for (ups = upslist_head; ups != NULL; ups = ups->next)
344 if (nut_read_one (ups) == 0)
347 pthread_mutex_lock (&read_lock);
349 pthread_mutex_unlock (&read_lock);
351 return ((success != 0) ? 0 : -1);
354 static int nut_shutdown (void)
363 free_nut_ups_t (this);
368 } /* int nut_shutdown */
369 #endif /* NUT_HAVE_READ */
371 void module_register (modreg_e load)
373 if (load & MR_DATASETS)
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);
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);
393 } /* void module_register */
395 /* vim: set sw=2 ts=8 sts=2 tw=78 : */