Change in osmo-sysmon[master]: Add vty option to print output of shell cmd on every poll step

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Mon Mar 18 16:27:02 UTC 2019


Pau Espin Pedrol has uploaded this change for review. ( https://gerrit.osmocom.org/13304


Change subject: Add vty option to print output of shell cmd on every poll step
......................................................................

Add vty option to print output of shell cmd on every poll step

Change-Id: I005773b75f81fa5f6c90f53af508fc6debea208b
---
M doc/examples/osmo-sysmon.cfg
M src/Makefile.am
M src/osysmon.h
A src/osysmon_cmd.c
M src/osysmon_main.c
5 files changed, 178 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-sysmon refs/changes/04/13304/1

diff --git a/doc/examples/osmo-sysmon.cfg b/doc/examples/osmo-sysmon.cfg
index f944f72..fb40589 100644
--- a/doc/examples/osmo-sysmon.cfg
+++ b/doc/examples/osmo-sysmon.cfg
@@ -14,3 +14,4 @@
 ping example.com
 openvpn 127.0.0.1 1234
 file os-image /etc/image-datetime
+cmd kernel uname -a
diff --git a/src/Makefile.am b/src/Makefile.am
index f9b79f2..0f324c4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -36,6 +36,7 @@
 
 osmo_sysmon_SOURCES = \
 	value_node.c \
+	osysmon_cmd.c \
 	osysmon_ctrl.c \
 	osysmon_sysinfo.c \
 	osysmon_rtnl.c \
diff --git a/src/osysmon.h b/src/osysmon.h
index 2f82c47..4fba86c 100644
--- a/src/osysmon.h
+++ b/src/osysmon.h
@@ -13,6 +13,8 @@
 
 struct osysmon_state {
 	struct rtnl_client_state *rcs;
+	/* list of 'struct osysmon_cmd' */
+	struct llist_head cmds;
 	/* list of 'struct ctrl client' */
 	struct llist_head ctrl_clients;
 	/* list of 'struct openvpn_client' */
@@ -21,6 +23,7 @@
 	struct llist_head netdevs;
 	/* list of 'struct osysmon_file' */
 	struct llist_head files;
+	/* list of ping contexts */
 	struct ping_state *pings;
 };
 
@@ -36,6 +39,8 @@
 	PING_NODE,
 };
 
+int osysmon_cmd_init();
+int osysmon_cmd_poll(struct value_node *parent);
 
 int osysmon_ctrl_go_parent(struct vty *vty);
 int osysmon_ctrl_init();
diff --git a/src/osysmon_cmd.c b/src/osysmon_cmd.c
new file mode 100644
index 0000000..d26bf02
--- /dev/null
+++ b/src/osysmon_cmd.c
@@ -0,0 +1,168 @@
+/* Simple Osmocom System Monitor (osysmon): Support for monitoring through shell commands */
+
+/* (C) 2019 by sysmocom - s.f.m.c. GmbH <info at sysmocom.de>
+ * All Rights Reserved.
+ * Author: Pau Espin Pedrol <pespin at sysmocom.de>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ *  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 <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/sysinfo.h>
+
+#include <osmocom/vty/vty.h>
+#include <osmocom/vty/command.h>
+
+#include "osysmon.h"
+#include "value_node.h"
+
+/***********************************************************************
+ * Data model
+ ***********************************************************************/
+
+struct osysmon_cmd {
+	struct llist_head list;
+	struct {
+		const char *name;
+		const char *cmd;
+	} cfg;
+};
+
+static struct osysmon_cmd *osysmon_cmd_find(const char *name)
+{
+	struct osysmon_cmd *oc;
+
+	llist_for_each_entry(oc, &g_oss->cmds, list) {
+		if (!strcmp(oc->cfg.name, name))
+			return oc;
+	}
+	return NULL;
+}
+
+static struct osysmon_cmd *osysmon_cmd_add(const char *name, const char *cmd)
+{
+	struct osysmon_cmd *oc;
+
+	if (osysmon_cmd_find(name))
+		return NULL;
+
+	oc = talloc_zero(g_oss, struct osysmon_cmd);
+	OSMO_ASSERT(oc);
+	oc->cfg.name = talloc_strdup(oc, name);
+	oc->cfg.cmd = talloc_strdup(oc, cmd);
+	llist_add_tail(&oc->list, &g_oss->cmds);
+	return oc;
+}
+
+static void osysmon_cmd_destroy(struct osysmon_cmd *oc)
+{
+	llist_del(&oc->list);
+	talloc_free(oc);
+}
+
+static void osysmon_cmd_run(struct osysmon_cmd *oc, struct value_node *parent)
+{
+	char buf[512];
+	FILE *f;
+	long offset = 0;
+
+	f = popen(oc->cfg.cmd, "r");
+	if (!f) {
+		snprintf(buf, sizeof(buf), "<popen failed(%d)>", errno);
+		value_node_add(parent, oc->cfg.name, buf);
+		return;
+	}
+
+	while (fgets(buf + offset, sizeof(buf) - offset, f) != NULL)
+		offset = ftell(f);
+
+	if (offset == 0)
+		value_node_add(parent, oc->cfg.name, "<EMPTY>");
+
+	pclose(f);
+
+	value_node_add(parent, oc->cfg.name, buf);
+}
+
+/***********************************************************************
+ * VTY
+ ***********************************************************************/
+
+#define CMD_STR "Configure a command to be executed\n"
+DEFUN(cfg_cmd, cfg_cmd_cmd,
+	"cmd NAME .TEXT",
+	CMD_STR "Name of this command snippet\n" "Command to run\n")
+{
+	struct osysmon_cmd *oc;
+	char *concat = argv_concat(argv, argc, 1);
+	oc = osysmon_cmd_add(argv[0], concat);
+	talloc_free(concat);
+	if (!oc) {
+		vty_out(vty, "Couldn't add cmd, maybe it exists?%s", VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+	return CMD_SUCCESS;
+}
+
+DEFUN(cfg_no_cmd, cfg_no_cmd_cmd,
+	"no cmd NAME",
+	NO_STR CMD_STR "Name of this command snippet\n")
+{
+	struct osysmon_cmd *oc;
+	oc = osysmon_cmd_find(argv[0]);
+	if (!oc) {
+		vty_out(vty, "Cannot find cmd for '%s'%s", argv[0], VTY_NEWLINE);
+		return CMD_WARNING;
+	}
+	osysmon_cmd_destroy(oc);
+	return CMD_SUCCESS;
+}
+
+
+static void osysmon_cmd_vty_init(void)
+{
+	install_element(CONFIG_NODE, &cfg_cmd_cmd);
+	install_element(CONFIG_NODE, &cfg_no_cmd_cmd);
+}
+
+/***********************************************************************
+ * Runtime Code
+ ***********************************************************************/
+
+/* called once on startup before config file parsing */
+int osysmon_cmd_init()
+{
+	osysmon_cmd_vty_init();
+	return 0;
+}
+
+/* called periodically */
+int osysmon_cmd_poll(struct value_node *parent)
+{
+	struct value_node *vn_file;
+	struct osysmon_cmd *oc;
+
+	vn_file = value_node_add(parent, "cmd", NULL);
+
+	llist_for_each_entry(oc, &g_oss->cmds, list)
+		osysmon_cmd_run(oc, vn_file);
+
+	return 0;
+}
diff --git a/src/osysmon_main.c b/src/osysmon_main.c
index 1ba3753..f58e848 100644
--- a/src/osysmon_main.c
+++ b/src/osysmon_main.c
@@ -207,6 +207,7 @@
 		osysmon_ping_poll(root);
 
 	osysmon_file_poll(root);
+	osysmon_cmd_poll(root);
 
 	display_update(root);
 	value_node_del(root);
@@ -221,6 +222,7 @@
 	osmo_init_logging2(NULL, &log_info);
 
 	g_oss = talloc_zero(NULL, struct osysmon_state);
+	INIT_LLIST_HEAD(&g_oss->cmds);
 	INIT_LLIST_HEAD(&g_oss->ctrl_clients);
 	INIT_LLIST_HEAD(&g_oss->openvpn_clients);
 	INIT_LLIST_HEAD(&g_oss->netdevs);
@@ -229,6 +231,7 @@
 	vty_init(&vty_info);
 	handle_options(argc, argv);
 	osysmon_sysinfo_init();
+	osysmon_cmd_init();
 	osysmon_ctrl_init();
 	osysmon_openvpn_init();
 	osysmon_rtnl_init();

-- 
To view, visit https://gerrit.osmocom.org/13304
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-sysmon
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I005773b75f81fa5f6c90f53af508fc6debea208b
Gerrit-Change-Number: 13304
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190318/6cf3d12a/attachment.htm>


More information about the gerrit-log mailing list