Defined new entity and schema for CpuInfo.

This commit is contained in:
Fabio Manganiello 2023-04-15 01:23:24 +02:00
parent 186a21f715
commit 3e3c48d779
Signed by: blacklight
GPG key ID: D90FBA7F76362774
3 changed files with 451 additions and 239 deletions

View file

@ -0,0 +1,36 @@
from sqlalchemy import Column, ForeignKey, Integer, JSON, String
from platypush.common.db import Base
from . import Entity
if 'cpu_info' not in Base.metadata:
class CpuInfo(Entity):
"""
``CpuInfo`` ORM model.
"""
__tablename__ = 'cpu_info'
id = Column(
Integer, ForeignKey(Entity.id, ondelete='CASCADE'), primary_key=True
)
architecture = Column(String)
bits = Column(Integer)
cores = Column(Integer)
vendor = Column(String)
brand = Column(String)
frequency_advertised = Column(Integer)
frequency_actual = Column(Integer)
flags = Column(JSON)
l1_instruction_cache_size = Column(Integer)
l1_data_cache_size = Column(Integer)
l2_cache_size = Column(Integer)
l3_cache_size = Column(Integer)
__mapper_args__ = {
'polymorphic_identity': __tablename__,
}

View file

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Optional, List, Union from typing import Optional, List
from platypush.message.response import Response from platypush.message.response import Response
@ -28,60 +28,25 @@ class SensorResponse(SystemResponse):
pass pass
class CpuInfoResponse(CpuResponse):
def __init__(self,
arch: str,
bits: int,
count: int,
vendor_id: str,
brand: str,
hz_advertised: int,
hz_actual: int,
model: int,
flags: List[str],
family: Optional[int],
stepping: Optional[int],
l1_instruction_cache_size: Optional[Union[int, str]],
l1_data_cache_size: Optional[Union[int, str]],
l2_cache_size: Optional[Union[int, str]],
l3_cache_size: Optional[Union[int, str]],
*args, **kwargs):
super().__init__(
*args, output={
'arch': arch,
'bits': bits,
'count': count,
'vendor_id': vendor_id,
'brand': brand,
'hz_advertised': hz_advertised,
'hz_actual': hz_actual,
'stepping': stepping,
'model': model,
'family': family,
'flags': flags,
'l1_instruction_cache_size': l1_instruction_cache_size,
'l1_data_cache_size': l1_data_cache_size,
'l2_cache_size': l2_cache_size,
'l3_cache_size': l3_cache_size,
}, **kwargs
)
class CpuTimesResponse(CpuResponse): class CpuTimesResponse(CpuResponse):
def __init__(self, def __init__(
user: float, self,
nice: float, user: float,
system: float, nice: float,
idle: float, system: float,
iowait: float, idle: float,
irq: float, iowait: float,
softirq: float, irq: float,
steal: float, softirq: float,
guest: float, steal: float,
guest_nice: float, guest: float,
*args, **kwargs): guest_nice: float,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'user': user, 'user': user,
'nice': nice, 'nice': nice,
'system': system, 'system': system,
@ -92,58 +57,66 @@ class CpuTimesResponse(CpuResponse):
'steal': steal, 'steal': steal,
'guest': guest, 'guest': guest,
'guest_nice': guest_nice, 'guest_nice': guest_nice,
}, **kwargs },
**kwargs
) )
class CpuStatsResponse(CpuResponse): class CpuStatsResponse(CpuResponse):
def __init__(self, def __init__(
ctx_switches: int, self,
interrupts: int, ctx_switches: int,
soft_interrupts: int, interrupts: int,
syscalls: int, soft_interrupts: int,
*args, **kwargs): syscalls: int,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'ctx_switches': ctx_switches, 'ctx_switches': ctx_switches,
'interrupts': interrupts, 'interrupts': interrupts,
'soft_interrupts': soft_interrupts, 'soft_interrupts': soft_interrupts,
'syscalls': syscalls, 'syscalls': syscalls,
}, **kwargs },
**kwargs
) )
class CpuFrequencyResponse(CpuResponse): class CpuFrequencyResponse(CpuResponse):
# noinspection PyShadowingBuiltins # noinspection PyShadowingBuiltins
def __init__(self, def __init__(self, min: int, max: int, current: int, *args, **kwargs):
min: int,
max: int,
current: int,
*args, **kwargs):
super().__init__( super().__init__(
*args, output={ *args,
output={
'min': min, 'min': min,
'max': max, 'max': max,
'current': current, 'current': current,
}, **kwargs },
**kwargs
) )
class VirtualMemoryUsageResponse(MemoryResponse): class VirtualMemoryUsageResponse(MemoryResponse):
def __init__(self, def __init__(
total: int, self,
available: int, total: int,
percent: float, available: int,
used: int, percent: float,
free: int, used: int,
active: int, free: int,
inactive: int, active: int,
buffers: int, inactive: int,
cached: int, buffers: int,
shared: int, cached: int,
*args, **kwargs): shared: int,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'total': total, 'total': total,
'available': available, 'available': available,
'percent': percent, 'percent': percent,
@ -154,82 +127,102 @@ class VirtualMemoryUsageResponse(MemoryResponse):
'buffers': buffers, 'buffers': buffers,
'cached': cached, 'cached': cached,
'shared': shared, 'shared': shared,
}, **kwargs },
**kwargs
) )
class SwapMemoryUsageResponse(MemoryResponse): class SwapMemoryUsageResponse(MemoryResponse):
def __init__(self, def __init__(
total: int, self,
percent: float, total: int,
used: int, percent: float,
free: int, used: int,
sin: int, free: int,
sout: int, sin: int,
*args, **kwargs): sout: int,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'total': total, 'total': total,
'percent': percent, 'percent': percent,
'used': used, 'used': used,
'free': free, 'free': free,
'sin': sin, 'sin': sin,
'sout': sout, 'sout': sout,
}, **kwargs },
**kwargs
) )
class DiskPartitionResponse(DiskResponse): class DiskPartitionResponse(DiskResponse):
def __init__(self, def __init__(
device: str, self,
mount_point: str, device: str,
fstype: Optional[str] = None, mount_point: str,
opts: Optional[str] = None, fstype: Optional[str] = None,
*args, **kwargs): opts: Optional[str] = None,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
'device': device, output={
'mount_point': mount_point, 'device': device,
'fstype': fstype, 'mount_point': mount_point,
'opts': opts, 'fstype': fstype,
}, **kwargs 'opts': opts,
},
**kwargs
) )
class DiskUsageResponse(DiskResponse): class DiskUsageResponse(DiskResponse):
def __init__(self, def __init__(
path: str, self,
total: int, path: str,
used: int, total: int,
free: int, used: int,
percent: float, free: int,
*args, **kwargs): percent: float,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
'path': path, output={
'total': total, 'path': path,
'used': used, 'total': total,
'free': free, 'used': used,
'percent': percent, 'free': free,
}, **kwargs 'percent': percent,
},
**kwargs
) )
class DiskIoCountersResponse(DiskResponse): class DiskIoCountersResponse(DiskResponse):
def __init__(self, def __init__(
read_count: int, self,
write_count: int, read_count: int,
read_bytes: int, write_count: int,
write_bytes: int, read_bytes: int,
read_time: int, write_bytes: int,
write_time: int, read_time: int,
read_merged_count: int, write_time: int,
write_merged_count: int, read_merged_count: int,
busy_time: int, write_merged_count: int,
disk: Optional[str] = None, busy_time: int,
*args, **kwargs): disk: Optional[str] = None,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'read_count': read_count, 'read_count': read_count,
'write_count': write_count, 'write_count': write_count,
'read_bytes': read_bytes, 'read_bytes': read_bytes,
@ -240,24 +233,29 @@ class DiskIoCountersResponse(DiskResponse):
'write_merged_count': write_merged_count, 'write_merged_count': write_merged_count,
'busy_time': busy_time, 'busy_time': busy_time,
'disk': disk, 'disk': disk,
}, **kwargs },
**kwargs
) )
class NetworkIoCountersResponse(NetworkResponse): class NetworkIoCountersResponse(NetworkResponse):
def __init__(self, def __init__(
bytes_sent: int, self,
bytes_recv: int, bytes_sent: int,
packets_sent: int, bytes_recv: int,
packets_recv: int, packets_sent: int,
errin: int, packets_recv: int,
errout: int, errin: int,
dropin: int, errout: int,
dropout: int, dropin: int,
nic: Optional[str] = None, dropout: int,
*args, **kwargs): nic: Optional[str] = None,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'bytes_sent': bytes_sent, 'bytes_sent': bytes_sent,
'bytes_recv': bytes_recv, 'bytes_recv': bytes_recv,
'packets_sent': packets_sent, 'packets_sent': packets_sent,
@ -267,25 +265,30 @@ class NetworkIoCountersResponse(NetworkResponse):
'dropin': dropin, 'dropin': dropin,
'dropout': dropout, 'dropout': dropout,
'nic': nic, 'nic': nic,
}, **kwargs },
**kwargs
) )
class NetworkConnectionResponse(NetworkResponse): class NetworkConnectionResponse(NetworkResponse):
# noinspection PyShadowingBuiltins # noinspection PyShadowingBuiltins
def __init__(self, def __init__(
fd: int, self,
family: str, fd: int,
type: str, family: str,
local_address: str, type: str,
local_port: int, local_address: str,
remote_address: str, local_port: int,
remote_port: int, remote_address: str,
status: str, remote_port: int,
pid: int, status: str,
*args, **kwargs): pid: int,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'fd': fd, 'fd': fd,
'family': family, 'family': family,
'type': type, 'type': type,
@ -295,25 +298,30 @@ class NetworkConnectionResponse(NetworkResponse):
'remote_port': remote_port, 'remote_port': remote_port,
'status': status, 'status': status,
'pid': pid, 'pid': pid,
}, **kwargs },
**kwargs
) )
class NetworkAddressResponse(NetworkResponse): class NetworkAddressResponse(NetworkResponse):
def __init__(self, def __init__(
nic: str, self,
ipv4_address: Optional[str] = None, nic: str,
ipv4_netmask: Optional[str] = None, ipv4_address: Optional[str] = None,
ipv4_broadcast: Optional[str] = None, ipv4_netmask: Optional[str] = None,
ipv6_address: Optional[str] = None, ipv4_broadcast: Optional[str] = None,
ipv6_netmask: Optional[str] = None, ipv6_address: Optional[str] = None,
ipv6_broadcast: Optional[str] = None, ipv6_netmask: Optional[str] = None,
mac_address: Optional[str] = None, ipv6_broadcast: Optional[str] = None,
mac_broadcast: Optional[str] = None, mac_address: Optional[str] = None,
ptp: Optional[str] = None, mac_broadcast: Optional[str] = None,
*args, **kwargs): ptp: Optional[str] = None,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'nic': nic, 'nic': nic,
'ipv4_address': ipv4_address, 'ipv4_address': ipv4_address,
'ipv4_netmask': ipv4_netmask, 'ipv4_netmask': ipv4_netmask,
@ -324,123 +332,136 @@ class NetworkAddressResponse(NetworkResponse):
'mac_address': mac_address, 'mac_address': mac_address,
'mac_broadcast': mac_broadcast, 'mac_broadcast': mac_broadcast,
'ptp': ptp, 'ptp': ptp,
}, **kwargs },
**kwargs
) )
class NetworkInterfaceStatsResponse(NetworkResponse): class NetworkInterfaceStatsResponse(NetworkResponse):
def __init__(self, def __init__(
nic: str, self, nic: str, is_up: bool, duplex: str, speed: int, mtu: int, *args, **kwargs
is_up: bool, ):
duplex: str,
speed: int,
mtu: int,
*args, **kwargs):
super().__init__( super().__init__(
*args, output={ *args,
output={
'nic': nic, 'nic': nic,
'is_up': is_up, 'is_up': is_up,
'duplex': duplex, 'duplex': duplex,
'speed': speed, 'speed': speed,
'mtu': mtu, 'mtu': mtu,
}, **kwargs },
**kwargs
) )
class SensorTemperatureResponse(SensorResponse): class SensorTemperatureResponse(SensorResponse):
def __init__(self, def __init__(
name: str, self,
current: float, name: str,
high: Optional[float] = None, current: float,
critical: Optional[float] = None, high: Optional[float] = None,
label: Optional[str] = None, critical: Optional[float] = None,
*args, **kwargs): label: Optional[str] = None,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'name': name, 'name': name,
'current': current, 'current': current,
'high': high, 'high': high,
'critical': critical, 'critical': critical,
'label': label, 'label': label,
}, **kwargs },
**kwargs
) )
class SensorFanResponse(SensorResponse): class SensorFanResponse(SensorResponse):
def __init__(self, def __init__(
name: str, self, name: str, current: int, label: Optional[str] = None, *args, **kwargs
current: int, ):
label: Optional[str] = None,
*args, **kwargs):
super().__init__( super().__init__(
*args, output={ *args,
output={
'name': name, 'name': name,
'current': current, 'current': current,
'label': label, 'label': label,
}, **kwargs },
**kwargs
) )
class SensorBatteryResponse(SensorResponse): class SensorBatteryResponse(SensorResponse):
def __init__(self, def __init__(
percent: float, self, percent: float, secs_left: int, power_plugged: bool, *args, **kwargs
secs_left: int, ):
power_plugged: bool,
*args, **kwargs):
super().__init__( super().__init__(
*args, output={ *args,
output={
'percent': percent, 'percent': percent,
'secs_left': secs_left, 'secs_left': secs_left,
'power_plugged': power_plugged, 'power_plugged': power_plugged,
}, **kwargs },
**kwargs
) )
class ConnectUserResponse(SystemResponse): class ConnectUserResponse(SystemResponse):
def __init__(self, def __init__(
name: str, self,
terminal: str, name: str,
host: str, terminal: str,
started: datetime, host: str,
pid: Optional[int] = None, started: datetime,
*args, **kwargs): pid: Optional[int] = None,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'name': name, 'name': name,
'terminal': terminal, 'terminal': terminal,
'host': host, 'host': host,
'started': started, 'started': started,
'pid': pid, 'pid': pid,
}, **kwargs },
**kwargs
) )
class ProcessResponse(SystemResponse): class ProcessResponse(SystemResponse):
def __init__(self, def __init__(
pid: int, self,
name: str, pid: int,
started: datetime, name: str,
ppid: Optional[int], started: datetime,
children: Optional[List[int]] = None, ppid: Optional[int],
exe: Optional[List[str]] = None, children: Optional[List[int]] = None,
status: Optional[str] = None, exe: Optional[List[str]] = None,
username: Optional[str] = None, status: Optional[str] = None,
terminal: Optional[str] = None, username: Optional[str] = None,
cpu_user_time: Optional[float] = None, terminal: Optional[str] = None,
cpu_system_time: Optional[float] = None, cpu_user_time: Optional[float] = None,
cpu_children_user_time: Optional[float] = None, cpu_system_time: Optional[float] = None,
cpu_children_system_time: Optional[float] = None, cpu_children_user_time: Optional[float] = None,
mem_rss: Optional[int] = None, cpu_children_system_time: Optional[float] = None,
mem_vms: Optional[int] = None, mem_rss: Optional[int] = None,
mem_shared: Optional[int] = None, mem_vms: Optional[int] = None,
mem_text: Optional[int] = None, mem_shared: Optional[int] = None,
mem_data: Optional[int] = None, mem_text: Optional[int] = None,
mem_lib: Optional[int] = None, mem_data: Optional[int] = None,
mem_dirty: Optional[int] = None, mem_lib: Optional[int] = None,
mem_percent: Optional[float] = None, mem_dirty: Optional[int] = None,
*args, **kwargs): mem_percent: Optional[float] = None,
*args,
**kwargs
):
super().__init__( super().__init__(
*args, output={ *args,
output={
'pid': pid, 'pid': pid,
'name': name, 'name': name,
'started': started, 'started': started,
@ -462,7 +483,8 @@ class ProcessResponse(SystemResponse):
'mem_dirty': mem_dirty, 'mem_dirty': mem_dirty,
'mem_percent': mem_percent, 'mem_percent': mem_percent,
'children': children or [], 'children': children or [],
}, **kwargs },
**kwargs
) )

154
platypush/schemas/system.py Normal file
View file

@ -0,0 +1,154 @@
from dataclasses import dataclass, field
from typing import List, Optional
from marshmallow import pre_load
from marshmallow_dataclass import class_schema
from platypush.schemas.dataclasses import DataClassSchema
class CpuInfoBaseSchema(DataClassSchema):
"""
Base schema for CPU info.
"""
@pre_load
def pre_load(self, data: dict, **_) -> dict:
if data.get('hz_advertised'):
data['frequency_advertised'] = data.pop('hz_advertised')[0]
if data.get('hz_actual'):
data['frequency_actual'] = data.pop('hz_actual')[0]
return data
@dataclass
class CpuInfo:
"""
CPU info data class.
"""
architecture: Optional[str] = field(
metadata={
'data_key': 'arch_string_raw',
'metadata': {
'description': 'CPU architecture',
'example': 'x86_64',
},
}
)
bits: int = field(
metadata={
'metadata': {
'description': 'CPU bits / register size',
'example': 64,
}
}
)
cores: int = field(
metadata={
'data_key': 'count',
'metadata': {
'description': 'Number of cores',
'example': 4,
},
}
)
vendor: Optional[str] = field(
metadata={
'data_key': 'vendor_id_raw',
'metadata': {
'description': 'Vendor string',
'example': 'GenuineIntel',
},
}
)
brand: Optional[str] = field(
metadata={
'data_key': 'brand_raw',
'metadata': {
'description': 'CPU brand string',
'example': 'Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz',
},
}
)
frequency_advertised: Optional[int] = field(
metadata={
'metadata': {
'description': 'Advertised CPU frequency, in Hz',
'example': 2400000000,
}
}
)
frequency_actual: Optional[int] = field(
metadata={
'metadata': {
'description': 'Actual CPU frequency, in Hz',
'example': 2350000000,
}
}
)
flags: List[str] = field(
metadata={
'metadata': {
'description': 'CPU flags',
'example': ['acpi', 'aes', 'cpuid'],
}
}
)
l1_instruction_cache_size: Optional[int] = field(
metadata={
'metadata': {
'description': 'Size of the L1 instruction cache, in bytes',
'example': 65536,
}
}
)
l1_data_cache_size: Optional[int] = field(
metadata={
'metadata': {
'description': 'Size of the L1 data cache, in bytes',
'example': 65536,
}
}
)
l2_cache_size: Optional[int] = field(
metadata={
'metadata': {
'description': 'Size of the L2 cache, in bytes',
'example': 524288,
}
}
)
l3_cache_size: Optional[int] = field(
metadata={
'metadata': {
'description': 'Size of the L2 cache, in bytes',
'example': 4194304,
}
}
)
@dataclass
class SystemInfo:
"""
Aggregate system info dataclass.
"""
cpu_info: CpuInfo
CpuInfoSchema = class_schema(CpuInfo, base_schema=CpuInfoBaseSchema)
SystemInfoSchema = class_schema(SystemInfo, base_schema=DataClassSchema)