blob: fb01f5e1701a00300ed03a0f0e96c378ca3062ca [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>
Pau Espin Pedrolae253992018-12-12 19:37:02 +010029#include <getopt.h>
Harald Welte1a36f332018-06-04 17:36:33 +020030
Harald Welte54ca6f42018-06-04 21:52:30 +020031#include "config.h"
Harald Welte1a36f332018-06-04 17:36:33 +020032#include "osysmon.h"
33#include "value_node.h"
34
35#include <osmocom/core/msgb.h>
36#include <osmocom/core/logging.h>
37#include <osmocom/core/application.h>
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +010038#include <osmocom/core/timer.h>
Harald Welte1a36f332018-06-04 17:36:33 +020039
40static struct log_info log_info = {};
41
42static int osysmon_go_parent(struct vty *vty)
43{
44 switch (vty->node) {
45 case CTRL_CLIENT_NODE:
46 case CTRL_CLIENT_GETVAR_NODE:
47 return osysmon_ctrl_go_parent(vty);
48 }
49 return vty->node;
50}
51
52static int osysmon_is_config_node(struct vty *vty, int node)
53{
54 switch (node) {
55 /* no non-config-nodes */
56 default:
57 return 1;
58 }
59}
60
61
62static struct vty_app_info vty_info = {
63 .name = "osysmon",
64 .copyright =
65 "Copyright (C) 2008-2018 Harald Welte\r\n"
66 "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\r\n"
67 "This is free software: you are free to change and redistribute it.\r\n"
68 "There is NO WARRANTY, to the extent permitted by law.\r\n",
Harald Welte54ca6f42018-06-04 21:52:30 +020069 .version = PACKAGE_VERSION,
Harald Welte1a36f332018-06-04 17:36:33 +020070 .go_parent_cb = osysmon_go_parent,
71 .is_config_node = osysmon_is_config_node,
72};
73
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
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100113static void print_usage()
114{
115 printf("Usage: osmo-sysmon\n");
116}
117
118static void print_help()
119{
120 printf(" -h --help This text.\n");
121 printf(" -c --config-file filename The config file to use.\n");
122 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM Enable debugging.\n");
123 printf(" -D --daemonize Fork the process into a background daemon.\n");
124 printf(" -s --disable-color Do not print ANSI colors in the log\n");
125 printf(" -T --timestamp Prefix every log line with a timestamp.\n");
126 printf(" -e --log-level number Set a global loglevel.\n");
Oliver Smith433f7052019-02-22 10:40:08 +0100127 printf(" -V --version Print the version of osmo-sysmon.\n");
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100128}
129
130static struct {
131 const char *config_file;
132 bool daemonize;
133} cmdline_opts = {
134 .config_file = "osmo-sysmon.cfg",
135 .daemonize = false,
136};
137
138static void handle_options(int argc, char **argv)
139{
140 while (1) {
141 int option_index = 0, c;
142 static struct option long_options[] = {
143 {"help", 0, 0, 'h'},
144 {"config-file", 1, 0, 'c'},
145 {"debug", 1, 0, 'd'},
146 {"daemonize", 0, 0, 'D'},
147 {"disable-color", 0, 0, 's'},
148 {"log-level", 1, 0, 'e'},
149 {"timestamp", 0, 0, 'T'},
150 {"version", 0, 0, 'V' },
151 {0, 0, 0, 0}
152 };
153
154 c = getopt_long(argc, argv, "hc:d:Dse:TV",
155 long_options, &option_index);
156 if (c == -1)
157 break;
158
159 switch (c) {
160 case 'h':
161 print_usage();
162 print_help();
163 exit(0);
164 case 'c':
165 cmdline_opts.config_file = optarg;
166 break;
167 case 'd':
168 log_parse_category_mask(osmo_stderr_target, optarg);
169 break;
170 case 'D':
171 cmdline_opts.daemonize = 1;
172 break;
173 case 's':
174 log_set_use_color(osmo_stderr_target, 0);
175 break;
176 case 'e':
177 log_set_log_level(osmo_stderr_target, atoi(optarg));
178 break;
179 case 'T':
180 log_set_print_timestamp(osmo_stderr_target, 1);
181 break;
182 case 'V':
183 print_version(1);
184 exit(0);
185 break;
186 default:
187 /* catch unknown options *as well as* missing arguments. */
188 fprintf(stderr, "Error in command line options. Exiting.\n");
189 exit(-1);
190 break;
191 }
192 }
193}
194
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100195static struct osmo_timer_list print_timer;
196int ping_init;
197
198static void print_nodes(__attribute__((unused)) void *data)
199{
200 struct value_node *root = value_node_add(NULL, "root", NULL);
201 osysmon_openvpn_poll(root);
202 osysmon_sysinfo_poll(root);
203 osysmon_ctrl_poll(root);
204 osysmon_rtnl_poll(root);
205
206 if (ping_init == 0)
207 osysmon_ping_poll(root);
208
209 osysmon_file_poll(root);
Pau Espin Pedrol92016772019-03-18 17:24:34 +0100210 osysmon_shellcmd_poll(root);
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100211
212 display_update(root);
213 value_node_del(root);
214
215 osmo_timer_schedule(&print_timer, 1, 0);
216}
217
Harald Welte1a36f332018-06-04 17:36:33 +0200218int main(int argc, char **argv)
219{
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100220 int rc;
Harald Welte1a36f332018-06-04 17:36:33 +0200221
222 osmo_init_logging2(NULL, &log_info);
223
224 g_oss = talloc_zero(NULL, struct osysmon_state);
Pau Espin Pedrol92016772019-03-18 17:24:34 +0100225 INIT_LLIST_HEAD(&g_oss->shellcmds);
Harald Welte1a36f332018-06-04 17:36:33 +0200226 INIT_LLIST_HEAD(&g_oss->ctrl_clients);
Max9a852f22019-01-31 15:58:57 +0100227 INIT_LLIST_HEAD(&g_oss->openvpn_clients);
Harald Welte9e7fe002018-06-04 20:09:26 +0200228 INIT_LLIST_HEAD(&g_oss->netdevs);
Harald Welte81e20232018-06-04 21:25:56 +0200229 INIT_LLIST_HEAD(&g_oss->files);
Harald Welte1a36f332018-06-04 17:36:33 +0200230
231 vty_init(&vty_info);
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100232 handle_options(argc, argv);
Harald Welte32f7a992018-06-04 18:07:33 +0200233 osysmon_sysinfo_init();
Pau Espin Pedrol92016772019-03-18 17:24:34 +0100234 osysmon_shellcmd_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200235 osysmon_ctrl_init();
Max9a852f22019-01-31 15:58:57 +0100236 osysmon_openvpn_init();
Harald Welte9e7fe002018-06-04 20:09:26 +0200237 osysmon_rtnl_init();
Max8066a412019-01-28 23:59:03 +0100238 ping_init = osysmon_ping_init();
Harald Welte81e20232018-06-04 21:25:56 +0200239 osysmon_file_init();
Harald Welte1a36f332018-06-04 17:36:33 +0200240
Maxada0d1a2019-01-30 16:31:45 +0100241 signal(SIGUSR1, &signal_handler);
242 signal(SIGUSR2, &signal_handler);
243 osmo_init_ignore_signals();
244
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100245 rc = vty_read_config_file(cmdline_opts.config_file, NULL);
Harald Welte1a36f332018-06-04 17:36:33 +0200246 if (rc < 0) {
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100247 fprintf(stderr, "Failed to parse the config file %s\n",
248 cmdline_opts.config_file);
Harald Welte1a36f332018-06-04 17:36:33 +0200249 exit(2);
250 }
251
Pau Espin Pedrolae253992018-12-12 19:37:02 +0100252 if (cmdline_opts.daemonize) {
253 rc = osmo_daemonize();
254 if (rc < 0) {
255 perror("Error during daemonize");
256 exit(1);
257 }
258 }
259
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100260 osmo_timer_setup(&print_timer, print_nodes, NULL);
261 osmo_timer_schedule(&print_timer, 0, 0);
Max8066a412019-01-28 23:59:03 +0100262
Pau Espin Pedrol69d80b72019-03-15 18:34:36 +0100263 while (1)
Max9a852f22019-01-31 15:58:57 +0100264 osmo_select_main(0);
265
Harald Welte1a36f332018-06-04 17:36:33 +0200266 exit(0);
267}