blob: 954f3581c1beb30e621a4a521d71fabb1f467906 [file] [log] [blame]
Jacob Erlbeck45513e62015-10-19 15:14:13 +02001/* OpenBSC stats helper for the VTY */
2/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2009-2014 by Holger Hans Peter Freyther
4 * (C) 2015 by Sysmocom s.f.m.c. GmbH
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdlib.h>
24#include <string.h>
25
26#include "../../config.h"
27
28#include <osmocom/vty/command.h>
29#include <osmocom/vty/buffer.h>
30#include <osmocom/vty/vty.h>
31#include <osmocom/vty/telnet_interface.h>
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020032#include <osmocom/vty/telnet_interface.h>
Jacob Erlbeck45513e62015-10-19 15:14:13 +020033#include <osmocom/vty/misc.h>
34
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020035#include <osmocom/core/stats.h>
36
Jacob Erlbeck45513e62015-10-19 15:14:13 +020037#define CFG_STATS_STR "Configure stats sub-system\n"
38#define CFG_REPORTER_STR "Configure a stats reporter\n"
39
40#define SHOW_STATS_STR "Show statistical values\n"
41
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020042struct cmd_node cfg_stats_node = {
43 CFG_STATS_NODE,
44 "%s(config-stats)# ",
45 1
46};
47
48static struct stats_reporter *osmo_stats_vty2srep(struct vty *vty)
49{
50 if (vty->node == CFG_STATS_NODE)
51 return vty->index;
52
53 return NULL;
54}
55
56static int set_srep_parameter_str(struct vty *vty,
57 int (*fun)(struct stats_reporter *, const char *),
58 const char *val, const char *param_name)
59{
60 int rc;
61 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
62 OSMO_ASSERT(srep);
63
64 rc = fun(srep, val);
65 if (rc < 0) {
66 vty_out(vty, "%% Unable to set %s: %s%s",
67 param_name, strerror(-rc), VTY_NEWLINE);
68 return CMD_WARNING;
69 }
70
71 return CMD_SUCCESS;
72}
73
74static int set_srep_parameter_int(struct vty *vty,
75 int (*fun)(struct stats_reporter *, int),
76 const char *val, const char *param_name)
77{
78 int rc;
79 int int_val;
80 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
81 OSMO_ASSERT(srep);
82
83 int_val = atoi(val);
84
85 rc = fun(srep, int_val);
86 if (rc < 0) {
87 vty_out(vty, "%% Unable to set %s: %s%s",
88 param_name, strerror(-rc), VTY_NEWLINE);
89 return CMD_WARNING;
90 }
91
92 return CMD_SUCCESS;
93}
94
95DEFUN(cfg_stats_reporter_local_ip, cfg_stats_reporter_local_ip_cmd,
96 "local-ip ADDR",
97 "Set the IP address to which we bind locally\n"
98 "IP Address\n")
99{
100 return set_srep_parameter_str(vty, stats_reporter_set_local_addr,
101 argv[0], "local address");
102}
103
104DEFUN(cfg_no_stats_reporter_local_ip, cfg_no_stats_reporter_local_ip_cmd,
105 "no local-ip",
106 NO_STR
107 "Set the IP address to which we bind locally\n")
108{
109 return set_srep_parameter_str(vty, stats_reporter_set_local_addr,
110 NULL, "local address");
111}
112
113DEFUN(cfg_stats_reporter_remote_ip, cfg_stats_reporter_remote_ip_cmd,
114 "remote-ip ADDR",
115 "Set the remote IP address to which we connect\n"
116 "IP Address\n")
117{
118 return set_srep_parameter_str(vty, stats_reporter_set_remote_addr,
119 argv[0], "remote address");
120}
121
122DEFUN(cfg_stats_reporter_remote_port, cfg_stats_reporter_remote_port_cmd,
123 "remote-port <1-65535>",
124 "Set the remote port to which we connect\n"
125 "Remote port number\n")
126{
127 return set_srep_parameter_int(vty, stats_reporter_set_remote_port,
128 argv[0], "remote port");
129}
130
131DEFUN(cfg_stats_reporter_interval, cfg_stats_reporter_interval_cmd,
132 "interval <1-65535>",
133 "Set the reporting interval\n"
134 "Interval in seconds\n")
135{
136 return set_srep_parameter_int(vty, stats_reporter_set_interval,
137 argv[0], "reporting interval");
138}
139
140DEFUN(cfg_stats_reporter_prefix, cfg_stats_reporter_prefix_cmd,
141 "prefix PREFIX",
142 "Set the item name prefix\n"
143 "The prefix string\n")
144{
145 return set_srep_parameter_str(vty, stats_reporter_set_name_prefix,
146 argv[0], "prefix string");
147}
148
149DEFUN(cfg_no_stats_reporter_prefix, cfg_no_stats_reporter_prefix_cmd,
150 "no prefix",
151 NO_STR
152 "Set the item name prefix\n")
153{
154 return set_srep_parameter_str(vty, stats_reporter_set_name_prefix,
155 "", "prefix string");
156}
157
158DEFUN(cfg_stats_reporter_enable, cfg_stats_reporter_enable_cmd,
159 "enable",
160 "Enable the reporter\n")
161{
162 int rc;
163 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
164 OSMO_ASSERT(srep);
165
166 rc = stats_reporter_enable(srep);
167 if (rc < 0) {
168 vty_out(vty, "%% Unable to enable the reporter: %s%s",
169 strerror(-rc), VTY_NEWLINE);
170 return CMD_WARNING;
171 }
172
173 return CMD_SUCCESS;
174}
175
176DEFUN(cfg_stats_reporter_disable, cfg_stats_reporter_disable_cmd,
177 "disable",
178 "Disable the reporter\n")
179{
180 int rc;
181 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
182 OSMO_ASSERT(srep);
183
184 rc = stats_reporter_disable(srep);
185 if (rc < 0) {
186 vty_out(vty, "%% Unable to disable the reporter: %s%s",
187 strerror(-rc), VTY_NEWLINE);
188 return CMD_WARNING;
189 }
190
191 return CMD_SUCCESS;
192}
193
194DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd,
195 "stats reporter statsd",
196 CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
197{
198 struct stats_reporter *srep;
199
200 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
201 if (!srep) {
202 srep = stats_reporter_create_statsd(NULL);
203 if (!srep) {
204 vty_out(vty, "%% Unable to create statsd reporter%s",
205 VTY_NEWLINE);
206 return CMD_WARNING;
207 }
208 /* TODO: if needed, add stats_add_reporter(srep); */
209 }
210
211 vty->index = srep;
212 vty->node = CFG_STATS_NODE;
213
214 return CMD_SUCCESS;
215}
216
217DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd,
218 "no stats reporter statsd",
219 NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
220{
221 struct stats_reporter *srep;
222
223 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
224 if (!srep) {
225 vty_out(vty, "%% No statsd logging active%s",
226 VTY_NEWLINE);
227 return CMD_WARNING;
228 }
229
230 stats_reporter_free(srep);
231
232 return CMD_SUCCESS;
233}
234
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200235DEFUN(show_stats,
236 show_stats_cmd,
237 "show stats",
238 SHOW_STR SHOW_STATS_STR)
239{
240 vty_out_statistics_full(vty, "");
241
242 return CMD_SUCCESS;
243}
244
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200245static int config_write_stats_reporter(struct vty *vty, struct stats_reporter *srep)
246{
247 if (srep == NULL)
248 return 0;
249
250 switch (srep->type) {
251 case STATS_REPORTER_STATSD:
252 vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE);
253 break;
254 }
255
256 vty_out(vty, " disable%s", VTY_NEWLINE);
257
258 if (srep->dest_addr_str)
259 vty_out(vty, " remote-ip %s%s",
260 srep->dest_addr_str, VTY_NEWLINE);
261 if (srep->dest_port)
262 vty_out(vty, " remote-port %d%s",
263 srep->dest_port, VTY_NEWLINE);
264 if (srep->bind_addr_str)
265 vty_out(vty, " local-ip %s%s",
266 srep->bind_addr_str, VTY_NEWLINE);
267 if (srep->interval)
268 vty_out(vty, " interval %d%s",
269 srep->interval, VTY_NEWLINE);
270 if (srep->name_prefix && *srep->name_prefix)
271 vty_out(vty, " prefix %s%s",
272 srep->name_prefix, VTY_NEWLINE);
273 else
274 vty_out(vty, " no prefix%s", VTY_NEWLINE);
275
276 if (srep->enabled)
277 vty_out(vty, " enable%s", VTY_NEWLINE);
278
279 return 1;
280}
281
282static int config_write_stats(struct vty *vty)
283{
284 struct stats_reporter *srep;
285
286 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
287 config_write_stats_reporter(vty, srep);
288
289 return 1;
290}
291
292void stats_vty_add_cmds()
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200293{
294 install_element_ve(&show_stats_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200295
296 install_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd);
297 install_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd);
298
299 install_node(&cfg_stats_node, config_write_stats);
300 vty_install_default(CFG_STATS_NODE);
301
302 install_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd);
303 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd);
304 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd);
305 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd);
306 install_element(CFG_STATS_NODE, &cfg_stats_reporter_interval_cmd);
307 install_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd);
308 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd);
309 install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd);
310 install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd);
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200311}