2 * collectd - src/routeros.c
3 * Copyright (C) 2009,2010 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 <routeros_api.h>
35 ros_connection_t *connection;
42 bool collect_interface;
43 bool collect_regtable;
44 bool collect_cpu_load;
50 typedef struct cr_data_s cr_data_t;
52 static void cr_submit_io(cr_data_t *rd, const char *type, /* {{{ */
53 const char *type_instance, derive_t rx, derive_t tx) {
54 value_list_t vl = VALUE_LIST_INIT;
56 {.derive = rx}, {.derive = tx},
60 vl.values_len = STATIC_ARRAY_SIZE(values);
61 sstrncpy(vl.host, rd->node, sizeof(vl.host));
62 sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
63 sstrncpy(vl.type, type, sizeof(vl.type));
64 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
66 plugin_dispatch_values(&vl);
67 } /* }}} void cr_submit_io */
69 static void submit_interface(cr_data_t *rd, /* {{{ */
70 const ros_interface_t *i) {
75 submit_interface(rd, i->next);
79 cr_submit_io(rd, "if_packets", i->name, (derive_t)i->rx_packets,
80 (derive_t)i->tx_packets);
81 cr_submit_io(rd, "if_octets", i->name, (derive_t)i->rx_bytes,
82 (derive_t)i->tx_bytes);
83 cr_submit_io(rd, "if_errors", i->name, (derive_t)i->rx_errors,
84 (derive_t)i->tx_errors);
85 cr_submit_io(rd, "if_dropped", i->name, (derive_t)i->rx_drops,
86 (derive_t)i->tx_drops);
88 submit_interface(rd, i->next);
89 } /* }}} void submit_interface */
91 static int handle_interface(__attribute__((unused))
92 ros_connection_t *c, /* {{{ */
93 const ros_interface_t *i, void *user_data) {
94 if ((i == NULL) || (user_data == NULL))
97 submit_interface(user_data, i);
99 } /* }}} int handle_interface */
101 static void cr_submit_gauge(cr_data_t *rd, const char *type, /* {{{ */
102 const char *type_instance, gauge_t value) {
104 value_list_t vl = VALUE_LIST_INIT;
106 values[0].gauge = value;
109 vl.values_len = STATIC_ARRAY_SIZE(values);
110 sstrncpy(vl.host, rd->node, sizeof(vl.host));
111 sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
112 sstrncpy(vl.type, type, sizeof(vl.type));
113 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
115 plugin_dispatch_values(&vl);
116 } /* }}} void cr_submit_gauge */
118 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
119 static void cr_submit_counter(cr_data_t *rd, const char *type, /* {{{ */
120 const char *type_instance, derive_t value) {
122 value_list_t vl = VALUE_LIST_INIT;
124 values[0].derive = value;
127 vl.values_len = STATIC_ARRAY_SIZE(values);
128 sstrncpy(vl.host, rd->node, sizeof(vl.host));
129 sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
130 sstrncpy(vl.type, type, sizeof(vl.type));
131 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
133 plugin_dispatch_values(&vl);
134 } /* }}} void cr_submit_gauge */
137 static void submit_regtable(cr_data_t *rd, /* {{{ */
138 const ros_registration_table_t *r) {
139 char type_instance[DATA_MAX_NAME_LEN];
145 snprintf(type_instance, sizeof(type_instance), "%s-%s-rx", r->interface,
146 r->radio_name ? r->radio_name : "default");
147 cr_submit_gauge(rd, "bitrate", type_instance,
148 (gauge_t)(1000000.0 * r->rx_rate));
149 cr_submit_gauge(rd, "signal_power", type_instance,
150 (gauge_t)r->rx_signal_strength);
151 cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->rx_ccq);
154 snprintf(type_instance, sizeof(type_instance), "%s-%s-tx", r->interface,
155 r->radio_name ? r->radio_name : "default");
156 cr_submit_gauge(rd, "bitrate", type_instance,
157 (gauge_t)(1000000.0 * r->tx_rate));
158 cr_submit_gauge(rd, "signal_power", type_instance,
159 (gauge_t)r->tx_signal_strength);
160 cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->tx_ccq);
163 snprintf(type_instance, sizeof(type_instance), "%s-%s", r->interface,
164 r->radio_name ? r->radio_name : "default");
165 cr_submit_io(rd, "if_octets", type_instance, (derive_t)r->rx_bytes,
166 (derive_t)r->tx_bytes);
167 cr_submit_gauge(rd, "snr", type_instance, (gauge_t)r->signal_to_noise);
169 submit_regtable(rd, r->next);
170 } /* }}} void submit_regtable */
172 static int handle_regtable(__attribute__((unused))
173 ros_connection_t *c, /* {{{ */
174 const ros_registration_table_t *r, void *user_data) {
175 if ((r == NULL) || (user_data == NULL))
178 submit_regtable(user_data, r);
180 } /* }}} int handle_regtable */
182 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
183 static int handle_system_resource(__attribute__((unused))
184 ros_connection_t *c, /* {{{ */
185 const ros_system_resource_t *r,
186 __attribute__((unused)) void *user_data) {
189 if ((r == NULL) || (user_data == NULL))
193 if (rd->collect_cpu_load)
194 cr_submit_gauge(rd, "gauge", "cpu_load", (gauge_t)r->cpu_load);
196 if (rd->collect_memory) {
197 cr_submit_gauge(rd, "memory", "used",
198 (gauge_t)(r->total_memory - r->free_memory));
199 cr_submit_gauge(rd, "memory", "free", (gauge_t)r->free_memory);
202 if (rd->collect_df) {
203 cr_submit_gauge(rd, "df_complex", "used",
204 (gauge_t)(r->total_memory - r->free_memory));
205 cr_submit_gauge(rd, "df_complex", "free", (gauge_t)r->free_memory);
208 if (rd->collect_disk) {
209 cr_submit_counter(rd, "counter", "sectors_written",
210 (derive_t)r->write_sect_total);
211 cr_submit_gauge(rd, "gauge", "bad_blocks", (gauge_t)r->bad_blocks);
215 } /* }}} int handle_system_resource */
217 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
218 static int handle_system_health(__attribute__((unused))
219 ros_connection_t *c, /* {{{ */
220 const ros_system_health_t *r,
221 __attribute__((unused)) void *user_data) {
224 if ((r == NULL) || (user_data == NULL))
228 cr_submit_gauge(rd, "voltage", "system", (gauge_t)r->voltage);
229 cr_submit_gauge(rd, "temperature", "system", (gauge_t)r->temperature);
232 } /* }}} int handle_system_health */
236 static int cr_read(user_data_t *user_data) /* {{{ */
241 if (user_data == NULL)
244 rd = user_data->data;
248 if (rd->connection == NULL) {
250 ros_connect(rd->node, rd->service, rd->username, rd->password);
251 if (rd->connection == NULL) {
252 ERROR("routeros plugin: ros_connect failed: %s", STRERRNO);
256 assert(rd->connection != NULL);
258 if (rd->collect_interface) {
259 status = ros_interface(rd->connection, handle_interface,
260 /* user data = */ rd);
262 ERROR("routeros plugin: ros_interface failed: %s", STRERROR(status));
263 ros_disconnect(rd->connection);
264 rd->connection = NULL;
269 if (rd->collect_regtable) {
270 status = ros_registration_table(rd->connection, handle_regtable,
271 /* user data = */ rd);
273 ERROR("routeros plugin: ros_registration_table failed: %s",
275 ros_disconnect(rd->connection);
276 rd->connection = NULL;
281 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
282 if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
284 status = ros_system_resource(rd->connection, handle_system_resource,
285 /* user data = */ rd);
287 ERROR("routeros plugin: ros_system_resource failed: %s",
289 ros_disconnect(rd->connection);
290 rd->connection = NULL;
295 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
296 if (rd->collect_health) {
297 status = ros_system_health(rd->connection, handle_system_health,
298 /* user data = */ rd);
301 ERROR("routeros plugin: ros_system_health failed: %s",
302 sstrerror(status, errbuf, sizeof(errbuf)));
303 ros_disconnect(rd->connection);
304 rd->connection = NULL;
312 } /* }}} int cr_read */
314 static void cr_free_data(cr_data_t *ptr) /* {{{ */
319 ros_disconnect(ptr->connection);
320 ptr->connection = NULL;
324 sfree(ptr->username);
325 sfree(ptr->password);
328 } /* }}} void cr_free_data */
330 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
332 cr_data_t *router_data;
336 router_data = calloc(1, sizeof(*router_data));
337 if (router_data == NULL)
339 router_data->connection = NULL;
340 router_data->node = NULL;
341 router_data->service = NULL;
342 router_data->username = NULL;
343 router_data->password = NULL;
346 for (int i = 0; i < ci->children_num; i++) {
347 oconfig_item_t *child = ci->children + i;
349 if (strcasecmp("Host", child->key) == 0)
350 status = cf_util_get_string(child, &router_data->node);
351 else if (strcasecmp("Port", child->key) == 0)
352 status = cf_util_get_service(child, &router_data->service);
353 else if (strcasecmp("User", child->key) == 0)
354 status = cf_util_get_string(child, &router_data->username);
355 else if (strcasecmp("Password", child->key) == 0)
356 status = cf_util_get_string(child, &router_data->password);
357 else if (strcasecmp("CollectInterface", child->key) == 0)
358 cf_util_get_boolean(child, &router_data->collect_interface);
359 else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
360 cf_util_get_boolean(child, &router_data->collect_regtable);
361 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
362 else if (strcasecmp("CollectCPULoad", child->key) == 0)
363 cf_util_get_boolean(child, &router_data->collect_cpu_load);
364 else if (strcasecmp("CollectMemory", child->key) == 0)
365 cf_util_get_boolean(child, &router_data->collect_memory);
366 else if (strcasecmp("CollectDF", child->key) == 0)
367 cf_util_get_boolean(child, &router_data->collect_df);
368 else if (strcasecmp("CollectDisk", child->key) == 0)
369 cf_util_get_boolean(child, &router_data->collect_disk);
370 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
371 else if (strcasecmp("CollectHealth", child->key) == 0)
372 cf_util_get_boolean(child, &router_data->collect_health);
376 WARNING("routeros plugin: Unknown config option `%s'.", child->key);
384 if (router_data->node == NULL) {
385 ERROR("routeros plugin: No `Host' option within a `Router' block. "
386 "Where should I connect to?");
390 if (router_data->password == NULL) {
391 ERROR("routeros plugin: No `Password' option within a `Router' block. "
392 "How should I authenticate?");
397 if (router_data->collect_interface)
399 if (router_data->collect_regtable)
401 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
402 if (router_data->collect_cpu_load)
404 if (router_data->collect_memory)
406 if (router_data->collect_df)
408 if (router_data->collect_disk)
410 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 3)
411 if (router_data->collect_health)
417 ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
418 "What statistics should I collect?");
423 if ((status == 0) && (router_data->username == NULL)) {
424 router_data->username = sstrdup("admin");
425 if (router_data->username == NULL) {
426 ERROR("routeros plugin: sstrdup failed.");
432 cr_free_data(router_data);
436 snprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
437 return plugin_register_complex_read(
438 /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
440 .data = router_data, .free_func = (void *)cr_free_data,
442 } /* }}} int cr_config_router */
444 static int cr_config(oconfig_item_t *ci) {
445 for (int i = 0; i < ci->children_num; i++) {
446 oconfig_item_t *child = ci->children + i;
448 if (strcasecmp("Router", child->key) == 0)
449 cr_config_router(child);
451 WARNING("routeros plugin: Unknown config option `%s'.", child->key);
456 } /* }}} int cr_config */
458 void module_register(void) {
459 plugin_register_complex_config("routeros", cr_config);
460 } /* void module_register */