1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

69 lines
2.1 KiB
Python
Raw Normal View History

"""Utility functions for gathering system information."""
# Standard Library
import os
import platform
2021-07-03 23:02:14 -07:00
from typing import Dict, Tuple, Union
# Third Party
import psutil as _psutil
from cpuinfo import get_cpu_info as _get_cpu_info # type: ignore
# Project
from hyperglass.constants import __version__
2021-07-03 23:02:14 -07:00
# Local
from .frontend import get_node_version
2021-07-03 23:02:14 -07:00
SystemData = Dict[str, Tuple[Union[str, int], str]]
def _cpu() -> SystemData:
"""Construct CPU Information."""
cpu_info = _get_cpu_info()
brand = cpu_info.get("brand_raw", "")
cores_logical = _psutil.cpu_count()
cores_raw = _psutil.cpu_count(logical=False)
cpu_ghz = _psutil.cpu_freq().current / 1000
return (brand, cores_logical, cores_raw, cpu_ghz)
2021-07-03 23:02:14 -07:00
def _memory() -> SystemData:
"""Construct RAM Information."""
mem_info = _psutil.virtual_memory()
total_gb = round(mem_info.total / 1e9, 2)
usage_percent = mem_info.percent
return (total_gb, usage_percent)
2021-07-03 23:02:14 -07:00
def _disk() -> SystemData:
"""Construct Disk Information."""
disk_info = _psutil.disk_usage("/")
total_gb = round(disk_info.total / 1e9, 2)
usage_percent = disk_info.percent
return (total_gb, usage_percent)
2021-07-03 23:02:14 -07:00
def get_system_info() -> SystemData:
"""Get system info."""
2021-07-03 23:02:14 -07:00
cpu_info, cpu_logical, cpu_physical, cpu_speed = _cpu()
mem_total, mem_usage = _memory()
disk_total, disk_usage = _disk()
2021-07-03 23:02:14 -07:00
return {
"hyperglass Version": (__version__, "text"),
"hyperglass Path": (os.environ["hyperglass_directory"], "code"),
"Python Version": (platform.python_version(), "code"),
"Node Version": (".".join(str(v) for v in get_node_version()), "code"),
"Platform Info": (platform.platform(), "code"),
"CPU Info": (cpu_info, "text"),
"Logical Cores": (cpu_logical, "code"),
"Physical Cores": (cpu_physical, "code"),
"Processor Speed": (f"{cpu_speed}GHz", "code"),
"Total Memory": (f"{mem_total} GB", "text"),
"Memory Utilization": (f"{mem_usage}%", "text"),
"Total Disk Space": (f"{disk_total} GB", "text"),
"Disk Utilization": (f"{disk_usage}%", "text"),
}