diff options
author | Christian Poessinger <christian@poessinger.com> | 2021-10-25 21:02:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-25 21:02:17 +0200 |
commit | 316ae2ae4e7e06cbb3a59cc09002d1c48e3e5f07 (patch) | |
tree | fc4f6ad13fea094c7883760e48a620fa1f6cb33f /src/op_mode | |
parent | 8cf5a4f023c5459cad4c84e93f73a9ddd69be81a (diff) | |
parent | c2866603751f85b98b88d445874e6f1945647c30 (diff) | |
download | vyos-1x-316ae2ae4e7e06cbb3a59cc09002d1c48e3e5f07.tar.gz vyos-1x-316ae2ae4e7e06cbb3a59cc09002d1c48e3e5f07.zip |
Merge pull request #1041 from dmbaturin/T3938
T3938: rewrite the uptime script in Python
Diffstat (limited to 'src/op_mode')
-rwxr-xr-x | src/op_mode/show_uptime.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/op_mode/show_uptime.py b/src/op_mode/show_uptime.py new file mode 100755 index 000000000..c3dea52e6 --- /dev/null +++ b/src/op_mode/show_uptime.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2021 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# 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, see <http://www.gnu.org/licenses/>. + +def get_uptime_seconds(): + from re import search + from vyos.util import read_file + + data = read_file("/proc/uptime") + seconds = search("([0-9\.]+)\s", data).group(1) + + return int(float(seconds)) + +def get_load_averages(): + from re import search + from vyos.util import cmd + + data = cmd("uptime") + matches = search(r"load average:\s*(?P<one>[0-9\.]+)\s*,\s*(?P<five>[0-9\.]+)\s*,\s*(?P<fifteen>[0-9\.]+)\s*", data) + + res = {} + res[1] = float(matches["one"]) + res[5] = float(matches["five"]) + res[15] = float(matches["fifteen"]) + + return res + +if __name__ == '__main__': + from vyos.util import seconds_to_human + + print("Uptime: {}\n".format(seconds_to_human(get_uptime_seconds()))) + + avgs = get_load_averages() + + print("Load averages:") + print("1 minute: {:.02f}%".format(avgs[1]*100)) + print("5 minutes: {:.02f}%".format(avgs[5]*100)) + print("15 minutes: {:.02f}%".format(avgs[15]*100)) |