summaryrefslogtreecommitdiff
path: root/src/op_mode/cpu.py
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@vyos.io>2022-07-07 09:49:34 -0400
committerDaniil Baturin <daniil@vyos.io>2022-07-07 09:49:34 -0400
commitee5f697065eb1e50ea87bb1a6423b7533c03170a (patch)
tree5fe22f08a8f2b735c335d202d4fd811be02acb30 /src/op_mode/cpu.py
parentdc4b80f1aee3c786889d9e34cf6b54a1a5b83b56 (diff)
downloadvyos-1x-ee5f697065eb1e50ea87bb1a6423b7533c03170a.tar.gz
vyos-1x-ee5f697065eb1e50ea87bb1a6423b7533c03170a.zip
T2719: rework 'show hardware cpu *' commands in the new style
Diffstat (limited to 'src/op_mode/cpu.py')
-rwxr-xr-xsrc/op_mode/cpu.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/op_mode/cpu.py b/src/op_mode/cpu.py
new file mode 100755
index 000000000..cb55ce407
--- /dev/null
+++ b/src/op_mode/cpu.py
@@ -0,0 +1,85 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2016-2022 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 or later 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/>.
+
+import json
+import sys
+import typing
+
+import vyos.cpu
+import vyos.opmode
+
+from jinja2 import Template
+from vyos.util import popen, DEVNULL
+
+cpu_template = Template("""
+{% for cpu in cpus %}
+{% if 'physical id' in cpu %}CPU socket: {{cpu['physical id']}}{% endif %}
+{% if 'vendor_id' in cpu %}CPU Vendor: {{cpu['vendor_id']}}{% endif %}
+{% if 'model name' in cpu %}Model: {{cpu['model name']}}{% endif %}
+{% if 'cpu cores' in cpu %}Cores: {{cpu['cpu cores']}}{% endif %}
+{% if 'cpu MHz' in cpu %}Current MHz: {{cpu['cpu MHz']}}{% endif %}
+{% endfor %}
+""")
+
+cpu_summary_template = Template("""
+Physical CPU cores: {{count}}
+CPU model(s): {{models | join(", ")}}
+""")
+
+def _get_raw_data():
+ return vyos.cpu.get_cpus()
+
+def _format_cpus(cpu_data):
+ env = {'cpus': cpu_data}
+ return cpu_template.render(env).strip()
+
+def _get_summary_data():
+ count = vyos.cpu.get_core_count()
+ cpu_data = vyos.cpu.get_cpus()
+ models = [c['model name'] for c in cpu_data]
+ env = {'count': count, "models": models}
+
+ return env
+
+def _format_cpu_summary(summary_data):
+ return cpu_summary_template.render(summary_data).strip()
+
+def show(raw: bool):
+ cpu_data = _get_raw_data()
+
+ if raw:
+ return cpu_data
+ else:
+ return _format_cpus(cpu_data)
+
+def show_summary(raw: bool):
+ cpu_summary_data = _get_summary_data()
+
+ if raw:
+ return cpu_summary_data
+ else:
+ return _format_cpu_summary(cpu_summary_data)
+
+
+if __name__ == '__main__':
+ try:
+ res = vyos.opmode.run(sys.modules[__name__])
+ if res:
+ print(res)
+ except ValueError as e:
+ print(e)
+ sys.exit(1)
+