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