blob: 2d5ebfa69ddf3daa14c48233d7cd37f172c88b6c [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.
Harald Welte1a36f332018-06-04 17:36:33 +020017 */
18
19#include <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <signal.h>
Pau Espin Pedrolae253992018-12-12 19:37:02 +010024#include <getopt.h>
Harald Welte1a36f332018-06-04 17:36:33 +020025
Harald Welte54ca6f42018-06-04 21:52:30 +020026#include "config.h"
Harald Welte1a36f332018-06-04 17:36:33 +020027#include "osysmon.h"
28#include "value_node.h"
29
30#include <osmocom/core/msgb.h>
31#include <osmocom/core/logging.h>
32#include <osmocom/core/application.h>
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +010033#include <osmocom/core/timer.h>
Harald Welte1a36f332018-06-04 17:36:33 +020034
35static struct log_info log_info = {};
36
37static int osysmon_go_parent(struct vty *vty)
38{
39 switch (vty->node) {
40 case CTRL_CLIENT_NODE:
41 case CTRL_CLIENT_GETVAR_NODE:
42 return osysmon_ctrl_go_parent(vty);
43 }
44 return vty->node;
45}
46
47static int osysmon_is_config_node(struct vty *vty, int node)
48{
49 switch (node) {
50 /* no non-config-nodes */
51 default:
52 return 1;
53 }
54}
55
56
57static struct vty_app_info vty_info = {
58 .name = "osysmon",
59 .copyright =
60 "Copyright (C) 2008-2018 Harald Welte\r\n"
61 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\r\n"
62 "This is free software: you are free to change and redistribute it.\r\n"
63 "There is NO WARRANTY, to the extent permitted by law.\r\n",
Harald Welte54ca6f42018-06-04 21:52:30 +020064 .version = PACKAGE_VERSION,
Harald Welte1a36f332018-06-04 17:36:33 +020065 .go_parent_cb = osysmon_go_parent,
66 .is_config_node = osysmon_is_config_node,
67};
68
Harald Welte1a36f332018-06-04 17:36:33 +020069struct osysmon_state *g_oss;
70
71
Harald Welte3dada482018-06-04 18:22:03 +020072static void print_node(struct value_node *node, unsigned int indent)
Harald Welte1a36f332018-06-04 17:36:33 +020073{
Harald Welte3dada482018-06-04 18:22:03 +020074 unsigned int i;
75
Harald Welte1a36f332018-06-04 17:36:33 +020076 if (node->value) {
Harald Welte3dada482018-06-04 18:22:03 +020077 for (i = 0; i < indent; i++)
78 fputc(' ', stdout);
Harald Welte1a36f332018-06-04 17:36:33 +020079 printf("%s: %s\n", node->name, node->value);
80 } else {
81 struct value_node *vn;
Harald Welte3dada482018-06-04 18:22:03 +020082 for (i = 0; i < indent; i++)
83 fputc(' ', stdout);
84 printf("%s\n", node->name);
Harald Welte1a36f332018-06-04 17:36:33 +020085 llist_for_each_entry(vn, &node->children, list)
Harald Welte3dada482018-06-04 18:22:03 +020086 print_node(vn, indent+2);
Harald Welte1a36f332018-06-04 17:36:33 +020087 }
88}
89
90static void display_update(struct value_node *root)
91{
Harald Welte3dada482018-06-04 18:22:03 +020092 print_node(root, 0);
Harald Welte1a36f332018-06-04 17:36:33 +020093}
94
95static void signal_handler(int signal)
96{
97 fprintf(stderr, "Signal %u received", signal);
98
99 switch(signal) {
100 case SIGUSR1:
101 talloc_report(g_oss, stderr);
102 break;
103 default:
104 break;
105 }
106}
107
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100108static void print_usage()
109{
110 printf("Usage: osmo-sysmon\n");
111}
112
113static void print_help()
114{
115 printf(" -h --help This text.\n");
Daniel Willmannebbfbbd2019-11-07 17:04:23 +0100116 printf(" -o --oneshot Oneshot mode. Execute queries once, then exit.\n");
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100117 printf(" -c --config-file filename The config file to use.\n");
118 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
119 printf(" -D --daemonize Fork the process into a background daemon.\n");
120 printf(" -s --disable-color Do not print ANSI colors in the log\n");
121 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
122 printf(" -e --log-level number Set a global loglevel.\n");
Oliver Smith433f7052019-02-22 10:40:08 +0100123 printf(" -V --version Print the version of osmo-sysmon.\n");
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100124}
125
126static struct {
127 const char *config_file;
128 bool daemonize;
Daniel Willmannebbfbbd2019-11-07 17:04:23 +0100129 bool oneshot;
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100130} cmdline_opts = {
131 .config_file = "osmo-sysmon.cfg",
132 .daemonize = false,
Daniel Willmannebbfbbd2019-11-07 17:04:23 +0100133 .oneshot = false,
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100134};
135
136static void handle_options(int argc, char **argv)
137{
138 while (1) {
139 int option_index = 0, c;
140 static struct option long_options[] = {
141 {"help", 0, 0, 'h'},
Daniel Willmannebbfbbd2019-11-07 17:04:23 +0100142 {"oneshot", 0, 0, 'o'},
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100143 {"config-file", 1, 0, 'c'},
144 {"debug", 1, 0, 'd'},
145 {"daemonize", 0, 0, 'D'},
146 {"disable-color", 0, 0, 's'},
147 {"log-level", 1, 0, 'e'},
148 {"timestamp", 0, 0, 'T'},
149 {"version", 0, 0, 'V' },
150 {0, 0, 0, 0}
151 };
152
Daniel Willmannebbfbbd2019-11-07 17:04:23 +0100153 c = getopt_long(argc, argv, "hoc:d:Dse:TV",
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100154 long_options, &option_index);
155 if (c == -1)
156 break;
157
158 switch (c) {
159 case 'h':
160 print_usage();
161 print_help();
162 exit(0);
Daniel Willmannebbfbbd2019-11-07 17:04:23 +0100163 case 'o':
164 cmdline_opts.oneshot = true;
165 break;
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100166 case 'c':
167 cmdline_opts.config_file = optarg;
168 break;
169 case 'd':
170 log_parse_category_mask(osmo_stderr_target, optarg);
171 break;
172 case 'D':
173 cmdline_opts.daemonize = 1;
174 break;
175 case 's':
176 log_set_use_color(osmo_stderr_target, 0);
177 break;
178 case 'e':
179 log_set_log_level(osmo_stderr_target, atoi(optarg));
180 break;
181 case 'T':
182 log_set_print_timestamp(osmo_stderr_target, 1);
183 break;
184 case 'V':
185 print_version(1);
186 exit(0);
187 break;
188 default:
189 /* catch unknown options *as well as* missing arguments. */
190 fprintf(stderr, "Error in command line options. Exiting.\n");
191 exit(-1);
192 break;
193 }
194 }
Harald Welte8984bea2019-12-03 22:29:26 +0100195
196 if (argc > optind) {
197 fprintf(stderr, "Unsupported positional arguments on command line\n");
198 exit(2);
199 }
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100200}
201
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100202static struct osmo_timer_list print_timer;
203int ping_init;
204
205static void print_nodes(__attribute__((unused)) void *data)
206{
207 struct value_node *root = value_node_add(NULL, "root", NULL);
208 osysmon_openvpn_poll(root);
209 osysmon_sysinfo_poll(root);
210 osysmon_ctrl_poll(root);
211 osysmon_rtnl_poll(root);
212
213 if (ping_init == 0)
214 osysmon_ping_poll(root);
215
216 osysmon_file_poll(root);
Pau Espin Pedrol92016772019-03-18 17:24:34 +0100217 osysmon_shellcmd_poll(root);
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100218
219 display_update(root);
220 value_node_del(root);
221
Daniel Willmannebbfbbd2019-11-07 17:04:23 +0100222 if (cmdline_opts.oneshot)
223 exit(0);
224
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100225 osmo_timer_schedule(&print_timer, 1, 0);
226}
227
Harald Welte1a36f332018-06-04 17:36:33 +0200228int main(int argc, char **argv)
229{
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100230 int rc;
Harald Welte1a36f332018-06-04 17:36:33 +0200231
232 osmo_init_logging2(NULL, &log_info);
233
234 g_oss = talloc_zero(NULL, struct osysmon_state);
Pau Espin Pedrol92016772019-03-18 17:24:34 +0100235 INIT_LLIST_HEAD(&g_oss->shellcmds);
Harald Welte1a36f332018-06-04 17:36:33 +0200236 INIT_LLIST_HEAD(&g_oss->ctrl_clients);
Max9a852f22019-01-31 15:58:57 +0100237 INIT_LLIST_HEAD(&g_oss->openvpn_clients);
Harald Welte9e7fe002018-06-04 20:09:26 +0200238 INIT_LLIST_HEAD(&g_oss->netdevs);
Harald Welte81e20232018-06-04 21:25:56 +0200239 INIT_LLIST_HEAD(&g_oss->files);
Harald Welte1a36f332018-06-04 17:36:33 +0200240
241 vty_init(&vty_info);
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100242 handle_options(argc, argv);
Harald Welte32f7a992018-06-04 18:07:33 +0200243 osysmon_sysinfo_init();
Pau Espin Pedrol92016772019-03-18 17:24:34 +0100244 osysmon_shellcmd_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200245 osysmon_ctrl_init();
Max9a852f22019-01-31 15:58:57 +0100246 osysmon_openvpn_init();
Harald Welte9e7fe002018-06-04 20:09:26 +0200247 osysmon_rtnl_init();
Max8066a412019-01-28 23:59:03 +0100248 ping_init = osysmon_ping_init();
Harald Welte81e20232018-06-04 21:25:56 +0200249 osysmon_file_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200250
Maxada0d1a2019-01-30 16:31:45 +0100251 signal(SIGUSR1, &signal_handler);
252 signal(SIGUSR2, &signal_handler);
253 osmo_init_ignore_signals();
254
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100255 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
Harald Welte1a36f332018-06-04 17:36:33 +0200256 if (rc < 0) {
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100257 fprintf(stderr, "Failed to parse the config file %s\n",
258 cmdline_opts.config_file);
Harald Welte1a36f332018-06-04 17:36:33 +0200259 exit(2);
260 }
261
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100262 if (cmdline_opts.daemonize) {
263 rc = osmo_daemonize();
264 if (rc < 0) {
265 perror("Error during daemonize");
266 exit(1);
267 }
268 }
269
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100270 osmo_timer_setup(&print_timer, print_nodes, NULL);
271 osmo_timer_schedule(&print_timer, 0, 0);
Max8066a412019-01-28 23:59:03 +0100272
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100273 while (1)
Max9a852f22019-01-31 15:58:57 +0100274 osmo_select_main(0);
275
Harald Welte1a36f332018-06-04 17:36:33 +0200276 exit(0);
277}