blob: 50616bce647151e654cc840f70325b47f6e7964b [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.
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 <string.h>
25#include <sys/sysinfo.h>
26
27#include <osmocom/vty/vty.h>
28#include <osmocom/vty/command.h>
29
30#include "osysmon.h"
31#include "value_node.h"
32
33/***********************************************************************
34 * Runtime Code
35 ***********************************************************************/
36
37/* called once on startup before config file parsing */
38int osysmon_sysinfo_init()
39{
40 return 0;
41}
42
43static float loadfac(unsigned long in) {
44 return in/65536.0;
45}
46
47#define to_mbytes(in) ((in)/((1024*1024)/si.mem_unit))
48
49#define SECS_PER_MIN 60UL
50#define MINS_PER_HOUR 60UL
51#define HOURS_PER_DAY 24UL
52
53#define to_days(in) ((in)/(SECS_PER_MIN*MINS_PER_HOUR*HOURS_PER_DAY))
54#define to_hours(in) (((in)/(SECS_PER_MIN*MINS_PER_HOUR))%HOURS_PER_DAY)
55#define to_minutes(in) (((in)/(SECS_PER_MIN))%MINS_PER_HOUR)
56#define to_seconds(in) ((in)%SECS_PER_MIN)
57
58/* called periodically */
59int osysmon_sysinfo_poll(struct value_node *parent)
60{
61 struct sysinfo si;
62 struct value_node *vn_sysinfo;
63 char buf[32];
64 int rc;
65
66 vn_sysinfo = value_node_add(parent, parent, "sysinfo", NULL);
67
68 rc = sysinfo(&si);
69 if (rc < 0)
70 return rc;
71
72 /* Load Factor 1/5/15min */
73 snprintf(buf, sizeof(buf), "%.2f/%.2f/%.2f",
74 loadfac(si.loads[0]), loadfac(si.loads[1]), loadfac(si.loads[2]));
75 value_node_add(vn_sysinfo, vn_sysinfo, "load", buf);
76
77 /* RAM information (total/free/sared) in megabytes */
78 snprintf(buf, sizeof(buf), "%lu/%lu/%lu",
79 to_mbytes(si.totalram), to_mbytes(si.freeram), to_mbytes(si.sharedram));
80 value_node_add(vn_sysinfo, vn_sysinfo, "ram", buf);
81
82 /* uptime in days/hours/minutes/seconds */
83 snprintf(buf, sizeof(buf), "%lud %02lu:%02lu:%02lu", to_days(si.uptime),
84 to_hours(si.uptime), to_minutes(si.uptime), to_seconds(si.uptime));
85 value_node_add(vn_sysinfo, vn_sysinfo, "uptime", buf);
86
87 return 0;
88}