blob: 12ca58639d2998825fab7d3fcd7c1f7766dfdad9 [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
30#include "osysmon.h"
31#include "value_node.h"
32
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/logging.h>
35#include <osmocom/core/application.h>
36
37static struct log_info log_info = {};
38
39static int osysmon_go_parent(struct vty *vty)
40{
41 switch (vty->node) {
42 case CTRL_CLIENT_NODE:
43 case CTRL_CLIENT_GETVAR_NODE:
44 return osysmon_ctrl_go_parent(vty);
45 }
46 return vty->node;
47}
48
49static int osysmon_is_config_node(struct vty *vty, int node)
50{
51 switch (node) {
52 /* no non-config-nodes */
53 default:
54 return 1;
55 }
56}
57
58
59static struct vty_app_info vty_info = {
60 .name = "osysmon",
61 .copyright =
62 "Copyright (C) 2008-2018 Harald Welte\r\n"
63 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\r\n"
64 "This is free software: you are free to change and redistribute it.\r\n"
65 "There is NO WARRANTY, to the extent permitted by law.\r\n",
66 .version = "0.0", //PACKAGE_VERSION
67 .go_parent_cb = osysmon_go_parent,
68 .is_config_node = osysmon_is_config_node,
69};
70
71
72static const char *config_file = "osysmon.cfg";
73struct osysmon_state *g_oss;
74
75
Harald Welte3dada482018-06-04 18:22:03 +020076static void print_node(struct value_node *node, unsigned int indent)
Harald Welte1a36f332018-06-04 17:36:33 +020077{
Harald Welte3dada482018-06-04 18:22:03 +020078 unsigned int i;
79
Harald Welte1a36f332018-06-04 17:36:33 +020080 if (node->value) {
Harald Welte3dada482018-06-04 18:22:03 +020081 for (i = 0; i < indent; i++)
82 fputc(' ', stdout);
Harald Welte1a36f332018-06-04 17:36:33 +020083 printf("%s: %s\n", node->name, node->value);
84 } else {
85 struct value_node *vn;
Harald Welte3dada482018-06-04 18:22:03 +020086 for (i = 0; i < indent; i++)
87 fputc(' ', stdout);
88 printf("%s\n", node->name);
Harald Welte1a36f332018-06-04 17:36:33 +020089 llist_for_each_entry(vn, &node->children, list)
Harald Welte3dada482018-06-04 18:22:03 +020090 print_node(vn, indent+2);
Harald Welte1a36f332018-06-04 17:36:33 +020091 }
92}
93
94static void display_update(struct value_node *root)
95{
Harald Welte3dada482018-06-04 18:22:03 +020096 print_node(root, 0);
Harald Welte1a36f332018-06-04 17:36:33 +020097}
98
99static void signal_handler(int signal)
100{
101 fprintf(stderr, "Signal %u received", signal);
102
103 switch(signal) {
104 case SIGUSR1:
105 talloc_report(g_oss, stderr);
106 break;
107 default:
108 break;
109 }
110}
111
112static void exit_help(void)
113{
114 printf("Usage:\n");
115 printf("\tosmo-ctrl-client HOST PORT get VARIABLE\n");
116 printf("\tosmo-ctrl-client HOST PORT set VARIABLE VALUE\n");
117 printf("\tosmo-ctrl-client HOST PORT monitor\n");
118 exit(2);
119}
120
121int main(int argc, char **argv)
122{
123 int rc;
124
125 osmo_init_logging2(NULL, &log_info);
126
127 g_oss = talloc_zero(NULL, struct osysmon_state);
128 INIT_LLIST_HEAD(&g_oss->ctrl_clients);
129
130 vty_init(&vty_info);
Harald Welte32f7a992018-06-04 18:07:33 +0200131 osysmon_sysinfo_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200132 osysmon_ctrl_init();
133
134 rc = vty_read_config_file(config_file, NULL);
135 if (rc < 0) {
136 fprintf(stderr, "Failed to parse the config file\n");
137 exit(2);
138 }
139
140 signal(SIGUSR1, &signal_handler);
141 signal(SIGUSR2, &signal_handler);
142 osmo_init_ignore_signals();
143
144 while (1) {
145 struct value_node *root = value_node_add(g_oss, NULL, "root", NULL);
Harald Welte32f7a992018-06-04 18:07:33 +0200146 osysmon_sysinfo_poll(root);
Harald Welte1a36f332018-06-04 17:36:33 +0200147 osysmon_ctrl_poll(root);
Harald Welte32f7a992018-06-04 18:07:33 +0200148
Harald Welte1a36f332018-06-04 17:36:33 +0200149 display_update(root);
150 value_node_del(root);
151 sleep(1);
152 }
153
154 exit(0);
155}