blob: f072b1ba575c69aae0518a36aafb2baa60b9065b [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
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010048static struct osmo_stats_reporter *osmo_stats_vty2srep(struct vty *vty)
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020049{
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,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010057 int (*fun)(struct osmo_stats_reporter *, const char *),
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020058 const char *val, const char *param_name)
59{
60 int rc;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010061 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020062 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,
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010075 int (*fun)(struct osmo_stats_reporter *, int),
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020076 const char *val, const char *param_name)
77{
78 int rc;
79 int int_val;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +010080 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +020081 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{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100100 return set_srep_parameter_str(vty, osmo_stats_reporter_set_local_addr,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200101 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{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100109 return set_srep_parameter_str(vty, osmo_stats_reporter_set_local_addr,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200110 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{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100118 return set_srep_parameter_str(vty, osmo_stats_reporter_set_remote_addr,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200119 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{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100127 return set_srep_parameter_int(vty, osmo_stats_reporter_set_remote_port,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200128 argv[0], "remote port");
129}
130
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100131DEFUN(cfg_stats_reporter_mtu, cfg_stats_reporter_mtu_cmd,
132 "mtu <100-65535>",
133 "Set the maximum packet size\n"
134 "Size in byte\n")
135{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100136 return set_srep_parameter_int(vty, osmo_stats_reporter_set_mtu,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100137 argv[0], "mtu");
138}
139
140DEFUN(cfg_no_stats_reporter_mtu, cfg_no_stats_reporter_mtu_cmd,
141 "no mtu",
142 NO_STR "Set the maximum packet size\n")
143{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100144 return set_srep_parameter_int(vty, osmo_stats_reporter_set_mtu,
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100145 0, "mtu");
146}
147
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200148DEFUN(cfg_stats_reporter_prefix, cfg_stats_reporter_prefix_cmd,
149 "prefix PREFIX",
150 "Set the item name prefix\n"
151 "The prefix string\n")
152{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100153 return set_srep_parameter_str(vty, osmo_stats_reporter_set_name_prefix,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200154 argv[0], "prefix string");
155}
156
157DEFUN(cfg_no_stats_reporter_prefix, cfg_no_stats_reporter_prefix_cmd,
158 "no prefix",
159 NO_STR
160 "Set the item name prefix\n")
161{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100162 return set_srep_parameter_str(vty, osmo_stats_reporter_set_name_prefix,
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200163 "", "prefix string");
164}
165
166DEFUN(cfg_stats_reporter_enable, cfg_stats_reporter_enable_cmd,
167 "enable",
168 "Enable the reporter\n")
169{
170 int rc;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100171 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200172 OSMO_ASSERT(srep);
173
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100174 rc = osmo_stats_reporter_enable(srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200175 if (rc < 0) {
176 vty_out(vty, "%% Unable to enable the reporter: %s%s",
177 strerror(-rc), VTY_NEWLINE);
178 return CMD_WARNING;
179 }
180
181 return CMD_SUCCESS;
182}
183
184DEFUN(cfg_stats_reporter_disable, cfg_stats_reporter_disable_cmd,
185 "disable",
186 "Disable the reporter\n")
187{
188 int rc;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100189 struct osmo_stats_reporter *srep = osmo_stats_vty2srep(vty);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200190 OSMO_ASSERT(srep);
191
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100192 rc = osmo_stats_reporter_disable(srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200193 if (rc < 0) {
194 vty_out(vty, "%% Unable to disable the reporter: %s%s",
195 strerror(-rc), VTY_NEWLINE);
196 return CMD_WARNING;
197 }
198
199 return CMD_SUCCESS;
200}
201
202DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd,
203 "stats reporter statsd",
204 CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
205{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100206 struct osmo_stats_reporter *srep;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200207
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100208 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200209 if (!srep) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100210 srep = osmo_stats_reporter_create_statsd(NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200211 if (!srep) {
212 vty_out(vty, "%% Unable to create statsd reporter%s",
213 VTY_NEWLINE);
214 return CMD_WARNING;
215 }
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100216 /* TODO: if needed, add osmo_stats_add_reporter(srep); */
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200217 }
218
219 vty->index = srep;
220 vty->node = CFG_STATS_NODE;
221
222 return CMD_SUCCESS;
223}
224
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100225DEFUN(cfg_stats_interval, cfg_stats_interval_cmd,
226 "stats interval <1-65535>",
227 CFG_STATS_STR "Set the reporting interval\n"
228 "Interval in seconds\n")
229{
230 int rc;
231 int interval = atoi(argv[0]);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100232 rc = osmo_stats_set_interval(interval);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100233 if (rc < 0) {
234 vty_out(vty, "%% Unable to set interval: %s%s",
235 strerror(-rc), VTY_NEWLINE);
236 return CMD_WARNING;
237 }
238
239 return CMD_SUCCESS;
240}
241
242
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200243DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd,
244 "no stats reporter statsd",
245 NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
246{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100247 struct osmo_stats_reporter *srep;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200248
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100249 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200250 if (!srep) {
251 vty_out(vty, "%% No statsd logging active%s",
252 VTY_NEWLINE);
253 return CMD_WARNING;
254 }
255
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100256 osmo_stats_reporter_free(srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200257
258 return CMD_SUCCESS;
259}
260
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100261DEFUN(cfg_stats_reporter_log, cfg_stats_reporter_log_cmd,
262 "stats reporter log",
263 CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n")
264{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100265 struct osmo_stats_reporter *srep;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100266
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100267 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100268 if (!srep) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100269 srep = osmo_stats_reporter_create_log(NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100270 if (!srep) {
271 vty_out(vty, "%% Unable to create log reporter%s",
272 VTY_NEWLINE);
273 return CMD_WARNING;
274 }
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100275 /* TODO: if needed, add osmo_stats_add_reporter(srep); */
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100276 }
277
278 vty->index = srep;
279 vty->node = CFG_STATS_NODE;
280
281 return CMD_SUCCESS;
282}
283
284DEFUN(cfg_no_stats_reporter_log, cfg_no_stats_reporter_log_cmd,
285 "no stats reporter log",
286 NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to the logger\n")
287{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100288 struct osmo_stats_reporter *srep;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100289
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100290 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100291 if (!srep) {
292 vty_out(vty, "%% No log reporting active%s",
293 VTY_NEWLINE);
294 return CMD_WARNING;
295 }
296
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100297 osmo_stats_reporter_free(srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100298
299 return CMD_SUCCESS;
300}
301
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200302DEFUN(show_stats,
303 show_stats_cmd,
304 "show stats",
305 SHOW_STR SHOW_STATS_STR)
306{
307 vty_out_statistics_full(vty, "");
308
309 return CMD_SUCCESS;
310}
311
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100312static int config_write_stats_reporter(struct vty *vty, struct osmo_stats_reporter *srep)
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200313{
314 if (srep == NULL)
315 return 0;
316
317 switch (srep->type) {
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100318 case OSMO_STATS_REPORTER_STATSD:
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200319 vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE);
320 break;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100321 case OSMO_STATS_REPORTER_LOG:
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100322 vty_out(vty, "stats reporter log%s", VTY_NEWLINE);
323 break;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200324 }
325
326 vty_out(vty, " disable%s", VTY_NEWLINE);
327
Jacob Erlbecked197fd2015-10-27 14:43:24 +0100328 if (srep->have_net_config) {
329 if (srep->dest_addr_str)
330 vty_out(vty, " remote-ip %s%s",
331 srep->dest_addr_str, VTY_NEWLINE);
332 if (srep->dest_port)
333 vty_out(vty, " remote-port %d%s",
334 srep->dest_port, VTY_NEWLINE);
335 if (srep->bind_addr_str)
336 vty_out(vty, " local-ip %s%s",
337 srep->bind_addr_str, VTY_NEWLINE);
338 if (srep->mtu)
339 vty_out(vty, " mtu %d%s",
340 srep->mtu, VTY_NEWLINE);
341 }
342
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200343 if (srep->name_prefix && *srep->name_prefix)
344 vty_out(vty, " prefix %s%s",
345 srep->name_prefix, VTY_NEWLINE);
346 else
347 vty_out(vty, " no prefix%s", VTY_NEWLINE);
348
349 if (srep->enabled)
350 vty_out(vty, " enable%s", VTY_NEWLINE);
351
352 return 1;
353}
354
355static int config_write_stats(struct vty *vty)
356{
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100357 struct osmo_stats_reporter *srep;
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200358
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100359 /* TODO: loop through all reporters */
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100360 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_STATSD, NULL);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200361 config_write_stats_reporter(vty, srep);
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100362 srep = osmo_stats_reporter_find(OSMO_STATS_REPORTER_LOG, NULL);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100363 config_write_stats_reporter(vty, srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200364
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100365 vty_out(vty, "stats interval %d%s", osmo_stats_config->interval, VTY_NEWLINE);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100366
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200367 return 1;
368}
369
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100370void osmo_stats_vty_add_cmds()
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200371{
372 install_element_ve(&show_stats_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200373
374 install_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd);
375 install_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100376 install_element(CONFIG_NODE, &cfg_stats_reporter_log_cmd);
377 install_element(CONFIG_NODE, &cfg_no_stats_reporter_log_cmd);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100378 install_element(CONFIG_NODE, &cfg_stats_interval_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200379
380 install_node(&cfg_stats_node, config_write_stats);
381 vty_install_default(CFG_STATS_NODE);
382
383 install_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd);
384 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd);
385 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd);
386 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd);
Jacob Erlbeckd01acfc2015-10-26 16:22:45 +0100387 install_element(CFG_STATS_NODE, &cfg_stats_reporter_mtu_cmd);
388 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_mtu_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200389 install_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd);
390 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd);
391 install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd);
392 install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd);
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200393}