2019-04-03 15:40:03 +00:00
|
|
|
"""Support gathering system information of hosts which are running glances."""
|
2016-02-19 05:27:50 +00:00
|
|
|
from datetime import timedelta
|
2018-08-15 05:49:34 +00:00
|
|
|
import logging
|
2015-11-29 21:49:05 +00:00
|
|
|
|
2016-08-19 12:57:14 +00:00
|
|
|
import voluptuous as vol
|
2015-09-14 12:08:30 +00:00
|
|
|
|
2016-08-19 12:57:14 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_SSL,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
CONF_RESOURCES,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2018-08-15 05:49:34 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
2015-09-14 12:08:30 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-08-15 05:49:34 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_VERSION = "version"
|
2016-08-20 22:40:16 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_HOST = "localhost"
|
|
|
|
DEFAULT_NAME = "Glances"
|
|
|
|
DEFAULT_PORT = "61208"
|
2018-08-15 05:49:34 +00:00
|
|
|
DEFAULT_VERSION = 2
|
2015-09-14 12:08:30 +00:00
|
|
|
|
2017-06-27 08:56:25 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
2016-10-01 15:35:32 +00:00
|
|
|
|
2015-09-14 12:08:30 +00:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"disk_use_percent": ["Disk used percent", "%", "mdi:harddisk"],
|
|
|
|
"disk_use": ["Disk used", "GiB", "mdi:harddisk"],
|
|
|
|
"disk_free": ["Disk free", "GiB", "mdi:harddisk"],
|
|
|
|
"memory_use_percent": ["RAM used percent", "%", "mdi:memory"],
|
|
|
|
"memory_use": ["RAM used", "MiB", "mdi:memory"],
|
|
|
|
"memory_free": ["RAM free", "MiB", "mdi:memory"],
|
|
|
|
"swap_use_percent": ["Swap used percent", "%", "mdi:memory"],
|
|
|
|
"swap_use": ["Swap used", "GiB", "mdi:memory"],
|
|
|
|
"swap_free": ["Swap free", "GiB", "mdi:memory"],
|
|
|
|
"processor_load": ["CPU load", "15 min", "mdi:memory"],
|
|
|
|
"process_running": ["Running", "Count", "mdi:memory"],
|
|
|
|
"process_total": ["Total", "Count", "mdi:memory"],
|
|
|
|
"process_thread": ["Thread", "Count", "mdi:memory"],
|
|
|
|
"process_sleeping": ["Sleeping", "Count", "mdi:memory"],
|
|
|
|
"cpu_use_percent": ["CPU used", "%", "mdi:memory"],
|
|
|
|
"cpu_temp": ["CPU Temp", TEMP_CELSIUS, "mdi:thermometer"],
|
|
|
|
"docker_active": ["Containers active", "", "mdi:docker"],
|
|
|
|
"docker_cpu_use": ["Containers CPU used", "%", "mdi:docker"],
|
|
|
|
"docker_memory_use": ["Containers RAM used", "MiB", "mdi:docker"],
|
2015-09-14 12:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_SSL, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
|
|
|
|
vol.Optional(CONF_RESOURCES, default=["disk_use"]): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_VERSION, default=DEFAULT_VERSION): vol.In([2, 3]),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-08-15 05:49:34 +00:00
|
|
|
"""Set up the Glances sensors."""
|
|
|
|
from glances_api import Glances
|
|
|
|
|
2018-11-21 11:38:42 +00:00
|
|
|
name = config[CONF_NAME]
|
|
|
|
host = config[CONF_HOST]
|
|
|
|
port = config[CONF_PORT]
|
|
|
|
version = config[CONF_VERSION]
|
|
|
|
var_conf = config[CONF_RESOURCES]
|
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
ssl = config[CONF_SSL]
|
|
|
|
verify_ssl = config[CONF_VERIFY_SSL]
|
|
|
|
|
|
|
|
session = async_get_clientsession(hass, verify_ssl)
|
2018-08-15 05:49:34 +00:00
|
|
|
glances = GlancesData(
|
2019-07-31 19:25:30 +00:00
|
|
|
Glances(
|
|
|
|
hass.loop,
|
|
|
|
session,
|
|
|
|
host=host,
|
|
|
|
port=port,
|
|
|
|
version=version,
|
|
|
|
username=username,
|
|
|
|
password=password,
|
|
|
|
ssl=ssl,
|
|
|
|
)
|
|
|
|
)
|
2018-08-15 05:49:34 +00:00
|
|
|
|
|
|
|
await glances.async_update()
|
|
|
|
|
|
|
|
if glances.api.data is None:
|
|
|
|
raise PlatformNotReady
|
2015-09-14 12:08:30 +00:00
|
|
|
|
|
|
|
dev = []
|
2015-11-14 14:23:20 +00:00
|
|
|
for resource in var_conf:
|
2018-08-15 05:49:34 +00:00
|
|
|
dev.append(GlancesSensor(glances, name, resource))
|
2015-09-14 12:08:30 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(dev, True)
|
2015-09-14 12:08:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GlancesSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Implementation of a Glances sensor."""
|
2015-09-14 12:08:30 +00:00
|
|
|
|
2018-08-15 05:49:34 +00:00
|
|
|
def __init__(self, glances, name, sensor_type):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2018-08-15 05:49:34 +00:00
|
|
|
self.glances = glances
|
2015-12-15 22:12:43 +00:00
|
|
|
self._name = name
|
2015-09-14 12:08:30 +00:00
|
|
|
self.type = sensor_type
|
2017-10-23 11:12:14 +00:00
|
|
|
self._state = None
|
2015-09-14 12:08:30 +00:00
|
|
|
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the name of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "{} {}".format(self._name, SENSOR_TYPES[self.type][0])
|
2015-09-14 12:08:30 +00:00
|
|
|
|
2017-10-23 07:54:57 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return SENSOR_TYPES[self.type][2]
|
|
|
|
|
2015-09-14 12:08:30 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit the value is expressed in."""
|
2015-09-14 12:08:30 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2017-06-21 20:45:15 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Could the device be accessed during the last update call."""
|
2018-08-15 05:49:34 +00:00
|
|
|
return self.glances.available
|
2017-06-21 20:45:15 +00:00
|
|
|
|
2015-09-14 12:08:30 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the resources."""
|
2017-06-27 08:56:25 +00:00
|
|
|
return self._state
|
|
|
|
|
2018-08-15 05:49:34 +00:00
|
|
|
async def async_update(self):
|
2017-06-27 08:56:25 +00:00
|
|
|
"""Get the latest data from REST API."""
|
2018-08-15 05:49:34 +00:00
|
|
|
await self.glances.async_update()
|
|
|
|
value = self.glances.api.data
|
2015-09-14 12:08:30 +00:00
|
|
|
|
|
|
|
if value is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.type == "disk_use_percent":
|
|
|
|
self._state = value["fs"][0]["percent"]
|
|
|
|
elif self.type == "disk_use":
|
|
|
|
self._state = round(value["fs"][0]["used"] / 1024 ** 3, 1)
|
|
|
|
elif self.type == "disk_free":
|
2015-11-18 18:19:27 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = round(value["fs"][0]["free"] / 1024 ** 3, 1)
|
2015-11-18 18:19:27 +00:00
|
|
|
except KeyError:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = round(
|
|
|
|
(value["fs"][0]["size"] - value["fs"][0]["used"]) / 1024 ** 3, 1
|
|
|
|
)
|
|
|
|
elif self.type == "memory_use_percent":
|
|
|
|
self._state = value["mem"]["percent"]
|
|
|
|
elif self.type == "memory_use":
|
|
|
|
self._state = round(value["mem"]["used"] / 1024 ** 2, 1)
|
|
|
|
elif self.type == "memory_free":
|
|
|
|
self._state = round(value["mem"]["free"] / 1024 ** 2, 1)
|
|
|
|
elif self.type == "swap_use_percent":
|
|
|
|
self._state = value["memswap"]["percent"]
|
|
|
|
elif self.type == "swap_use":
|
|
|
|
self._state = round(value["memswap"]["used"] / 1024 ** 3, 1)
|
|
|
|
elif self.type == "swap_free":
|
|
|
|
self._state = round(value["memswap"]["free"] / 1024 ** 3, 1)
|
|
|
|
elif self.type == "processor_load":
|
2017-03-27 20:11:15 +00:00
|
|
|
# Windows systems don't provide load details
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = value["load"]["min15"]
|
2017-03-27 20:11:15 +00:00
|
|
|
except KeyError:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = value["cpu"]["total"]
|
|
|
|
elif self.type == "process_running":
|
|
|
|
self._state = value["processcount"]["running"]
|
|
|
|
elif self.type == "process_total":
|
|
|
|
self._state = value["processcount"]["total"]
|
|
|
|
elif self.type == "process_thread":
|
|
|
|
self._state = value["processcount"]["thread"]
|
|
|
|
elif self.type == "process_sleeping":
|
|
|
|
self._state = value["processcount"]["sleeping"]
|
|
|
|
elif self.type == "cpu_use_percent":
|
|
|
|
self._state = value["quicklook"]["cpu"]
|
|
|
|
elif self.type == "cpu_temp":
|
|
|
|
for sensor in value["sensors"]:
|
|
|
|
if sensor["label"] in [
|
2019-09-19 07:55:07 +00:00
|
|
|
"amdgpu 1",
|
|
|
|
"aml_thermal",
|
|
|
|
"Core 0",
|
|
|
|
"Core 1",
|
2019-07-31 19:25:30 +00:00
|
|
|
"CPU Temperature",
|
2019-09-19 07:55:07 +00:00
|
|
|
"CPU",
|
2019-07-31 19:25:30 +00:00
|
|
|
"cpu-thermal 1",
|
2019-09-19 07:55:07 +00:00
|
|
|
"cpu_thermal 1",
|
2019-07-31 19:25:30 +00:00
|
|
|
"exynos-therm 1",
|
2019-09-19 07:55:07 +00:00
|
|
|
"Package id 0",
|
|
|
|
"Physical id 0",
|
|
|
|
"radeon 1",
|
2019-07-31 19:25:30 +00:00
|
|
|
"soc-thermal 1",
|
2019-09-19 07:55:07 +00:00
|
|
|
"soc_thermal 1",
|
2019-07-31 19:25:30 +00:00
|
|
|
]:
|
|
|
|
self._state = sensor["value"]
|
|
|
|
elif self.type == "docker_active":
|
2018-03-10 17:11:53 +00:00
|
|
|
count = 0
|
2019-04-07 17:07:05 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
for container in value["docker"]["containers"]:
|
|
|
|
if (
|
|
|
|
container["Status"] == "running"
|
|
|
|
or "Up" in container["Status"]
|
|
|
|
):
|
2019-04-07 17:07:05 +00:00
|
|
|
count += 1
|
|
|
|
self._state = count
|
|
|
|
except KeyError:
|
|
|
|
self._state = count
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "docker_cpu_use":
|
2019-04-07 17:07:05 +00:00
|
|
|
cpu_use = 0.0
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
for container in value["docker"]["containers"]:
|
|
|
|
if (
|
|
|
|
container["Status"] == "running"
|
|
|
|
or "Up" in container["Status"]
|
|
|
|
):
|
|
|
|
cpu_use += container["cpu"]["total"]
|
2019-04-07 17:07:05 +00:00
|
|
|
self._state = round(cpu_use, 1)
|
|
|
|
except KeyError:
|
|
|
|
self._state = STATE_UNAVAILABLE
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "docker_memory_use":
|
2019-04-07 17:07:05 +00:00
|
|
|
mem_use = 0.0
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
for container in value["docker"]["containers"]:
|
|
|
|
if (
|
|
|
|
container["Status"] == "running"
|
|
|
|
or "Up" in container["Status"]
|
|
|
|
):
|
|
|
|
mem_use += container["memory"]["usage"]
|
|
|
|
self._state = round(mem_use / 1024 ** 2, 1)
|
2019-04-07 17:07:05 +00:00
|
|
|
except KeyError:
|
|
|
|
self._state = STATE_UNAVAILABLE
|
2015-09-14 12:08:30 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class GlancesData:
|
2016-03-08 15:46:34 +00:00
|
|
|
"""The class for handling the data retrieval."""
|
|
|
|
|
2018-08-15 05:49:34 +00:00
|
|
|
def __init__(self, api):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the data object."""
|
2018-08-15 05:49:34 +00:00
|
|
|
self.api = api
|
|
|
|
self.available = True
|
2015-09-14 12:08:30 +00:00
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
2018-08-15 05:49:34 +00:00
|
|
|
async def async_update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from the Glances REST API."""
|
2018-08-15 05:49:34 +00:00
|
|
|
from glances_api.exceptions import GlancesApiError
|
|
|
|
|
2015-09-14 12:08:30 +00:00
|
|
|
try:
|
2018-08-15 05:49:34 +00:00
|
|
|
await self.api.get_data()
|
|
|
|
self.available = True
|
|
|
|
except GlancesApiError:
|
|
|
|
_LOGGER.error("Unable to fetch data from Glances")
|
|
|
|
self.available = False
|