Skip to content

Commit

Permalink
Code clean-up, fixing variable names and the like
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkhamLee committed Apr 2, 2024
1 parent f05a3f9 commit 35ae66c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 44 deletions.
20 changes: 10 additions & 10 deletions hardware_monitoring/raspberrypi_4b/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


# method that runs monitoring loop
def monitor(client: object, getData: object, topic: str):
def monitor(client: object, get_data: object, topic: str):

DEVICE_ID = os.environ['DEVICE_ID']
INTERVAL = int(os.environ['INTERVAL'])
Expand All @@ -32,22 +32,22 @@ def monitor(client: object, getData: object, topic: str):
while True:

# get CPU utilization
cpuUtil = getData.getCPUData()
cpu_util = get_data.get_cpu_data()

# get current RAM use
ramUse = getData.getRamData()
ram_use = get_data.get_ram_data()

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

# get CPU temperature
cpuTemp = getData.get_rpi4b_temps()
cpu_temp = get_data.get_rpi4b_temps()

payload = {
"cpuTemp": cpuTemp,
"cpuFreq": cpuFreq,
"cpuUse": cpuUtil,
"ramUse": ramUse
"cpuTemp": cpu_temp,
"cpuFreq": cpu_freq,
"cpuUse": cpu_util,
"ramUse": ram_use
}

payload = json.dumps(payload)
Expand All @@ -58,7 +58,7 @@ def monitor(client: object, getData: object, topic: str):

logger.debug(f'MQTT publishing failure for hardware monitoring on: {DEVICE_ID}, return code: {status}') # noqa: E501

del payload, cpuUtil, ramUse, cpuFreq, cpuTemp, status, result
del payload, cpu_util, ram_use, cpu_freq, cpu_temp, status, result
gc.collect()
time.sleep(INTERVAL)

Expand Down
6 changes: 3 additions & 3 deletions hardware_monitoring/raspberrypi_4b/rpi4b_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ def __init__(self):
self.coreCount = psutil.cpu_count(logical=False)

# get average clock speed for all cores
def getFreq(self, all_cpu=False):
def get_freq(self, all_cpu=False):

allFreq = psutil.cpu_freq(percpu=all_cpu)[0]
allFreq = round(allFreq, 1)

return allFreq, self.coreCount

# CPU load/utilization
def getCPUData(self):
def get_cpu_data(self):

cpuUtil = (psutil.cpu_percent(interval=1))
cpuUtil = round(cpuUtil, 1)

return cpuUtil

# get current RAM used
def getRamData(self):
def get_ram_data(self):

ramUse = (psutil.virtual_memory()[3]) / 1073741824
ramUse = round(ramUse, 2)
Expand Down
45 changes: 23 additions & 22 deletions hardware_monitoring/rockchip3588/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,42 +47,43 @@ def monitor(client: object, bucket: str, interval: int, base_payload: dict):

try:
# get CPU utilization
cpuUtil = rockchip_data.getCPUData()
cpu_util = rockchip_data.get_cpu_data()

# get current RAM use
ramUse = rockchip_data.getRamData()
ram_use = rockchip_data.get_ram_data()

# get per CPU frequencies (bigCore0, bigCore1, littleCore)
littleCoreFreq, bigCore0Freq, bigCore1Freq = rockchip_data.\
getRockChip3588Freqs()
little_core_freq, big_core0_freq, big_core1_freq = rockchip_data.\
get_rockchip_3588_freqs()

# get system temperatures
socTemp, bigCore0Temp, bigCore1Temp, littleCoreTemp, centerTemp, \
gpuTemp, npuTemp, nvmeTemp = rockchip_data.sysTemps()
soc_temp, big_core0_temp, big_core1_temp, little_core_temp, \
center_temp, gpu_temp, npu_temp, \
nvme_temp = rockchip_data.sys_temps()

payload = {
"SOC": socTemp,
"bigCore0": bigCore0Temp,
"bigCore1": bigCore1Temp,
"littleCore": littleCoreTemp,
"Center": centerTemp,
"GPU": gpuTemp,
"NPU": npuTemp,
"NVME": nvmeTemp,
"littleCoreFreq": littleCoreFreq,
"bigCore0Freq": bigCore0Freq,
"bigCore1Freq": bigCore1Freq,
"cpuUse": cpuUtil,
"ramUse": ramUse
"SOC": soc_temp,
"bigCore0": big_core0_temp,
"bigCore1": big_core1_temp,
"littleCore": little_core_temp,
"Center": center_temp,
"GPU": gpu_temp,
"NPU": npu_temp,
"NVME": nvme_temp,
"littleCoreFreq": little_core_freq,
"bigCore0Freq": big_core0_freq,
"bigCore1Freq": big_core1_freq,
"cpuUse": cpu_util,
"ramUse": ram_use
}

# write data to InfluxDB
influxdb_write.write_influx_data(client, base_payload,
payload, bucket)

del payload, socTemp, bigCore0Temp, bigCore1Temp, \
littleCoreTemp, centerTemp, gpuTemp, npuTemp, nvmeTemp,
littleCoreFreq, bigCore0Freq, bigCore1Freq, cpuUtil, ramUse
del payload, soc_temp, big_core0_temp, big_core1_temp, \
little_core_temp, center_temp, gpu_temp, npu_temp, nvme_temp,
little_core_freq, big_core0_freq, big_core1_freq, cpu_util, ram_use
gc.collect()

except Exception as e:
Expand Down
18 changes: 9 additions & 9 deletions hardware_monitoring/rockchip3588/rockchip_3588.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self):
# for instances where we want to get data per core, we can pass the
# function that retrieves that data to this one and then build the
# "per core" payload
def buildPayload(self, inputFunction, index=0):
def build_payload(self, inputFunction, index=0):

temp_dict = {}

Expand All @@ -37,7 +37,7 @@ def buildPayload(self, inputFunction, index=0):
return payload

# getting temps per core
def getTemps(self, index=0):
def get_temps(self, index=0):

if self.core_count > 1:

Expand All @@ -53,37 +53,37 @@ def getTemps(self, index=0):
return temp_payload

# returns CPU package temp
def coreTemp(self):
def core_temp(self):

core_temp = psutil.sensors_temperatures()['coretemp'][0].current

return core_temp

# get average clock speed for all cores
def getFreq(self, all_cpu=False):
def get_freq(self, all_cpu=False):

all_freq = psutil.cpu_freq(percpu=all_cpu)[0]
all_freq = round(all_freq, 1)

return all_freq, self.core_count

# get frequency per core
def freqPerCore(self, all_cpu=True):
def freq_per_core(self, all_cpu=True):

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

return per_core_freq

# CPU load
def getCPUData(self):
def get_cpu_data(self):

cpu_util = (psutil.cpu_percent(interval=1))
cpu_util = round(cpu_util, 1)

return cpu_util

# get current RAM used
def getRamData(self):
def get_ram_data(self):

ram_use = (psutil.virtual_memory()[3]) / 1073741824
ram_use = round(ram_use, 2)
Expand All @@ -92,7 +92,7 @@ def getRamData(self):

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

soc_temp = psutil.sensors_temperatures()['soc_thermal'][0].current
big_core_0temp = psutil.sensors_temperatures()['bigcore0_thermal'][0].\
Expand All @@ -112,7 +112,7 @@ def sysTemps():

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

freq = psutil.cpu_freq(percpu=True)
little_core = freq[0].current
Expand Down

0 comments on commit 35ae66c

Please sign in to comment.