blob: a4fd7b05838dce31296e6b393e4840cc7d564501 [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 Erlbeckadc900e2015-10-20 19:05:52 +0200131DEFUN(cfg_stats_reporter_prefix, cfg_stats_reporter_prefix_cmd,
132 "prefix PREFIX",
133 "Set the item name prefix\n"
134 "The prefix string\n")
135{
136 return set_srep_parameter_str(vty, stats_reporter_set_name_prefix,
137 argv[0], "prefix string");
138}
139
140DEFUN(cfg_no_stats_reporter_prefix, cfg_no_stats_reporter_prefix_cmd,
141 "no prefix",
142 NO_STR
143 "Set the item name prefix\n")
144{
145 return set_srep_parameter_str(vty, stats_reporter_set_name_prefix,
146 "", "prefix string");
147}
148
149DEFUN(cfg_stats_reporter_enable, cfg_stats_reporter_enable_cmd,
150 "enable",
151 "Enable the reporter\n")
152{
153 int rc;
154 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
155 OSMO_ASSERT(srep);
156
157 rc = stats_reporter_enable(srep);
158 if (rc < 0) {
159 vty_out(vty, "%% Unable to enable the reporter: %s%s",
160 strerror(-rc), VTY_NEWLINE);
161 return CMD_WARNING;
162 }
163
164 return CMD_SUCCESS;
165}
166
167DEFUN(cfg_stats_reporter_disable, cfg_stats_reporter_disable_cmd,
168 "disable",
169 "Disable the reporter\n")
170{
171 int rc;
172 struct stats_reporter *srep = osmo_stats_vty2srep(vty);
173 OSMO_ASSERT(srep);
174
175 rc = stats_reporter_disable(srep);
176 if (rc < 0) {
177 vty_out(vty, "%% Unable to disable the reporter: %s%s",
178 strerror(-rc), VTY_NEWLINE);
179 return CMD_WARNING;
180 }
181
182 return CMD_SUCCESS;
183}
184
185DEFUN(cfg_stats_reporter_statsd, cfg_stats_reporter_statsd_cmd,
186 "stats reporter statsd",
187 CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
188{
189 struct stats_reporter *srep;
190
191 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
192 if (!srep) {
193 srep = stats_reporter_create_statsd(NULL);
194 if (!srep) {
195 vty_out(vty, "%% Unable to create statsd reporter%s",
196 VTY_NEWLINE);
197 return CMD_WARNING;
198 }
199 /* TODO: if needed, add stats_add_reporter(srep); */
200 }
201
202 vty->index = srep;
203 vty->node = CFG_STATS_NODE;
204
205 return CMD_SUCCESS;
206}
207
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100208DEFUN(cfg_stats_interval, cfg_stats_interval_cmd,
209 "stats interval <1-65535>",
210 CFG_STATS_STR "Set the reporting interval\n"
211 "Interval in seconds\n")
212{
213 int rc;
214 int interval = atoi(argv[0]);
215 rc = stats_set_interval(interval);
216 if (rc < 0) {
217 vty_out(vty, "%% Unable to set interval: %s%s",
218 strerror(-rc), VTY_NEWLINE);
219 return CMD_WARNING;
220 }
221
222 return CMD_SUCCESS;
223}
224
225
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200226DEFUN(cfg_no_stats_reporter_statsd, cfg_no_stats_reporter_statsd_cmd,
227 "no stats reporter statsd",
228 NO_STR CFG_STATS_STR CFG_REPORTER_STR "Report to a STATSD server\n")
229{
230 struct stats_reporter *srep;
231
232 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
233 if (!srep) {
234 vty_out(vty, "%% No statsd logging active%s",
235 VTY_NEWLINE);
236 return CMD_WARNING;
237 }
238
239 stats_reporter_free(srep);
240
241 return CMD_SUCCESS;
242}
243
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200244DEFUN(show_stats,
245 show_stats_cmd,
246 "show stats",
247 SHOW_STR SHOW_STATS_STR)
248{
249 vty_out_statistics_full(vty, "");
250
251 return CMD_SUCCESS;
252}
253
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200254static int config_write_stats_reporter(struct vty *vty, struct stats_reporter *srep)
255{
256 if (srep == NULL)
257 return 0;
258
259 switch (srep->type) {
260 case STATS_REPORTER_STATSD:
261 vty_out(vty, "stats reporter statsd%s", VTY_NEWLINE);
262 break;
263 }
264
265 vty_out(vty, " disable%s", VTY_NEWLINE);
266
267 if (srep->dest_addr_str)
268 vty_out(vty, " remote-ip %s%s",
269 srep->dest_addr_str, VTY_NEWLINE);
270 if (srep->dest_port)
271 vty_out(vty, " remote-port %d%s",
272 srep->dest_port, VTY_NEWLINE);
273 if (srep->bind_addr_str)
274 vty_out(vty, " local-ip %s%s",
275 srep->bind_addr_str, VTY_NEWLINE);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200276 if (srep->name_prefix && *srep->name_prefix)
277 vty_out(vty, " prefix %s%s",
278 srep->name_prefix, VTY_NEWLINE);
279 else
280 vty_out(vty, " no prefix%s", VTY_NEWLINE);
281
282 if (srep->enabled)
283 vty_out(vty, " enable%s", VTY_NEWLINE);
284
285 return 1;
286}
287
288static int config_write_stats(struct vty *vty)
289{
290 struct stats_reporter *srep;
291
292 srep = stats_reporter_find(STATS_REPORTER_STATSD, NULL);
293 config_write_stats_reporter(vty, srep);
294
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100295 vty_out(vty, "stats interval %d%s", stats_config->interval, VTY_NEWLINE);
296
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200297 return 1;
298}
299
300void stats_vty_add_cmds()
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200301{
302 install_element_ve(&show_stats_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200303
304 install_element(CONFIG_NODE, &cfg_stats_reporter_statsd_cmd);
305 install_element(CONFIG_NODE, &cfg_no_stats_reporter_statsd_cmd);
Jacob Erlbeckb1dbfb42015-10-26 11:58:38 +0100306 install_element(CONFIG_NODE, &cfg_stats_interval_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200307
308 install_node(&cfg_stats_node, config_write_stats);
309 vty_install_default(CFG_STATS_NODE);
310
311 install_element(CFG_STATS_NODE, &cfg_stats_reporter_local_ip_cmd);
312 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_local_ip_cmd);
313 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_ip_cmd);
314 install_element(CFG_STATS_NODE, &cfg_stats_reporter_remote_port_cmd);
Jacob Erlbeckadc900e2015-10-20 19:05:52 +0200315 install_element(CFG_STATS_NODE, &cfg_stats_reporter_prefix_cmd);
316 install_element(CFG_STATS_NODE, &cfg_no_stats_reporter_prefix_cmd);
317 install_element(CFG_STATS_NODE, &cfg_stats_reporter_enable_cmd);
318 install_element(CFG_STATS_NODE, &cfg_stats_reporter_disable_cmd);
Jacob Erlbeck45513e62015-10-19 15:14:13 +0200319}