summaryrefslogtreecommitdiffstats
path: root/openbsc/src/vty_interface_cmds.c
blob: c1a5eee711df5a5f7398e72856a1bc6c1351c7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* OpenBSC logging helper for the VTY */
/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
 * (C) 2009-2010 by Holger Hans Peter Freyther
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

#include <openbsc/vty.h>
#include <openbsc/telnet_interface.h>

#include <osmocore/talloc.h>

#include <vty/command.h>
#include <vty/buffer.h>
#include <vty/vty.h>

#include <stdlib.h>

#define LOGGING_STR	"Configure log message to this terminal\n"

static void _vty_output(struct log_target *tgt, const char *line)
{
	struct vty *vty = tgt->tgt_vty.vty;
	vty_out(vty, "%s", line);
	/* This is an ugly hack, but there is no easy way... */
	if (strchr(line, '\n'))
		vty_out(vty, "\r");
}

struct log_target *log_target_create_vty(struct vty *vty)
{
	struct log_target *target;

	target = log_target_create();
	if (!target)
		return NULL;

	target->tgt_vty.vty = vty;
	target->output = _vty_output;
	return target;
}

DEFUN(enable_logging,
      enable_logging_cmd,
      "logging enable",
	LOGGING_STR
      "Enables logging to this vty\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (conn->dbg) {
		vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	conn->dbg = log_target_create_vty(vty);
	if (!conn->dbg)
		return CMD_WARNING;

	log_add_target(conn->dbg);
	return CMD_SUCCESS;
}

DEFUN(logging_fltr_imsi,
      logging_fltr_imsi_cmd,
      "logging filter imsi IMSI",
	LOGGING_STR
      "Print all messages related to a IMSI\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg) {
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	log_set_imsi_filter(conn->dbg, argv[0]);
	return CMD_SUCCESS;
}

DEFUN(logging_fltr_all,
      logging_fltr_all_cmd,
      "logging filter all <0-1>",
	LOGGING_STR
      "Print all messages to the console\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg) {
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	log_set_all_filter(conn->dbg, atoi(argv[0]));
	return CMD_SUCCESS;
}

DEFUN(logging_use_clr,
      logging_use_clr_cmd,
      "logging color <0-1>",
	LOGGING_STR
      "Use color for printing messages\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg) {
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	log_set_use_color(conn->dbg, atoi(argv[0]));
	return CMD_SUCCESS;
}

DEFUN(logging_prnt_timestamp,
      logging_prnt_timestamp_cmd,
      "logging timestamp <0-1>",
	LOGGING_STR
      "Print the timestamp of each message\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg) {
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	log_set_print_timestamp(conn->dbg, atoi(argv[0]));
	return CMD_SUCCESS;
}

/* FIXME: those have to be kept in sync with the log levels and categories */
#define VTY_DEBUG_CATEGORIES "(rll|cc|mm|rr|rsl|nm|sms|pag|mncc|inp|mi|mib|mux|meas|sccp|msc|mgcp|ho|db|ref|gprs|ns|bssgp|all)"
#define CATEGORIES_HELP	\
	"A-bis Radio Link Layer (RLL)\n"			\
	"Layer3 Call Control (CC)\n"				\
	"Layer3 Mobility Management (MM)\n"			\
	"Layer3 Radio Resource (RR)\n"				\
	"A-bis Radio Signalling Link (RSL)\n"			\
	"A-bis Network Management / O&M (NM/OML)\n"		\
	"Layer3 Short Messagaging Service (SMS)\n"		\
	"Paging Subsystem\n"					\
	"MNCC API for Call Control application\n"		\
	"A-bis Input Subsystem\n"				\
	"A-bis Input Driver for Signalling\n"			\
	"A-bis Input Driver for B-Channel (voice data)\n"	\
	"A-bis B-Channel / Sub-channel Multiplexer\n"		\
	"Radio Measurements\n"					\
	"SCCP\n"						\
	"Mobile Switching Center\n"				\
	"Media Gateway Control Protocol\n"			\
	"Hand-over\n"						\
	"Database Layer\n"					\
	"Reference Counting\n"					\
	"GPRS Core\n"						\
	"GPRS Network Service (NS)\n"				\
	"GPRS BSS Gateway Protocol (BSSGP)\n"			\
	"Global setting for all subsytems\n"

#define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)"
#define LEVELS_HELP	\
	"Log simply everything\n"				\
	"Log debug messages and higher levels\n"		\
	"Log informational messages and higher levels\n"	\
	"Log noticable messages and higher levels\n"		\
	"Log error messages and higher levels\n"		\
	"Log only fatal messages\n"
DEFUN(logging_level,
      logging_level_cmd,
      "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
      LOGGING_STR
      "Set the log level for a specified category\n"
      CATEGORIES_HELP
      LEVELS_HELP)
{
	struct telnet_connection *conn;
	int category = log_parse_category(argv[0]);
	int level = log_parse_level(argv[1]);

	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg) {
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	if (level < 0) {
		vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
		return CMD_WARNING;
	}

	/* Check for special case where we want to set global log level */
	if (!strcmp(argv[0], "all")) {
		log_set_log_level(conn->dbg, level);
		return CMD_SUCCESS;
	}

	if (category < 0) {
		vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
		return CMD_WARNING;
	}

	conn->dbg->categories[category].enabled = 1;
	conn->dbg->categories[category].loglevel = level;

	return CMD_SUCCESS;
}

DEFUN(logging_set_category_mask,
      logging_set_category_mask_cmd,
      "logging set log mask MASK",
	LOGGING_STR
      "Decide which categories to output.\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg) {
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	log_parse_category_mask(conn->dbg, argv[0]);
	return CMD_SUCCESS;
}

DEFUN(diable_logging,
      disable_logging_cmd,
      "logging disable",
	LOGGING_STR
      "Disables logging to this vty\n")
{
	struct telnet_connection *conn;

	conn = (struct telnet_connection *) vty->priv;
	if (!conn->dbg) {
		vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
		return CMD_WARNING;
	}

	log_del_target(conn->dbg);
	talloc_free(conn->dbg);
	conn->dbg = NULL;
	return CMD_SUCCESS;
}

void openbsc_vty_print_statistics(struct vty *vty, struct gsm_network *net)
{
	vty_out(vty, "Channel Requests        : %lu total, %lu no channel%s",
		counter_get(net->stats.chreq.total),
		counter_get(net->stats.chreq.no_channel), VTY_NEWLINE);
	vty_out(vty, "Channel Failures        : %lu rf_failures, %lu rll failures%s",
		counter_get(net->stats.chan.rf_fail),
		counter_get(net->stats.chan.rll_err), VTY_NEWLINE);
	vty_out(vty, "Paging                  : %lu attempted, %lu complete, %lu expired%s",
		counter_get(net->stats.paging.attempted),
		counter_get(net->stats.paging.completed),
		counter_get(net->stats.paging.expired), VTY_NEWLINE);
	vty_out(vty, "BTS failures            : %lu OML, %lu RSL%s",
		counter_get(net->stats.bts.oml_fail),
		counter_get(net->stats.bts.rsl_fail), VTY_NEWLINE);
}

void openbsc_vty_add_cmds()
{
	install_element(VIEW_NODE, &enable_logging_cmd);
	install_element(VIEW_NODE, &disable_logging_cmd);
	install_element(VIEW_NODE, &logging_fltr_imsi_cmd);
	install_element(VIEW_NODE, &logging_fltr_all_cmd);
	install_element(VIEW_NODE, &logging_use_clr_cmd);
	install_element(VIEW_NODE, &logging_prnt_timestamp_cmd);
	install_element(VIEW_NODE, &logging_set_category_mask_cmd);
	install_element(VIEW_NODE, &logging_level_cmd);

}