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> |
| 29 | |
Harald Welte | 54ca6f4 | 2018-06-04 21:52:30 +0200 | [diff] [blame] | 30 | #include "config.h" |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 31 | #include "osysmon.h" |
| 32 | #include "value_node.h" |
| 33 | |
| 34 | #include <osmocom/core/msgb.h> |
| 35 | #include <osmocom/core/logging.h> |
| 36 | #include <osmocom/core/application.h> |
| 37 | |
| 38 | static struct log_info log_info = {}; |
| 39 | |
| 40 | static int osysmon_go_parent(struct vty *vty) |
| 41 | { |
| 42 | switch (vty->node) { |
| 43 | case CTRL_CLIENT_NODE: |
| 44 | case CTRL_CLIENT_GETVAR_NODE: |
| 45 | return osysmon_ctrl_go_parent(vty); |
| 46 | } |
| 47 | return vty->node; |
| 48 | } |
| 49 | |
| 50 | static int osysmon_is_config_node(struct vty *vty, int node) |
| 51 | { |
| 52 | switch (node) { |
| 53 | /* no non-config-nodes */ |
| 54 | default: |
| 55 | return 1; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static struct vty_app_info vty_info = { |
| 61 | .name = "osysmon", |
| 62 | .copyright = |
| 63 | "Copyright (C) 2008-2018 Harald Welte\r\n" |
| 64 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\r\n" |
| 65 | "This is free software: you are free to change and redistribute it.\r\n" |
| 66 | "There is NO WARRANTY, to the extent permitted by law.\r\n", |
Harald Welte | 54ca6f4 | 2018-06-04 21:52:30 +0200 | [diff] [blame] | 67 | .version = PACKAGE_VERSION, |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 68 | .go_parent_cb = osysmon_go_parent, |
| 69 | .is_config_node = osysmon_is_config_node, |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | static const char *config_file = "osysmon.cfg"; |
| 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 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 113 | int main(int argc, char **argv) |
| 114 | { |
| 115 | int rc; |
| 116 | |
| 117 | osmo_init_logging2(NULL, &log_info); |
| 118 | |
| 119 | g_oss = talloc_zero(NULL, struct osysmon_state); |
| 120 | INIT_LLIST_HEAD(&g_oss->ctrl_clients); |
Harald Welte | 9e7fe00 | 2018-06-04 20:09:26 +0200 | [diff] [blame] | 121 | INIT_LLIST_HEAD(&g_oss->netdevs); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 122 | INIT_LLIST_HEAD(&g_oss->files); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 123 | |
| 124 | vty_init(&vty_info); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 125 | osysmon_sysinfo_init(); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 126 | osysmon_ctrl_init(); |
Harald Welte | 9e7fe00 | 2018-06-04 20:09:26 +0200 | [diff] [blame] | 127 | osysmon_rtnl_init(); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 128 | osysmon_file_init(); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 129 | |
| 130 | rc = vty_read_config_file(config_file, NULL); |
| 131 | if (rc < 0) { |
| 132 | fprintf(stderr, "Failed to parse the config file\n"); |
| 133 | exit(2); |
| 134 | } |
| 135 | |
| 136 | signal(SIGUSR1, &signal_handler); |
| 137 | signal(SIGUSR2, &signal_handler); |
| 138 | osmo_init_ignore_signals(); |
| 139 | |
| 140 | while (1) { |
| 141 | struct value_node *root = value_node_add(g_oss, NULL, "root", NULL); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 142 | osysmon_sysinfo_poll(root); |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 143 | osysmon_ctrl_poll(root); |
Harald Welte | 9e7fe00 | 2018-06-04 20:09:26 +0200 | [diff] [blame] | 144 | osysmon_rtnl_poll(root); |
Harald Welte | 81e2023 | 2018-06-04 21:25:56 +0200 | [diff] [blame] | 145 | osysmon_file_poll(root); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 146 | |
Harald Welte | 1a36f33 | 2018-06-04 17:36:33 +0200 | [diff] [blame] | 147 | display_update(root); |
| 148 | value_node_del(root); |
| 149 | sleep(1); |
| 150 | } |
| 151 | |
| 152 | exit(0); |
| 153 | } |