Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 1 | /* 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 */ |
| 38 | int osysmon_sysinfo_init() |
| 39 | { |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static 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 */ |
| 59 | int 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 | |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame] | 66 | vn_sysinfo = value_node_add(parent, "sysinfo", NULL); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 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])); |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame] | 75 | value_node_add(vn_sysinfo, "load", buf); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 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)); |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame] | 80 | value_node_add(vn_sysinfo, "ram", buf); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 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)); |
Max | f41973e | 2019-01-25 18:40:11 +0100 | [diff] [blame] | 85 | value_node_add(vn_sysinfo, "uptime", buf); |
Harald Welte | 32f7a99 | 2018-06-04 18:07:33 +0200 | [diff] [blame] | 86 | |
| 87 | return 0; |
| 88 | } |