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.orgPau Espin Pedrol has submitted this change and it was merged. ( 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
M src/osysmon_main.c
A src/osysmon_shellcmd.c
5 files changed, 185 insertions(+), 1 deletion(-)
Approvals:
Jenkins Builder: Verified
Harald Welte: Looks good to me, approved
diff --git a/doc/examples/osmo-sysmon.cfg b/doc/examples/osmo-sysmon.cfg
index f944f72..22c27cb 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
+shellcmd kernel uname -a
diff --git a/src/Makefile.am b/src/Makefile.am
index f9b79f2..a4663fd 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -42,6 +42,7 @@
osysmon_file.c \
osysmon_ping.c \
osysmon_openvpn.c \
+ osysmon_shellcmd.c \
osysmon_main.c \
$(NULL)
diff --git a/src/osysmon.h b/src/osysmon.h
index 2f82c47..36cd608 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 shellcmds;
/* 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,7 +39,6 @@
PING_NODE,
};
-
int osysmon_ctrl_go_parent(struct vty *vty);
int osysmon_ctrl_init();
int osysmon_ctrl_poll(struct value_node *parent);
@@ -56,3 +58,6 @@
int osysmon_file_init();
int osysmon_file_poll(struct value_node *parent);
+
+int osysmon_shellcmd_init();
+int osysmon_shellcmd_poll(struct value_node *parent);
diff --git a/src/osysmon_main.c b/src/osysmon_main.c
index 1ba3753..fb01f5e 100644
--- a/src/osysmon_main.c
+++ b/src/osysmon_main.c
@@ -207,6 +207,7 @@
osysmon_ping_poll(root);
osysmon_file_poll(root);
+ osysmon_shellcmd_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->shellcmds);
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_shellcmd_init();
osysmon_ctrl_init();
osysmon_openvpn_init();
osysmon_rtnl_init();
diff --git a/src/osysmon_shellcmd.c b/src/osysmon_shellcmd.c
new file mode 100644
index 0000000..347b982
--- /dev/null
+++ b/src/osysmon_shellcmd.c
@@ -0,0 +1,174 @@
+/* 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_shellcmd {
+ struct llist_head list;
+ struct {
+ const char *name;
+ const char *cmd;
+ } cfg;
+};
+
+static struct osysmon_shellcmd *osysmon_shellcmd_find(const char *name)
+{
+ struct osysmon_shellcmd *oc;
+
+ llist_for_each_entry(oc, &g_oss->shellcmds, list) {
+ if (!strcmp(oc->cfg.name, name))
+ return oc;
+ }
+ return NULL;
+}
+
+static struct osysmon_shellcmd *osysmon_shellcmd_add(const char *name, const char *cmd)
+{
+ struct osysmon_shellcmd *oc;
+
+ if (osysmon_shellcmd_find(name))
+ return NULL;
+
+ oc = talloc_zero(g_oss, struct osysmon_shellcmd);
+ OSMO_ASSERT(oc);
+ oc->cfg.name = talloc_strdup(oc, name);
+ oc->cfg.cmd = talloc_strdup(oc, cmd);
+ llist_add_tail(&oc->list, &g_oss->shellcmds);
+ return oc;
+}
+
+static void osysmon_shellcmd_destroy(struct osysmon_shellcmd *oc)
+{
+ llist_del(&oc->list);
+ talloc_free(oc);
+}
+
+static void osysmon_shellcmd_run(struct osysmon_shellcmd *oc, struct value_node *parent)
+{
+ char buf[512];
+ FILE *f;
+ char *p = buf;
+ 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 ((offset = fread(p, 1, sizeof(buf) - 1 - (p - buf), f)) != 0) {
+ p += offset;
+ *p = '\0';
+ }
+
+ pclose(f);
+
+ if (buf != p) {
+ if (*(p - 1) == '\n') /* Remove final new line if exists */
+ *(p - 1) = '\0';
+ value_node_add(parent, oc->cfg.name, buf);
+ } else {
+ value_node_add(parent, oc->cfg.name, "<EMPTY>");
+ }
+}
+
+/***********************************************************************
+ * VTY
+ ***********************************************************************/
+
+#define CMD_STR "Configure a shell command to be executed\n"
+DEFUN(cfg_shellcmd, cfg_shellcmd_cmd,
+ "shellcmd NAME .TEXT",
+ CMD_STR "Name of this shell command snippet\n" "Command to run\n")
+{
+ struct osysmon_shellcmd *oc;
+ char *concat = argv_concat(argv, argc, 1);
+ oc = osysmon_shellcmd_add(argv[0], concat);
+ talloc_free(concat);
+ if (!oc) {
+ vty_out(vty, "Couldn't add shell cmd, maybe it exists?%s", VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ return CMD_SUCCESS;
+}
+
+DEFUN(cfg_no_shellcmd, cfg_no_shellcmd_cmd,
+ "no shellcmd NAME",
+ NO_STR CMD_STR "Name of this shell command snippet\n")
+{
+ struct osysmon_shellcmd *oc;
+ oc = osysmon_shellcmd_find(argv[0]);
+ if (!oc) {
+ vty_out(vty, "Cannot find shell cmd for '%s'%s", argv[0], VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+ osysmon_shellcmd_destroy(oc);
+ return CMD_SUCCESS;
+}
+
+
+static void osysmon_shellcmd_vty_init(void)
+{
+ install_element(CONFIG_NODE, &cfg_shellcmd_cmd);
+ install_element(CONFIG_NODE, &cfg_no_shellcmd_cmd);
+}
+
+/***********************************************************************
+ * Runtime Code
+ ***********************************************************************/
+
+/* called once on startup before config file parsing */
+int osysmon_shellcmd_init()
+{
+ osysmon_shellcmd_vty_init();
+ return 0;
+}
+
+/* called periodically */
+int osysmon_shellcmd_poll(struct value_node *parent)
+{
+ struct value_node *vn_file;
+ struct osysmon_shellcmd *oc;
+
+ vn_file = value_node_add(parent, "shellcmd", NULL);
+
+ llist_for_each_entry(oc, &g_oss->shellcmds, list)
+ osysmon_shellcmd_run(oc, vn_file);
+
+ return 0;
+}
--
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: merged
Gerrit-Change-Id: I005773b75f81fa5f6c90f53af508fc6debea208b
Gerrit-Change-Number: 13304
Gerrit-PatchSet: 3
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Max <msuraev at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190319/09f93f2b/attachment.htm>