collectd.spec: the dpdk is actually called dpdkstat...
[collectd.git] / src / nut.c
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  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #include <upsclient.h>
33
34 #if HAVE_UPSCONN_T
35 typedef UPSCONN_t collectd_upsconn_t;
36 #elif HAVE_UPSCONN
37 typedef UPSCONN collectd_upsconn_t;
38 #else
39 # error "Unable to determine the UPS connection type."
40 #endif
41
42 struct nut_ups_s;
43 typedef struct nut_ups_s nut_ups_t;
44 struct nut_ups_s
45 {
46   collectd_upsconn_t *conn;
47   char      *upsname;
48   char      *hostname;
49   int        port;
50   nut_ups_t *next;
51 };
52
53 static nut_ups_t *upslist_head = NULL;
54
55 static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
56 static int read_busy = 0;
57
58 static const char *config_keys[] =
59 {
60   "UPS"
61 };
62 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
63
64 static void free_nut_ups_t (nut_ups_t *ups)
65 {
66   if (ups->conn != NULL)
67   {
68     upscli_disconnect (ups->conn);
69     sfree (ups->conn);
70   }
71   sfree (ups->hostname);
72   sfree (ups->upsname);
73   sfree (ups);
74 } /* void free_nut_ups_t */
75
76 static int nut_add_ups (const char *name)
77 {
78   nut_ups_t *ups;
79   int status;
80
81   DEBUG ("nut plugin: nut_add_ups (name = %s);", name);
82
83   ups = calloc (1, sizeof (*ups));
84   if (ups == NULL)
85   {
86     ERROR ("nut plugin: nut_add_ups: calloc failed.");
87     return (1);
88   }
89
90   status = upscli_splitname (name, &ups->upsname, &ups->hostname,
91       &ups->port);
92   if (status != 0)
93   {
94     ERROR ("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
95     free_nut_ups_t (ups);
96     return (1);
97   }
98
99   if (upslist_head == NULL)
100     upslist_head = ups;
101   else
102   {
103     nut_ups_t *last = upslist_head;
104     while (last->next != NULL)
105       last = last->next;
106     last->next = ups;
107   }
108
109   return (0);
110 } /* int nut_add_ups */
111
112 static int nut_config (const char *key, const char *value)
113 {
114   if (strcasecmp (key, "UPS") == 0)
115     return (nut_add_ups (value));
116   else
117     return (-1);
118 } /* int nut_config */
119
120 static void nut_submit (nut_ups_t *ups, const char *type,
121     const char *type_instance, gauge_t value)
122 {
123   value_list_t vl = VALUE_LIST_INIT;
124
125   vl.values = &(value_t) { .gauge = value };
126   vl.values_len = 1;
127   sstrncpy (vl.host,
128       (strcasecmp (ups->hostname, "localhost") == 0)
129       ? hostname_g
130       : ups->hostname,
131       sizeof (vl.host));
132   sstrncpy (vl.plugin, "nut", sizeof (vl.plugin));
133   sstrncpy (vl.plugin_instance, ups->upsname, sizeof (vl.plugin_instance));
134   sstrncpy (vl.type, type, sizeof (vl.type));
135   sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
136
137   plugin_dispatch_values (&vl);
138 } /* void nut_submit */
139
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;
147
148   /* (Re-)Connect if we have no connection */
149   if (ups->conn == NULL)
150   {
151     ups->conn = malloc (sizeof (*ups->conn));
152     if (ups->conn == NULL)
153     {
154       ERROR ("nut plugin: malloc failed.");
155       return (-1);
156     }
157
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     }
167
168     INFO ("nut plugin: Connection to (%s, %i) established.",
169         ups->hostname, ups->port);
170   } /* if (ups->conn == NULL) */
171
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   }
183
184   while ((status = upscli_list_next (ups->conn, query_num, query,
185           &answer_num, &answer)) == 1)
186   {
187     char  *key;
188     double value;
189
190     if (answer_num < 4)
191       continue;
192
193     key = answer[2];
194     value = atof (answer[3]);
195
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) */
242
243   return (0);
244 } /* int nut_read_one */
245
246 static int nut_read (void)
247 {
248   int success = 0;
249
250   pthread_mutex_lock (&read_lock);
251   success = read_busy;
252   read_busy = 1;
253   pthread_mutex_unlock (&read_lock);
254
255   if (success != 0)
256     return (0);
257
258   for (nut_ups_t *ups = upslist_head; ups != NULL; ups = ups->next)
259     if (nut_read_one (ups) == 0)
260       success++;
261
262   pthread_mutex_lock (&read_lock);
263   read_busy = 0;
264   pthread_mutex_unlock (&read_lock);
265
266   return ((success != 0) ? 0 : -1);
267 } /* int nut_read */
268
269 static int nut_shutdown (void)
270 {
271   nut_ups_t *this;
272   nut_ups_t *next;
273
274   this = upslist_head;
275   while (this != NULL)
276   {
277     next = this->next;
278     free_nut_ups_t (this);
279     this = next;
280   }
281
282   return (0);
283 } /* int nut_shutdown */
284
285 void module_register (void)
286 {
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 */
291
292 /* vim: set sw=2 ts=8 sts=2 tw=78 : */