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>
36 ros_connection_t *connection;
43 _Bool collect_interface;
44 _Bool collect_regtable;
45 _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)
56 value_list_t vl = VALUE_LIST_INIT;
58 values[0].derive = rx;
59 values[1].derive = tx;
62 vl.values_len = STATIC_ARRAY_SIZE (values);
63 sstrncpy (vl.host, rd->node, sizeof (vl.host));
64 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
65 sstrncpy (vl.type, type, sizeof (vl.type));
66 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
68 plugin_dispatch_values (&vl);
69 } /* }}} void cr_submit_io */
71 static void submit_interface (cr_data_t *rd, /* {{{ */
72 const ros_interface_t *i)
79 submit_interface (rd, i->next);
83 cr_submit_io (rd, "if_packets", i->name,
84 (derive_t) i->rx_packets, (derive_t) i->tx_packets);
85 cr_submit_io (rd, "if_octets", i->name,
86 (derive_t) i->rx_bytes, (derive_t) i->tx_bytes);
87 cr_submit_io (rd, "if_errors", i->name,
88 (derive_t) i->rx_errors, (derive_t) i->tx_errors);
89 cr_submit_io (rd, "if_dropped", i->name,
90 (derive_t) i->rx_drops, (derive_t) i->tx_drops);
92 submit_interface (rd, i->next);
93 } /* }}} void submit_interface */
95 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
96 const ros_interface_t *i, void *user_data)
98 if ((i == NULL) || (user_data == NULL))
101 submit_interface (user_data, i);
103 } /* }}} int handle_interface */
105 static void cr_submit_gauge (cr_data_t *rd, const char *type, /* {{{ */
106 const char *type_instance, gauge_t value)
109 value_list_t vl = VALUE_LIST_INIT;
111 values[0].gauge = value;
114 vl.values_len = STATIC_ARRAY_SIZE (values);
115 sstrncpy (vl.host, rd->node, sizeof (vl.host));
116 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
117 sstrncpy (vl.type, type, sizeof (vl.type));
118 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
120 plugin_dispatch_values (&vl);
121 } /* }}} void cr_submit_gauge */
123 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
124 static void cr_submit_counter (cr_data_t *rd, const char *type, /* {{{ */
125 const char *type_instance, derive_t value)
128 value_list_t vl = VALUE_LIST_INIT;
130 values[0].derive = value;
133 vl.values_len = STATIC_ARRAY_SIZE (values);
134 sstrncpy (vl.host, rd->node, sizeof (vl.host));
135 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
136 sstrncpy (vl.type, type, sizeof (vl.type));
137 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
139 plugin_dispatch_values (&vl);
140 } /* }}} void cr_submit_gauge */
143 static void submit_regtable (cr_data_t *rd, /* {{{ */
144 const ros_registration_table_t *r)
146 char type_instance[DATA_MAX_NAME_LEN];
152 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
153 r->interface, r->radio_name);
154 cr_submit_gauge (rd, "bitrate", type_instance,
155 (gauge_t) (1000000.0 * r->rx_rate));
156 cr_submit_gauge (rd, "signal_power", type_instance,
157 (gauge_t) r->rx_signal_strength);
158 cr_submit_gauge (rd, "signal_quality", type_instance,
159 (gauge_t) r->rx_ccq);
162 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
163 r->interface, r->radio_name);
164 cr_submit_gauge (rd, "bitrate", type_instance,
165 (gauge_t) (1000000.0 * r->tx_rate));
166 cr_submit_gauge (rd, "signal_power", type_instance,
167 (gauge_t) r->tx_signal_strength);
168 cr_submit_gauge (rd, "signal_quality", type_instance,
169 (gauge_t) r->tx_ccq);
172 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
173 r->interface, r->radio_name);
174 cr_submit_io (rd, "if_octets", type_instance,
175 (derive_t) r->rx_bytes, (derive_t) r->tx_bytes);
176 cr_submit_gauge (rd, "snr", type_instance, (gauge_t) r->signal_to_noise);
178 submit_regtable (rd, r->next);
179 } /* }}} void submit_regtable */
181 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
182 const ros_registration_table_t *r, void *user_data)
184 if ((r == NULL) || (user_data == NULL))
187 submit_regtable (user_data, r);
189 } /* }}} int handle_regtable */
191 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
192 static int handle_system_resource (__attribute__((unused)) ros_connection_t *c, /* {{{ */
193 const ros_system_resource_t *r,
194 __attribute__((unused)) void *user_data)
198 if ((r == NULL) || (user_data == NULL))
202 if (rd->collect_cpu_load)
203 cr_submit_gauge (rd, "gauge", "cpu_load", (gauge_t) r->cpu_load);
205 if (rd->collect_memory)
207 cr_submit_gauge (rd, "memory", "used",
208 (gauge_t) (r->total_memory - r->free_memory));
209 cr_submit_gauge (rd, "memory", "free", (gauge_t) r->free_memory);
214 cr_submit_gauge (rd, "df_complex", "used",
215 (gauge_t) (r->total_memory - r->free_memory));
216 cr_submit_gauge (rd, "df_complex", "free", (gauge_t) r->free_memory);
219 if (rd->collect_disk)
221 cr_submit_counter (rd, "counter", "secors_written", (derive_t) r->write_sect_total);
222 cr_submit_gauge (rd, "gauge", "bad_blocks", (gauge_t) r->bad_blocks);
226 } /* }}} int handle_system_resource */
229 static int cr_read (user_data_t *user_data) /* {{{ */
234 if (user_data == NULL)
237 rd = user_data->data;
241 if (rd->connection == NULL)
243 rd->connection = ros_connect (rd->node, rd->service,
244 rd->username, rd->password);
245 if (rd->connection == NULL)
248 ERROR ("routeros plugin: ros_connect failed: %s",
249 sstrerror (errno, errbuf, sizeof (errbuf)));
253 assert (rd->connection != NULL);
255 if (rd->collect_interface)
257 status = ros_interface (rd->connection, handle_interface,
258 /* user data = */ rd);
262 ERROR ("routeros plugin: ros_interface failed: %s",
263 sstrerror (status, errbuf, sizeof (errbuf)));
264 ros_disconnect (rd->connection);
265 rd->connection = NULL;
270 if (rd->collect_regtable)
272 status = ros_registration_table (rd->connection, handle_regtable,
273 /* user data = */ rd);
277 ERROR ("routeros plugin: ros_registration_table failed: %s",
278 sstrerror (status, errbuf, sizeof (errbuf)));
279 ros_disconnect (rd->connection);
280 rd->connection = NULL;
285 #if ROS_VERSION >= ROS_VERSION_ENCODE(1, 1, 0)
286 if (rd->collect_cpu_load
287 || rd->collect_memory
291 status = ros_system_resource (rd->connection, handle_system_resource,
292 /* user data = */ rd);
296 ERROR ("routeros plugin: ros_system_resource failed: %s",
297 sstrerror (status, errbuf, sizeof (errbuf)));
298 ros_disconnect (rd->connection);
299 rd->connection = NULL;
306 } /* }}} int cr_read */
308 static void cr_free_data (cr_data_t *ptr) /* {{{ */
313 ros_disconnect (ptr->connection);
314 ptr->connection = NULL;
317 sfree (ptr->service);
318 sfree (ptr->username);
319 sfree (ptr->password);
322 } /* }}} void cr_free_data */
324 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
326 cr_data_t *router_data;
328 user_data_t user_data;
331 router_data = calloc (1, sizeof (*router_data));
332 if (router_data == NULL)
334 router_data->connection = NULL;
335 router_data->node = NULL;
336 router_data->service = NULL;
337 router_data->username = NULL;
338 router_data->password = NULL;
341 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);
369 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
378 if (router_data->node == NULL)
380 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
381 "Where should I connect to?");
385 if (router_data->password == NULL)
387 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
388 "How should I authenticate?");
392 if (!router_data->collect_interface
393 && !router_data->collect_regtable)
395 ERROR ("routeros plugin: No `Collect*' option within a `Router' block. "
396 "What statistics should I collect?");
401 if ((status == 0) && (router_data->username == NULL))
403 router_data->username = sstrdup ("admin");
404 if (router_data->username == NULL)
406 ERROR ("routeros plugin: sstrdup failed.");
411 ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
412 user_data.data = router_data;
413 user_data.free_func = (void *) cr_free_data;
415 status = plugin_register_complex_read (/* group = */ NULL, read_name,
416 cr_read, /* interval = */ 0, &user_data);
419 cr_free_data (router_data);
422 } /* }}} int cr_config_router */
424 static int cr_config (oconfig_item_t *ci)
426 for (int i = 0; i < ci->children_num; i++)
428 oconfig_item_t *child = ci->children + i;
430 if (strcasecmp ("Router", child->key) == 0)
431 cr_config_router (child);
434 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
439 } /* }}} int cr_config */
441 void module_register (void)
443 plugin_register_complex_config ("routeros", cr_config);
444 } /* void module_register */
446 /* vim: set sw=2 noet fdm=marker : */