2 * collectd - src/load.c
3 * Copyright (C) 2009 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at verplant.org>
26 #include <routeros_api.h>
30 ros_connection_t *connection;
37 typedef struct cr_data_s cr_data_t;
39 static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */
40 counter_t rx, counter_t tx)
43 value_list_t vl = VALUE_LIST_INIT;
45 values[0].counter = rx;
46 values[1].counter = tx;
49 vl.values_len = STATIC_ARRAY_SIZE (values);
50 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
51 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
52 sstrncpy (vl.type, type, sizeof (vl.type));
53 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
55 plugin_dispatch_values (&vl);
56 } /* }}} void cr_submit_io */
58 static void submit_interface (const ros_interface_t *i) /* {{{ */
65 submit_interface (i->next);
69 cr_submit_io ("if_packets", i->name,
70 (counter_t) i->rx_packets, (counter_t) i->tx_packets);
71 cr_submit_io ("if_octets", i->name,
72 (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
73 cr_submit_io ("if_errors", i->name,
74 (counter_t) i->rx_errors, (counter_t) i->tx_errors);
75 cr_submit_io ("if_dropped", i->name,
76 (counter_t) i->rx_drops, (counter_t) i->tx_drops);
78 submit_interface (i->next);
79 } /* }}} void submit_interface */
81 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
82 const ros_interface_t *i, __attribute__((unused)) void *user_data)
86 } /* }}} int handle_interface */
88 static void cr_submit_gauge (const char *type, /* {{{ */
89 const char *type_instance, gauge_t value)
92 value_list_t vl = VALUE_LIST_INIT;
94 values[0].gauge = value;
97 vl.values_len = STATIC_ARRAY_SIZE (values);
98 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
99 sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
100 sstrncpy (vl.type, type, sizeof (vl.type));
101 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
103 plugin_dispatch_values (&vl);
104 } /* }}} void cr_submit_gauge */
106 static void submit_regtable (const ros_registration_table_t *r) /* {{{ */
108 char type_instance[DATA_MAX_NAME_LEN];
114 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
115 r->interface, r->radio_name);
116 cr_submit_gauge ("bitrate", type_instance,
117 (gauge_t) (1000000.0 * r->rx_rate));
118 cr_submit_gauge ("signal_power", type_instance,
119 (gauge_t) r->rx_signal_strength);
120 cr_submit_gauge ("signal_quality", type_instance,
121 (gauge_t) r->rx_ccq);
124 ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
125 r->interface, r->radio_name);
126 cr_submit_gauge ("bitrate", type_instance,
127 (gauge_t) (1000000.0 * r->tx_rate));
128 cr_submit_gauge ("signal_power", type_instance,
129 (gauge_t) r->tx_signal_strength);
130 cr_submit_gauge ("signal_quality", type_instance,
131 (gauge_t) r->tx_ccq);
134 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
135 r->interface, r->radio_name);
136 cr_submit_io ("if_octets", type_instance,
137 (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
138 cr_submit_gauge ("snr", type_instance, (gauge_t) r->signal_to_noise);
140 submit_regtable (r->next);
141 } /* }}} void submit_regtable */
143 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
144 const ros_registration_table_t *r,
145 __attribute__((unused)) void *user_data)
149 } /* }}} int handle_regtable */
151 static int cr_read (user_data_t *user_data) /* {{{ */
156 if (user_data == NULL)
159 rd = user_data->data;
163 if (rd->connection == NULL)
165 rd->connection = ros_connect (rd->node, rd->service,
166 rd->username, rd->password);
167 if (rd->connection == NULL)
170 ERROR ("routeros plugin: ros_connect failed: %s",
171 sstrerror (errno, errbuf, sizeof (errbuf)));
175 assert (rd->connection != NULL);
177 status = ros_interface (rd->connection, handle_interface,
178 /* user data = */ NULL);
182 ERROR ("routeros plugin: ros_interface failed: %s",
183 sstrerror (status, errbuf, sizeof (errbuf)));
184 ros_disconnect (rd->connection);
185 rd->connection = NULL;
189 status = ros_registration_table (rd->connection, handle_regtable,
190 /* user data = */ NULL);
194 ERROR ("routeros plugin: ros_registration_table failed: %s",
195 sstrerror (status, errbuf, sizeof (errbuf)));
196 ros_disconnect (rd->connection);
197 rd->connection = NULL;
202 } /* }}} int cr_read */
204 static void cr_free_data (cr_data_t *ptr) /* {{{ */
209 ros_disconnect (ptr->connection);
210 ptr->connection = NULL;
213 sfree (ptr->service);
214 sfree (ptr->username);
215 sfree (ptr->password);
218 } /* }}} void cr_free_data */
220 static int cr_config_router (oconfig_item_t *ci) /* {{{ */
222 cr_data_t *router_data;
224 user_data_t user_data;
228 router_data = malloc (sizeof (*router_data));
229 if (router_data == NULL)
231 memset (router_data, 0, sizeof (router_data));
232 router_data->connection = NULL;
233 router_data->node = NULL;
234 router_data->service = NULL;
235 router_data->username = NULL;
236 router_data->password = NULL;
239 for (i = 0; i < ci->children_num; i++)
241 oconfig_item_t *child = ci->children + i;
243 if (strcasecmp ("Host", child->key) == 0)
244 status = cf_util_get_string (child, &router_data->node);
245 else if (strcasecmp ("Port", child->key) == 0)
246 status = cf_util_get_string (child, &router_data->service);
247 else if (strcasecmp ("User", child->key) == 0)
248 status = cf_util_get_string (child, &router_data->username);
249 else if (strcasecmp ("Password", child->key) == 0)
250 status = cf_util_get_string (child, &router_data->service);
253 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
262 if (router_data->node == NULL)
264 ERROR ("routeros plugin: No `Host' option within a `Router' block. "
265 "Where should I connect to?");
269 if (router_data->password == NULL)
271 ERROR ("routeros plugin: No `Password' option within a `Router' block. "
272 "How should I authenticate?");
277 if ((status == 0) && (router_data->username == NULL))
279 router_data->username = sstrdup ("admin");
280 if (router_data->username == NULL)
282 ERROR ("routeros plugin: sstrdup failed.");
287 ssnprintf (read_name, sizeof (read_name), "routeros/%s", router_data->node);
288 user_data.data = router_data;
289 user_data.free_func = (void *) cr_free_data;
291 status = plugin_register_complex_read (read_name, cr_read,
292 /* interval = */ NULL, &user_data);
295 cr_free_data (router_data);
298 } /* }}} int cr_config_router */
300 static int cr_config (oconfig_item_t *ci)
304 for (i = 0; i < ci->children_num; i++)
306 oconfig_item_t *child = ci->children + i;
308 if (strcasecmp ("Router", child->key) == 0)
309 cr_config_router (child);
312 WARNING ("routeros plugin: Unknown config option `%s'.", child->key);
317 } /* }}} int cr_config */
319 void module_register (void)
321 plugin_register_complex_config ("routeros", cr_config);
322 } /* void module_register */
324 /* vim: set sw=2 noet fdm=marker : */