blob: 4bfee48a244cc21d3303dac566c55d2894d52b14 [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
76static void print_node(struct value_node *node)
77{
78 if (node->value) {
79 printf("%s: %s\n", node->name, node->value);
80 } else {
81 struct value_node *vn;
82 llist_for_each_entry(vn, &node->children, list)
83 print_node(vn);
84 }
85}
86
87static void display_update(struct value_node *root)
88{
89 print_node(root);
90}
91
92static void signal_handler(int signal)
93{
94 fprintf(stderr, "Signal %u received", signal);
95
96 switch(signal) {
97 case SIGUSR1:
98 talloc_report(g_oss, stderr);
99 break;
100 default:
101 break;
102 }
103}
104
105static void exit_help(void)
106{
107 printf("Usage:\n");
108 printf("\tosmo-ctrl-client HOST PORT get VARIABLE\n");
109 printf("\tosmo-ctrl-client HOST PORT set VARIABLE VALUE\n");
110 printf("\tosmo-ctrl-client HOST PORT monitor\n");
111 exit(2);
112}
113
114int main(int argc, char **argv)
115{
116 int rc;
117
118 osmo_init_logging2(NULL, &log_info);
119
120 g_oss = talloc_zero(NULL, struct osysmon_state);
121 INIT_LLIST_HEAD(&g_oss->ctrl_clients);
122
123 vty_init(&vty_info);
Harald Welte32f7a992018-06-04 18:07:33 +0200124 osysmon_sysinfo_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200125 osysmon_ctrl_init();
126
127 rc = vty_read_config_file(config_file, NULL);
128 if (rc < 0) {
129 fprintf(stderr, "Failed to parse the config file\n");
130 exit(2);
131 }
132
133 signal(SIGUSR1, &signal_handler);
134 signal(SIGUSR2, &signal_handler);
135 osmo_init_ignore_signals();
136
137 while (1) {
138 struct value_node *root = value_node_add(g_oss, NULL, "root", NULL);
Harald Welte32f7a992018-06-04 18:07:33 +0200139 osysmon_sysinfo_poll(root);
Harald Welte1a36f332018-06-04 17:36:33 +0200140 osysmon_ctrl_poll(root);
Harald Welte32f7a992018-06-04 18:07:33 +0200141
Harald Welte1a36f332018-06-04 17:36:33 +0200142 display_update(root);
143 value_node_del(root);
144 sleep(1);
145 }
146
147 exit(0);
148}