3 * Copyright (C) 2007 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
32 #include <upsclient.h>
35 typedef UPSCONN_t collectd_upsconn_t;
37 typedef UPSCONN collectd_upsconn_t;
39 #error "Unable to determine the UPS connection type."
43 typedef struct nut_ups_s nut_ups_t;
45 collectd_upsconn_t *conn;
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[] = {"UPS", "FORCESSL"};
58 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
59 static int force_ssl = 0; // Initialized to default of 0 (false)
61 static void free_nut_ups_t(nut_ups_t *ups) {
62 if (ups->conn != NULL) {
63 upscli_disconnect(ups->conn);
69 } /* void free_nut_ups_t */
71 static int nut_add_ups(const char *name) {
75 DEBUG("nut plugin: nut_add_ups (name = %s);", name);
77 ups = calloc(1, sizeof(*ups));
79 ERROR("nut plugin: nut_add_ups: calloc failed.");
83 status = upscli_splitname(name, &ups->upsname, &ups->hostname, &ups->port);
85 ERROR("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
90 if (upslist_head == NULL)
93 nut_ups_t *last = upslist_head;
94 while (last->next != NULL)
100 } /* int nut_add_ups */
102 static int nut_force_ssl(const char *value) {
103 if (strcasecmp(value, "true") == 0)
105 else if (strcasecmp(value, "false") == 0)
106 force_ssl = 0; // Should already be set to 0 from initialization
109 WARNING("nut plugin: nut_force_ssl: invalid FORCESSL value "
110 "found. Defaulting to false.");
113 } /* int nut_parse_force_ssl */
115 static int nut_config(const char *key, const char *value) {
116 if (strcasecmp(key, "UPS") == 0)
117 return (nut_add_ups(value));
118 else if (strcasecmp(key, "FORCESSL") == 0)
119 return (nut_force_ssl(value));
122 } /* int nut_config */
124 static void nut_submit(nut_ups_t *ups, const char *type,
125 const char *type_instance, gauge_t value) {
126 value_list_t vl = VALUE_LIST_INIT;
128 vl.values = &(value_t){.gauge = value};
131 (strcasecmp(ups->hostname, "localhost") == 0) ? hostname_g
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 const char *query[3] = {"VAR", ups->upsname, NULL};
144 unsigned int query_num = 2;
146 unsigned int answer_num;
151 /* (Re-)Connect if we have no connection */
152 if (ups->conn == NULL) {
153 ups->conn = malloc(sizeof(*ups->conn));
154 if (ups->conn == NULL) {
155 ERROR("nut plugin: malloc failed.");
160 ssl_flags = UPSCLI_CONN_REQSSL;
162 ssl_flags = UPSCLI_CONN_TRYSSL;
164 status = upscli_connect(ups->conn, ups->hostname, ups->port, ssl_flags);
167 ERROR("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
168 ups->hostname, ups->port, upscli_strerror(ups->conn));
171 } /* if (status != 0) */
173 INFO("nut plugin: Connection to (%s, %i) established.", ups->hostname,
176 // Output INFO or WARNING based on SSL and VERIFICATION
177 ssl_status = upscli_ssl(ups->conn); // 1 for SSL, 0 for not, -1 for error
178 if (ssl_status == 1){
179 INFO("nut plugin: Connection is secured with SSL.");
181 else if (ssl_status == 0){
182 WARNING("nut plugin: Connection is unsecured (no SSL).");
184 ERROR("nut plugin: nut_read_one: upscli_ssl failed: %s",
185 upscli_strerror(ups->conn));
188 } /* if (ssl_status == 1 && verify_peer == 1) */
190 } /* if (ups->conn == NULL) */
192 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
194 status = upscli_list_start(ups->conn, query_num, query);
196 ERROR("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
197 ups->upsname, upscli_strerror(ups->conn));
198 upscli_disconnect(ups->conn);
203 while ((status = upscli_list_next(ups->conn, query_num, query, &answer_num,
212 value = atof(answer[3]);
214 if (strncmp("ambient.", key, 8) == 0) {
215 if (strcmp("ambient.humidity", key) == 0)
216 nut_submit(ups, "humidity", "ambient", value);
217 else if (strcmp("ambient.temperature", key) == 0)
218 nut_submit(ups, "temperature", "ambient", value);
219 } else if (strncmp("battery.", key, 8) == 0) {
220 if (strcmp("battery.charge", key) == 0)
221 nut_submit(ups, "percent", "charge", value);
222 else if (strcmp("battery.current", key) == 0)
223 nut_submit(ups, "current", "battery", value);
224 else if (strcmp("battery.runtime", key) == 0)
225 nut_submit(ups, "timeleft", "battery", value);
226 else if (strcmp("battery.temperature", key) == 0)
227 nut_submit(ups, "temperature", "battery", value);
228 else if (strcmp("battery.voltage", key) == 0)
229 nut_submit(ups, "voltage", "battery", value);
230 } else if (strncmp("input.", key, 6) == 0) {
231 if (strcmp("input.frequency", key) == 0)
232 nut_submit(ups, "frequency", "input", value);
233 else if (strcmp("input.voltage", key) == 0)
234 nut_submit(ups, "voltage", "input", value);
235 } else if (strncmp("output.", key, 7) == 0) {
236 if (strcmp("output.current", key) == 0)
237 nut_submit(ups, "current", "output", value);
238 else if (strcmp("output.frequency", key) == 0)
239 nut_submit(ups, "frequency", "output", value);
240 else if (strcmp("output.voltage", key) == 0)
241 nut_submit(ups, "voltage", "output", value);
242 } else if (strncmp("ups.", key, 4) == 0) {
243 if (strcmp("ups.load", key) == 0)
244 nut_submit(ups, "percent", "load", value);
245 else if (strcmp("ups.power", key) == 0)
246 nut_submit(ups, "power", "ups", value);
247 else if (strcmp("ups.temperature", key) == 0)
248 nut_submit(ups, "temperature", "ups", value);
250 } /* while (upscli_list_next) */
253 } /* int nut_read_one */
255 static int nut_read(void) {
258 pthread_mutex_lock(&read_lock);
261 pthread_mutex_unlock(&read_lock);
266 for (nut_ups_t *ups = upslist_head; ups != NULL; ups = ups->next)
267 if (nut_read_one(ups) == 0)
270 pthread_mutex_lock(&read_lock);
272 pthread_mutex_unlock(&read_lock);
274 return ((success != 0) ? 0 : -1);
277 static int nut_shutdown(void) {
282 while (this != NULL) {
284 free_nut_ups_t(this);
289 } /* int nut_shutdown */
291 void module_register(void) {
292 plugin_register_config("nut", nut_config, config_keys, config_keys_num);
293 plugin_register_read("nut", nut_read);
294 plugin_register_shutdown("nut", nut_shutdown);
295 } /* void module_register */