-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtemperature-warning.sh
More file actions
118 lines (95 loc) · 4.3 KB
/
temperature-warning.sh
File metadata and controls
118 lines (95 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Send warning to syslog when temperatures reaches specific threshold
#
#jas-update=temperature-warning.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
TRIGGER_TEMPERATURE=80 # target temperature at which log the warning
WARNING_COOLDOWN=300 # how long to wait (seconds) before logging another warning
EXECUTE_COMMAND="" # execute a command each time warning is issued (receives arguments: $1 = sensors - "cpu 2.4g 5g 6g")
load_script_config
state_file="$TMP_DIR/$script_name"
validate_config() {
[ -z "$TRIGGER_TEMPERATURE" ] && { logecho "Error: TRIGGER_TEMPERATURE is not set" error; exit 1; }
[ -z "$WARNING_COOLDOWN" ] && { logecho "Error: WARNING_COOLDOWN is not set" error; exit 1; }
}
get_temperatures() {
local _eth_24g=""
local _eth_5g=""
local _eth_6g=""
local _interface _status
for _interface in /sys/class/net/eth*; do
[ ! -d "$_interface" ] && continue
_interface="$(basename "$_interface")"
_status="$(wl -i "$_interface" status 2> /dev/null)"
if echo "$_status" | grep -Fq "2.4GHz"; then
_eth_24g="$_interface"
elif echo "$_status" | grep -Fq "5GHz"; then
_eth_5g="$_interface"
elif echo "$_status" | grep -Fq "6GHz"; then
_eth_6g="$_interface"
fi
[ -n "$_eth_24g" ] && [ -n "$_eth_5g" ] && break
done
[ -f /sys/class/thermal/thermal_zone0/temp ] && cpu_temperature="$(awk '{print $1 / 1000}' < /sys/class/thermal/thermal_zone0/temp)"
[ -n "$_eth_24g" ] && wifi_24g_temperature="$(wl -i "$_eth_24g" phy_tempsense | awk '{print $1 / 2 + 20}')"
[ -n "$_eth_5g" ] && wifi_5g_temperature="$(wl -i "$_eth_5g" phy_tempsense | awk '{print $1 / 2 + 20}')"
[ -n "$_eth_6g" ] && wifi_6g_temperature="$(wl -i "$_eth_6g" phy_tempsense | awk '{print $1 / 2 + 20}')"
}
case "$1" in
"run")
validate_config
uptime="$(awk -F '.' '{print $1}' /proc/uptime)"
uptime_cached="$([ -f "$state_file" ] && cat "$state_file" || echo "0")"
if [ -z "$uptime_cached" ] || [ "$((uptime_cached+WARNING_COOLDOWN))" -le "$uptime" ]; then
get_temperatures
if [ "$(printf "%.0f" "$cpu_temperature")" -ge "$TRIGGER_TEMPERATURE" ]; then
logecho "CPU temperature warning: $cpu_temperature C" alert
warning="cpu"
fi
if [ -n "$wifi_24g_temperature" ] && [ "$(printf "%.0f\n" "$wifi_24g_temperature")" -ge "$TRIGGER_TEMPERATURE" ]; then
logecho "WiFi 2.4G temperature warning: $wifi_24g_temperature C" alert
warning="$warning 2.4g"
fi
if [ -n "$wifi_5g_temperature" ] && [ "$(printf "%.0f\n" "$wifi_5g_temperature")" -ge "$TRIGGER_TEMPERATURE" ]; then
logecho "WiFi 5G temperature warning: $wifi_5g_temperature C" alert
warning="$warning 5g"
fi
if [ -n "$wifi_6g_temperature" ] && [ "$(printf "%.0f\n" "$wifi_6g_temperature")" -ge "$TRIGGER_TEMPERATURE" ]; then
logecho "WiFi 6G temperature warning: $wifi_6g_temperature C" alert
warning="$warning 6g"
fi
if [ -n "$warning" ]; then
echo "$uptime" > "$state_file"
[ -n "$EXECUTE_COMMAND" ] && eval "$EXECUTE_COMMAND $(echo "$warning" | awk '{$1=$1};1')"
fi
fi
;;
"check")
get_temperatures
[ -n "$cpu_temperature" ] && echo "CPU temperature: $cpu_temperature C"
[ -n "$wifi_24g_temperature" ] && echo "WiFi 2.4G temperature: $wifi_24g_temperature C ($_eth_24g)"
[ -n "$wifi_5g_temperature" ] && echo "WiFi 5G temperature: $wifi_5g_temperature C ($_eth_5g)"
[ -n "$wifi_6g_temperature" ] && echo "WiFi 6G temperature: $wifi_6g_temperature C ($_eth_5g)"
;;
"start")
validate_config
crontab_entry add "*/1 * * * * $script_path run"
;;
"stop")
crontab_entry delete
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart|check"
exit 1
;;
esac