Skip to content

Instantly share code, notes, and snippets.

@ItsQuadrus
Created May 12, 2024 18:17
Show Gist options
  • Save ItsQuadrus/33adcb01725c9cdd2eb2938b07251527 to your computer and use it in GitHub Desktop.
Save ItsQuadrus/33adcb01725c9cdd2eb2938b07251527 to your computer and use it in GitHub Desktop.
This script retrieves temperature readings from specified sensors, compares them to a threshold, and sends a notification to a specified ntfy.sh channel with the priority level indicating the severity. I recommend adding it to your crontab.
#!/bin/bash
channel="https://ntfy.sh/YOURTOPICHERE"
sensors=(
"nvme-pci-0900:Composite"
# Add as many sensors as you want here
)
threshold=55
# Extract the temperature value from the JSON output of sensors command
get_temperature() {
sensor_name="$1"
attribute="$2"
value=$(sensors -j | jq -r --arg sensor "$sensor_name" --arg attr "$attribute" '.[$sensor][$attr]["temp1_input"]')
echo "$value"
}
notify() {
priority=1
message=""
# Loop through the sensors to construct the notification message
for sensor in "${sensors[@]}"; do
# Split the sensor name and attribute
IFS=':' read -r -a parts <<< "$sensor"
sensor_name="${parts[0]}"
attribute="${parts[1]}"
# Get the current value of the attribute using get_temperature function
value=$(get_temperature "$sensor_name" "$attribute")
# Construct the sensor information for the notification message
sensor_info="$sensor_name at $value"
# Check if the value exceeds the threshold
if (( $(echo "$value > $threshold" | bc -l) )); then
priority=4
fi
# Append the sensor information to the notification message
message=$(echo -e "$message\n$sensor_info")
done
curl -X POST -H "Content-Type: application/json" -H "Tags: satellite" -H "X-Priority: $priority" -H "X-Title: $(hostname) Sensors Report" -d "$message" "$channel"
}
notify
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment