blob: 59832121da0c6b84d5736e852742e35c64c4491d [file] [log] [blame]
Harald Welte1a36f332018-06-04 17:36:33 +02001/* Simple Osmocom System Monitor (osysmon) */
2
3/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
4 * All Rights Reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
22 */
23
24#include <unistd.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <signal.h>
29
Harald Welte54ca6f42018-06-04 21:52:30 +020030#include "config.h"
Harald Welte1a36f332018-06-04 17:36:33 +020031#include "osysmon.h"
32#include "value_node.h"
33
34#include <osmocom/core/msgb.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/application.h>
37
38static struct log_info log_info = {};
39
40static int osysmon_go_parent(struct vty *vty)
41{
42 switch (vty->node) {
43 case CTRL_CLIENT_NODE:
44 case CTRL_CLIENT_GETVAR_NODE:
45 return osysmon_ctrl_go_parent(vty);
46 }
47 return vty->node;
48}
49
50static int osysmon_is_config_node(struct vty *vty, int node)
51{
52 switch (node) {
53 /* no non-config-nodes */
54 default:
55 return 1;
56 }
57}
58
59
60static struct vty_app_info vty_info = {
61 .name = "osysmon",
62 .copyright =
63 "Copyright (C) 2008-2018 Harald Welte\r\n"
64 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\r\n"
65 "This is free software: you are free to change and redistribute it.\r\n"
66 "There is NO WARRANTY, to the extent permitted by law.\r\n",
Harald Welte54ca6f42018-06-04 21:52:30 +020067 .version = PACKAGE_VERSION,
Harald Welte1a36f332018-06-04 17:36:33 +020068 .go_parent_cb = osysmon_go_parent,
69 .is_config_node = osysmon_is_config_node,
70};
71
72
Maxbf357192018-11-22 13:32:27 +010073static const char *config_file = "osmo-sysmon.cfg";
Harald Welte1a36f332018-06-04 17:36:33 +020074struct osysmon_state *g_oss;
75
76
Harald Welte3dada482018-06-04 18:22:03 +020077static void print_node(struct value_node *node, unsigned int indent)
Harald Welte1a36f332018-06-04 17:36:33 +020078{
Harald Welte3dada482018-06-04 18:22:03 +020079 unsigned int i;
80
Harald Welte1a36f332018-06-04 17:36:33 +020081 if (node->value) {
Harald Welte3dada482018-06-04 18:22:03 +020082 for (i = 0; i < indent; i++)
83 fputc(' ', stdout);
Harald Welte1a36f332018-06-04 17:36:33 +020084 printf("%s: %s\n", node->name, node->value);
85 } else {
86 struct value_node *vn;
Harald Welte3dada482018-06-04 18:22:03 +020087 for (i = 0; i < indent; i++)
88 fputc(' ', stdout);
89 printf("%s\n", node->name);
Harald Welte1a36f332018-06-04 17:36:33 +020090 llist_for_each_entry(vn, &node->children, list)
Harald Welte3dada482018-06-04 18:22:03 +020091 print_node(vn, indent+2);
Harald Welte1a36f332018-06-04 17:36:33 +020092 }
93}
94
95static void display_update(struct value_node *root)
96{
Harald Welte3dada482018-06-04 18:22:03 +020097 print_node(root, 0);
Harald Welte1a36f332018-06-04 17:36:33 +020098}
99
100static void signal_handler(int signal)
101{
102 fprintf(stderr, "Signal %u received", signal);
103
104 switch(signal) {
105 case SIGUSR1:
106 talloc_report(g_oss, stderr);
107 break;
108 default:
109 break;
110 }
111}
112
Harald Welte1a36f332018-06-04 17:36:33 +0200113int main(int argc, char **argv)
114{
115 int rc;
116
117 osmo_init_logging2(NULL, &log_info);
118
119 g_oss = talloc_zero(NULL, struct osysmon_state);
120 INIT_LLIST_HEAD(&g_oss->ctrl_clients);
Harald Welte9e7fe002018-06-04 20:09:26 +0200121 INIT_LLIST_HEAD(&g_oss->netdevs);
Harald Welte81e20232018-06-04 21:25:56 +0200122 INIT_LLIST_HEAD(&g_oss->files);
Harald Welte1a36f332018-06-04 17:36:33 +0200123
124 vty_init(&vty_info);
Harald Welte32f7a992018-06-04 18:07:33 +0200125 osysmon_sysinfo_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200126 osysmon_ctrl_init();
Harald Welte9e7fe002018-06-04 20:09:26 +0200127 osysmon_rtnl_init();
Harald Welte81e20232018-06-04 21:25:56 +0200128 osysmon_file_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200129
130 rc = vty_read_config_file(config_file, NULL);
131 if (rc < 0) {
Max77be9ca2018-11-22 13:29:25 +0100132 fprintf(stderr, "Failed to parse the config file %s\n", config_file);
Harald Welte1a36f332018-06-04 17:36:33 +0200133 exit(2);
134 }
135
136 signal(SIGUSR1, &signal_handler);
137 signal(SIGUSR2, &signal_handler);
138 osmo_init_ignore_signals();
139
140 while (1) {
141 struct value_node *root = value_node_add(g_oss, NULL, "root", NULL);
Harald Welte32f7a992018-06-04 18:07:33 +0200142 osysmon_sysinfo_poll(root);
Harald Welte1a36f332018-06-04 17:36:33 +0200143 osysmon_ctrl_poll(root);
Harald Welte9e7fe002018-06-04 20:09:26 +0200144 osysmon_rtnl_poll(root);
Harald Welte81e20232018-06-04 21:25:56 +0200145 osysmon_file_poll(root);
Harald Welte32f7a992018-06-04 18:07:33 +0200146
Harald Welte1a36f332018-06-04 17:36:33 +0200147 display_update(root);
148 value_node_del(root);
149 sleep(1);
150 }
151
152 exit(0);
153}