aboutsummaryrefslogtreecommitdiffstats
path: root/osysmon_main.c
blob: eaed1a9c75de22348e3f195126553d0be5fd480f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Simple Osmocom System Monitor (osysmon) */

/* (C) 2018 by Harald Welte <laforge@gnumonks.org>
 * All Rights Reserved.
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301, USA.
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>

#include "config.h"
#include "osysmon.h"
#include "value_node.h"

#include <osmocom/core/msgb.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/application.h>

static struct log_info log_info = {};

static int osysmon_go_parent(struct vty *vty)
{
	switch (vty->node) {
	case CTRL_CLIENT_NODE:
	case CTRL_CLIENT_GETVAR_NODE:
		return osysmon_ctrl_go_parent(vty);
	}
	return vty->node;
}

static int osysmon_is_config_node(struct vty *vty, int node)
{
	switch (node) {
	/* no non-config-nodes */
	default:
		return 1;
	}
}


static struct vty_app_info vty_info = {
	.name = "osysmon",
	.copyright =
	"Copyright (C) 2008-2018 Harald Welte\r\n"
	"License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>\r\n"
	"This is free software: you are free to change and redistribute it.\r\n"
	"There is NO WARRANTY, to the extent permitted by law.\r\n",
	.version = PACKAGE_VERSION,
	.go_parent_cb = osysmon_go_parent,
	.is_config_node = osysmon_is_config_node,
};


static const char *config_file = "osysmon.cfg";
struct osysmon_state *g_oss;


static void print_node(struct value_node *node, unsigned int indent)
{
	unsigned int i;

	if (node->value) {
		for (i = 0; i < indent; i++)
			fputc(' ', stdout);
		printf("%s: %s\n", node->name, node->value);
	} else {
		struct value_node *vn;
		for (i = 0; i < indent; i++)
			fputc(' ', stdout);
		printf("%s\n", node->name);
		llist_for_each_entry(vn, &node->children, list)
			print_node(vn, indent+2);
	}
}

static void display_update(struct value_node *root)
{
	print_node(root, 0);
}

static void signal_handler(int signal)
{
	fprintf(stderr, "Signal %u received", signal);

	switch(signal) {
	case SIGUSR1:
		talloc_report(g_oss, stderr);
		break;
	default:
		break;
	}
}

int main(int argc, char **argv)
{
	int rc;

	osmo_init_logging2(NULL, &log_info);

	g_oss = talloc_zero(NULL, struct osysmon_state);
	INIT_LLIST_HEAD(&g_oss->ctrl_clients);
	INIT_LLIST_HEAD(&g_oss->netdevs);
	INIT_LLIST_HEAD(&g_oss->files);

	vty_init(&vty_info);
	osysmon_sysinfo_init();
	osysmon_ctrl_init();
	osysmon_rtnl_init();
	osysmon_file_init();

	rc = vty_read_config_file(config_file, NULL);
	if (rc < 0) {
		fprintf(stderr, "Failed to parse the config file\n");
		exit(2);
	}

	signal(SIGUSR1, &signal_handler);
	signal(SIGUSR2, &signal_handler);
	osmo_init_ignore_signals();

	while (1) {
		struct value_node *root = value_node_add(g_oss, NULL, "root", NULL);
		osysmon_sysinfo_poll(root);
		osysmon_ctrl_poll(root);
		osysmon_rtnl_poll(root);
		osysmon_file_poll(root);

		display_update(root);
		value_node_del(root);
		sleep(1);
	}

	exit(0);
}