blob: 839dc829450104c319654321caed47ec6586c867 [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
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{
136 return set_srep_parameter_int(vty, stats_reporter_set_mtu,
137 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{
144 return set_srep_parameter_int(vty, stats_reporter_set_mtu,
145 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{
153 return set_srep_parameter_str(vty, stats_reporter_set_name_prefix,
154 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{
162 return set_srep_parameter_str(vty, stats_reporter_set_name_prefix,
163 "", "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;
171 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
172 OSMO_ASSERT(srep);
173
174 rc = stats_reporter_enable(srep);
175 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;
189 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
190 OSMO_ASSERT(srep);
191
192 rc = stats_reporter_disable(srep);
193 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{
206 struct stats_reporter *srep;
207
208 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
209 if (!srep) {
210 srep = stats_reporter_create_statsd(NULL);
211 if (!srep) {
212 vty_out(vty, "%% Unable to create statsd reporter%s",
213 VTY_NEWLINE);
214 return CMD_WARNING;
215 }
216 /* TODO: if needed, add stats_add_reporter(srep); */
217 }
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]);
232 rc = stats_set_interval(interval);
233 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{
247 struct stats_reporter *srep;
248
249 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
250 if (!srep) {
251 vty_out(vty, "%% No statsd logging active%s",
252 VTY_NEWLINE);
253 return CMD_WARNING;
254 }
255
256 stats_reporter_free(srep);
257
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{
265 struct stats_reporter *srep;
266
267 srep = stats_reporter_find(STATS_REPORTER_LOG, NULL);
268 if (!srep) {
269 srep = stats_reporter_create_log(NULL);
270 if (!srep) {
271 vty_out(vty, "%% Unable to create log reporter%s",
272 VTY_NEWLINE);
273 return CMD_WARNING;
274 }
275 /* TODO: if needed, add stats_add_reporter(srep); */
276 }
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{
288 struct stats_reporter *srep;
289
290 srep = stats_reporter_find(STATS_REPORTER_LOG, NULL);
291 if (!srep) {
292 vty_out(vty, "%% No log reporting active%s",
293 VTY_NEWLINE);
294 return CMD_WARNING;
295 }
296
297 stats_reporter_free(srep);
298
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 Erlbeckadc900e2015-10-20 19:05:52 +0200312static int config_write_stats_reporter(struct vty *vty, struct stats_reporter *srep)
313{
314 if (srep == NULL)
315 return 0;
316
317 switch (srep->type) {
318 case STATS_REPORTER_STATSD:
319 vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE);
320 break;
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100321 case STATS_REPORTER_LOG:
322 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{
357 struct stats_reporter *srep;
358
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100359 /* TODO: loop through all reporters */
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200360 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
361 config_write_stats_reporter(vty, srep);
Jacob Erlbeckbc4f7ae2015-10-28 21:47:45 +0100362 srep = stats_reporter_find(STATS_REPORTER_LOG, NULL);
363 config_write_stats_reporter(vty, srep);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200364
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100365 vty_out(vty, "stats interval %d%s", stats_config->interval, VTY_NEWLINE);
366
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200367 return 1;
368}
369
370void 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}