blob: 619caf56d61f20dceedb972e123d81087acbed8d [file] [log] [blame]
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +02001/* OpenBSC logging helper for the VTY */
2/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2009-2010 by Holger Hans Peter Freyther
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
Harald Weltedcccb182010-05-16 20:52:23 +020022#include <stdlib.h>
23#include <string.h>
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +020024
25#include <osmocore/talloc.h>
26
Harald Weltedcccb182010-05-16 20:52:23 +020027#include <openbsc/vty.h>
28#include <openbsc/telnet_interface.h>
29#include <openbsc/gsm_data.h>
30#include <openbsc/debug.h>
31
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +020032#include <vty/command.h>
33#include <vty/buffer.h>
34#include <vty/vty.h>
35
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +020036static void _vty_output(struct log_target *tgt, const char *line)
37{
38 struct vty *vty = tgt->tgt_vty.vty;
39 vty_out(vty, "%s", line);
40 /* This is an ugly hack, but there is no easy way... */
41 if (strchr(line, '\n'))
42 vty_out(vty, "\r");
43}
44
45struct log_target *log_target_create_vty(struct vty *vty)
46{
47 struct log_target *target;
48
49 target = log_target_create();
50 if (!target)
51 return NULL;
52
53 target->tgt_vty.vty = vty;
54 target->output = _vty_output;
55 return target;
56}
57
Harald Welte62ab20c2010-05-14 18:59:17 +020058/* Down vty node level. */
59gDEFUN(ournode_exit,
60 ournode_exit_cmd, "exit", "Exit current mode and down to previous mode\n")
61{
62 switch (vty->node) {
63 case GSMNET_NODE:
64 vty->node = CONFIG_NODE;
65 vty->index = NULL;
66 break;
67 case BTS_NODE:
68 vty->node = GSMNET_NODE;
69 {
70 /* set vty->index correctly ! */
71 struct gsm_bts *bts = vty->index;
72 vty->index = bts->network;
73 vty->index_sub = NULL;
74 }
75 break;
76 case TRX_NODE:
77 vty->node = BTS_NODE;
78 {
79 /* set vty->index correctly ! */
80 struct gsm_bts_trx *trx = vty->index;
81 vty->index = trx->bts;
82 vty->index_sub = &trx->bts->description;
83 }
84 break;
85 case TS_NODE:
86 vty->node = TRX_NODE;
87 {
88 /* set vty->index correctly ! */
89 struct gsm_bts_trx_ts *ts = vty->index;
90 vty->index = ts->trx;
91 vty->index_sub = &ts->trx->description;
92 }
93 break;
94 case MGCP_NODE:
95 case GBPROXY_NODE:
96 case SGSN_NODE:
97 case NS_NODE:
98 vty->node = CONFIG_NODE;
99 vty->index = NULL;
100 break;
101 default:
102 break;
103 }
104 return CMD_SUCCESS;
105}
106
Harald Welte54f74242010-05-14 19:11:04 +0200107/* End of configuration. */
108gDEFUN(ournode_end,
109 ournode_end_cmd, "end", "End current mode and change to enable mode.")
110{
111 switch (vty->node) {
112 case VIEW_NODE:
113 case ENABLE_NODE:
114 /* Nothing to do. */
115 break;
116 case CONFIG_NODE:
117 case GSMNET_NODE:
118 case BTS_NODE:
119 case TRX_NODE:
120 case TS_NODE:
121 case MGCP_NODE:
122 case GBPROXY_NODE:
123 case SGSN_NODE:
124 case NS_NODE:
125 case VTY_NODE:
126 vty_config_unlock(vty);
127 vty->node = ENABLE_NODE;
128 vty->index = NULL;
129 vty->index_sub = NULL;
130 break;
131 default:
132 break;
133 }
134 return CMD_SUCCESS;
135}
136
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200137DEFUN(enable_logging,
138 enable_logging_cmd,
139 "logging enable",
Harald Weltecd7c1be2010-05-11 10:41:34 +0200140 LOGGING_STR
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200141 "Enables logging to this vty\n")
142{
143 struct telnet_connection *conn;
144
145 conn = (struct telnet_connection *) vty->priv;
146 if (conn->dbg) {
147 vty_out(vty, "Logging already enabled.%s", VTY_NEWLINE);
148 return CMD_WARNING;
149 }
150
151 conn->dbg = log_target_create_vty(vty);
152 if (!conn->dbg)
153 return CMD_WARNING;
154
155 log_add_target(conn->dbg);
156 return CMD_SUCCESS;
157}
158
159DEFUN(logging_fltr_imsi,
160 logging_fltr_imsi_cmd,
161 "logging filter imsi IMSI",
Harald Welte95647152010-05-16 00:00:04 +0200162 LOGGING_STR FILTER_STR
163 "Filter log messages by IMSI\n" "IMSI to be used as filter\n")
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200164{
165 struct telnet_connection *conn;
166
167 conn = (struct telnet_connection *) vty->priv;
168 if (!conn->dbg) {
169 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
170 return CMD_WARNING;
171 }
172
173 log_set_imsi_filter(conn->dbg, argv[0]);
174 return CMD_SUCCESS;
175}
176
177DEFUN(logging_fltr_all,
178 logging_fltr_all_cmd,
Harald Welte95647152010-05-16 00:00:04 +0200179 "logging filter all (0|1)",
180 LOGGING_STR FILTER_STR
181 "Do you want to log all messages?\n"
182 "Only print messages matched by other filters\n"
183 "Bypass filter and print all messages\n")
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200184{
185 struct telnet_connection *conn;
186
187 conn = (struct telnet_connection *) vty->priv;
188 if (!conn->dbg) {
189 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
190 return CMD_WARNING;
191 }
192
193 log_set_all_filter(conn->dbg, atoi(argv[0]));
194 return CMD_SUCCESS;
195}
196
197DEFUN(logging_use_clr,
198 logging_use_clr_cmd,
Harald Welte95647152010-05-16 00:00:04 +0200199 "logging color (0|1)",
200 LOGGING_STR "Configure color-printing for log messages\n"
201 "Don't use color for printing messages\n"
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200202 "Use color for printing messages\n")
203{
204 struct telnet_connection *conn;
205
206 conn = (struct telnet_connection *) vty->priv;
207 if (!conn->dbg) {
208 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
209 return CMD_WARNING;
210 }
211
212 log_set_use_color(conn->dbg, atoi(argv[0]));
213 return CMD_SUCCESS;
214}
215
216DEFUN(logging_prnt_timestamp,
217 logging_prnt_timestamp_cmd,
Harald Welte95647152010-05-16 00:00:04 +0200218 "logging timestamp (0|1)",
219 LOGGING_STR "Configure log message timestamping\n"
220 "Don't prefix each log message\n"
221 "Prefix each log message with current timestamp\n")
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200222{
223 struct telnet_connection *conn;
224
225 conn = (struct telnet_connection *) vty->priv;
226 if (!conn->dbg) {
227 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
228 return CMD_WARNING;
229 }
230
231 log_set_print_timestamp(conn->dbg, atoi(argv[0]));
232 return CMD_SUCCESS;
233}
234
235/* FIXME: those have to be kept in sync with the log levels and categories */
Harald Welteb2e49182010-05-13 19:47:01 +0200236#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|llc|sndcp|all)"
Harald Weltecd7c1be2010-05-11 10:41:34 +0200237#define CATEGORIES_HELP \
238 "A-bis Radio Link Layer (RLL)\n" \
239 "Layer3 Call Control (CC)\n" \
240 "Layer3 Mobility Management (MM)\n" \
241 "Layer3 Radio Resource (RR)\n" \
242 "A-bis Radio Signalling Link (RSL)\n" \
243 "A-bis Network Management / O&M (NM/OML)\n" \
244 "Layer3 Short Messagaging Service (SMS)\n" \
245 "Paging Subsystem\n" \
246 "MNCC API for Call Control application\n" \
247 "A-bis Input Subsystem\n" \
248 "A-bis Input Driver for Signalling\n" \
249 "A-bis Input Driver for B-Channel (voice data)\n" \
250 "A-bis B-Channel / Sub-channel Multiplexer\n" \
251 "Radio Measurements\n" \
252 "SCCP\n" \
253 "Mobile Switching Center\n" \
254 "Media Gateway Control Protocol\n" \
255 "Hand-over\n" \
256 "Database Layer\n" \
257 "Reference Counting\n" \
258 "GPRS Core\n" \
259 "GPRS Network Service (NS)\n" \
260 "GPRS BSS Gateway Protocol (BSSGP)\n" \
Harald Welteb2e49182010-05-13 19:47:01 +0200261 "GPRS Logical Link Control Protocol (LLC)\n" \
262 "GPRS Sub-Network Dependent Control Protocol (SNDCP)\n" \
Harald Weltecd7c1be2010-05-11 10:41:34 +0200263 "Global setting for all subsytems\n"
264
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200265#define VTY_DEBUG_LEVELS "(everything|debug|info|notice|error|fatal)"
Harald Weltecd7c1be2010-05-11 10:41:34 +0200266#define LEVELS_HELP \
267 "Log simply everything\n" \
268 "Log debug messages and higher levels\n" \
269 "Log informational messages and higher levels\n" \
270 "Log noticable messages and higher levels\n" \
271 "Log error messages and higher levels\n" \
272 "Log only fatal messages\n"
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200273DEFUN(logging_level,
274 logging_level_cmd,
275 "logging level " VTY_DEBUG_CATEGORIES " " VTY_DEBUG_LEVELS,
Harald Weltecd7c1be2010-05-11 10:41:34 +0200276 LOGGING_STR
277 "Set the log level for a specified category\n"
278 CATEGORIES_HELP
279 LEVELS_HELP)
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200280{
281 struct telnet_connection *conn;
282 int category = log_parse_category(argv[0]);
283 int level = log_parse_level(argv[1]);
284
285 conn = (struct telnet_connection *) vty->priv;
286 if (!conn->dbg) {
287 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
288 return CMD_WARNING;
289 }
290
Harald Welte6fab2362010-05-11 10:21:45 +0200291 if (level < 0) {
292 vty_out(vty, "Invalid level `%s'%s", argv[1], VTY_NEWLINE);
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200293 return CMD_WARNING;
294 }
295
Harald Welte6fab2362010-05-11 10:21:45 +0200296 /* Check for special case where we want to set global log level */
297 if (!strcmp(argv[0], "all")) {
298 log_set_log_level(conn->dbg, level);
299 return CMD_SUCCESS;
300 }
301
302 if (category < 0) {
303 vty_out(vty, "Invalid category `%s'%s", argv[0], VTY_NEWLINE);
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200304 return CMD_WARNING;
305 }
306
307 conn->dbg->categories[category].enabled = 1;
308 conn->dbg->categories[category].loglevel = level;
309
310 return CMD_SUCCESS;
311}
312
313DEFUN(logging_set_category_mask,
314 logging_set_category_mask_cmd,
315 "logging set log mask MASK",
Harald Weltecd7c1be2010-05-11 10:41:34 +0200316 LOGGING_STR
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200317 "Decide which categories to output.\n")
318{
319 struct telnet_connection *conn;
320
321 conn = (struct telnet_connection *) vty->priv;
322 if (!conn->dbg) {
323 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
324 return CMD_WARNING;
325 }
326
327 log_parse_category_mask(conn->dbg, argv[0]);
328 return CMD_SUCCESS;
329}
330
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200331DEFUN(diable_logging,
332 disable_logging_cmd,
333 "logging disable",
Harald Weltecd7c1be2010-05-11 10:41:34 +0200334 LOGGING_STR
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200335 "Disables logging to this vty\n")
336{
337 struct telnet_connection *conn;
338
339 conn = (struct telnet_connection *) vty->priv;
340 if (!conn->dbg) {
341 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
342 return CMD_WARNING;
343 }
344
345 log_del_target(conn->dbg);
346 talloc_free(conn->dbg);
347 conn->dbg = NULL;
348 return CMD_SUCCESS;
349}
350
Harald Welte6f656962010-05-11 11:12:37 +0200351static void vty_print_logtarget(struct vty *vty, const struct log_info *info,
352 const struct log_target *tgt)
353{
354 unsigned int i;
355
356 vty_out(vty, " Global Loglevel: %s%s",
357 log_level_str(tgt->loglevel), VTY_NEWLINE);
358 vty_out(vty, " Use color: %s, Print Timestamp: %s%s",
359 tgt->use_color ? "On" : "Off",
360 tgt->print_timestamp ? "On" : "Off", VTY_NEWLINE);
361
362 vty_out(vty, " Log Level specific information:%s", VTY_NEWLINE);
363
364 for (i = 0; i < info->num_cat; i++) {
365 const struct log_category *cat = &tgt->categories[i];
366 vty_out(vty, " %-10s %-10s %-8s %s%s",
367 info->cat[i].name+1, log_level_str(cat->loglevel),
368 cat->enabled ? "Enabled" : "Disabled",
369 info->cat[i].description,
370 VTY_NEWLINE);
371 }
372}
373
374#define SHOW_LOG_STR "Show current logging configuration\n"
375
376DEFUN(show_logging_vty,
377 show_logging_vty_cmd,
378 "show logging vty",
379 SHOW_STR SHOW_LOG_STR
380 "Show current logging configuration for this vty\n")
381{
382 struct telnet_connection *conn;
383
384 conn = (struct telnet_connection *) vty->priv;
385 if (!conn->dbg) {
386 vty_out(vty, "Logging was not enabled.%s", VTY_NEWLINE);
387 return CMD_WARNING;
388 }
389 vty_print_logtarget(vty, &log_info, conn->dbg);
390
391 return CMD_SUCCESS;
392}
393
Harald Welte197dea92010-05-14 17:59:53 +0200394gDEFUN(cfg_description, cfg_description_cmd,
395 "description .TEXT",
396 "Save human-readable decription of the object\n")
397{
398 char **dptr = vty->index_sub;
Harald Welte197dea92010-05-14 17:59:53 +0200399
400 if (!dptr) {
401 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
402 return CMD_WARNING;
403 }
404
Harald Welte20474ad2010-05-16 19:28:32 +0200405 *dptr = argv_concat(argv, argc, 0);
406 if (!dptr)
Harald Welte197dea92010-05-14 17:59:53 +0200407 return CMD_WARNING;
408
Harald Welte197dea92010-05-14 17:59:53 +0200409 return CMD_SUCCESS;
410}
411
412gDEFUN(cfg_no_description, cfg_no_description_cmd,
413 "no description",
414 NO_STR
415 "Remove description of the object\n")
416{
417 char **dptr = vty->index_sub;
418
419 if (!dptr) {
420 vty_out(vty, "vty->index_sub == NULL%s", VTY_NEWLINE);
421 return CMD_WARNING;
422 }
423
424 if (*dptr) {
425 talloc_free(*dptr);
426 *dptr = NULL;
427 }
428
429 return CMD_SUCCESS;
430}
431
Holger Hans Peter Freythere0ec3262010-04-15 11:28:14 +0200432void openbsc_vty_print_statistics(struct vty *vty, struct gsm_network *net)
433{
434 vty_out(vty, "Channel Requests : %lu total, %lu no channel%s",
435 counter_get(net->stats.chreq.total),
436 counter_get(net->stats.chreq.no_channel), VTY_NEWLINE);
Holger Hans Peter Freyther3ba36d52010-04-17 06:48:29 +0200437 vty_out(vty, "Channel Failures : %lu rf_failures, %lu rll failures%s",
438 counter_get(net->stats.chan.rf_fail),
439 counter_get(net->stats.chan.rll_err), VTY_NEWLINE);
Holger Hans Peter Freythere0ec3262010-04-15 11:28:14 +0200440 vty_out(vty, "Paging : %lu attempted, %lu complete, %lu expired%s",
441 counter_get(net->stats.paging.attempted),
442 counter_get(net->stats.paging.completed),
443 counter_get(net->stats.paging.expired), VTY_NEWLINE);
Holger Hans Peter Freytherbb110f92010-04-12 10:45:52 +0200444 vty_out(vty, "BTS failures : %lu OML, %lu RSL%s",
445 counter_get(net->stats.bts.oml_fail),
446 counter_get(net->stats.bts.rsl_fail), VTY_NEWLINE);
Holger Hans Peter Freythere0ec3262010-04-15 11:28:14 +0200447}
448
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200449void openbsc_vty_add_cmds()
450{
Harald Welteb4d5b172010-05-12 16:10:35 +0000451 install_element_ve(&enable_logging_cmd);
452 install_element_ve(&disable_logging_cmd);
453 install_element_ve(&logging_fltr_imsi_cmd);
454 install_element_ve(&logging_fltr_all_cmd);
455 install_element_ve(&logging_use_clr_cmd);
456 install_element_ve(&logging_prnt_timestamp_cmd);
457 install_element_ve(&logging_set_category_mask_cmd);
458 install_element_ve(&logging_level_cmd);
459 install_element_ve(&show_logging_vty_cmd);
Holger Hans Peter Freyther3c712322010-04-06 11:55:37 +0200460}