Support for CPU load_average entity.

This commit is contained in:
Fabio Manganiello 2023-04-18 18:26:02 +02:00
parent 1cee0459cf
commit 0073239a40
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 18 additions and 3 deletions

View file

@ -1,10 +1,11 @@
import socket
from datetime import datetime
from typing import Union, List, Optional, Dict
from typing import Tuple, Union, List, Optional, Dict
from typing_extensions import override
from platypush.entities import Entity
from platypush.entities.devices import Device
from platypush.entities.managers import EntityManager
from platypush.entities.sensors import NumericSensor, PercentSensor
from platypush.entities.system import (
@ -194,7 +195,7 @@ class SystemPlugin(SensorPlugin, EntityManager):
return self._cpu_frequency_per_cpu() if per_cpu else self._cpu_frequency_avg()
@action
def load_avg(self) -> List[float]:
def load_avg(self) -> Tuple[float, float, float]:
"""
Get the average load as a vector that represents the load within the last 1, 5 and 15 minutes.
"""
@ -792,6 +793,7 @@ class SystemPlugin(SensorPlugin, EntityManager):
'cpu': {
'frequency': self._cpu_frequency_avg(),
'info': self._cpu_info,
'load_avg': self.load_avg().output,
'stats': self._cpu_stats(),
'times': self._cpu_times_avg(),
'percent': self.cpu_percent().output / 100.0, # type: ignore
@ -839,6 +841,18 @@ class SystemPlugin(SensorPlugin, EntityManager):
for key, time_percent in cpu['times'].items()
],
),
Device(
id='system:cpu:load_avg',
name='Load Average',
children=[
NumericSensor(
id=f'system:cpu:load_avg:{mins}',
name=f'Last {mins} minute(s)',
value=round(val, 2),
)
for val, mins in zip(cpu['load_avg'], [1, 5, 15])
],
),
NumericSensor(
id='system:cpu:frequency',
name='Frequency',

View file

@ -1,5 +1,5 @@
from dataclasses import dataclass, field
from typing import List, Optional
from typing import List, Optional, Tuple
from marshmallow import pre_load
from marshmallow.validate import Range
@ -222,6 +222,7 @@ class CpuData:
times: CpuTimes
frequency: CpuFrequency
stats: CpuStats
load_avg: Tuple[float, float, float]
percent: float = percent_field()