better sysinfo & console log handler

This commit is contained in:
jr-k 2024-05-26 02:13:31 +02:00
parent 50e83206b5
commit 657370b819
5 changed files with 135 additions and 1 deletions

View File

@ -4,3 +4,4 @@ cron-descriptor
waitress
flask-login
pysqlite3
psutil

View File

@ -12,6 +12,7 @@ from src.service.ModelStore import ModelStore
from src.interface.ObController import ObController
from src.utils import get_ip_address, am_i_in_docker
from src.service.OsInfos import get_all
class SysinfoController(ObController):
@ -23,6 +24,10 @@ class SysinfoController(ObController):
def sysinfo(self):
ipaddr = get_ip_address()
print(self._model_store.logging().get_last_lines_of_stdout(10))
print(get_all())
return render_template(
'sysinfo/list.jinja.html',
ipaddr=ipaddr if ipaddr else self._model_store.lang().map().get('common_unknown_ipaddr'),

View File

@ -1,6 +1,7 @@
import sys
import logging
from io import StringIO
from src.manager.ConfigManager import ConfigManager
@ -18,6 +19,7 @@ class LoggingManager:
self._add_file_handler(file_path=c_map.get('log_file'))
if c_map.get('log_stdout'):
self.console_output = StringIO()
self._add_stdout_handler()
def _add_file_handler(self, file_path: str) -> None:
@ -27,8 +29,12 @@ class LoggingManager:
self._logger.addHandler(file_handler)
def _add_stdout_handler(self) -> None:
console_handler = logging.StreamHandler(sys.stdout)
console_handler = logging.StreamHandler(self.console_output)
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
self._logger.addHandler(console_handler)
def get_last_lines_of_stdout(self, lines: int = 10) -> str:
self.console_output.seek(0)
stdout_lines = self.console_output.readlines()
return ''.join(stdout_lines[-lines:])

111
src/service/OsInfos.py Normal file
View File

@ -0,0 +1,111 @@
import os
import platform
import psutil
import socket
from src.utils import convert_size
def get_rpi_model():
try:
if os.path.exists('/proc/device-tree/model'):
with open('/proc/device-tree/model', 'r') as file:
model = file.read().strip()
return model
else:
return "Not a Raspberry Pi or model information not available."
except Exception as e:
return f"Error retrieving RPi model: {e}"
def get_free_space():
try:
usage = psutil.disk_usage('/')
return convert_size(usage.free)
except Exception as e:
return f"Error retrieving free space: {e}"
def get_memory_usage():
try:
memory_info = psutil.virtual_memory()
return {
'total': convert_size(memory_info.total),
'available': convert_size(memory_info.available),
'percent': memory_info.percent,
'used': convert_size(memory_info.used),
'free': convert_size(memory_info.free)
}
except Exception as e:
return f"Error retrieving memory usage: {e}"
def get_network_info():
try:
addrs = psutil.net_if_addrs()
stats = psutil.net_if_stats()
for iface, addr_list in addrs.items():
if stats[iface].isup:
mac_address = None
ip_address = None
for addr in addr_list:
if addr.family == psutil.AF_LINK:
mac_address = addr.address
elif addr.family == socket.AF_INET:
ip_address = addr.address
if mac_address and ip_address:
return {
'interface': iface,
'mac_address': mac_address,
'ip_address': ip_address
}
return "No active network interface found."
except Exception as e:
return f"Error retrieving network information: {e}"
def get_os_version():
try:
return platform.platform()
except Exception as e:
return f"Error retrieving OS version: {e}"
def get_last_lines_of_logs(logfile, lines):
try:
if not os.path.exists(logfile):
return f"Log file {logfile} does not exist."
with open(logfile, 'r') as file:
logs = file.readlines()
return ''.join(logs[-lines:])
except Exception as e:
return f"Error retrieving log lines: {e}"
def get_default_log_file():
os_type = platform.system()
if os_type == 'Linux':
return '/var/log/syslog'
elif os_type == 'Darwin': # macOS
return '/var/log/system.log'
elif os_type == 'Windows':
return 'C:\\Windows\\System32\\LogFiles\\WMI\\SysEvent.evt'
else:
return None
def get_all():
infos = {}
network_info = get_network_info()
if isinstance(network_info, dict):
infos["Network Interface"] = network_info['interface']
infos["MAC Address"] = network_info['mac_address']
infos["IP Address"] = network_info['ip_address']
return {
"Raspberry Pi Model": get_rpi_model(),
"Storage Free Space": get_free_space(),
"Memory Usage": get_memory_usage(),
"OS Version": get_os_version(),
}

View File

@ -5,6 +5,7 @@ import logging
import subprocess
import unicodedata
import platform
import math
from typing import Optional, List, Dict
@ -223,3 +224,13 @@ def randomize_filename(old_filename: str) -> str:
new_uuid = str(uuid.uuid4())
_, extension = os.path.splitext(old_filename)
return f"{new_uuid}{extension}"
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return f"{s} {size_name[i]}"