Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 1 | /* 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 Pedrol | ae25399 | 2018-12-12 19:37:02 +0100 | [diff] [blame] | 29 | #include <getopt.h> |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 30 | |
Harald Welte | 54ca6f4 | 2018-06-04 21:52:30 +0200 | [diff] [blame] | 31 | #include "config.h" |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 32 | #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 Pedrol | 69d80b7 | 2019-03-15 18:34:36 +0100 | [diff] [blame] | 38 | #include <osmocom/core/timer.h> |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 39 | |
| 40 | static struct log_info log_info = {}; |
| 41 | |
| 42 | static 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 | |
| 52 | static 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 | |
| 62 | static 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 Welte | 54ca6f4 | 2018-06-04 21:52:30 +0200 | [diff] [blame] | 69 | .version = PACKAGE_VERSION, |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 70 | .go_parent_cb = osysmon_go_parent, |
| 71 | .is_config_node = osysmon_is_config_node, |
| 72 | }; |
| 73 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 74 | struct osysmon_state *g_oss; |
| 75 | |
| 76 | |
Harald Welte | 3dada48 | 2018-06-04 18:22:03 +0200 | [diff] [blame] | 77 | static void print_node(struct value_node *node, unsigned int indent) |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 78 | { |
Harald Welte | 3dada48 | 2018-06-04 18:22:03 +0200 | [diff] [blame] | 79 | unsigned int i; |
| 80 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 81 | if (node->value) { |
Harald Welte | 3dada48 | 2018-06-04 18:22:03 +0200 | [diff] [blame] | 82 | for (i = 0; i < indent; i++) |
| 83 | fputc(' ', stdout); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 84 | printf("%s: %s\n", node->name, node->value); |
| 85 | } else { |
| 86 | struct value_node *vn; |
Harald Welte | 3dada48 | 2018-06-04 18:22:03 +0200 | [diff] [blame] | 87 | for (i = 0; i < indent; i++) |
| 88 | fputc(' ', stdout); |
| 89 | printf("%s\n", node->name); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 90 | llist_for_each_entry(vn, &node->children, list) |
Harald Welte | 3dada48 | 2018-06-04 18:22:03 +0200 | [diff] [blame] | 91 | print_node(vn, indent+2); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | static void display_update(struct value_node *root) |
| 96 | { |
Harald Welte | 3dada48 | 2018-06-04 18:22:03 +0200 | [diff] [blame] | 97 | print_node(root, 0); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static 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 Pedrol | ae25399 | 2018-12-12 19:37:02 +0100 | [diff] [blame] | 113 | static void print_usage() |
| 114 | { |
| 115 | printf("Usage: osmo-sysmon\n"); |
| 116 | } |
| 117 | |
| 118 | static 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 Smith | 433f705 | 2019-02-22 10:40:08 +0100 | [diff] [blame] | 127 | printf(" -V --version Print the version of osmo-sysmon.\n"); |
Pau Espin Pedrol | ae25399 | 2018-12-12 19:37:02 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static struct { |
| 131 | const char *config_file; |
| 132 | bool daemonize; |
| 133 | } cmdline_opts = { |
| 134 | .config_file = "osmo-sysmon.cfg", |
| 135 | .daemonize = false, |
| 136 | }; |
| 137 | |
| 138 | static 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 Pedrol | 69d80b7 | 2019-03-15 18:34:36 +0100 | [diff] [blame] | 195 | static struct osmo_timer_list print_timer; |
| 196 | int ping_init; |
| 197 | |
| 198 | static 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 Pedrol | 9201677 | 2019-03-18 17:24:34 +0100 | [diff] [blame] | 210 | osysmon_shellcmd_poll(root); |
Pau Espin Pedrol | 69d80b7 | 2019-03-15 18:34:36 +0100 | [diff] [blame] | 211 | |
| 212 | display_update(root); |
| 213 | value_node_del(root); |
| 214 | |
| 215 | osmo_timer_schedule(&print_timer, 1, 0); |
| 216 | } |
| 217 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 218 | int main(int argc, char **argv) |
| 219 | { |
Pau Espin Pedrol | 69d80b7 | 2019-03-15 18:34:36 +0100 | [diff] [blame] | 220 | int rc; |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 221 | |
| 222 | osmo_init_logging2(NULL, &log_info); |
| 223 | |
| 224 | g_oss = talloc_zero(NULL, struct osysmon_state); |
Pau Espin Pedrol | 9201677 | 2019-03-18 17:24:34 +0100 | [diff] [blame] | 225 | INIT_LLIST_HEAD(&g_oss->shellcmds); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 226 | INIT_LLIST_HEAD(&g_oss->ctrl_clients); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 227 | INIT_LLIST_HEAD(&g_oss->openvpn_clients); |
Harald Welte | 9e7fe00 | 2018-06-04 20:09:26 +0200 | [diff] [blame] | 228 | INIT_LLIST_HEAD(&g_oss->netdevs); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 229 | INIT_LLIST_HEAD(&g_oss->files); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 230 | |
| 231 | vty_init(&vty_info); |
Pau Espin Pedrol | ae25399 | 2018-12-12 19:37:02 +0100 | [diff] [blame] | 232 | handle_options(argc, argv); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 233 | osysmon_sysinfo_init(); |
Pau Espin Pedrol | 9201677 | 2019-03-18 17:24:34 +0100 | [diff] [blame] | 234 | osysmon_shellcmd_init(); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 235 | osysmon_ctrl_init(); |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 236 | osysmon_openvpn_init(); |
Harald Welte | 9e7fe00 | 2018-06-04 20:09:26 +0200 | [diff] [blame] | 237 | osysmon_rtnl_init(); |
Max | 8066a41 | 2019-01-28 23:59:03 +0100 | [diff] [blame] | 238 | ping_init = osysmon_ping_init(); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 239 | osysmon_file_init(); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 240 | |
Max | ada0d1a | 2019-01-30 16:31:45 +0100 | [diff] [blame] | 241 | signal(SIGUSR1, &signal_handler); |
| 242 | signal(SIGUSR2, &signal_handler); |
| 243 | osmo_init_ignore_signals(); |
| 244 | |
Pau Espin Pedrol | ae25399 | 2018-12-12 19:37:02 +0100 | [diff] [blame] | 245 | rc = vty_read_config_file(cmdline_opts.config_file, NULL); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 246 | if (rc < 0) { |
Pau Espin Pedrol | ae25399 | 2018-12-12 19:37:02 +0100 | [diff] [blame] | 247 | fprintf(stderr, "Failed to parse the config file %s\n", |
| 248 | cmdline_opts.config_file); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 249 | exit(2); |
| 250 | } |
| 251 | |
Pau Espin Pedrol | ae25399 | 2018-12-12 19:37:02 +0100 | [diff] [blame] | 252 | 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 Pedrol | 69d80b7 | 2019-03-15 18:34:36 +0100 | [diff] [blame] | 260 | osmo_timer_setup(&print_timer, print_nodes, NULL); |
| 261 | osmo_timer_schedule(&print_timer, 0, 0); |
Max | 8066a41 | 2019-01-28 23:59:03 +0100 | [diff] [blame] | 262 | |
Pau Espin Pedrol | 69d80b7 | 2019-03-15 18:34:36 +0100 | [diff] [blame] | 263 | while (1) |
Max | 9a852f2 | 2019-01-31 15:58:57 +0100 | [diff] [blame] | 264 | osmo_select_main(0); |
| 265 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 266 | exit(0); |
| 267 | } |