3 * Copyright (C) 2007 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
32 #include <upsclient.h>
35 typedef UPSCONN_t collectd_upsconn_t;
37 typedef UPSCONN collectd_upsconn_t;
39 # error "Unable to determine the UPS connection type."
43 typedef struct nut_ups_s nut_ups_t;
46 collectd_upsconn_t *conn;
53 static nut_ups_t *upslist_head = NULL;
55 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
56 static int read_busy = 0;
58 static const char *config_keys[] =
62 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
64 static void free_nut_ups_t (nut_ups_t *ups)
66 if (ups->conn != NULL)
68 upscli_disconnect (ups->conn);
71 sfree (ups->hostname);
74 } /* void free_nut_ups_t */
76 static int nut_add_ups (const char *name)
81 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
83 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
86 ERROR ("nut plugin: nut_add_ups: malloc failed.");
89 memset (ups, '\0', sizeof (nut_ups_t));
91 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
95 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
100 if (upslist_head == NULL)
104 nut_ups_t *last = upslist_head;
105 while (last->next != NULL)
111 } /* int nut_add_ups */
113 static int nut_config (const char *key, const char *value)
115 if (strcasecmp (key, "UPS") == 0)
116 return (nut_add_ups (value));
119 } /* int nut_config */
121 static void nut_submit (nut_ups_t *ups, const char *type,
122 const char *type_instance, gauge_t value)
125 value_list_t vl = VALUE_LIST_INIT;
127 values[0].gauge = value;
130 vl.values_len = STATIC_ARRAY_SIZE (values);
132 (strcasecmp (ups->hostname, "localhost") == 0)
136 sstrncpy (vl.plugin, "nut", sizeof (vl.plugin));
137 sstrncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
138 sstrncpy (vl.type, type, sizeof (vl.type));
139 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
141 plugin_dispatch_values (&vl);
142 } /* void nut_submit */
144 static int nut_read_one (nut_ups_t *ups)
146 const char *query[3] = {"VAR", ups->upsname, NULL};
147 unsigned int query_num = 2;
149 unsigned int answer_num;
152 /* (Re-)Connect if we have no connection */
153 if (ups->conn == NULL)
155 ups->conn = (collectd_upsconn_t *) malloc (sizeof (collectd_upsconn_t));
156 if (ups->conn == NULL)
158 ERROR ("nut plugin: malloc failed.");
162 status = upscli_connect (ups->conn, ups->hostname, ups->port,
166 ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
167 ups->hostname, ups->port, upscli_strerror (ups->conn));
172 INFO ("nut plugin: Connection to (%s, %i) established.",
173 ups->hostname, ups->port);
174 } /* if (ups->conn == NULL) */
176 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
178 status = upscli_list_start (ups->conn, query_num, query);
181 ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
182 ups->upsname, upscli_strerror (ups->conn));
183 upscli_disconnect (ups->conn);
188 while ((status = upscli_list_next (ups->conn, query_num, query,
189 &answer_num, &answer)) == 1)
198 value = atof (answer[3]);
200 if (strncmp ("ambient.", key, 8) == 0)
202 if (strcmp ("ambient.humidity", key) == 0)
203 nut_submit (ups, "humidity", "ambient", value);
204 else if (strcmp ("ambient.temperature", key) == 0)
205 nut_submit (ups, "temperature", "ambient", value);
207 else if (strncmp ("battery.", key, 8) == 0)
209 if (strcmp ("battery.charge", key) == 0)
210 nut_submit (ups, "percent", "charge", value);
211 else if (strcmp ("battery.current", key) == 0)
212 nut_submit (ups, "current", "battery", value);
213 else if (strcmp ("battery.runtime", key) == 0)
214 nut_submit (ups, "timeleft", "battery", value);
215 else if (strcmp ("battery.temperature", key) == 0)
216 nut_submit (ups, "temperature", "battery", value);
217 else if (strcmp ("battery.voltage", key) == 0)
218 nut_submit (ups, "voltage", "battery", value);
220 else if (strncmp ("input.", key, 6) == 0)
222 if (strcmp ("input.frequency", key) == 0)
223 nut_submit (ups, "frequency", "input", value);
224 else if (strcmp ("input.voltage", key) == 0)
225 nut_submit (ups, "voltage", "input", value);
227 else if (strncmp ("output.", key, 7) == 0)
229 if (strcmp ("output.current", key) == 0)
230 nut_submit (ups, "current", "output", value);
231 else if (strcmp ("output.frequency", key) == 0)
232 nut_submit (ups, "frequency", "output", value);
233 else if (strcmp ("output.voltage", key) == 0)
234 nut_submit (ups, "voltage", "output", value);
236 else if (strncmp ("ups.", key, 4) == 0)
238 if (strcmp ("ups.load", key) == 0)
239 nut_submit (ups, "percent", "load", value);
240 else if (strcmp ("ups.power", key) == 0)
241 nut_submit (ups, "power", "ups", value);
242 else if (strcmp ("ups.temperature", key) == 0)
243 nut_submit (ups, "temperature", "ups", value);
245 } /* while (upscli_list_next) */
248 } /* int nut_read_one */
250 static int nut_read (void)
255 pthread_mutex_lock (&read_lock);
258 pthread_mutex_unlock (&read_lock);
263 for (ups = upslist_head; ups != NULL; ups = ups->next)
264 if (nut_read_one (ups) == 0)
267 pthread_mutex_lock (&read_lock);
269 pthread_mutex_unlock (&read_lock);
271 return ((success != 0) ? 0 : -1);
274 static int nut_shutdown (void)
283 free_nut_ups_t (this);
288 } /* int nut_shutdown */
290 void module_register (void)
292 plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
293 plugin_register_read ("nut", nut_read);
294 plugin_register_shutdown ("nut", nut_shutdown);
295 } /* void module_register */
297 /* vim: set sw=2 ts=8 sts=2 tw=78 : */