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

Change-Id: I005773b75f81fa5f6c90f53af508fc6debea208b
changes/04/13304/3
Pau Espin 4 years ago
parent d7321347b9
commit 9201677917
  1. 1
      doc/examples/osmo-sysmon.cfg
  2. 1
      src/Makefile.am
  3. 7
      src/osysmon.h
  4. 3
      src/osysmon_main.c
  5. 174
      src/osysmon_shellcmd.c

@ -14,3 +14,4 @@ netdev tun0
ping example.com
openvpn 127.0.0.1 1234
file os-image /etc/image-datetime
shellcmd kernel uname -a

@ -42,6 +42,7 @@ osmo_sysmon_SOURCES = \
osysmon_file.c \
osysmon_ping.c \
osysmon_openvpn.c \
osysmon_shellcmd.c \
osysmon_main.c \
$(NULL)

@ -13,6 +13,8 @@ struct ping_state;
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 osysmon_state {
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 @@ enum osysmon_vty_node {
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_openvpn_poll(struct value_node *parent);
int osysmon_file_init();
int osysmon_file_poll(struct value_node *parent);
int osysmon_shellcmd_init();
int osysmon_shellcmd_poll(struct value_node *parent);

@ -207,6 +207,7 @@ static void print_nodes(__attribute__((unused)) void *data)
osysmon_ping_poll(root);
osysmon_file_poll(root);
osysmon_shellcmd_poll(root);
display_update(root);
value_node_del(root);
@ -221,6 +222,7 @@ int main(int argc, char **argv)
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 @@ int main(int argc, char **argv)
vty_init(&vty_info);
handle_options(argc, argv);
osysmon_sysinfo_init();
osysmon_shellcmd_init();
osysmon_ctrl_init();
osysmon_openvpn_init();
osysmon_rtnl_init();

@ -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@sysmocom.de>
* All Rights Reserved.
* Author: Pau Espin Pedrol <pespin@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;
}
Loading…
Cancel
Save