93c281272570b14435bea7ce42d2b3b4a1ad6c87
[collectd.git] / src / routeros.c
1 /**
2  * collectd - src/load.c
3  * Copyright (C) 2009  Florian octo Forster
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include <routeros_api.h>
27
28 static ros_connection_t *connection = NULL;
29
30 static char *conf_node = "router.example.com";
31 static char *conf_service = NULL;
32 static char *conf_username = "collectd";
33 static char *conf_password = "secret";
34
35 static void cr_submit_io (const char *type, const char *type_instance, /* {{{ */
36     counter_t rx, counter_t tx)
37 {
38         value_t values[2];
39         value_list_t vl = VALUE_LIST_INIT;
40
41         values[0].counter = rx;
42         values[1].counter = tx;
43
44         vl.values = values;
45         vl.values_len = STATIC_ARRAY_SIZE (values);
46         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
47         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
48         sstrncpy (vl.type, type, sizeof (vl.type));
49         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
50
51         plugin_dispatch_values (&vl);
52 } /* }}} void cr_submit_io */
53
54 static void submit_interface (const ros_interface_t *i) /* {{{ */
55 {
56   if (i == NULL)
57     return;
58
59   if (!i->running)
60   {
61     submit_interface (i->next);
62     return;
63   }
64
65   cr_submit_io ("if_packets", i->name,
66       (counter_t) i->rx_packets, (counter_t) i->tx_packets);
67   cr_submit_io ("if_octets", i->name,
68       (counter_t) i->rx_bytes, (counter_t) i->tx_bytes);
69   cr_submit_io ("if_errors", i->name,
70       (counter_t) i->rx_errors, (counter_t) i->tx_errors);
71   cr_submit_io ("if_dropped", i->name,
72       (counter_t) i->rx_drops, (counter_t) i->tx_drops);
73
74   submit_interface (i->next);
75 } /* }}} void submit_interface */
76
77 static int handle_interface (__attribute__((unused)) ros_connection_t *c, /* {{{ */
78     const ros_interface_t *i, __attribute__((unused)) void *user_data)
79 {
80   submit_interface (i);
81   return (0);
82 } /* }}} int handle_interface */
83
84 static void cr_submit_gauge (const char *type, /* {{{ */
85     const char *type_instance, gauge_t value)
86 {
87         value_t values[1];
88         value_list_t vl = VALUE_LIST_INIT;
89
90         values[0].gauge = value;
91
92         vl.values = values;
93         vl.values_len = STATIC_ARRAY_SIZE (values);
94         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
95         sstrncpy (vl.plugin, "routeros", sizeof (vl.plugin));
96         sstrncpy (vl.type, type, sizeof (vl.type));
97         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
98
99         plugin_dispatch_values (&vl);
100 } /* }}} void cr_submit_gauge */
101
102 static void submit_regtable (const ros_registration_table_t *r) /* {{{ */
103 {
104   char type_instance[DATA_MAX_NAME_LEN];
105
106   if (r == NULL)
107     return;
108
109   /*** RX ***/
110   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-rx",
111       r->interface, r->radio_name);
112   cr_submit_gauge ("bitrate", type_instance,
113       (gauge_t) (1000000.0 * r->rx_rate));
114   cr_submit_gauge ("signal_power", type_instance,
115       (gauge_t) r->rx_signal_strength);
116   cr_submit_gauge ("signal_quality", type_instance,
117       (gauge_t) r->rx_ccq);
118
119   /*** TX ***/
120   ssnprintf (type_instance, sizeof (type_instance), "%s-%s-tx",
121       r->interface, r->radio_name);
122   cr_submit_gauge ("bitrate", type_instance,
123       (gauge_t) (1000000.0 * r->tx_rate));
124   cr_submit_gauge ("signal_power", type_instance,
125       (gauge_t) r->tx_signal_strength);
126   cr_submit_gauge ("signal_quality", type_instance,
127       (gauge_t) r->tx_ccq);
128
129   /*** RX / TX ***/
130   ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
131       r->interface, r->radio_name);
132   cr_submit_io ("if_octets", type_instance,
133       (counter_t) r->rx_bytes, (counter_t) r->tx_bytes);
134   cr_submit_gauge ("snr", type_instance, (gauge_t) r->signal_to_noise);
135
136   submit_regtable (r->next);
137 } /* }}} void submit_regtable */
138
139 static int handle_regtable (__attribute__((unused)) ros_connection_t *c, /* {{{ */
140     const ros_registration_table_t *r,
141     __attribute__((unused)) void *user_data)
142 {
143   submit_regtable (r);
144   return (0);
145 } /* }}} int handle_regtable */
146
147 static int cr_read (void) /* {{{ */
148 {
149   int status;
150
151   if (connection == NULL)
152   {
153     connection = ros_connect (conf_node, conf_service,
154         conf_username, conf_password);
155     if (connection == NULL)
156     {
157       char errbuf[128];
158       ERROR ("routeros plugin: ros_connect failed: %s",
159           sstrerror (errno, errbuf, sizeof (errbuf)));
160       return (-1);
161     }
162   }
163   assert (connection != NULL);
164
165   status = ros_interface (connection, handle_interface,
166       /* user data = */ NULL);
167   if (status != 0)
168   {
169     char errbuf[128];
170     ERROR ("routeros plugin: ros_interface failed: %s",
171         sstrerror (status, errbuf, sizeof (errbuf)));
172     ros_disconnect (connection);
173     connection = NULL;
174     return (-1);
175   }
176
177   status = ros_registration_table (connection, handle_regtable,
178       /* user data = */ NULL);
179   if (status != 0)
180   {
181     char errbuf[128];
182     ERROR ("routeros plugin: ros_registration_table failed: %s",
183         sstrerror (status, errbuf, sizeof (errbuf)));
184     ros_disconnect (connection);
185     connection = NULL;
186     return (-1);
187   }
188
189   return (0);
190 } /* }}} int cr_read */
191
192 void module_register (void)
193 {
194         plugin_register_read ("routeros", cr_read);
195 } /* void module_register */
196
197 /* vim: set sw=2 noet fdm=marker : */