Skip to content

Commit

Permalink
Merge pull request #3684 from dmbaturin/T6498-uptime-helpers
Browse files Browse the repository at this point in the history
op mode: T6498: move uptime helpers to vyos.utils.system
  • Loading branch information
jestabro committed Jun 21, 2024
2 parents 9428146 + d91fa43 commit 569b29e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
32 changes: 31 additions & 1 deletion python/vyos/utils/system.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 VyOS maintainers and contributors <maintainers@vyos.io>
# Copyright 2023-2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -98,3 +98,33 @@ def load_as_module(name: str, path: str):
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod

def get_uptime_seconds():
""" Returns system uptime in seconds """
from re import search
from vyos.utils.file import read_file

data = read_file("/proc/uptime")
seconds = search(r"([0-9\.]+)\s", data).group(1)
res = int(float(seconds))

return res

def get_load_averages():
""" Returns load averages for 1, 5, and 15 minutes as a dict """
from re import search
from vyos.utils.file import read_file
from vyos.utils.cpu import get_core_count

data = read_file("/proc/loadavg")
matches = search(r"\s*(?P<one>[0-9\.]+)\s+(?P<five>[0-9\.]+)\s+(?P<fifteen>[0-9\.]+)\s*", data)

core_count = get_core_count()

res = {}
res[1] = float(matches["one"]) / core_count
res[5] = float(matches["five"]) / core_count
res[15] = float(matches["fifteen"]) / core_count

return res

33 changes: 4 additions & 29 deletions src/op_mode/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,14 @@

import vyos.opmode

def _get_uptime_seconds():
from re import search
from vyos.utils.file 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.utils.cpu import get_core_count
from vyos.utils.process 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)

core_count = get_core_count()

res = {}
res[1] = float(matches["one"]) / core_count
res[5] = float(matches["five"]) / core_count
res[15] = float(matches["fifteen"]) / core_count

return res

def _get_raw_data():
from vyos.utils.system import get_uptime_seconds, get_load_averages
from vyos.utils.convert import seconds_to_human

res = {}
res["uptime_seconds"] = _get_uptime_seconds()
res["uptime"] = seconds_to_human(_get_uptime_seconds(), separator=' ')
res["load_average"] = _get_load_averages()
uptime_seconds = get_uptime_seconds()
res["uptime"] = seconds_to_human(uptime_seconds, separator=' ')
res["load_average"] = get_load_averages()

return res

Expand Down

0 comments on commit 569b29e

Please sign in to comment.