From 41ce9913d2af8616841f9d368e22544aeefb1fd7 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Wed, 14 Aug 2019 10:47:11 +0200 Subject: [PATCH] Stats percent (#1205) * Fix stats and add Memory percent * Fix tasks * round percent --- .vscode/tasks.json | 4 ++-- API.md | 4 ++++ hassio/api/addons.py | 2 ++ hassio/api/dns.py | 2 ++ hassio/api/homeassistant.py | 2 ++ hassio/api/supervisor.py | 2 ++ hassio/bootstrap.py | 2 ++ hassio/config.py | 2 +- hassio/const.py | 1 + hassio/docker/stats.py | 22 ++++++++++++++++------ 10 files changed, 34 insertions(+), 9 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c0f3f86b1..ea1a90de5 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -60,7 +60,7 @@ { "label": "Flake8", "type": "shell", - "command": "flake8 homeassistant tests", + "command": "flake8 hassio tests", "group": { "kind": "test", "isDefault": true, @@ -74,7 +74,7 @@ { "label": "Pylint", "type": "shell", - "command": "pylint homeassistant", + "command": "pylint hassio", "dependsOn": [ "Install all Requirements" ], diff --git a/API.md b/API.md index 7a6e7c14c..58e635a72 100644 --- a/API.md +++ b/API.md @@ -105,6 +105,7 @@ Output is the raw docker log. "cpu_percent": 0.0, "memory_usage": 283123, "memory_limit": 329392, + "memory_percent": 1.4, "network_tx": 0, "network_rx": 0, "blk_read": 0, @@ -421,6 +422,7 @@ Proxy to real websocket instance. "cpu_percent": 0.0, "memory_usage": 283123, "memory_limit": 329392, + "memory_percent": 1.4, "network_tx": 0, "network_rx": 0, "blk_read": 0, @@ -595,6 +597,7 @@ Write data to add-on stdin "cpu_percent": 0.0, "memory_usage": 283123, "memory_limit": 329392, + "memory_percent": 1.4, "network_tx": 0, "network_rx": 0, "blk_read": 0, @@ -775,6 +778,7 @@ return: "cpu_percent": 0.0, "memory_usage": 283123, "memory_limit": 329392, + "memory_percent": 1.4, "network_tx": 0, "network_rx": 0, "blk_read": 0, diff --git a/hassio/api/addons.py b/hassio/api/addons.py index ddd524408..c2a3feec3 100644 --- a/hassio/api/addons.py +++ b/hassio/api/addons.py @@ -59,6 +59,7 @@ from ..const import ( ATTR_MAINTAINER, ATTR_MEMORY_LIMIT, ATTR_MEMORY_USAGE, + ATTR_MEMORY_PERCENT, ATTR_NAME, ATTR_NETWORK, ATTR_NETWORK_DESCRIPTION, @@ -307,6 +308,7 @@ class APIAddons(CoreSysAttributes): ATTR_CPU_PERCENT: stats.cpu_percent, ATTR_MEMORY_USAGE: stats.memory_usage, ATTR_MEMORY_LIMIT: stats.memory_limit, + ATTR_MEMORY_PERCENT: stats.memory_percent, ATTR_NETWORK_RX: stats.network_rx, ATTR_NETWORK_TX: stats.network_tx, ATTR_BLK_READ: stats.blk_read, diff --git a/hassio/api/dns.py b/hassio/api/dns.py index 11a1ac239..b643a6539 100644 --- a/hassio/api/dns.py +++ b/hassio/api/dns.py @@ -14,6 +14,7 @@ from ..const import ( ATTR_LATEST_VERSION, ATTR_MEMORY_LIMIT, ATTR_MEMORY_USAGE, + ATTR_MEMORY_PERCENT, ATTR_NETWORK_RX, ATTR_NETWORK_TX, ATTR_SERVERS, @@ -65,6 +66,7 @@ class APICoreDNS(CoreSysAttributes): ATTR_CPU_PERCENT: stats.cpu_percent, ATTR_MEMORY_USAGE: stats.memory_usage, ATTR_MEMORY_LIMIT: stats.memory_limit, + ATTR_MEMORY_PERCENT: stats.memory_percent, ATTR_NETWORK_RX: stats.network_rx, ATTR_NETWORK_TX: stats.network_tx, ATTR_BLK_READ: stats.blk_read, diff --git a/hassio/api/homeassistant.py b/hassio/api/homeassistant.py index 4264fc682..fad4f5424 100644 --- a/hassio/api/homeassistant.py +++ b/hassio/api/homeassistant.py @@ -18,6 +18,7 @@ from ..const import ( ATTR_MACHINE, ATTR_MEMORY_LIMIT, ATTR_MEMORY_USAGE, + ATTR_MEMORY_PERCENT, ATTR_NETWORK_RX, ATTR_NETWORK_TX, ATTR_PASSWORD, @@ -121,6 +122,7 @@ class APIHomeAssistant(CoreSysAttributes): ATTR_CPU_PERCENT: stats.cpu_percent, ATTR_MEMORY_USAGE: stats.memory_usage, ATTR_MEMORY_LIMIT: stats.memory_limit, + ATTR_MEMORY_PERCENT: stats.memory_percent, ATTR_NETWORK_RX: stats.network_rx, ATTR_NETWORK_TX: stats.network_tx, ATTR_BLK_READ: stats.blk_read, diff --git a/hassio/api/supervisor.py b/hassio/api/supervisor.py index fafe40d63..66c6bb632 100644 --- a/hassio/api/supervisor.py +++ b/hassio/api/supervisor.py @@ -25,6 +25,7 @@ from ..const import ( ATTR_LOGO, ATTR_MEMORY_LIMIT, ATTR_MEMORY_USAGE, + ATTR_MEMORY_PERCENT, ATTR_NAME, ATTR_NETWORK_RX, ATTR_NETWORK_TX, @@ -140,6 +141,7 @@ class APISupervisor(CoreSysAttributes): ATTR_CPU_PERCENT: stats.cpu_percent, ATTR_MEMORY_USAGE: stats.memory_usage, ATTR_MEMORY_LIMIT: stats.memory_limit, + ATTR_MEMORY_PERCENT: stats.memory_percent, ATTR_NETWORK_RX: stats.network_rx, ATTR_NETWORK_TX: stats.network_tx, ATTR_BLK_READ: stats.blk_read, diff --git a/hassio/bootstrap.py b/hassio/bootstrap.py index 5463d9ab3..f4f0d8bdc 100644 --- a/hassio/bootstrap.py +++ b/hassio/bootstrap.py @@ -141,6 +141,8 @@ def initialize_system_data(coresys: CoreSys): if bool(os.environ.get("SUPERVISOR_DEV", 0)): _LOGGER.warning("SUPERVISOR_DEV is set") coresys.updater.channel = CHANNEL_DEV + coresys.config.logging = "debug" + coresys.config.debug = True def migrate_system_env(coresys: CoreSys): diff --git a/hassio/config.py b/hassio/config.py index 00c642d9f..a00b741e3 100644 --- a/hassio/config.py +++ b/hassio/config.py @@ -100,7 +100,7 @@ class CoreConfig(JsonConfig): def modify_log_level(self) -> None: """Change log level.""" lvl = getattr(logging, self.logging.upper()) - logging.basicConfig(level=lvl) + logging.getLogger("hassio").setLevel(lvl) @property def last_boot(self): diff --git a/hassio/const.py b/hassio/const.py index 94d84d184..db6984ac9 100644 --- a/hassio/const.py +++ b/hassio/const.py @@ -164,6 +164,7 @@ ATTR_NETWORK_RX = "network_rx" ATTR_NETWORK_TX = "network_tx" ATTR_MEMORY_LIMIT = "memory_limit" ATTR_MEMORY_USAGE = "memory_usage" +ATTR_MEMORY_PERCENT = "memory_percent" ATTR_BLK_READ = "blk_read" ATTR_BLK_WRITE = "blk_write" ATTR_ADDON = "addon" diff --git a/hassio/docker/stats.py b/hassio/docker/stats.py index 9ef46682a..9c630fd69 100644 --- a/hassio/docker/stats.py +++ b/hassio/docker/stats.py @@ -20,6 +20,12 @@ class DockerStats: self._memory_usage = 0 self._memory_limit = 0 + # Calculate percent usage + if self._memory_limit != 0: + self._memory_percent = self._memory_usage / self._memory_limit * 100.0 + else: + self._memory_percent = 0 + with suppress(KeyError): self._calc_cpu_percent(stats) @@ -39,13 +45,12 @@ class DockerStats: stats["cpu_stats"]["system_cpu_usage"] - stats["precpu_stats"]["system_cpu_usage"] ) + online_cpu = stats["cpu_stats"]["online_cpus"] + if online_cpu == 0.0: + online_cpu = len(stats["cpu_stats"]["cpu_usage"]["percpu_usage"]) if system_delta > 0.0 and cpu_delta > 0.0: - self._cpu = ( - (cpu_delta / system_delta) - * len(stats["cpu_stats"]["cpu_usage"]["percpu_usage"]) - * 100.0 - ) + self._cpu = (cpu_delta / system_delta) * online_cpu * 100.0 def _calc_network(self, networks): """Calculate Network IO stats.""" @@ -64,7 +69,7 @@ class DockerStats: @property def cpu_percent(self): """Return CPU percent.""" - return self._cpu + return round(self._cpu, 2) @property def memory_usage(self): @@ -76,6 +81,11 @@ class DockerStats: """Return memory limit.""" return self._memory_limit + @property + def memory_percent(self): + """Return memory usage in percent.""" + return round(self._memory_percent, 2) + @property def network_rx(self): """Return network rx stats."""