hwmonitor/hwmon-collector/init.py

101 lines
2.8 KiB
Python

#
# Copyright (C) John Solntsev <johnsol@internet.ru> (2024)
#
# This file is part of hwmon-getter.
#
# hwmon-getter is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# hwmon-getter is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with hwmon-getter. If not, see <https://www.gnu.org/licenses/>.
#
import socket
import json
from config import SERVERS
def parse_json_data(func):
def wrapper(*args, **kwargs):
return json.loads(func(*args, **kwargs))
return wrapper
@parse_json_data
def get_from_server(server_addr, server_port=9900):
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_sock.connect((server_addr, server_port))
client_sock.send(b"GET_MON_ALL")
data = client_sock.recv(65535)
client_sock.close()
return data.decode("utf-8")
def print_val(elem_dict, print_name, print_value, print_rate):
if print_name:
print("\n {} = ".format(elem_dict["name"]), end="")
if print_value:
print("{}".format(elem_dict["value"]), end="")
if print_rate:
print("{}".format(elem_dict["rate"]), end="")
else:
print("None", end="")
print()
def server_data_parse(server_data):
for sensor_name in [*server_data]:
server_sensor = server_data[sensor_name]
if len(server_sensor) == 0:
print("- {} - None".format(sensor_name))
else:
print("- {}:".format(sensor_name))
for val in [*server_sensor]:
elem = server_sensor[val]
print_name = False
print_value = False
print_rate = False
print(" - {}: ".format(val), end="")
if type(elem) is dict:
if "name" in elem.keys():
print_name = True
if "value" in elem.keys():
print_value = True
if "rate" in elem.keys():
print_rate = True
else:
print_value = True
elem = {"value": elem}
print_val(elem, print_name, print_value, print_rate)
print()
if __name__ == "__main__":
for server in [*SERVERS]:
print("{:=^40}\n".format(f"[ {SERVERS[server]['name']} ]"))
server_data = get_from_server(server)
server_data_parse(server_data)