From f73126c31acdd496dcc88c69059a5e0103d6a2ff Mon Sep 17 00:00:00 2001 From: Stephen Worley Date: Thu, 13 Jun 2019 17:46:29 -0400 Subject: [PATCH] lib,vtysh: Add vtysh commands for log-filter Add vtysh commands to add/del/clear/show filters across all daemons and independently on each one. Add automake and clippy boilerplate for those commands as well. Signed-off-by: Stephen Worley --- lib/command.h | 1 + lib/libfrr.c | 2 + lib/log_vty.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/log_vty.h | 24 ++++++++++++ lib/subdir.am | 4 ++ vtysh/vtysh.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 230 insertions(+) create mode 100644 lib/log_vty.c create mode 100644 lib/log_vty.h diff --git a/lib/command.h b/lib/command.h index e02f1134bd..95be7ccb6e 100644 --- a/lib/command.h +++ b/lib/command.h @@ -397,6 +397,7 @@ struct cmd_node { #define SR_STR "Segment-Routing specific commands\n" #define WATCHFRR_STR "watchfrr information\n" #define ZEBRA_STR "Zebra information\n" +#define FILTER_LOG_STR "Filter Logs\n" #define CMD_VNI_RANGE "(1-16777215)" #define CONF_BACKUP_EXT ".sav" diff --git a/lib/libfrr.c b/lib/libfrr.c index ed784fc73a..3294a61295 100644 --- a/lib/libfrr.c +++ b/lib/libfrr.c @@ -31,6 +31,7 @@ #include "command.h" #include "version.h" #include "memory_vty.h" +#include "log_vty.h" #include "zclient.h" #include "log_int.h" #include "module.h" @@ -677,6 +678,7 @@ struct thread_master *frr_init(void) vty_init(master, di->log_always); memory_init(); + log_filter_cmd_init(); log_ref_init(); lib_error_init(); diff --git a/lib/log_vty.c b/lib/log_vty.c new file mode 100644 index 0000000000..a3fa08711e --- /dev/null +++ b/lib/log_vty.c @@ -0,0 +1,99 @@ +/* + * Logging - VTY code + * Copyright (C) 2019 Cumulus Networks, Inc. + * Stephen Worley + * + * 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; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +#include "lib/log_vty.h" +#include "command.h" +#include "lib/vty.h" +#include "lib/log.h" +#ifndef VTYSH_EXTRACT_PL +#include "lib/log_vty_clippy.c" +#endif + +/* Log filter */ +DEFPY (log_filter, + log_filter_cmd, + "[no] log-filter WORD$filter", + NO_STR + FILTER_LOG_STR + "String to filter by\n") +{ + int ret = 0; + + if (no) + ret = zlog_filter_del(filter); + else + ret = zlog_filter_add(filter); + + if (ret == 1) { + vty_out(vty, "\tfilter table full\n"); + return CMD_WARNING; + } else if (ret != 0) { + vty_out(vty, "\tfailed to %s log filter\n", + (no ? "remove" : "apply")); + return CMD_WARNING; + } + + vty_out(vty, "\t%s\n", filter); + return CMD_SUCCESS; +} + +/* Clear all log filters */ +DEFPY (log_filter_clear, + log_filter_clear_cmd, + "clear log-filter", + CLEAR_STR + FILTER_LOG_STR) +{ + zlog_filter_clear(); + vty_out(vty, "\tcleared all filters\n"); + return CMD_SUCCESS; +} + +/* Show log filter */ +DEFPY (show_log_filter, + show_log_filter_cmd, + "show log-filter", + SHOW_STR + FILTER_LOG_STR) +{ + char log_filters[ZLOG_FILTERS_MAX * (ZLOG_FILTER_LENGTH_MAX + 3)] = ""; + int len = 0; + + len = zlog_filter_dump(log_filters, sizeof(log_filters)); + + if (len == -1) { + vty_out(vty, "\tfailed to get filters\n"); + return CMD_WARNING; + } + + if (len != 0) + vty_out(vty, "%s", log_filters); + + return CMD_SUCCESS; +} + +void log_filter_cmd_init(void) +{ + install_element(VIEW_NODE, &show_log_filter_cmd); + install_element(CONFIG_NODE, &log_filter_cmd); + install_element(CONFIG_NODE, &log_filter_clear_cmd); +} diff --git a/lib/log_vty.h b/lib/log_vty.h new file mode 100644 index 0000000000..fa5627e4bd --- /dev/null +++ b/lib/log_vty.h @@ -0,0 +1,24 @@ +/* + * Logging - VTY library + * Copyright (C) 2019 Cumulus Networks, Inc. + * Stephen Worley + * + * 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; see the file COPYING; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LOG_VTY_H__ +#define __LOG_VTY_H__ +extern void log_filter_cmd_init(void); +#endif /* __LOG_VTY_H__ */ diff --git a/lib/subdir.am b/lib/subdir.am index 8b6cbe2aeb..3750d3620f 100644 --- a/lib/subdir.am +++ b/lib/subdir.am @@ -42,6 +42,7 @@ lib_libfrr_la_SOURCES = \ lib/libfrr.c \ lib/linklist.c \ lib/log.c \ + lib/log_vty.c \ lib/md5.c \ lib/memory.c \ lib/memory_vty.c \ @@ -137,6 +138,8 @@ lib/northbound_cli_clippy.c: $(CLIPPY_DEPS) lib/northbound_cli.lo: lib/northbound_cli_clippy.c lib/vty_clippy.c: $(CLIPPY_DEPS) lib/vty.lo: lib/vty_clippy.c +lib/log_vty_clippy.c: $(CLIPPY_DEPS) +lib/log_vty.lo: lib/log_vty_clippy.c pkginclude_HEADERS += \ lib/agg_table.h \ @@ -179,6 +182,7 @@ pkginclude_HEADERS += \ lib/libospf.h \ lib/linklist.h \ lib/log.h \ + lib/log_vty.h \ lib/md5.h \ lib/memory.h \ lib/memory_vty.h \ diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 56b366f082..804dc38b14 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -2641,6 +2641,103 @@ DEFUNSH(VTYSH_ALL, no_vtysh_config_enable_password, return CMD_SUCCESS; } +/* Log filter */ +DEFUN (vtysh_log_filter, + vtysh_log_filter_cmd, + "[no] log-filter WORD ["DAEMONS_LIST"]", + NO_STR + FILTER_LOG_STR + "String to filter by\n" + DAEMONS_STR) +{ + char *filter = NULL; + char *daemon = NULL; + int found = 0; + int idx = 0; + int daemon_idx = 2; + int total_len = 0; + int len = 0; + + char line[ZLOG_FILTER_LENGTH_MAX + 20]; + + found = argv_find(argv, argc, "no", &idx); + if (found == 1) { + len = snprintf(line, sizeof(line), "no log-filter"); + daemon_idx += 1; + } else + len = snprintf(line, sizeof(line), "log-filter"); + + total_len += len; + + idx = 1; + found = argv_find(argv, argc, "WORD", &idx); + if (found != 1) { + vty_out(vty, "No filter string given\n"); + return CMD_WARNING; + } + filter = argv[idx]->arg; + + if (strnlen(filter, ZLOG_FILTER_LENGTH_MAX + 1) + > ZLOG_FILTER_LENGTH_MAX) { + vty_out(vty, "Filter is too long\n"); + return CMD_WARNING; + } + + len = snprintf(line + total_len, sizeof(line) - total_len, " %s\n", + filter); + + if ((len < 0) || (size_t)(total_len + len) > sizeof(line)) { + vty_out(vty, "Error buffering filter to daemons\n"); + return CMD_ERR_INCOMPLETE; + } + + if (argc >= (daemon_idx + 1)) + daemon = argv[daemon_idx]->text; + + if (daemon != NULL) { + vty_out(vty, "Applying log filter change to %s:\n", daemon); + return vtysh_client_execute_name(daemon, line); + } else + return show_per_daemon(line, + "Applying log filter change to %s:\n"); +} + +/* Clear log filters */ +DEFUN (vtysh_log_filter_clear, + vtysh_log_filter_clear_cmd, + "log-filter clear ["DAEMONS_LIST"]", + FILTER_LOG_STR + CLEAR_STR + DAEMONS_STR) +{ + char *daemon = NULL; + int daemon_idx = 2; + + char line[] = "clear log-filter\n"; + + if (argc >= (daemon_idx + 1)) + daemon = argv[daemon_idx]->text; + + if (daemon != NULL) { + vty_out(vty, "Clearing all filters applied to %s:\n", daemon); + return vtysh_client_execute_name(daemon, line); + } else + return show_per_daemon(line, + "Clearing all filters applied to %s:\n"); +} + +/* Show log filter */ +DEFUN (vtysh_show_log_filter, + vtysh_show_log_filter_cmd, + "show log-filter", + SHOW_STR + FILTER_LOG_STR) +{ + char line[] = "do show log-filter\n"; + + return show_per_daemon(line, "Log filters applied to %s:\n"); +} + DEFUN (vtysh_write_terminal, vtysh_write_terminal_cmd, "write terminal ["DAEMONS_LIST"]", @@ -3865,6 +3962,9 @@ void vtysh_init_vty(void) /* Logging */ install_element(VIEW_NODE, &vtysh_show_logging_cmd); + install_element(VIEW_NODE, &vtysh_show_log_filter_cmd); + install_element(CONFIG_NODE, &vtysh_log_filter_cmd); + install_element(CONFIG_NODE, &vtysh_log_filter_clear_cmd); install_element(CONFIG_NODE, &vtysh_log_stdout_cmd); install_element(CONFIG_NODE, &vtysh_log_stdout_level_cmd); install_element(CONFIG_NODE, &no_vtysh_log_stdout_cmd); -- 2.39.5