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 #include <pthread.h>
27 #include <upsclient.h>
29 #if HAVE_UPSCONN_T
30 typedef UPSCONN_t collectd_upsconn_t;
31 #elif HAVE_UPSCONN
32 typedef UPSCONN collectd_upsconn_t;
33 #else
34 # error "Unable to determine the UPS connection type."
35 #endif
37 struct nut_ups_s;
38 typedef struct nut_ups_s nut_ups_t;
39 struct nut_ups_s
40 {
41 collectd_upsconn_t *conn;
42 char *upsname;
43 char *hostname;
44 int port;
45 nut_ups_t *next;
46 };
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[] =
54 {
55 "UPS"
56 };
57 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
59 static void free_nut_ups_t (nut_ups_t *ups)
60 {
61 if (ups->conn != NULL)
62 {
63 upscli_disconnect (ups->conn);
64 sfree (ups->conn);
65 }
66 sfree (ups->hostname);
67 sfree (ups->upsname);
68 sfree (ups);
69 } /* void free_nut_ups_t */
71 static int nut_add_ups (const char *name)
72 {
73 nut_ups_t *ups;
74 int status;
76 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
78 ups = (nut_ups_t *) malloc (sizeof (nut_ups_t));
79 if (ups == NULL)
80 {
81 ERROR ("nut plugin: nut_add_ups: malloc failed.");
82 return (1);
83 }
84 memset (ups, '\0', sizeof (nut_ups_t));
86 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
87 &ups->port);
88 if (status != 0)
89 {
90 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
91 free_nut_ups_t (ups);
92 return (1);
93 }
95 if (upslist_head == NULL)
96 upslist_head = ups;
97 else
98 {
99 nut_ups_t *last = upslist_head;
100 while (last->next != NULL)
101 last = last->next;
102 last->next = ups;
103 }
105 return (0);
106 } /* int nut_add_ups */
108 static int nut_config (const char *key, const char *value)
109 {
110 if (strcasecmp (key, "UPS") == 0)
111 return (nut_add_ups (value));
112 else
113 return (-1);
114 } /* int nut_config */
116 static void nut_submit (nut_ups_t *ups, const char *type,
117 const char *type_instance, gauge_t value)
118 {
119 value_t values[1];
120 value_list_t vl = VALUE_LIST_INIT;
122 values[0].gauge = value;
124 vl.values = values;
125 vl.values_len = STATIC_ARRAY_SIZE (values);
126 vl.time = time (NULL);
127 strncpy (vl.host,
128 (strcasecmp (ups->hostname, "localhost") == 0)
129 ? hostname_g
130 : ups->hostname,
131 sizeof (vl.host));
132 strcpy (vl.plugin, "nut");
133 strncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
134 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
136 vl.host[sizeof (vl.host) - 1] = '\0';
137 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
138 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
140 plugin_dispatch_values (type, &vl);
141 } /* void nut_submit */
143 static int nut_read_one (nut_ups_t *ups)
144 {
145 const char *query[3] = {"VAR", ups->upsname, NULL};
146 unsigned int query_num = 2;
147 char **answer;
148 unsigned int answer_num;
149 int status;
151 /* (Re-)Connect if we have no connection */
152 if (ups->conn == NULL)
153 {
154 ups->conn = (collectd_upsconn_t *) malloc (sizeof (collectd_upsconn_t));
155 if (ups->conn == NULL)
156 {
157 ERROR ("nut plugin: malloc failed.");
158 return (-1);
159 }
161 status = upscli_connect (ups->conn, ups->hostname, ups->port,
162 UPSCLI_CONN_TRYSSL);
163 if (status != 0)
164 {
165 ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
166 ups->hostname, ups->port, upscli_strerror (ups->conn));
167 sfree (ups->conn);
168 return (-1);
169 }
171 INFO ("nut plugin: Connection to (%s, %i) established.",
172 ups->hostname, ups->port);
173 } /* if (ups->conn == NULL) */
175 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
176 * error */
177 status = upscli_list_start (ups->conn, query_num, query);
178 if (status != 0)
179 {
180 ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
181 ups->upsname, upscli_strerror (ups->conn));
182 upscli_disconnect (ups->conn);
183 sfree (ups->conn);
184 return (-1);
185 }
187 while ((status = upscli_list_next (ups->conn, query_num, query,
188 &answer_num, &answer)) == 1)
189 {
190 char *key;
191 double value;
193 if (answer_num < 4)
194 continue;
196 key = answer[2];
197 value = atof (answer[3]);
199 if (strncmp ("ambient.", key, 8) == 0)
200 {
201 if (strcmp ("ambient.humidity", key) == 0)
202 nut_submit (ups, "humidity", "ambient", value);
203 else if (strcmp ("ambient.temperature", key) == 0)
204 nut_submit (ups, "temperature", "ambient", value);
205 }
206 else if (strncmp ("battery.", key, 8) == 0)
207 {
208 if (strcmp ("battery.charge", key) == 0)
209 nut_submit (ups, "percent", "charge", value);
210 else if (strcmp ("battery.current", key) == 0)
211 nut_submit (ups, "current", "battery", value);
212 else if (strcmp ("battery.runtime", key) == 0)
213 nut_submit (ups, "timeleft", "battery", value);
214 else if (strcmp ("battery.temperature", key) == 0)
215 nut_submit (ups, "temperature", "battery", value);
216 else if (strcmp ("battery.voltage", key) == 0)
217 nut_submit (ups, "voltage", "battery", value);
218 }
219 else if (strncmp ("input.", key, 6) == 0)
220 {
221 if (strcmp ("input.frequency", key) == 0)
222 nut_submit (ups, "frequency", "input", value);
223 else if (strcmp ("input.voltage", key) == 0)
224 nut_submit (ups, "voltage", "input", value);
225 }
226 else if (strncmp ("output.", key, 7) == 0)
227 {
228 if (strcmp ("output.current", key) == 0)
229 nut_submit (ups, "current", "output", value);
230 else if (strcmp ("output.frequency", key) == 0)
231 nut_submit (ups, "frequency", "output", value);
232 else if (strcmp ("output.voltage", key) == 0)
233 nut_submit (ups, "voltage", "output", value);
234 }
235 else if (strncmp ("ups.", key, 4) == 0)
236 {
237 if (strcmp ("ups.load", key) == 0)
238 nut_submit (ups, "percent", "load", value);
239 else if (strcmp ("ups.power", key) == 0)
240 nut_submit (ups, "power", "ups", value);
241 else if (strcmp ("ups.temperature", key) == 0)
242 nut_submit (ups, "temperature", "ups", value);
243 }
244 } /* while (upscli_list_next) */
246 return (0);
247 } /* int nut_read_one */
249 static int nut_read (void)
250 {
251 nut_ups_t *ups;
252 int success = 0;
254 pthread_mutex_lock (&read_lock);
255 success = read_busy;
256 read_busy = 1;
257 pthread_mutex_unlock (&read_lock);
259 if (success != 0)
260 return (0);
262 for (ups = upslist_head; ups != NULL; ups = ups->next)
263 if (nut_read_one (ups) == 0)
264 success++;
266 pthread_mutex_lock (&read_lock);
267 read_busy = 0;
268 pthread_mutex_unlock (&read_lock);
270 return ((success != 0) ? 0 : -1);
271 } /* int nut_read */
273 static int nut_shutdown (void)
274 {
275 nut_ups_t *this;
276 nut_ups_t *next;
278 this = upslist_head;
279 while (this != NULL)
280 {
281 next = this->next;
282 free_nut_ups_t (this);
283 this = next;
284 }
286 return (0);
287 } /* int nut_shutdown */
289 void module_register (void)
290 {
291 plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
292 plugin_register_read ("nut", nut_read);
293 plugin_register_shutdown ("nut", nut_shutdown);
294 } /* void module_register */
296 /* vim: set sw=2 ts=8 sts=2 tw=78 : */