Skip to content

Commit

Permalink
General refactoring, adding env vars to windows monitoring scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkhamLee committed Apr 4, 2024
1 parent b9bf7f0 commit e8bc910
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 150 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
/hardwareSensors/opi5plus.py
/windows/hardwareData.log
/windows/config/secrets.json
/windows/config/windowsConfig.json
/windows/config/windowsConfig.json
/windows/hardwareDataWindows.log
/windows/__pycache__/*
/linux/__pycache__/*
/linux/config/config.json
Expand Down
2 changes: 1 addition & 1 deletion common/device_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import psutil
import uuid
from paho.mqtt import client as mqtt
from logging_util import logger
from common.logging_util import logger


class DeviceUtilities():
Expand Down
14 changes: 7 additions & 7 deletions common/nvidia_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def gpu_query():
query = ("temperature.gpu,utilization.gpu,memory.used,power.draw,"
"clocks.current.graphics,encoder.stats.averageFps")

data = NvidiaSensors.smiParser(query)
data = NvidiaSensors.smi_parser(query)

# split out each value from the returned list of values

Expand All @@ -66,7 +66,7 @@ def gpu_query():
def gpu_load():

query = "utilization.gpu"
data = NvidiaSensors.smiParser(query)
data = NvidiaSensors.smi_parser(query)
data = int(data[0])

return data
Expand All @@ -75,7 +75,7 @@ def gpu_load():
def gpu_temp():

query = "temperature.gpu"
data = NvidiaSensors.smiParser(query)
data = NvidiaSensors.smi_parser(query)
data = int(data[0])

return data
Expand All @@ -84,7 +84,7 @@ def gpu_temp():
def vram_used():

query = "memory.used"
data = NvidiaSensors.smiParser(query)
data = NvidiaSensors.smi_parser(query)
data = round((float(data[0]) / 1024), 2)

return data
Expand All @@ -93,7 +93,7 @@ def vram_used():
def gpu_power():

query = 'power.draw'
data = NvidiaSensors.smiParser(query)
data = NvidiaSensors.smi_parser(query)
data = round((float(data[0])) / 100, 2)

return data
Expand All @@ -102,7 +102,7 @@ def gpu_power():
def gpu_fps():

query = 'encoder.stats.averageFps'
data = NvidiaSensors.smiParser(query)
data = NvidiaSensors.smi_parser(query)
data = int(data[0])

return data
Expand All @@ -111,7 +111,7 @@ def gpu_fps():
def gpu_clock():

query = 'clocks.current.graphics'
data = NvidiaSensors.smiParser(query)
data = NvidiaSensors.smi_parser(query)
data = int(data[0])

return data
56 changes: 3 additions & 53 deletions linux/linux_cpu_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def get_temps(self, index=0):

if self.core_count > 1:

temp_payload = self.buildPayload(psutil.sensors_temperatures()
['coretemp'], index=0)
temp_payload = self.build_payload(psutil.sensors_temperatures()
['coretemp'], index=0)

else:
core_temp = \
Expand Down Expand Up @@ -70,7 +70,7 @@ def get_freq(self, all_cpu=False):
# get frequency per core
def freq_per_core(self, all_cpu=True):

per_core_freq = self.buildPayload(psutil.cpu_freq(percpu=all_cpu))
per_core_freq = self.build_payload(psutil.cpu_freq(percpu=all_cpu))

return per_core_freq

Expand All @@ -90,56 +90,6 @@ def get_ram_data(self):

return ram_use

# acquiring temperature sensor data for Rockchip 3588 devices
@staticmethod
def sys_temps():

soc_temp = psutil.sensors_temperatures()['soc_thermal'][0].current
big_core_0temp = psutil.sensors_temperatures()['bigcore0_thermal'][0].\
current
big_core_1temp = psutil.sensors_temperatures()['bigcore1_thermal'][0].\
current
little_core_temp = psutil.\
sensors_temperatures()['littlecore_thermal'][0].current
center_temp = \
psutil.sensors_temperatures()['center_thermal'][0].current
gpu_temp = psutil.sensors_temperatures()['gpu_thermal'][0].current
npu_temp = psutil.sensors_temperatures()['npu_thermal'][0].current
nvme_temp = psutil.sensors_temperatures()['nvme'][0].current

return soc_temp, big_core_0temp, big_core_1temp, little_core_temp, \
center_temp, gpu_temp, npu_temp, nvme_temp

# CPU frequencies for the various cores of a Rockchip 3588 device
@staticmethod
def get_rock_chip_3588_freqs():

freq = psutil.cpu_freq(percpu=True)
little_core = freq[0].current
big_core0 = freq[1].current
big_core1 = freq[2].current

return little_core, big_core0, big_core1

# get CPU temp for Raspberry Pi 4B
@staticmethod
def get_rpi_4b_temps():

rpi_cpu_temp = psutil.sensors_temperatures()['cpu_thermal'][0].current

return rpi_cpu_temp

@staticmethod
def rockchip_3566_temps():

return psutil.sensors_temperatures()['cpu_thermal'][0].current, \
psutil.sensors_temperatures()['gpu_thermal'][0].current

@staticmethod
def libre_lepotato_temps():

return psutil.sensors_temperatures()['scpi_sensors'][0].current

@staticmethod
def amd_linux_data():

Expand Down
8 changes: 4 additions & 4 deletions linux/monitor_amd_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ def main():
MQTT_PORT = int(os.environ['MQTT_PORT'])

# get unique client ID
client_id = device_utilities.getClientID()
client_id = device_utilities.get_client_id()

# get mqtt client
client, code = device_utilities.mqttClient(client_id, MQTT_USER,
MQTT_SECRET, MQTT_BROKER,
MQTT_PORT)
client, code = device_utilities.mqtt_client(client_id, MQTT_USER,
MQTT_SECRET, MQTT_BROKER,
MQTT_PORT)

# instantiate CPU & GPU data classes
get_data = LinuxCpuData()
Expand Down
23 changes: 10 additions & 13 deletions linux/monitor_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def monitor(client: object, get_data: object, TOPIC: str, INTERVAL: int):
ram_use = get_data.get_ram_data()

# get current freq and core count
cpu_freq, coreCount = get_data.getFreq()
cpu_freq, core_count = get_data.get_freq()

# get CPU temperature
cpu_temp = get_data.core_temp()
Expand Down Expand Up @@ -64,13 +64,10 @@ def monitor(client: object, get_data: object, TOPIC: str, INTERVAL: int):
def main():

# instantiate utilities class
deviceUtilities = DeviceUtilities()
device_utilities = DeviceUtilities()

# parse command line arguments
args = sys.argv[1:]

TOPIC = args[0]
INTERVAL = args[1]
TOPIC = os.environ['TOPIC']
INTERVAL = os.environ['INTERVAL']

# load environmental variables
MQTT_BROKER = os.environ["MQTT_BROKER"]
Expand All @@ -79,19 +76,19 @@ def main():
MQTT_PORT = int(os.environ['MQTT_PORT'])

# get unique client ID
clientID = deviceUtilities.getClientID()
client_id = device_utilities.get_client_id()

# get mqtt client
client, code = deviceUtilities.mqttClient(clientID, MQTT_USER,
MQTT_SECRET, MQTT_BROKER,
MQTT_PORT)
client, code = device_utilities.mqtt_client(client_id, MQTT_USER,
MQTT_SECRET, MQTT_BROKER,
MQTT_PORT)

# instantiate CPU & GPU data classes
getData = LinuxCpuData()
get_data = LinuxCpuData()

# start data monitoring
try:
monitor(client, getData, TOPIC, INTERVAL)
monitor(client, get_data, TOPIC, INTERVAL)

finally:
client.loop_stop()
Expand Down
8 changes: 4 additions & 4 deletions linux/monitor_linux_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ def main():
MQTT_PORT = int(os.environ['MQTT_PORT'])

# get unique client ID
client_id = device_utilities.getClientID()
client_id = device_utilities.get_client_id()

# get mqtt client
client, code = device_utilities.mqttClient(client_id, MQTT_USER,
MQTT_SECRET, MQTT_BROKER,
MQTT_PORT)
client, code = device_utilities.mqtt_client(client_id, MQTT_USER,
MQTT_SECRET, MQTT_BROKER,
MQTT_PORT)

# instantiate CPU & GPU data classes
get_gpu_data = NvidiaSensors()
Expand Down
82 changes: 42 additions & 40 deletions windows/monitor_windows_gpu.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Markham Lee (C) 2023
# Markham Lee (C) 2023 - 2024
# Hardware Monitor for Linux & Windows:
# https://github.com/MarkhamLee/HardwareMonitoring
# script to retrieve Windows data and publish it to an MQTT topic
# leverages psutil + the LibreHardwareMonitor library

import json
import time
import gc
import os
import logging
import os
import sys
import time
from windows_data import WindowsSensors

# this allows us to import modules, classes, scripts et al from the
Expand All @@ -27,77 +26,80 @@
: %(message)s')


def monitor(client: object, cpu_data: object, gpu_data: object, topic: str):
def monitor(client: object, cpu_data: object, gpu_data: object, TOPIC: str):

logging.debug('Windows HW monitoring started....')

logging.debug('Windows HW monitoring started')
INTERVAL = int(os.environ['GAME_INTERVAL'])

while True:

# get CPU data clock speed and temperature
bigF_freq, littleFreq, cpuTemp = cpu_data.getLibreData()
big_freq, little_freq, cpu_temp = cpu_data.get_libre_data()

# get GPU data
gpuTemp, gpuUtilization, vramUse, gpuPower, \
gpuClock = gpu_data.gpuQuery()
gpu_temp, gpu_utilization, vram_use, gpu_power, \
gpu_clock = gpu_data.gpu_query()

# get CPU load
cpuLoad = cpu_data.getCPUData()
cpu_load = cpu_data.get_cpu_data()

# get RAM use
ramUse = cpu_data.getRAM()
ram_use = cpu_data.get_ram()

payload = {
"gpuTemp": gpuTemp,
"cpuTemp": cpuTemp,
"cpuFreqBig": bigFreq,
"cpuFreqLittle": littleFreq,
"gpuLoad": gpuUtilization,
"cpuLoad": cpuLoad,
"ramUtilization": ramUse,
"vramUtilization": vramUse,
"gpuClock": gpuClock,
"gpuPower": gpuPower
"gpuTemp": gpu_temp,
"cpuTemp": cpu_temp,
"cpuFreqBig": big_freq,
"cpuFreqLittle": little_freq,
"gpuLoad": gpu_utilization,
"cpuLoad": cpu_load,
"ramUtilization": ram_use,
"vramUtilization": vram_use,
"gpuClock": gpu_clock,
"gpuPower": gpu_power
}

payload = json.dumps(payload)

result = client.publish(topic, payload)
result = client.publish(TOPIC, payload)
status = result[0]
if status == 0:
print(f'Data {payload} was published to: {topic} with status:\
print(f'Data {payload} was published to: {TOPIC} with status:\
{status}')
else:
print(f'Failed to send {payload} to: {topic}')
print(f'Failed to send {payload} to: {TOPIC}')

del payload, gpuTemp, cpuTemp, bigFreq, littleFreq, gpuUtilization, \
cpuLoad, ramUse, vramUse,
gpuClock, gpuPower
del payload, gpu_temp, cpu_temp, big_freq, little_freq, \
gpu_utilization, cpu_load, ram_use, vram_use, \
gpu_clock, gpu_power

gc.collect()
time.sleep(1)
time.sleep(INTERVAL)


def main():

# instantiate utilities class
device_utilities = DeviceUtilities()

# parse command line arguments
args = sys.argv[1:]

configFile = args[0]
secrets = args[1]
# operating parameters
TOPIC = os.environ['GAME_TOPIC']

# load config file(s)
broker, port, topic, user, pwd = device_utilities.loadConfigs(configFile,
secrets)
# load environmental variables
MQTT_BROKER = os.environ["MQTT_BROKER"]
MQTT_USER = os.environ['MQTT_USER']
MQTT_SECRET = os.environ['MQTT_SECRET']
MQTT_PORT = int(os.environ['MQTT_PORT'])

# get unique client ID
clientID = device_utilities.getClientID()
client_id = device_utilities.get_client_id()

# get mqtt client
client, code = device_utilities.mqttClient(clientID, user, pwd, broker,
port)
client, code = device_utilities.mqtt_client(client_id, MQTT_USER,
MQTT_SECRET,
MQTT_BROKER,
MQTT_PORT)

# instantiate CPU data class
win_data = WindowsSensors()
Expand All @@ -107,7 +109,7 @@ def main():

# start monitoring
try:
monitor(client, win_data, gpu_data, topic)
monitor(client, win_data, gpu_data, TOPIC)

finally:
client.loop_stop()
Expand Down
Loading

0 comments on commit e8bc910

Please sign in to comment.