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