CPU Temperature Monitoring Script

1. Introduction

Monitoring CPU temperature is an important part of system health checks, especially for devices running in high-load or harsh environmental conditions.
This simple Python script reads the CPU temperature from the system’s thermal interface and outputs it in Celsius.

Example Use Cases:

  • Quickly check if CPU overheating could be affecting network performance
  • Gather baseline temperature metrics for performance benchmarking
  • Monitor thermal behavior during load testing

2. Overview of the Script

This Python script:

  1. Reads the raw CPU temperature value from /sys/class/thermal/thermal_zone0/temp
  2. Converts the value from millidegrees Celsius to standard Celsius
  3. Outputs the temperature in a human-readable format

Prerequisites:

  • Linux-based NetBeez agent
  • Python 3 installed
  • Access to /sys/class/thermal/thermal_zone0/temp

Expected Output:

  • The CPU temperature in Celsius, with one decimal place of precision

3. Script Code

#!/usr/bin/env python3

# Read the temperature from the system file
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
    raw_temp = int(f.read().strip())

# Convert to Celsius
temp_c = raw_temp / 1000

# Print in the desired format
print(f"temperature={temp_c:.1f}")

4. Sample Output

temperature=47.3

5. Closing Remarks

This script offers a quick way to check CPU temperature directly from a NetBeez agent.
It can be helpful in diagnosing thermal throttling issues or identifying hardware that’s running hotter than expected.

Have ideas to extend it? You could:

  • Output both Celsius and Fahrenheit

  • Log readings over time for trend analysis

  • Trigger alerts if the temperature exceeds a threshold

Share your modifications in the comments so others can benefit.

A few comments - -
1 - since this is looking at just this systems values, you need to understand the elements around you.

  • Like is there a Hot area and a Cold area,
  • Where is your device with respect to those areas?
  • In a Rack - can you also monitor the devices higher and lower positions than you?
  • Can you also gather the Intake and outlet temp values to see if something external to you are causing temp changes?
  • Does CPU usage values matter to the measurements?
  • While the Thermal levels may be much higher than what you comfortable running at, If you need to alert, how much time until you reach those Thermal levels based on the current rise.

You may need to understand the whole datacenter air handling.
we recently had a case where the ATS Switch did not switch the Cooling liquid pumps though everything else was switched when we lost consumer power. That caused the Room to heat up so fast, we had less than 90 minutes once the consumer power was lost. No time to bring in extra cooling.