]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib,vtysh: Add vtysh commands for log-filter
authorStephen Worley <sworley@cumulusnetworks.com>
Thu, 13 Jun 2019 21:46:29 +0000 (17:46 -0400)
committerStephen Worley <sworley@cumulusnetworks.com>
Wed, 19 Jun 2019 21:20:24 +0000 (17:20 -0400)
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 <sworley@cumulusnetworks.com>
lib/command.h
lib/libfrr.c
lib/log_vty.c [new file with mode: 0644]
lib/log_vty.h [new file with mode: 0644]
lib/subdir.am
vtysh/vtysh.c

index e02f1134bd3b4fec4a215767ec986540a39464cc..95be7ccb6ec75c3592512166021cdf66290d0e76 100644 (file)
@@ -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"
index ed784fc73a18a0c52575a2d0724475d2754d5471..3294a612952f2971b567e4708080c95cad0a876c 100644 (file)
@@ -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 (file)
index 0000000..a3fa087
--- /dev/null
@@ -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 <zebra.h>
+
+#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 (file)
index 0000000..fa5627e
--- /dev/null
@@ -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__ */
index 8b6cbe2aeb2f8b93d06c249a99da329efcac7194..3750d3620fce4d5aed66f8fa09107fabd3b25a7e 100644 (file)
@@ -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 \
index 56b366f082dcfe3ce79beffc302727ba6e6dc011..804dc38b14e0c4d1a1acc52ddcfddf5f630e7b82 100644 (file)
@@ -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);