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"};
58 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
60 static void free_nut_ups_t(nut_ups_t *ups) {
61 if (ups->conn != NULL) {
62 upscli_disconnect(ups->conn);
68 } /* void free_nut_ups_t */
70 static int nut_add_ups(const char *name) {
74 DEBUG("nut plugin: nut_add_ups (name = %s);", name);
76 ups = calloc(1, sizeof(*ups));
78 ERROR("nut plugin: nut_add_ups: calloc failed.");
82 status = upscli_splitname(name, &ups->upsname, &ups->hostname, &ups->port);
84 ERROR("nut plugin: nut_add_ups: upscli_splitname (%s) failed.", name);
89 if (upslist_head == NULL)
92 nut_ups_t *last = upslist_head;
93 while (last->next != NULL)
99 } /* int nut_add_ups */
101 static int nut_config(const char *key, const char *value) {
102 if (strcasecmp(key, "UPS") == 0)
103 return (nut_add_ups(value));
106 } /* int nut_config */
108 static void nut_submit(nut_ups_t *ups, const char *type,
109 const char *type_instance, gauge_t value) {
110 value_list_t vl = VALUE_LIST_INIT;
112 vl.values = &(value_t){.gauge = value};
115 (strcasecmp(ups->hostname, "localhost") == 0) ? hostname_g
118 sstrncpy(vl.plugin, "nut", sizeof(vl.plugin));
119 sstrncpy(vl.plugin_instance, ups->upsname, sizeof(vl.plugin_instance));
120 sstrncpy(vl.type, type, sizeof(vl.type));
121 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
123 plugin_dispatch_values(&vl);
124 } /* void nut_submit */
126 static int nut_read_one(nut_ups_t *ups) {
127 const char *query[3] = {"VAR", ups->upsname, NULL};
128 unsigned int query_num = 2;
130 unsigned int answer_num;
133 /* (Re-)Connect if we have no connection */
134 if (ups->conn == NULL) {
135 ups->conn = malloc(sizeof(*ups->conn));
136 if (ups->conn == NULL) {
137 ERROR("nut plugin: malloc failed.");
142 upscli_connect(ups->conn, ups->hostname, ups->port, UPSCLI_CONN_TRYSSL);
144 ERROR("nut plugin: nut_read_one: upscli_connect (%s, %i) failed: %s",
145 ups->hostname, ups->port, upscli_strerror(ups->conn));
150 INFO("nut plugin: Connection to (%s, %i) established.", ups->hostname,
152 } /* if (ups->conn == NULL) */
154 /* nut plugin: nut_read_one: upscli_list_start (adpos) failed: Protocol
156 status = upscli_list_start(ups->conn, query_num, query);
158 ERROR("nut plugin: nut_read_one: upscli_list_start (%s) failed: %s",
159 ups->upsname, upscli_strerror(ups->conn));
160 upscli_disconnect(ups->conn);
165 while ((status = upscli_list_next(ups->conn, query_num, query, &answer_num,
174 value = atof(answer[3]);
176 if (strncmp("ambient.", key, 8) == 0) {
177 if (strcmp("ambient.humidity", key) == 0)
178 nut_submit(ups, "humidity", "ambient", value);
179 else if (strcmp("ambient.temperature", key) == 0)
180 nut_submit(ups, "temperature", "ambient", value);
181 } else if (strncmp("battery.", key, 8) == 0) {
182 if (strcmp("battery.charge", key) == 0)
183 nut_submit(ups, "percent", "charge", value);
184 else if (strcmp("battery.current", key) == 0)
185 nut_submit(ups, "current", "battery", value);
186 else if (strcmp("battery.runtime", key) == 0)
187 nut_submit(ups, "timeleft", "battery", value);
188 else if (strcmp("battery.temperature", key) == 0)
189 nut_submit(ups, "temperature", "battery", value);
190 else if (strcmp("battery.voltage", key) == 0)
191 nut_submit(ups, "voltage", "battery", value);
192 } else if (strncmp("input.", key, 6) == 0) {
193 if (strcmp("input.frequency", key) == 0)
194 nut_submit(ups, "frequency", "input", value);
195 else if (strcmp("input.voltage", key) == 0)
196 nut_submit(ups, "voltage", "input", value);
197 } else if (strncmp("output.", key, 7) == 0) {
198 if (strcmp("output.current", key) == 0)
199 nut_submit(ups, "current", "output", value);
200 else if (strcmp("output.frequency", key) == 0)
201 nut_submit(ups, "frequency", "output", value);
202 else if (strcmp("output.voltage", key) == 0)
203 nut_submit(ups, "voltage", "output", value);
204 } else if (strncmp("ups.", key, 4) == 0) {
205 if (strcmp("ups.load", key) == 0)
206 nut_submit(ups, "percent", "load", value);
207 else if (strcmp("ups.power", key) == 0)
208 nut_submit(ups, "power", "ups", value);
209 else if (strcmp("ups.temperature", key) == 0)
210 nut_submit(ups, "temperature", "ups", value);
212 } /* while (upscli_list_next) */
215 } /* int nut_read_one */
217 static int nut_read(void) {
220 pthread_mutex_lock(&read_lock);
223 pthread_mutex_unlock(&read_lock);
228 for (nut_ups_t *ups = upslist_head; ups != NULL; ups = ups->next)
229 if (nut_read_one(ups) == 0)
232 pthread_mutex_lock(&read_lock);
234 pthread_mutex_unlock(&read_lock);
236 return ((success != 0) ? 0 : -1);
239 static int nut_shutdown(void) {
244 while (this != NULL) {
246 free_nut_ups_t(this);
251 } /* int nut_shutdown */
253 void module_register(void) {
254 plugin_register_config("nut", nut_config, config_keys, config_keys_num);
255 plugin_register_read("nut", nut_read);
256 plugin_register_shutdown("nut", nut_shutdown);
257 } /* void module_register */