-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcpu_temp.sh
executable file
·276 lines (253 loc) · 7.15 KB
/
cpu_temp.sh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env bash
# Script Name: cpu_temp.sh
# Description: Displays the current CPU temperature with advanced options.
# Usage: cpu_temp.sh [options]
#
# Options:
# -h, --help Display this help message and exit.
# -v, --verbose Enable verbose output.
# -u, --unit UNIT Specify temperature unit: C (Celsius), F (Fahrenheit), K (Kelvin). Default is C.
# -j, --json Output in JSON format.
# -l, --log-file FILE Log output to specified file.
# -m, --monitor INTERVAL Monitor CPU temperature at specified interval in seconds.
# -o, --output FILE Save output to specified file.
# -V, --version Display script version and exit.
#
# Examples:
# cpu_temp.sh --unit F
# cpu_temp.sh -v -m 5
# cpu_temp.sh --json --output cpu_temp.json
set -euo pipefail
VERSION="1.0.0"
# Default configurations
VERBOSE=false
UNIT="C"
OUTPUT_JSON=false
LOG_FILE=""
LOG_ENABLED=false
MONITOR_INTERVAL=0
OUTPUT_FILE=""
# Function to display usage information
print_usage() {
cat << EOF
Usage: $0 [options]
Options:
-h, --help Display this help message and exit.
-v, --verbose Enable verbose output.
-u, --unit UNIT Specify temperature unit: C (Celsius), F (Fahrenheit), K (Kelvin). Default is C.
-j, --json Output in JSON format.
-l, --log-file FILE Log output to specified file.
-m, --monitor INTERVAL Monitor CPU temperature at specified interval in seconds.
-o, --output FILE Save output to specified file.
-V, --version Display script version and exit.
Examples:
$0 --unit F
$0 -v -m 5
$0 --json --output cpu_temp.json
EOF
}
# Function to display version information
print_version() {
echo "$0 version $VERSION"
}
# Function for logging
log_action() {
local message="$1"
if [[ "$LOG_ENABLED" == true ]]; then
echo "$(date +"%Y-%m-%d %T"): $message" >> "$LOG_FILE"
fi
if [[ "$VERBOSE" == true ]]; then
echo "$message"
fi
}
# Function to convert temperature units
convert_temp() {
local temp_c="$1"
local unit="$2"
local temp_output
case "$unit" in
C|c)
temp_output="$temp_c"
;;
F|f)
temp_output=$(echo "scale=2; ($temp_c * 9/5) + 32" | bc)
;;
K|k)
temp_output=$(echo "scale=2; $temp_c + 273.15" | bc)
;;
*)
echo "Invalid unit: $unit"
exit 1
;;
esac
echo "$temp_output"
}
# Function to get the CPU temperature on Linux
get_linux_cpu_temp() {
local temp_paths=(
"/sys/class/thermal/thermal_zone*/temp"
"/sys/class/hwmon/hwmon*/temp1_input"
)
for path in "${temp_paths[@]}"; do
for file in $path; do
if [[ -r "$file" ]]; then
local temp_raw
temp_raw=$(cat "$file")
if [[ "$temp_raw" -gt 1000 ]]; then
temp_c=$(echo "scale=2; $temp_raw / 1000" | bc)
else
temp_c="$temp_raw"
fi
local temp
temp=$(convert_temp "$temp_c" "$UNIT")
echo "$temp"
return
fi
done
done
echo "Could not find a valid temperature file in common paths." >&2
exit 1
}
# Function to get the CPU temperature on macOS
get_macos_cpu_temp() {
if ! command -v osx-cpu-temp &> /dev/null; then
echo "Error: 'osx-cpu-temp' utility is not installed. Please install it from https://github.com/lavoiesl/osx-cpu-temp" >&2
exit 1
fi
local temp_c
temp_c=$(osx-cpu-temp -C | grep -o '[0-9]*\.[0-9]*')
if [[ -z "$temp_c" ]]; then
echo "Could not retrieve CPU temperature. Ensure you have the necessary permissions." >&2
exit 1
fi
local temp
temp=$(convert_temp "$temp_c" "$UNIT")
echo "$temp"
}
# Function to get the CPU temperature on FreeBSD
get_freebsd_cpu_temp() {
if sysctl hw.acpi.thermal.tz0.temperature >/dev/null 2>&1; then
local temp_str
temp_str=$(sysctl hw.acpi.thermal.tz0.temperature | awk '{print $2}')
local temp_c=${temp_str%.*}
local temp
temp=$(convert_temp "$temp_c" "$UNIT")
echo "$temp"
else
echo "Could not retrieve CPU temperature on FreeBSD." >&2
exit 1
fi
}
# Main function to get CPU temperature
get_cpu_temp() {
local temp
case "$(uname -s)" in
Linux*)
temp=$(get_linux_cpu_temp)
;;
Darwin*)
temp=$(get_macos_cpu_temp)
;;
FreeBSD*)
temp=$(get_freebsd_cpu_temp)
;;
*)
echo "Unsupported OS. This script works on Linux, macOS, and FreeBSD only." >&2
exit 1
;;
esac
echo "$temp"
}
# Parse command-line arguments
ARGS=("$@")
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
print_usage
exit 0
;;
-v|--verbose)
VERBOSE=true
shift
;;
-u|--unit)
if [[ -n "${2-}" ]]; then
UNIT="$2"
shift 2
else
echo "Error: --unit requires a value."
exit 1
fi
;;
-j|--json)
OUTPUT_JSON=true
shift
;;
-l|--log-file)
if [[ -n "${2-}" ]]; then
LOG_FILE="$2"
LOG_ENABLED=true
shift 2
else
echo "Error: --log-file requires a value."
exit 1
fi
;;
-m|--monitor)
if [[ -n "${2-}" ]]; then
MONITOR_INTERVAL="$2"
shift 2
else
echo "Error: --monitor requires an interval in seconds."
exit 1
fi
;;
-o|--output)
if [[ -n "${2-}" ]]; then
OUTPUT_FILE="$2"
shift 2
else
echo "Error: --output requires a file path."
exit 1
fi
;;
-V|--version)
print_version
exit 0
;;
*)
echo "Unknown option: $1"
print_usage
exit 1
;;
esac
done
# Redirect output to file if specified
if [[ -n "$OUTPUT_FILE" ]]; then
exec > >(tee -a "$OUTPUT_FILE")
fi
# Main execution function
main() {
if [[ "$MONITOR_INTERVAL" -gt 0 ]]; then
while true; do
temp=$(get_cpu_temp)
if [[ "$OUTPUT_JSON" == true ]]; then
echo "{\"cpu_temperature\": \"$temp\", \"unit\": \"$UNIT\"}"
else
echo "CPU Temperature: $temp°$UNIT"
fi
log_action "CPU Temperature: $temp°$UNIT"
sleep "$MONITOR_INTERVAL"
done
else
temp=$(get_cpu_temp)
if [[ "$OUTPUT_JSON" == true ]]; then
echo "{\"cpu_temperature\": \"$temp\", \"unit\": \"$UNIT\"}"
else
echo "CPU Temperature: $temp°$UNIT"
fi
log_action "CPU Temperature: $temp°$UNIT"
fi
}
main