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", "secors_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 static int handle_system_health(__attribute__((unused))
218 ros_connection_t *c, /* {{{ */
219 const ros_system_health_t *r,
220 __attribute__((unused)) void *user_data) {
223 if ((r == NULL) || (user_data == NULL))
227 cr_submit_gauge(rd, "gauge", "voltage", (gauge_t)r->voltage);
228 cr_submit_gauge(rd, "gauge", "temperature", (gauge_t)r->temperature);
231 } /* }}} int handle_system_health */
234 static int cr_read(user_data_t *user_data) /* {{{ */
239 if (user_data == NULL)
242 rd = user_data->data;
246 if (rd->connection == NULL) {
248 ros_connect(rd->node, rd->service, rd->username, rd->password);
249 if (rd->connection == NULL) {
250 ERROR("routeros plugin: ros_connect failed: %s", STRERRNO);
254 assert(rd->connection != NULL);
256 if (rd->collect_interface) {
257 status = ros_interface(rd->connection, handle_interface,
258 /* user data = */ rd);
260 ERROR("routeros plugin: ros_interface failed: %s", STRERROR(status));
261 ros_disconnect(rd->connection);
262 rd->connection = NULL;
267 if (rd->collect_regtable) {
268 status = ros_registration_table(rd->connection, handle_regtable,
269 /* user data = */ rd);
271 ERROR("routeros plugin: ros_registration_table failed: %s",
273 ros_disconnect(rd->connection);
274 rd->connection = NULL;
279 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
280 if (rd->collect_cpu_load || rd->collect_memory || rd->collect_df ||
282 status = ros_system_resource(rd->connection, handle_system_resource,
283 /* user data = */ rd);
285 ERROR("routeros plugin: ros_system_resource failed: %s",
287 ros_disconnect(rd->connection);
288 rd->connection = NULL;
293 if (rd->collect_health) {
294 status = ros_system_health(rd->connection, handle_system_health,
295 /* user data = */ rd);
298 ERROR("routeros plugin: ros_system_health failed: %s",
299 sstrerror(status, errbuf, sizeof(errbuf)));
300 ros_disconnect(rd->connection);
301 rd->connection = NULL;
308 } /* }}} int cr_read */
310 static void cr_free_data(cr_data_t *ptr) /* {{{ */
315 ros_disconnect(ptr->connection);
316 ptr->connection = NULL;
320 sfree(ptr->username);
321 sfree(ptr->password);
324 } /* }}} void cr_free_data */
326 static int cr_config_router(oconfig_item_t *ci) /* {{{ */
328 cr_data_t *router_data;
332 router_data = calloc(1, sizeof(*router_data));
333 if (router_data == NULL)
335 router_data->connection = NULL;
336 router_data->node = NULL;
337 router_data->service = NULL;
338 router_data->username = NULL;
339 router_data->password = NULL;
342 for (int i = 0; i < ci->children_num; i++) {
343 oconfig_item_t *child = ci->children + i;
345 if (strcasecmp("Host", child->key) == 0)
346 status = cf_util_get_string(child, &router_data->node);
347 else if (strcasecmp("Port", child->key) == 0)
348 status = cf_util_get_service(child, &router_data->service);
349 else if (strcasecmp("User", child->key) == 0)
350 status = cf_util_get_string(child, &router_data->username);
351 else if (strcasecmp("Password", child->key) == 0)
352 status = cf_util_get_string(child, &router_data->password);
353 else if (strcasecmp("CollectInterface", child->key) == 0)
354 cf_util_get_boolean(child, &router_data->collect_interface);
355 else if (strcasecmp("CollectRegistrationTable", child->key) == 0)
356 cf_util_get_boolean(child, &router_data->collect_regtable);
357 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
358 else if (strcasecmp("CollectCPULoad", child->key) == 0)
359 cf_util_get_boolean(child, &router_data->collect_cpu_load);
360 else if (strcasecmp("CollectMemory", child->key) == 0)
361 cf_util_get_boolean(child, &router_data->collect_memory);
362 else if (strcasecmp("CollectDF", child->key) == 0)
363 cf_util_get_boolean(child, &router_data->collect_df);
364 else if (strcasecmp("CollectDisk", child->key) == 0)
365 cf_util_get_boolean(child, &router_data->collect_disk);
366 else if (strcasecmp("CollectHealth", child->key) == 0)
367 cf_util_get_boolean(child, &router_data->collect_health);
370 WARNING("routeros plugin: Unknown config option `%s'.", child->key);
378 if (router_data->node == NULL) {
379 ERROR("routeros plugin: No `Host' option within a `Router' block. "
380 "Where should I connect to?");
384 if (router_data->password == NULL) {
385 ERROR("routeros plugin: No `Password' option within a `Router' block. "
386 "How should I authenticate?");
390 if (!router_data->collect_interface && !router_data->collect_regtable) {
391 ERROR("routeros plugin: No `Collect*' option within a `Router' block. "
392 "What statistics should I collect?");
397 if ((status == 0) && (router_data->username == NULL)) {
398 router_data->username = sstrdup("admin");
399 if (router_data->username == NULL) {
400 ERROR("routeros plugin: sstrdup failed.");
406 cr_free_data(router_data);
410 snprintf(read_name, sizeof(read_name), "routeros/%s", router_data->node);
411 return plugin_register_complex_read(
412 /* group = */ NULL, read_name, cr_read, /* interval = */ 0,
414 .data = router_data, .free_func = (void *)cr_free_data,
416 } /* }}} int cr_config_router */
418 static int cr_config(oconfig_item_t *ci) {
419 for (int i = 0; i < ci->children_num; i++) {
420 oconfig_item_t *child = ci->children + i;
422 if (strcasecmp("Router", child->key) == 0)
423 cr_config_router(child);
425 WARNING("routeros plugin: Unknown config option `%s'.", child->key);
430 } /* }}} int cr_config */
432 void module_register(void) {
433 plugin_register_complex_config("routeros", cr_config);
434 } /* void module_register */