summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2022-09-09 08:03:52 +0200
committerGitHub <noreply@github.com>2022-09-09 08:03:52 +0200
commit1c45ecfc963253123b8d7bada5971a30edda4f53 (patch)
tree58d235969d429ec7154914e3f4aba9de24eed649
parent627cfc6d6733998a721ac97f9f5bc44c4c2c6797 (diff)
parentc9e1392379958222211d9794a3751a129bbff75a (diff)
downloadvyos-1x-1c45ecfc963253123b8d7bada5971a30edda4f53.tar.gz
vyos-1x-1c45ecfc963253123b8d7bada5971a30edda4f53.zip
Merge pull request #1528 from jestabro/standardize-show-uptime
system: T4681: convert 'show_uptime.py' script to standardized format
-rw-r--r--data/op-mode-standardized.json1
-rw-r--r--op-mode-definitions/show-system.xml.in4
-rwxr-xr-xsrc/op_mode/uptime.py (renamed from src/op_mode/show_uptime.py)36
3 files changed, 29 insertions, 12 deletions
diff --git a/data/op-mode-standardized.json b/data/op-mode-standardized.json
index db13eeb5a..e120336f3 100644
--- a/data/op-mode-standardized.json
+++ b/data/op-mode-standardized.json
@@ -9,6 +9,7 @@
"openconnect.py",
"route.py",
"ipsec.py",
+"uptime.py",
"version.py",
"vrf.py"
]
diff --git a/op-mode-definitions/show-system.xml.in b/op-mode-definitions/show-system.xml.in
index 60ed28b6f..6962ad374 100644
--- a/op-mode-definitions/show-system.xml.in
+++ b/op-mode-definitions/show-system.xml.in
@@ -142,7 +142,7 @@
<properties>
<help>Show summary of system processes</help>
</properties>
- <command>${vyos_op_scripts_dir}/show_uptime.py</command>
+ <command>${vyos_op_scripts_dir}/uptime.py show</command>
</leafNode>
<leafNode name="tree">
<properties>
@@ -168,7 +168,7 @@
<properties>
<help>Show system uptime and load averages</help>
</properties>
- <command>${vyos_op_scripts_dir}/show_uptime.py</command>
+ <command>${vyos_op_scripts_dir}/uptime.py show</command>
</leafNode>
</children>
</node>
diff --git a/src/op_mode/show_uptime.py b/src/op_mode/uptime.py
index b70c60cf8..2ebe6783b 100755
--- a/src/op_mode/show_uptime.py
+++ b/src/op_mode/uptime.py
@@ -14,7 +14,11 @@
# 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():
+import sys
+
+import vyos.opmode
+
+def _get_uptime_seconds():
from re import search
from vyos.util import read_file
@@ -23,7 +27,7 @@ def get_uptime_seconds():
return int(float(seconds))
-def get_load_averages():
+def _get_load_averages():
from re import search
from vyos.util import cmd
from vyos.cpu import get_core_count
@@ -40,19 +44,17 @@ def get_load_averages():
return res
-def get_raw_data():
+def _get_raw_data():
from vyos.util import seconds_to_human
res = {}
- res["uptime_seconds"] = get_uptime_seconds()
- res["uptime"] = seconds_to_human(get_uptime_seconds())
- res["load_average"] = get_load_averages()
+ res["uptime_seconds"] = _get_uptime_seconds()
+ res["uptime"] = seconds_to_human(_get_uptime_seconds())
+ res["load_average"] = _get_load_averages()
return res
-def get_formatted_output():
- data = get_raw_data()
-
+def _get_formatted_output(data):
out = "Uptime: {}\n\n".format(data["uptime"])
avgs = data["load_average"]
out += "Load averages:\n"
@@ -62,5 +64,19 @@ def get_formatted_output():
return out
+def show(raw: bool):
+ uptime_data = _get_raw_data()
+
+ if raw:
+ return uptime_data
+ else:
+ return _get_formatted_output(uptime_data)
+
if __name__ == '__main__':
- print(get_formatted_output())
+ try:
+ res = vyos.opmode.run(sys.modules[__name__])
+ if res:
+ print(res)
+ except (ValueError, vyos.opmode.Error) as e:
+ print(e)
+ sys.exit(1)