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;
49 typedef struct cr_data_s cr_data_t;
51 static void cr_submit_io(cr_data_t *rd, const char *type, /* {{{ */
52 const char *type_instance, derive_t rx, derive_t tx) {
53 value_list_t vl = VALUE_LIST_INIT;
55 {.derive = rx}, {.derive = tx},
59 vl.values_len = STATIC_ARRAY_SIZE(values);
60 sstrncpy(vl.host, rd->node, sizeof(vl.host));
61 sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
62 sstrncpy(vl.type, type, sizeof(vl.type));
63 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
65 plugin_dispatch_values(&vl);
66 } /* }}} void cr_submit_io */
68 static void submit_interface(cr_data_t *rd, /* {{{ */
69 const ros_interface_t *i) {
74 submit_interface(rd, i->next);
78 cr_submit_io(rd, "if_packets", i->name, (derive_t)i->rx_packets,
79 (derive_t)i->tx_packets);
80 cr_submit_io(rd, "if_octets", i->name, (derive_t)i->rx_bytes,
81 (derive_t)i->tx_bytes);
82 cr_submit_io(rd, "if_errors", i->name, (derive_t)i->rx_errors,
83 (derive_t)i->tx_errors);
84 cr_submit_io(rd, "if_dropped", i->name, (derive_t)i->rx_drops,
85 (derive_t)i->tx_drops);
87 submit_interface(rd, i->next);
88 } /* }}} void submit_interface */
90 static int handle_interface(__attribute__((unused))
91 ros_connection_t *c, /* {{{ */
92 const ros_interface_t *i, void *user_data) {
93 if ((i == NULL) || (user_data == NULL))
96 submit_interface(user_data, i);
98 } /* }}} int handle_interface */
100 static void cr_submit_gauge(cr_data_t *rd, const char *type, /* {{{ */
101 const char *type_instance, gauge_t value) {
103 value_list_t vl = VALUE_LIST_INIT;
105 values[0].gauge = value;
108 vl.values_len = STATIC_ARRAY_SIZE(values);
109 sstrncpy(vl.host, rd->node, sizeof(vl.host));
110 sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
111 sstrncpy(vl.type, type, sizeof(vl.type));
112 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
114 plugin_dispatch_values(&vl);
115 } /* }}} void cr_submit_gauge */
117 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
118 static void cr_submit_counter(cr_data_t *rd, const char *type, /* {{{ */
119 const char *type_instance, derive_t value) {
121 value_list_t vl = VALUE_LIST_INIT;
123 values[0].derive = value;
126 vl.values_len = STATIC_ARRAY_SIZE(values);
127 sstrncpy(vl.host, rd->node, sizeof(vl.host));
128 sstrncpy(vl.plugin, "routeros", sizeof(vl.plugin));
129 sstrncpy(vl.type, type, sizeof(vl.type));
130 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
132 plugin_dispatch_values(&vl);
133 } /* }}} void cr_submit_gauge */
136 static void submit_regtable(cr_data_t *rd, /* {{{ */
137 const ros_registration_table_t *r) {
138 char type_instance[DATA_MAX_NAME_LEN];
144 snprintf(type_instance, sizeof(type_instance), "%s-%s-rx", r->interface,
146 cr_submit_gauge(rd, "bitrate", type_instance,
147 (gauge_t)(1000000.0 * r->rx_rate));
148 cr_submit_gauge(rd, "signal_power", type_instance,
149 (gauge_t)r->rx_signal_strength);
150 cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->rx_ccq);
153 snprintf(type_instance, sizeof(type_instance), "%s-%s-tx", r->interface,
155 cr_submit_gauge(rd, "bitrate", type_instance,
156 (gauge_t)(1000000.0 * r->tx_rate));
157 cr_submit_gauge(rd, "signal_power", type_instance,
158 (gauge_t)r->tx_signal_strength);
159 cr_submit_gauge(rd, "signal_quality", type_instance, (gauge_t)r->tx_ccq);
162 snprintf(type_instance, sizeof(type_instance), "%s-%s", r->interface,
164 cr_submit_io(rd, "if_octets", type_instance, (derive_t)r->rx_bytes,
165 (derive_t)r->tx_bytes);
166 cr_submit_gauge(rd, "snr", type_instance, (gauge_t)r->signal_to_noise);
168 submit_regtable(rd, r->next);
169 } /* }}} void submit_regtable */
171 static int handle_regtable(__attribute__((unused))
172 ros_connection_t *c, /* {{{ */
173 const ros_registration_table_t *r, void *user_data) {
174 if ((r == NULL) || (user_data == NULL))
177 submit_regtable(user_data, r);
179 } /* }}} int handle_regtable */
181 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
182 static int handle_system_resource(__attribute__((unused))
183 ros_connection_t *c, /* {{{ */
184 const ros_system_resource_t *r,
185 __attribute__((unused)) void *user_data) {
188 if ((r == NULL) || (user_data == NULL))
192 if (rd->collect_cpu_load)
193 cr_submit_gauge(rd, "gauge", "cpu_load", (gauge_t)r->cpu_load);
195 if (rd->collect_memory) {
196 cr_submit_gauge(rd, "memory", "used",
197 (gauge_t)(r->total_memory - r->free_memory));
198 cr_submit_gauge(rd, "memory", "free", (gauge_t)r->free_memory);
201 if (rd->collect_df) {
202 cr_submit_gauge(rd, "df_complex", "used",
203 (gauge_t)(r->total_memory - r->free_memory));
204 cr_submit_gauge(rd, "df_complex", "free", (gauge_t)r->free_memory);
207 if (rd->collect_disk) {
208 cr_submit_counter(rd, "counter", "secors_written",
209 (derive_t)r->write_sect_total);
210 cr_submit_gauge(rd, "gauge", "bad_blocks", (gauge_t)r->bad_blocks);
214 } /* }}} int handle_system_resource */
217 static int cr_read(user_data_t *user_data) /* {{{ */
222 if (user_data == NULL)
225 rd = user_data->data;
229 if (rd->connection == NULL) {
231 ros_connect(rd->node, rd->service, rd->username, rd->password);
232 if (rd->connection == NULL) {
234 ERROR("routeros plugin: ros_connect failed: %s",
235 sstrerror(errno, errbuf, sizeof(errbuf)));
239 assert(rd->connection != NULL);
241 if (rd->collect_interface) {
242 status = ros_interface(rd->connection, handle_interface,
243 /* user data = */ rd);
246 ERROR("routeros plugin: ros_interface failed: %s",
247 sstrerror(status, errbuf, sizeof(errbuf)));
248 ros_disconnect(rd->connection);
249 rd->connection = NULL;
254 if (rd->collect_regtable) {
255 status = ros_registration_table(rd->connection, handle_regtable,
256 /* user data = */ rd);
259 ERROR("routeros plugin: ros_registration_table failed: %s",
260 sstrerror(status, errbuf, sizeof(errbuf)));
261 ros_disconnect(rd->connection);
262 rd->connection = NULL;
267 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
268 if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
270 status = ros_system_resource(rd->connection, handle_system_resource,
271 /* user data = */ rd);
274 ERROR("routeros plugin: ros_system_resource failed: %s",
275 sstrerror(status, errbuf, sizeof(errbuf)));
276 ros_disconnect(rd->connection);
277 rd->connection = NULL;
284 } /* }}} int cr_read */
286 static void cr_free_data(cr_data_t *ptr) /* {{{ */
291 ros_disconnect(ptr->connection);
292 ptr->connection = NULL;
296 sfree(ptr->username);
297 sfree(ptr->password);
300 } /* }}} void cr_free_data */
302 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
304 cr_data_t *router_data;
308 router_data = calloc(1, sizeof(*router_data));
309 if (router_data == NULL)
311 router_data->connection = NULL;
312 router_data->node = NULL;
313 router_data->service = NULL;
314 router_data->username = NULL;
315 router_data->password = NULL;
318 for (int i = 0; i < ci->children_num; i++) {
319 oconfig_item_t *child = ci->children + i;
321 if (strcasecmp("Host", child->key) == 0)
322 status = cf_util_get_string(child, &router_data->node);
323 else if (strcasecmp("Port", child->key) == 0)
324 status = cf_util_get_service(child, &router_data->service);
325 else if (strcasecmp("User", child->key) == 0)
326 status = cf_util_get_string(child, &router_data->username);
327 else if (strcasecmp("Password", child->key) == 0)
328 status = cf_util_get_string(child, &router_data->password);
329 else if (strcasecmp("CollectInterface", child->key) == 0)
330 cf_util_get_boolean(child, &router_data->collect_interface);
331 else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
332 cf_util_get_boolean(child, &router_data->collect_regtable);
333 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
334 else if (strcasecmp("CollectCPULoad", child->key) == 0)
335 cf_util_get_boolean(child, &router_data->collect_cpu_load);
336 else if (strcasecmp("CollectMemory", child->key) == 0)
337 cf_util_get_boolean(child, &router_data->collect_memory);
338 else if (strcasecmp("CollectDF", child->key) == 0)
339 cf_util_get_boolean(child, &router_data->collect_df);
340 else if (strcasecmp("CollectDisk", child->key) == 0)
341 cf_util_get_boolean(child, &router_data->collect_disk);
344 WARNING("routeros plugin: Unknown config option `%s'.", child->key);
352 if (router_data->node == NULL) {
353 ERROR("routeros plugin: No `Host' option within a `Router' block. "
354 "Where should I connect to?");
358 if (router_data->password == NULL) {
359 ERROR("routeros plugin: No `Password' option within a `Router' block. "
360 "How should I authenticate?");
364 if (!router_data->collect_interface && !router_data->collect_regtable) {
365 ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
366 "What statistics should I collect?");
371 if ((status == 0) && (router_data->username == NULL)) {
372 router_data->username = sstrdup("admin");
373 if (router_data->username == NULL) {
374 ERROR("routeros plugin: sstrdup failed.");
379 snprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
381 status = plugin_register_complex_read(
382 /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
384 .data = router_data, .free_func = (void *)cr_free_data,
388 cr_free_data(router_data);
391 } /* }}} int cr_config_router */
393 static int cr_config(oconfig_item_t *ci) {
394 for (int i = 0; i < ci->children_num; i++) {
395 oconfig_item_t *child = ci->children + i;
397 if (strcasecmp("Router", child->key) == 0)
398 cr_config_router(child);
400 WARNING("routeros plugin: Unknown config option `%s'.", child->key);
405 } /* }}} int cr_config */
407 void module_register(void) {
408 plugin_register_complex_config("routeros", cr_config);
409 } /* void module_register */