-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task292 - modified the script and add more information in README.md
- Loading branch information
Farhad Nateghi
committed
Sep 5, 2024
1 parent
ae46807
commit b662f2e
Showing
2 changed files
with
50 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,42 @@ | ||
#!/usr/local/bin/bash | ||
|
||
declare -i ECODE=0 | ||
declare STATUS="OK" | ||
declare TXT="" | ||
declare temp_warn=60 | ||
declare temp_crit=80 | ||
|
||
# Read the temperature from sysctl | ||
if sysctl -a | grep -q dev.amdtemp.0.core0.sensor0; then | ||
temp="$(sysctl -a | grep dev.amdtemp.0.core0.sensor0 | awk '{print $2}' | sed 's/.$//')" | ||
STATUS="$temp°C" | ||
else | ||
# If no temperature is found | ||
STATUS="UNKNOWN" | ||
ECODE=3 | ||
TXT="Cannot measure temperature" | ||
fi | ||
# Function to display temperatures for all found sensors of a specific type | ||
display_temps() { | ||
local sensor_pattern=$1 | ||
local description=$2 | ||
|
||
# Convert temperature to integer for comparison, if needed | ||
temp_int=$(echo "$temp" | awk '{printf "%.0f", $1}') | ||
# Collect all relevant sensor readings | ||
sysctl -a | grep -E "$sensor_pattern" | while read sensor_data; do | ||
local sensor_id=$(echo $sensor_data | awk '{print $1}') | ||
local temp=$(echo $sensor_data | awk '{print $2}' | sed 's/C//') # Removing 'C' from '44.0C' | ||
local temp_int=$(echo "$temp" | awk '{printf "%.0f", $1}') | ||
local ECODE=0 | ||
local TXT="CPU Temp normal" | ||
|
||
# Debug output | ||
echo "Temperature read: $temp_float" | ||
echo "Warning threshold: $temp_warn" | ||
echo "Critical threshold: $temp_crit" | ||
|
||
# Use bc for floating-point comparisons | ||
if (( $(echo "$temp >= $temp_crit" | bc -l) )); then | ||
ECODE=2 | ||
TXT="CPU Temp critical" | ||
elif (( $(echo "$temp >= $temp_warn" | bc -l) )); then | ||
ECODE=1 | ||
TXT="CPU Temp warning" | ||
# Determine status based on temperature thresholds | ||
if (( $(echo "$temp >= $temp_crit" | bc -l) )); then | ||
ECODE=2 | ||
TXT="CPU Temp critical" | ||
elif (( $(echo "$temp >= $temp_warn" | bc -l) )); then | ||
ECODE=1 | ||
TXT="CPU Temp warning" | ||
fi | ||
echo "$ECODE CPUTEMP - temperature is $temp°C - $TXT" | ||
done | ||
} | ||
# Main script execution to check different sensor types | ||
# Check AMD temperature sensors | ||
if sysctl -a | grep -q 'dev.amdtemp.0.core0.sensor0'; then | ||
display_temps 'dev.amdtemp.[0-9]+.core[0-1].sensor[0-1]' "AMD CPU Sensor" | ||
# Check Intel CPU temperature sensors | ||
elif sysctl -a | grep -q 'dev.cpu.0.temperature'; then | ||
display_temps 'dev.cpu.[0-9]+.temperature' "Intel CPU Sensor" | ||
# Check PCH temperature sensors, if any | ||
elif sysctl -a | grep -q 'dev.pchtherm.0.temperature'; then | ||
display_temps 'dev.pchtherm.[0-9]+.temperature' "PCH Sensor" | ||
else | ||
TXT="CPU Temp normal" | ||
echo "3 CPUTEMP - UNKNOWN - No thermal sensors found or active" | ||
fi | ||
|
||
# Output result and exit with appropriate code | ||
echo "$ECODE CPUTEMP - $STATUS - $TXT" | ||
exit $ECODE |