blob: 185c675649e7268e54fb2236017728beee7d561a [file] [log] [blame]
Harald Welte32f7a992018-06-04 18:07:33 +02001/* Simple Osmocom System Monitor (osysmon): Support for uptime/load/ram */
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 Welte32f7a992018-06-04 18:07:33 +020017 */
18
19#include <string.h>
20#include <sys/sysinfo.h>
21
22#include <osmocom/vty/vty.h>
23#include <osmocom/vty/command.h>
24
25#include "osysmon.h"
26#include "value_node.h"
27
Harald Welte32f7a992018-06-04 18:07:33 +020028static float loadfac(unsigned long in) {
29 return in/65536.0;
30}
31
32#define to_mbytes(in) ((in)/((1024*1024)/si.mem_unit))
33
34#define SECS_PER_MIN 60UL
35#define MINS_PER_HOUR 60UL
36#define HOURS_PER_DAY 24UL
37
38#define to_days(in) ((in)/(SECS_PER_MIN*MINS_PER_HOUR*HOURS_PER_DAY))
39#define to_hours(in) (((in)/(SECS_PER_MIN*MINS_PER_HOUR))%HOURS_PER_DAY)
40#define to_minutes(in) (((in)/(SECS_PER_MIN))%MINS_PER_HOUR)
41#define to_seconds(in) ((in)%SECS_PER_MIN)
42
Daniel Willmann85d0fb22019-11-07 16:59:15 +010043static bool sysinfo_enabled = true;
44
45/***********************************************************************
46 * VTY
47 ***********************************************************************/
48
49#define CMD_STR "Display sysinfo\n"
50DEFUN(cfg_sysinfo, cfg_sysinfo_cmd,
51 "sysinfo",
52 CMD_STR)
53{
54 sysinfo_enabled = true;
55
56 return CMD_SUCCESS;
57}
58
59DEFUN(cfg_no_sysinfo, cfg_no_sysinfo_cmd,
60 "no sysinfo",
61 NO_STR CMD_STR)
62{
63 sysinfo_enabled = false;
64
65 return CMD_SUCCESS;
66}
67
68static void osysmon_sysinfo_vty_init(void)
69{
70 install_element(CONFIG_NODE, &cfg_sysinfo_cmd);
71 install_element(CONFIG_NODE, &cfg_no_sysinfo_cmd);
72}
73
74/***********************************************************************
75 * Runtime Code
76 ***********************************************************************/
77
78/* called once on startup before config file parsing */
79int osysmon_sysinfo_init()
80{
81 osysmon_sysinfo_vty_init();
82 return 0;
83}
84
Harald Welte32f7a992018-06-04 18:07:33 +020085/* called periodically */
86int osysmon_sysinfo_poll(struct value_node *parent)
87{
88 struct sysinfo si;
89 struct value_node *vn_sysinfo;
90 char buf[32];
91 int rc;
92
Daniel Willmann85d0fb22019-11-07 16:59:15 +010093 if (!sysinfo_enabled)
94 return 0;
95
Maxf41973e2019-01-25 18:40:11 +010096 vn_sysinfo = value_node_add(parent, "sysinfo", NULL);
Harald Welte32f7a992018-06-04 18:07:33 +020097
98 rc = sysinfo(&si);
99 if (rc < 0)
100 return rc;
101
102 /* Load Factor 1/5/15min */
103 snprintf(buf, sizeof(buf), "%.2f/%.2f/%.2f",
104 loadfac(si.loads[0]), loadfac(si.loads[1]), loadfac(si.loads[2]));
Maxf41973e2019-01-25 18:40:11 +0100105 value_node_add(vn_sysinfo, "load", buf);
Harald Welte32f7a992018-06-04 18:07:33 +0200106
107 /* RAM information (total/free/sared) in megabytes */
108 snprintf(buf, sizeof(buf), "%lu/%lu/%lu",
109 to_mbytes(si.totalram), to_mbytes(si.freeram), to_mbytes(si.sharedram));
Maxf41973e2019-01-25 18:40:11 +0100110 value_node_add(vn_sysinfo, "ram", buf);
Harald Welte32f7a992018-06-04 18:07:33 +0200111
112 /* uptime in days/hours/minutes/seconds */
113 snprintf(buf, sizeof(buf), "%lud %02lu:%02lu:%02lu", to_days(si.uptime),
114 to_hours(si.uptime), to_minutes(si.uptime), to_seconds(si.uptime));
Maxf41973e2019-01-25 18:40:11 +0100115 value_node_add(vn_sysinfo, "uptime", buf);
Harald Welte32f7a992018-06-04 18:07:33 +0200116
117 return 0;
118}