blob: d50fd0011b05cbd105011dee342d31c788973fef [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
73static const char *config_file = "osysmon.cfg";
74struct 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
113static void exit_help(void)
114{
115 printf("Usage:\n");
116 printf("\tosmo-ctrl-client HOST PORT get VARIABLE\n");
117 printf("\tosmo-ctrl-client HOST PORT set VARIABLE VALUE\n");
118 printf("\tosmo-ctrl-client HOST PORT monitor\n");
119 exit(2);
120}
121
122int main(int argc, char **argv)
123{
124 int rc;
125
126 osmo_init_logging2(NULL, &log_info);
127
128 g_oss = talloc_zero(NULL, struct osysmon_state);
129 INIT_LLIST_HEAD(&g_oss->ctrl_clients);
Harald Welte9e7fe002018-06-04 20:09:26 +0200130 INIT_LLIST_HEAD(&g_oss->netdevs);
Harald Welte81e20232018-06-04 21:25:56 +0200131 INIT_LLIST_HEAD(&g_oss->files);
Harald Welte1a36f332018-06-04 17:36:33 +0200132
133 vty_init(&vty_info);
Harald Welte32f7a992018-06-04 18:07:33 +0200134 osysmon_sysinfo_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200135 osysmon_ctrl_init();
Harald Welte9e7fe002018-06-04 20:09:26 +0200136 osysmon_rtnl_init();
Harald Welte81e20232018-06-04 21:25:56 +0200137 osysmon_file_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200138
139 rc = vty_read_config_file(config_file, NULL);
140 if (rc < 0) {
141 fprintf(stderr, "Failed to parse the config file\n");
142 exit(2);
143 }
144
145 signal(SIGUSR1, &signal_handler);
146 signal(SIGUSR2, &signal_handler);
147 osmo_init_ignore_signals();
148
149 while (1) {
150 struct value_node *root = value_node_add(g_oss, NULL, "root", NULL);
Harald Welte32f7a992018-06-04 18:07:33 +0200151 osysmon_sysinfo_poll(root);
Harald Welte1a36f332018-06-04 17:36:33 +0200152 osysmon_ctrl_poll(root);
Harald Welte9e7fe002018-06-04 20:09:26 +0200153 osysmon_rtnl_poll(root);
Harald Welte81e20232018-06-04 21:25:56 +0200154 osysmon_file_poll(root);
Harald Welte32f7a992018-06-04 18:07:33 +0200155
Harald Welte1a36f332018-06-04 17:36:33 +0200156 display_update(root);
157 value_node_del(root);
158 sleep(1);
159 }
160
161 exit(0);
162}