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>
31 #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)
55 value_list_t vl = VALUE_LIST_INIT;
57 values[0].derive = rx;
58 values[1].derive = tx;
61 vl.values_len = STATIC_ARRAY_SIZE (values);
62 sstrncpy (vl.host, rd->node, sizeof (vl.host));
63 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
64 sstrncpy (vl.type, type, sizeof (vl.type));
65 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
67 plugin_dispatch_values (&vl);
68 } /* }}} void cr_submit_io */
70 static void submit_interface (cr_data_t *rd, /* {{{ */
71 const ros_interface_t *i)
78 submit_interface (rd, i->next);
82 cr_submit_io (rd, "if_packets", i->name,
83 (derive_t) i->rx_packets, (derive_t) i->tx_packets);
84 cr_submit_io (rd, "if_octets", i->name,
85 (derive_t) i->rx_bytes, (derive_t) i->tx_bytes);
86 cr_submit_io (rd, "if_errors", i->name,
87 (derive_t) i->rx_errors, (derive_t) i->tx_errors);
88 cr_submit_io (rd, "if_dropped", i->name,
89 (derive_t) i->rx_drops, (derive_t) i->tx_drops);
91 submit_interface (rd, i->next);
92 } /* }}} void submit_interface */
94 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
95 const ros_interface_t *i, void *user_data)
97 if ((i == NULL) || (user_data == NULL))
100 submit_interface (user_data, i);
102 } /* }}} int handle_interface */
104 static void cr_submit_gauge (cr_data_t *rd, const char *type, /* {{{ */
105 const char *type_instance, gauge_t value)
108 value_list_t vl = VALUE_LIST_INIT;
110 values[0].gauge = value;
113 vl.values_len = STATIC_ARRAY_SIZE (values);
114 sstrncpy (vl.host, rd->node, sizeof (vl.host));
115 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
116 sstrncpy (vl.type, type, sizeof (vl.type));
117 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
119 plugin_dispatch_values (&vl);
120 } /* }}} void cr_submit_gauge */
122 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
123 static void cr_submit_counter (cr_data_t *rd, const char *type, /* {{{ */
124 const char *type_instance, derive_t value)
127 value_list_t vl = VALUE_LIST_INIT;
129 values[0].derive = value;
132 vl.values_len = STATIC_ARRAY_SIZE (values);
133 sstrncpy (vl.host, rd->node, sizeof (vl.host));
134 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
135 sstrncpy (vl.type, type, sizeof (vl.type));
136 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
138 plugin_dispatch_values (&vl);
139 } /* }}} void cr_submit_gauge */
142 static void submit_regtable (cr_data_t *rd, /* {{{ */
143 const ros_registration_table_t *r)
145 char type_instance[DATA_MAX_NAME_LEN];
151 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
152 r->interface, r->radio_name);
153 cr_submit_gauge (rd, "bitrate", type_instance,
154 (gauge_t) (1000000.0 * r->rx_rate));
155 cr_submit_gauge (rd, "signal_power", type_instance,
156 (gauge_t) r->rx_signal_strength);
157 cr_submit_gauge (rd, "signal_quality", type_instance,
158 (gauge_t) r->rx_ccq);
161 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
162 r->interface, r->radio_name);
163 cr_submit_gauge (rd, "bitrate", type_instance,
164 (gauge_t) (1000000.0 * r->tx_rate));
165 cr_submit_gauge (rd, "signal_power", type_instance,
166 (gauge_t) r->tx_signal_strength);
167 cr_submit_gauge (rd, "signal_quality", type_instance,
168 (gauge_t) r->tx_ccq);
171 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
172 r->interface, r->radio_name);
173 cr_submit_io (rd, "if_octets", type_instance,
174 (derive_t) r->rx_bytes, (derive_t) r->tx_bytes);
175 cr_submit_gauge (rd, "snr", type_instance, (gauge_t) r->signal_to_noise);
177 submit_regtable (rd, r->next);
178 } /* }}} void submit_regtable */
180 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
181 const ros_registration_table_t *r, void *user_data)
183 if ((r == NULL) || (user_data == NULL))
186 submit_regtable (user_data, r);
188 } /* }}} int handle_regtable */
190 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
191 static int handle_system_resource (__attribute__((unused)) ros_connection_t *c, /* {{{ */
192 const ros_system_resource_t *r,
193 __attribute__((unused)) void *user_data)
197 if ((r == NULL) || (user_data == NULL))
201 if (rd->collect_cpu_load)
202 cr_submit_gauge (rd, "gauge", "cpu_load", (gauge_t) r->cpu_load);
204 if (rd->collect_memory)
206 cr_submit_gauge (rd, "memory", "used",
207 (gauge_t) (r->total_memory - r->free_memory));
208 cr_submit_gauge (rd, "memory", "free", (gauge_t) r->free_memory);
213 cr_submit_gauge (rd, "df_complex", "used",
214 (gauge_t) (r->total_memory - r->free_memory));
215 cr_submit_gauge (rd, "df_complex", "free", (gauge_t) r->free_memory);
218 if (rd->collect_disk)
220 cr_submit_counter (rd, "counter", "secors_written", (derive_t) r->write_sect_total);
221 cr_submit_gauge (rd, "gauge", "bad_blocks", (gauge_t) r->bad_blocks);
225 } /* }}} int handle_system_resource */
228 static int cr_read (user_data_t *user_data) /* {{{ */
233 if (user_data == NULL)
236 rd = user_data->data;
240 if (rd->connection == NULL)
242 rd->connection = ros_connect (rd->node, rd->service,
243 rd->username, rd->password);
244 if (rd->connection == NULL)
247 ERROR ("routeros plugin: ros_connect failed: %s",
248 sstrerror (errno, errbuf, sizeof (errbuf)));
252 assert (rd->connection != NULL);
254 if (rd->collect_interface)
256 status = ros_interface (rd->connection, handle_interface,
257 /* user data = */ rd);
261 ERROR ("routeros plugin: ros_interface failed: %s",
262 sstrerror (status, errbuf, sizeof (errbuf)));
263 ros_disconnect (rd->connection);
264 rd->connection = NULL;
269 if (rd->collect_regtable)
271 status = ros_registration_table (rd->connection, handle_regtable,
272 /* user data = */ rd);
276 ERROR ("routeros plugin: ros_registration_table failed: %s",
277 sstrerror (status, errbuf, sizeof (errbuf)));
278 ros_disconnect (rd->connection);
279 rd->connection = NULL;
284 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
285 if (rd->collect_cpu_load
286 || rd->collect_memory
290 status = ros_system_resource (rd->connection, handle_system_resource,
291 /* user data = */ rd);
295 ERROR ("routeros plugin: ros_system_resource failed: %s",
296 sstrerror (status, errbuf, sizeof (errbuf)));
297 ros_disconnect (rd->connection);
298 rd->connection = NULL;
305 } /* }}} int cr_read */
307 static void cr_free_data (cr_data_t *ptr) /* {{{ */
312 ros_disconnect (ptr->connection);
313 ptr->connection = NULL;
316 sfree (ptr->service);
317 sfree (ptr->username);
318 sfree (ptr->password);
321 } /* }}} void cr_free_data */
323 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
325 cr_data_t *router_data;
327 user_data_t user_data;
331 router_data = malloc (sizeof (*router_data));
332 if (router_data == NULL)
334 memset (router_data, 0, sizeof (router_data));
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 (i = 0; i < ci->children_num; i++)
344 oconfig_item_t *child = ci->children + i;
346 if (strcasecmp ("Host", child->key) == 0)
347 status = cf_util_get_string (child, &router_data->node);
348 else if (strcasecmp ("Port", child->key) == 0)
349 status = cf_util_get_service (child, &router_data->service);
350 else if (strcasecmp ("User", child->key) == 0)
351 status = cf_util_get_string (child, &router_data->username);
352 else if (strcasecmp ("Password", child->key) == 0)
353 status = cf_util_get_string (child, &router_data->password);
354 else if (strcasecmp ("CollectInterface", child->key) == 0)
355 cf_util_get_boolean (child, &router_data->collect_interface);
356 else if (strcasecmp ("CollectRegistrationTable", child->key) == 0)
357 cf_util_get_boolean (child, &router_data->collect_regtable);
358 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
359 else if (strcasecmp ("CollectCPULoad", child->key) == 0)
360 cf_util_get_boolean (child, &router_data->collect_cpu_load);
361 else if (strcasecmp ("CollectMemory", child->key) == 0)
362 cf_util_get_boolean (child, &router_data->collect_memory);
363 else if (strcasecmp ("CollectDF", child->key) == 0)
364 cf_util_get_boolean (child, &router_data->collect_df);
365 else if (strcasecmp ("CollectDisk", child->key) == 0)
366 cf_util_get_boolean (child, &router_data->collect_disk);
370 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
379 if (router_data->node == NULL)
381 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
382 "Where should I connect to?");
386 if (router_data->password == NULL)
388 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
389 "How should I authenticate?");
393 if (!router_data->collect_interface
394 && !router_data->collect_regtable)
396 ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
397 "What statistics should I collect?");
402 if ((status == 0) && (router_data->username == NULL))
404 router_data->username = sstrdup ("admin");
405 if (router_data->username == NULL)
407 ERROR ("routeros plugin: sstrdup failed.");
412 ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
413 user_data.data = router_data;
414 user_data.free_func = (void *) cr_free_data;
416 status = plugin_register_complex_read (/* group = */ NULL, read_name,
417 cr_read, /* interval = */ NULL, &user_data);
420 cr_free_data (router_data);
423 } /* }}} int cr_config_router */
425 static int cr_config (oconfig_item_t *ci)
429 for (i = 0; i < ci->children_num; i++)
431 oconfig_item_t *child = ci->children + i;
433 if (strcasecmp ("Router", child->key) == 0)
434 cr_config_router (child);
437 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
442 } /* }}} int cr_config */
444 void module_register (void)
446 plugin_register_complex_config ("routeros", cr_config);
447 } /* void module_register */
449 /* vim: set sw=2 noet fdm=marker : */