refactor: unify windows and linux scripts
This commit is contained in:
parent
8b2e8fa1b3
commit
4c9d3f9ffb
2 changed files with 128 additions and 138 deletions
185
sysmon.py
185
sysmon.py
|
@ -1,81 +1,152 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
from datetime import datetime
|
|
||||||
from datetime import timedelta
|
|
||||||
import psutil
|
import psutil
|
||||||
import serial
|
import serial
|
||||||
from time import sleep
|
import platform
|
||||||
import sensors
|
from time import sleep, time
|
||||||
import subprocess
|
from datetime import datetime
|
||||||
import math
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
CPU_TEMP_PATH = '/sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon2/temp1_input'
|
if platform.system() == "Windows":
|
||||||
|
import win32com.client
|
||||||
|
|
||||||
file_descriptor = '/dev/ttyUSB0'
|
# Constants
|
||||||
baud_rate = 9600
|
FILE_DESCRIPTOR = 'COM3' if platform.system() == 'Windows' else '/dev/ttyUSB0'
|
||||||
|
BAUD_RATE = 9600
|
||||||
|
SERIAL_TIMEOUT = 1 # Timeout for serial operations in seconds
|
||||||
|
CONNECTION_WAIT = 6 # Time to wait for the serial connection to establish
|
||||||
|
SEND_INTERVAL = 2 # Interval between sending data in seconds
|
||||||
|
|
||||||
arduino = serial.Serial(port=file_descriptor, baudrate=baud_rate)
|
# CPU temperature path for Linux
|
||||||
sleep(6)
|
#CPU_TEMP_PATH = '/sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon4/temp1_input'
|
||||||
|
CPU_TEMP_PATH = os.environ.get('CPU_TEMP_PATH', '/sys/devices/pci0000:00/0000:00:18.3/hwmon/hwmon3/temp1_input')
|
||||||
|
|
||||||
def send_command(command):
|
# Open serial connection
|
||||||
print("sent: " + command)
|
def open_serial_connection():
|
||||||
arduino.write(command.encode())
|
try:
|
||||||
sleep(0.1)
|
return serial.Serial(port=FILE_DESCRIPTOR, baudrate=BAUD_RATE, timeout=SERIAL_TIMEOUT)
|
||||||
|
except serial.SerialException as e:
|
||||||
def set_time(date_time):
|
sys.exit(f"Failed to connect to the serial port: {e}")
|
||||||
curr_time=date_time.strftime('%H:%M:%S')
|
|
||||||
return curr_time
|
|
||||||
|
|
||||||
|
|
||||||
def set_max():
|
# Send command to Arduino
|
||||||
ram_max = int(psutil.virtual_memory().total / (1024.*1024.))
|
def send_command(arduino, command):
|
||||||
# request = 'RamMax=%d\r\n' % ram_max
|
try:
|
||||||
# send_command(request)
|
arduino.write(command.encode())
|
||||||
return ram_max
|
sleep(0.1)
|
||||||
|
except serial.SerialException as e:
|
||||||
|
print(f"Failed to send command: {e}")
|
||||||
|
|
||||||
def set_free():
|
|
||||||
ram_free = int(psutil.virtual_memory().free / (1024.*1024.))
|
|
||||||
# request = 'RamFree=%d\r\n' % ram_free
|
|
||||||
# send_command(request)
|
|
||||||
return ram_free
|
|
||||||
|
|
||||||
def set_OS():
|
# Get the current time
|
||||||
with open('/etc/os-release', 'r') as f:
|
def get_time():
|
||||||
for line in f:
|
return datetime.now().strftime('%H:%M:%S')
|
||||||
if line.startswith('NAME='):
|
|
||||||
OSName = line.split('"')[1]
|
|
||||||
break
|
|
||||||
return OSName
|
|
||||||
|
|
||||||
def set_kernel_version():
|
|
||||||
with open('/proc/version', 'r') as f:
|
|
||||||
for line in f:
|
|
||||||
Kernel = line.split(' ')[2].split('-')[0]
|
|
||||||
break
|
|
||||||
return Kernel
|
|
||||||
|
|
||||||
def set_uptime():
|
# Get RAM information
|
||||||
|
def get_ram_info():
|
||||||
|
ram = psutil.virtual_memory()
|
||||||
|
return int(ram.total / (1024 * 1024)), int(ram.available / (1024 * 1024))
|
||||||
|
|
||||||
|
|
||||||
|
# Get OS name
|
||||||
|
def get_os_name():
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
return platform.system()
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
with open('/etc/os-release') as f:
|
||||||
|
for line in f:
|
||||||
|
if line.startswith('NAME='):
|
||||||
|
return line.split('=')[1].strip().strip('"')
|
||||||
|
except FileNotFoundError:
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
|
|
||||||
|
# Get kernel version
|
||||||
|
def get_kernel_version():
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
return platform.version()
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
with open('/proc/version') as f:
|
||||||
|
return f.readline().split()[2].split('-')[0]
|
||||||
|
except FileNotFoundError:
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
|
|
||||||
|
# Get system uptime
|
||||||
|
def get_uptime():
|
||||||
uptime_seconds = psutil.boot_time()
|
uptime_seconds = psutil.boot_time()
|
||||||
uptime = datetime.now() - datetime.fromtimestamp(uptime_seconds)
|
uptime = datetime.now() - datetime.fromtimestamp(uptime_seconds)
|
||||||
uptime_hours = int(uptime.total_seconds() // 3600)
|
hours, remainder = divmod(int(uptime.total_seconds()), 3600)
|
||||||
uptime_minutes = int((uptime.total_seconds() % 3600) // 60)
|
minutes = remainder // 60
|
||||||
uptime = f"{uptime_hours:02d}:{uptime_minutes:02d}"
|
return f"{hours:02d}:{minutes:02d}"
|
||||||
return uptime
|
|
||||||
|
|
||||||
def start():
|
|
||||||
while 1:
|
|
||||||
cpu_temp = int(subprocess.check_output(['cat', CPU_TEMP_PATH]))/1000
|
|
||||||
cpu_pct = psutil.cpu_percent(interval=None, percpu=False)
|
|
||||||
cpu_global = int(cpu_pct)
|
|
||||||
request = "\nCpuTemp="+str(math.trunc(cpu_temp))+",CpuUsage="+str(math.trunc(cpu_global))+",RamMax="+str(set_max())+",RamFree="+str(set_free())+",Time="+str(set_time(datetime.now()))+",OS="+str(set_OS())+",Kernel="+str(set_kernel_version())+",Uptime="+str(set_uptime())
|
|
||||||
send_command(request)
|
|
||||||
sleep(2)
|
|
||||||
|
|
||||||
|
|
||||||
|
# Get CPU temperature
|
||||||
|
def get_cpu_temp():
|
||||||
|
if platform.system() == 'Windows':
|
||||||
|
# For Windows, use WMI to get CPU temperature
|
||||||
|
wmi = win32com.client.Dispatch("WbemScripting.SWbemLocator")
|
||||||
|
service = wmi.ConnectServer(".", "root\cimv2")
|
||||||
|
temperature_info = service.ExecQuery(
|
||||||
|
"SELECT * FROM Win32_TemperatureProbe")
|
||||||
|
for sensor in temperature_info:
|
||||||
|
if sensor.CurrentReading is not None:
|
||||||
|
# Convert from tenths of Kelvin to Celsius
|
||||||
|
return sensor.CurrentReading / 10.0 - 273.15
|
||||||
|
return 0 # Return 0 if there's no temperature sensor data
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
with open(CPU_TEMP_PATH) as f:
|
||||||
|
return int(f.read().strip()) / 1000
|
||||||
|
except (FileNotFoundError, ValueError):
|
||||||
|
return 0 # Return 0 if there's an error reading CPU temperature
|
||||||
|
|
||||||
|
|
||||||
|
# Main data gathering and sending function
|
||||||
|
def gather_and_send_data(arduino, ram_max, os_name, kernel_version):
|
||||||
|
while True:
|
||||||
|
start_time = time()
|
||||||
|
try:
|
||||||
|
cpu_temp = int(get_cpu_temp())
|
||||||
|
cpu_usage = int(psutil.cpu_percent(interval=None))
|
||||||
|
_, ram_free = get_ram_info()
|
||||||
|
current_time = get_time()
|
||||||
|
uptime = get_uptime()
|
||||||
|
|
||||||
|
request = (f"\nCpuTemp={cpu_temp},CpuUsage={cpu_usage},RamMax={ram_max},"
|
||||||
|
f"RamFree={ram_free},Time={current_time},OS={os_name},"
|
||||||
|
f"Kernel={kernel_version},Uptime={uptime}")
|
||||||
|
|
||||||
|
send_command(arduino, request)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"An error occurred: {e}")
|
||||||
|
|
||||||
|
elapsed_time = time() - start_time
|
||||||
|
if elapsed_time < SEND_INTERVAL:
|
||||||
|
sleep(SEND_INTERVAL - elapsed_time)
|
||||||
|
|
||||||
|
|
||||||
|
# Main function to handle program start
|
||||||
def main():
|
def main():
|
||||||
start()
|
arduino = open_serial_connection()
|
||||||
|
sleep(CONNECTION_WAIT) # Wait for the connection to establish
|
||||||
|
|
||||||
|
ram_max, _ = get_ram_info()
|
||||||
|
os_name = get_os_name()
|
||||||
|
kernel_version = get_kernel_version()
|
||||||
|
|
||||||
|
try:
|
||||||
|
gather_and_send_data(arduino, ram_max, os_name, kernel_version)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Interrupted by user")
|
||||||
|
finally:
|
||||||
|
if arduino.is_open:
|
||||||
|
arduino.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,81 +0,0 @@
|
||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import sys
|
|
||||||
from datetime import datetime
|
|
||||||
from datetime import timedelta
|
|
||||||
import psutil
|
|
||||||
import serial
|
|
||||||
from time import sleep
|
|
||||||
import math
|
|
||||||
import platform
|
|
||||||
import clr
|
|
||||||
clr.AddReference(r'OpenHardwareMonitorLib')
|
|
||||||
from OpenHardwareMonitor import Hardware
|
|
||||||
|
|
||||||
file_descriptor = 'COM5'
|
|
||||||
baud_rate = 9600
|
|
||||||
|
|
||||||
arduino = serial.Serial(port=file_descriptor, baudrate=baud_rate)
|
|
||||||
sleep(6)
|
|
||||||
|
|
||||||
def send_command(command):
|
|
||||||
print("sent: " + command)
|
|
||||||
arduino.write(command.encode())
|
|
||||||
sleep(0.1)
|
|
||||||
|
|
||||||
def set_time(date_time):
|
|
||||||
curr_time=date_time.strftime('%H:%M:%S')
|
|
||||||
return curr_time
|
|
||||||
|
|
||||||
|
|
||||||
def set_max():
|
|
||||||
ram_max = int(psutil.virtual_memory().total / (1024.*1024.))
|
|
||||||
return ram_max
|
|
||||||
|
|
||||||
def set_free():
|
|
||||||
ram_free = int(psutil.virtual_memory().free / (1024.*1024.))
|
|
||||||
return ram_free
|
|
||||||
|
|
||||||
def set_OS():
|
|
||||||
OSName = platform.uname()[0]
|
|
||||||
return OSName
|
|
||||||
|
|
||||||
def set_kernel_version():
|
|
||||||
Kernel = platform.uname().version
|
|
||||||
return Kernel
|
|
||||||
|
|
||||||
def set_uptime():
|
|
||||||
uptime_seconds = psutil.boot_time()
|
|
||||||
uptime = datetime.now() - datetime.fromtimestamp(uptime_seconds)
|
|
||||||
uptime_hours = int(uptime.total_seconds() // 3600)
|
|
||||||
uptime_minutes = int((uptime.total_seconds() % 3600) // 60)
|
|
||||||
uptime = f"{uptime_hours:02d}:{uptime_minutes:02d}"
|
|
||||||
return uptime
|
|
||||||
|
|
||||||
def get_cpu_temperature():
|
|
||||||
computer = Hardware.Computer()
|
|
||||||
computer.Open()
|
|
||||||
computer.CPUEnabled = True
|
|
||||||
|
|
||||||
for hardware in computer.Hardware:
|
|
||||||
if hardware.HardwareType == Hardware.HardwareType.CPU:
|
|
||||||
hardware.Update()
|
|
||||||
for sensor in hardware.Sensors:
|
|
||||||
if sensor.SensorType == Hardware.SensorType.Temperature and sensor.Name == "CPU Package":
|
|
||||||
return sensor.Value
|
|
||||||
|
|
||||||
def start():
|
|
||||||
while 1:
|
|
||||||
cpu_temp = get_cpu_temperature()
|
|
||||||
cpu_pct = psutil.cpu_percent(interval=None, percpu=False)
|
|
||||||
cpu_global = int(cpu_pct)
|
|
||||||
request = "\nCpuTemp="+str(cpu_temp)+",CpuUsage="+str(math.trunc(cpu_global))+",RamMax="+str(set_max())+",RamFree="+str(set_free())+",Time="+str(set_time(datetime.now()))+",OS="+str(set_OS())+",Kernel="+str(set_kernel_version())+",Uptime="+str(set_uptime())
|
|
||||||
send_command(request)
|
|
||||||
sleep(2)
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
start()
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
Loading…
Reference in a new issue