1 /**
2 * collectd - src/nut.c
3 * Copyright (C) 2007 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #include <upsclient.h>
33 #if HAVE_UPSCONN_T
34 typedef UPSCONN_t collectd_upsconn_t;
35 #elif HAVE_UPSCONN
36 typedef UPSCONN collectd_upsconn_t;
37 #else
38 # error "Unable to determine the UPS connection type."
39 #endif
41 struct nut_ups_s;
42 typedef struct nut_ups_s nut_ups_t;
43 struct nut_ups_s
44 {
45 collectd_upsconn_t *conn;
46 char *upsname;
47 char *hostname;
48 int port;
49 nut_ups_t *next;
50 };
52 static nut_ups_t *upslist_head = NULL;
54 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
55 static int read_busy = 0;
57 static const char *config_keys[] =
58 {
59 "UPS"
60 };
61 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
63 static void free_nut_ups_t (nut_ups_t *ups)
64 {
65 if (ups->conn != NULL)
66 {
67 upscli_disconnect (ups->conn);
68 sfree (ups->conn);
69 }
70 sfree (ups->hostname);
71 sfree (ups->upsname);
72 sfree (ups);
73 } /* void free_nut_ups_t */
75 static int nut_add_ups (const char *name)
76 {
77 nut_ups_t *ups;
78 int status;
80 DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
82 ups = calloc (1, sizeof (*ups));
83 if (ups == NULL)
84 {
85 ERROR ("nut plugin: nut_add_ups: calloc failed.");
86 return (1);
87 }
89 status = upscli_splitname (name, &ups->upsname, &ups->hostname,
90 &ups->port);
91 if (status != 0)
92 {
93 ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
94 free_nut_ups_t (ups);
95 return (1);
96 }
98 if (upslist_head == NULL)
99 upslist_head = ups;
100 else
101 {
102 nut_ups_t *last = upslist_head;
103 while (last->next != NULL)
104 last = last->next;
105 last->next = ups;
106 }
108 return (0);
109 } /* int nut_add_ups */
111 static int nut_config (const char *key, const char *value)
112 {
113 if (strcasecmp (key, "UPS") == 0)
114 return (nut_add_ups (value));
115 else
116 return (-1);
117 } /* int nut_config */
119 static void nut_submit (nut_ups_t *ups, const char *type,
120 const char *type_instance, gauge_t value)
121 {
122 value_t values[1];
123 value_list_t vl = VALUE_LIST_INIT;
125 values[0].gauge = value;
127 vl.values = values;
128 vl.values_len = STATIC_ARRAY_SIZE (values);
129 sstrncpy (vl.host,
130 (strcasecmp (ups->hostname, "localhost") == 0)
131 ? hostname_g
132 : ups->hostname,
133 sizeof (vl.host));
134 sstrncpy (vl.plugin, "nut", sizeof (vl.plugin));
135 sstrncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
136 sstrncpy (vl.type, type, sizeof (vl.type));
137 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
139 plugin_dispatch_values (&vl);
140 } /* void nut_submit */
142 static int nut_read_one (nut_ups_t *ups)
143 {
144 const char *query[3] = {"VAR", ups->upsname, NULL};
145 unsigned int query_num = 2;
146 char **answer;
147 unsigned int answer_num;
148 int status;
150 /* (Re-)Connect if we have no connection */
151 if (ups->conn == NULL)
152 {
153 ups->conn = malloc (sizeof (*ups->conn));
154 if (ups->conn == NULL)
155 {
156 ERROR ("nut plugin: malloc failed.");
157 return (-1);
158 }
160 status = upscli_connect (ups->conn, ups->hostname, ups->port,
161 UPSCLI_CONN_TRYSSL);
162 if (status != 0)
163 {
164 ERROR ("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
165 ups->hostname, ups->port, upscli_strerror (ups->conn));
166 sfree (ups->conn);
167 return (-1);
168 }
170 INFO ("nut plugin: Connection to (%s, %i) established.",
171 ups->hostname, ups->port);
172 } /* if (ups->conn == NULL) */
174 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
175 * error */
176 status = upscli_list_start (ups->conn, query_num, query);
177 if (status != 0)
178 {
179 ERROR ("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
180 ups->upsname, upscli_strerror (ups->conn));
181 upscli_disconnect (ups->conn);
182 sfree (ups->conn);
183 return (-1);
184 }
186 while ((status = upscli_list_next (ups->conn, query_num, query,
187 &answer_num, &answer)) == 1)
188 {
189 char *key;
190 double value;
192 if (answer_num < 4)
193 continue;
195 key = answer[2];
196 value = atof (answer[3]);
198 if (strncmp ("ambient.", key, 8) == 0)
199 {
200 if (strcmp ("ambient.humidity", key) == 0)
201 nut_submit (ups, "humidity", "ambient", value);
202 else if (strcmp ("ambient.temperature", key) == 0)
203 nut_submit (ups, "temperature", "ambient", value);
204 }
205 else if (strncmp ("battery.", key, 8) == 0)
206 {
207 if (strcmp ("battery.charge", key) == 0)
208 nut_submit (ups, "percent", "charge", value);
209 else if (strcmp ("battery.current", key) == 0)
210 nut_submit (ups, "current", "battery", value);
211 else if (strcmp ("battery.runtime", key) == 0)
212 nut_submit (ups, "timeleft", "battery", value);
213 else if (strcmp ("battery.temperature", key) == 0)
214 nut_submit (ups, "temperature", "battery", value);
215 else if (strcmp ("battery.voltage", key) == 0)
216 nut_submit (ups, "voltage", "battery", value);
217 }
218 else if (strncmp ("input.", key, 6) == 0)
219 {
220 if (strcmp ("input.frequency", key) == 0)
221 nut_submit (ups, "frequency", "input", value);
222 else if (strcmp ("input.voltage", key) == 0)
223 nut_submit (ups, "voltage", "input", value);
224 }
225 else if (strncmp ("output.", key, 7) == 0)
226 {
227 if (strcmp ("output.current", key) == 0)
228 nut_submit (ups, "current", "output", value);
229 else if (strcmp ("output.frequency", key) == 0)
230 nut_submit (ups, "frequency", "output", value);
231 else if (strcmp ("output.voltage", key) == 0)
232 nut_submit (ups, "voltage", "output", value);
233 }
234 else if (strncmp ("ups.", key, 4) == 0)
235 {
236 if (strcmp ("ups.load", key) == 0)
237 nut_submit (ups, "percent", "load", value);
238 else if (strcmp ("ups.power", key) == 0)
239 nut_submit (ups, "power", "ups", value);
240 else if (strcmp ("ups.temperature", key) == 0)
241 nut_submit (ups, "temperature", "ups", value);
242 }
243 } /* while (upscli_list_next) */
245 return (0);
246 } /* int nut_read_one */
248 static int nut_read (void)
249 {
250 nut_ups_t *ups;
251 int success = 0;
253 pthread_mutex_lock (&read_lock);
254 success = read_busy;
255 read_busy = 1;
256 pthread_mutex_unlock (&read_lock);
258 if (success != 0)
259 return (0);
261 for (ups = upslist_head; ups != NULL; ups = ups->next)
262 if (nut_read_one (ups) == 0)
263 success++;
265 pthread_mutex_lock (&read_lock);
266 read_busy = 0;
267 pthread_mutex_unlock (&read_lock);
269 return ((success != 0) ? 0 : -1);
270 } /* int nut_read */
272 static int nut_shutdown (void)
273 {
274 nut_ups_t *this;
275 nut_ups_t *next;
277 this = upslist_head;
278 while (this != NULL)
279 {
280 next = this->next;
281 free_nut_ups_t (this);
282 this = next;
283 }
285 return (0);
286 } /* int nut_shutdown */
288 void module_register (void)
289 {
290 plugin_register_config ("nut", nut_config, config_keys, config_keys_num);
291 plugin_register_read ("nut", nut_read);
292 plugin_register_shutdown ("nut", nut_shutdown);
293 } /* void module_register */
295 /* vim: set sw=2 ts=8 sts=2 tw=78 : */