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>
27 #include <upsclient.h>
30 typedef UPSCONN_t collectd_upsconn_t;
32 typedef UPSCONN collectd_upsconn_t;
34 # error "Unable to determine the UPS connection type."
38 typedef struct nut_ups_s nut_ups_t;
41 collectd_upsconn_t *conn;
48 static nut_ups_t *upslist_head = NULL;
50 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
51 static int read_busy = 0;
53 static const char *config_keys[] =
57 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
59 static void free_nut_ups_t (nut_ups_t *ups)
61 if (ups->conn != NULL)
63 upscli_disconnect (ups->conn);
66 sfree (ups->hostname);
69 } /* void free_nut_ups_t */
71 static int nut_add_ups (const char *name)
76 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
78 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
81 ERROR ("nut plugin: nut_add_ups: malloc failed.");
84 memset (ups, '\0', sizeof (nut_ups_t));
86 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
90 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
95 if (upslist_head == NULL)
99 nut_ups_t *last = upslist_head;
100 while (last->next != NULL)
106 } /* int nut_add_ups */
108 static int nut_config (const char *key, const char *value)
110 if (strcasecmp (key, "UPS") == 0)
111 return (nut_add_ups (value));
114 } /* int nut_config */
116 static void nut_submit (nut_ups_t *ups, const char *type,
117 const char *type_instance, gauge_t value)
120 value_list_t vl = VALUE_LIST_INIT;
122 values[0].gauge = value;
125 vl.values_len = STATIC_ARRAY_SIZE (values);
127 (strcasecmp (ups->hostname, "localhost") == 0)
131 sstrncpy (vl.plugin, "nut", sizeof (vl.plugin));
132 sstrncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
133 sstrncpy (vl.type, type, sizeof (vl.type));
134 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
136 plugin_dispatch_values (&vl);
137 } /* void nut_submit */
139 static int nut_read_one (nut_ups_t *ups)
141 const char *query[3] = {"VAR", ups->upsname, NULL};
142 unsigned int query_num = 2;
144 unsigned int answer_num;
147 /* (Re-)Connect if we have no connection */
148 if (ups->conn == NULL)
150 ups->conn = (collectd_upsconn_t *) malloc (sizeof (collectd_upsconn_t));
151 if (ups->conn == NULL)
153 ERROR ("nut plugin: malloc failed.");
157 status = upscli_connect (ups->conn, ups->hostname, ups->port,
161 ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
162 ups->hostname, ups->port, upscli_strerror (ups->conn));
167 INFO ("nut plugin: Connection to (%s, %i) established.",
168 ups->hostname, ups->port);
169 } /* if (ups->conn == NULL) */
171 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
173 status = upscli_list_start (ups->conn, query_num, query);
176 ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
177 ups->upsname, upscli_strerror (ups->conn));
178 upscli_disconnect (ups->conn);
183 while ((status = upscli_list_next (ups->conn, query_num, query,
184 &answer_num, &answer)) == 1)
193 value = atof (answer[3]);
195 if (strncmp ("ambient.", key, 8) == 0)
197 if (strcmp ("ambient.humidity", key) == 0)
198 nut_submit (ups, "humidity", "ambient", value);
199 else if (strcmp ("ambient.temperature", key) == 0)
200 nut_submit (ups, "temperature", "ambient", value);
202 else if (strncmp ("battery.", key, 8) == 0)
204 if (strcmp ("battery.charge", key) == 0)
205 nut_submit (ups, "percent", "charge", value);
206 else if (strcmp ("battery.current", key) == 0)
207 nut_submit (ups, "current", "battery", value);
208 else if (strcmp ("battery.runtime", key) == 0)
209 nut_submit (ups, "timeleft", "battery", value);
210 else if (strcmp ("battery.temperature", key) == 0)
211 nut_submit (ups, "temperature", "battery", value);
212 else if (strcmp ("battery.voltage", key) == 0)
213 nut_submit (ups, "voltage", "battery", value);
215 else if (strncmp ("input.", key, 6) == 0)
217 if (strcmp ("input.frequency", key) == 0)
218 nut_submit (ups, "frequency", "input", value);
219 else if (strcmp ("input.voltage", key) == 0)
220 nut_submit (ups, "voltage", "input", value);
222 else if (strncmp ("output.", key, 7) == 0)
224 if (strcmp ("output.current", key) == 0)
225 nut_submit (ups, "current", "output", value);
226 else if (strcmp ("output.frequency", key) == 0)
227 nut_submit (ups, "frequency", "output", value);
228 else if (strcmp ("output.voltage", key) == 0)
229 nut_submit (ups, "voltage", "output", value);
231 else if (strncmp ("ups.", key, 4) == 0)
233 if (strcmp ("ups.load", key) == 0)
234 nut_submit (ups, "percent", "load", value);
235 else if (strcmp ("ups.power", key) == 0)
236 nut_submit (ups, "power", "ups", value);
237 else if (strcmp ("ups.temperature", key) == 0)
238 nut_submit (ups, "temperature", "ups", value);
240 } /* while (upscli_list_next) */
243 } /* int nut_read_one */
245 static int nut_read (void)
250 pthread_mutex_lock (&read_lock);
253 pthread_mutex_unlock (&read_lock);
258 for (ups = upslist_head; ups != NULL; ups = ups->next)
259 if (nut_read_one (ups) == 0)
262 pthread_mutex_lock (&read_lock);
264 pthread_mutex_unlock (&read_lock);
266 return ((success != 0) ? 0 : -1);
269 static int nut_shutdown (void)
278 free_nut_ups_t (this);
283 } /* int nut_shutdown */
285 void module_register (void)
287 plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
288 plugin_register_read ("nut", nut_read);
289 plugin_register_shutdown ("nut", nut_shutdown);
290 } /* void module_register */
292 /* vim: set sw=2 ts=8 sts=2 tw=78 : */