2020-07-19 14:42:54 -07:00
|
|
|
"""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
|
2020-07-19 14:42:54 -07:00
|
|
|
|
|
|
|
# Third Party
|
|
|
|
import psutil as _psutil
|
2021-09-07 22:58:39 -07:00
|
|
|
from cpuinfo import get_cpu_info as _get_cpu_info # type: ignore
|
2020-07-19 14:42:54 -07:00
|
|
|
|
|
|
|
# Project
|
|
|
|
from hyperglass.constants import __version__
|
|
|
|
|
2021-07-03 23:02:14 -07:00
|
|
|
# Local
|
|
|
|
from .frontend import get_node_version
|
2020-07-19 14:42:54 -07:00
|
|
|
|
2021-07-03 23:02:14 -07:00
|
|
|
SystemData = Dict[str, Tuple[Union[str, int], str]]
|
|
|
|
|
|
|
|
|
|
|
|
def _cpu() -> SystemData:
|
2020-07-19 14:42:54 -07:00
|
|
|
"""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:
|
2020-07-19 14:42:54 -07:00
|
|
|
"""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:
|
2020-07-19 14:42:54 -07:00
|
|
|
"""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:
|
2020-07-19 14:42:54 -07:00
|
|
|
"""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()
|
2020-07-19 14:42:54 -07:00
|
|
|
|
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"),
|
|
|
|
}
|