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 #if NUT_HAVE_READ
38 struct nut_ups_s;
39 typedef struct nut_ups_s nut_ups_t;
40 struct nut_ups_s
41 {
42 UPSCONN *conn;
43 char *upsname;
44 char *hostname;
45 int port;
46 nut_ups_t *next;
47 };
49 static nut_ups_t *upslist_head = NULL;
51 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
52 static int read_busy = 0;
54 static const char *config_keys[] =
55 {
56 "UPS"
57 };
58 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
60 static void free_nut_ups_t (nut_ups_t *ups)
61 {
62 if (ups->conn != NULL)
63 {
64 upscli_disconnect (ups->conn);
65 sfree (ups->conn);
66 }
67 sfree (ups->hostname);
68 sfree (ups->upsname);
69 sfree (ups);
70 } /* void free_nut_ups_t */
72 static int nut_add_ups (const char *name)
73 {
74 nut_ups_t *ups;
75 int status;
77 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
79 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
80 if (ups == NULL)
81 {
82 ERROR ("nut plugin: nut_add_ups: malloc failed.");
83 return (1);
84 }
85 memset (ups, '\0', sizeof (nut_ups_t));
87 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
88 &ups->port);
89 if (status != 0)
90 {
91 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
92 free_nut_ups_t (ups);
93 return (1);
94 }
96 if (upslist_head == NULL)
97 upslist_head = ups;
98 else
99 {
100 nut_ups_t *last = upslist_head;
101 while (last->next != NULL)
102 last = last->next;
103 last->next = ups;
104 }
106 return (0);
107 } /* int nut_add_ups */
109 static int nut_config (const char *key, const char *value)
110 {
111 if (strcasecmp (key, "UPS") == 0)
112 return (nut_add_ups (value));
113 else
114 return (-1);
115 } /* int nut_config */
117 static void nut_submit (nut_ups_t *ups, const char *type,
118 const char *type_instance, gauge_t value)
119 {
120 value_t values[1];
121 value_list_t vl = VALUE_LIST_INIT;
123 values[0].gauge = value;
125 vl.values = values;
126 vl.values_len = STATIC_ARRAY_SIZE (values);
127 vl.time = time (NULL);
128 strncpy (vl.host,
129 (strcasecmp (ups->hostname, "localhost") == 0)
130 ? hostname_g
131 : ups->hostname,
132 sizeof (vl.host));
133 strcpy (vl.plugin, "nut");
134 strncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
135 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
137 vl.host[sizeof (vl.host) - 1] = '\0';
138 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
139 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
141 plugin_dispatch_values (type, &vl);
142 } /* void nut_submit */
144 static int nut_read_one (nut_ups_t *ups)
145 {
146 const char *query[3] = {"VAR", ups->upsname, NULL};
147 unsigned int query_num = 2;
148 char **answer;
149 unsigned int answer_num;
150 int status;
152 /* (Re-)Connect if we have no connection */
153 if (ups->conn == NULL)
154 {
155 ups->conn = (UPSCONN *) malloc (sizeof (UPSCONN));
156 if (ups->conn == NULL)
157 {
158 ERROR ("nut plugin: malloc failed.");
159 return (-1);
160 }
162 status = upscli_connect (ups->conn, ups->hostname, ups->port,
163 UPSCLI_CONN_TRYSSL);
164 if (status != 0)
165 {
166 ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
167 ups->hostname, ups->port, upscli_strerror (ups->conn));
168 sfree (ups->conn);
169 return (-1);
170 }
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
177 * error */
178 status = upscli_list_start (ups->conn, query_num, query);
179 if (status != 0)
180 {
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);
184 sfree (ups->conn);
185 return (-1);
186 }
188 while ((status = upscli_list_next (ups->conn, query_num, query,
189 &answer_num, &answer)) == 1)
190 {
191 char *key;
192 double value;
194 if (answer_num < 4)
195 continue;
197 key = answer[2];
198 value = atof (answer[3]);
200 if (strncmp ("ambient.", key, 8) == 0)
201 {
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);
206 }
207 else if (strncmp ("battery.", key, 8) == 0)
208 {
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);
219 }
220 else if (strncmp ("input.", key, 6) == 0)
221 {
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);
226 }
227 else if (strncmp ("output.", key, 7) == 0)
228 {
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);
235 }
236 else if (strncmp ("ups.", key, 4) == 0)
237 {
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);
244 }
245 } /* while (upscli_list_next) */
247 return (0);
248 } /* int nut_read_one */
250 static int nut_read (void)
251 {
252 nut_ups_t *ups;
253 int success = 0;
255 pthread_mutex_lock (&read_lock);
256 success = read_busy;
257 read_busy = 1;
258 pthread_mutex_unlock (&read_lock);
260 if (success != 0)
261 return (0);
263 for (ups = upslist_head; ups != NULL; ups = ups->next)
264 if (nut_read_one (ups) == 0)
265 success++;
267 pthread_mutex_lock (&read_lock);
268 read_busy = 0;
269 pthread_mutex_unlock (&read_lock);
271 return ((success != 0) ? 0 : -1);
272 } /* int nut_read */
274 static int nut_shutdown (void)
275 {
276 nut_ups_t *this;
277 nut_ups_t *next;
279 this = upslist_head;
280 while (this != NULL)
281 {
282 next = this->next;
283 free_nut_ups_t (this);
284 this = next;
285 }
287 return (0);
288 } /* int nut_shutdown */
289 #endif /* NUT_HAVE_READ */
291 void module_register (void)
292 {
293 #if NUT_HAVE_READ
294 plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
295 plugin_register_read ("nut", nut_read);
296 plugin_register_shutdown ("nut", nut_shutdown);
297 #endif
298 } /* void module_register */
300 /* vim: set sw=2 ts=8 sts=2 tw=78 : */