forked from Frescha/check_raspberrypi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_raspberrypi.sh
executable file
·181 lines (159 loc) · 5.19 KB
/
check_raspberrypi.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
#! /bin/ksh
VERSION="Version 0.5"
AUTHOR="Andreas Karfusehr ([email protected])"
PROGNAME=`/usr/bin/basename $0`
# Constants
BYTES_IN_MB=$(( 1024 * 1024 ))
KB_IN_MB=1024
# Exit codes
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# Helper functions #############################################################
function print_revision {
# Print the revision number
echo "$PROGNAME - $VERSION"
}
function print_usage {
# Print a short usage statement
echo "Usage: $PROGNAME [-v] -w <limit> -c <limit>"
}
function print_help {
# Print detailed help information
print_revision
echo "$AUTHOR\n\nCheck temperature and cpufreq on your local Raspberry PI\n"
print_usage
/bin/cat <<__EOT
Options:
-h
Print detailed help screen
-V
Print version information
-m
Module: Temperature org Frequency
-c INTEGER
Exit with CRITICAL status if less than INTEGER
-c PERCENT%
Exit with CRITICAL status if less than PERCENT
-v
Verbose output
__EOT
}
function get_temperature {
temp=$(($(</sys/class/thermal/thermal_zone0/temp) / 1000))
#temp=51
if [[ -z "$thresh_warn" || -z "$thresh_crit" ]]; then
# One or both thresholds were not specified
echo "$PROGNAME: Threshold not set"
print_usage
exit $STATE_UNKNOWN
elif [[ "$thresh_warn" -gt "$thresh_crit" ]]; then
# The warning threshold must be greater than the critical threshold
echo "$PROGNAME: Warning free space should be more than critical free space"
print_usage
exit $STATE_UNKNOWN
fi
if [[ "$temp" -gt "$thresh_crit" ]]; then
# Free memory is less than the critical threshold
echo "CRITICAL - $temp °C CPU temperature is to high | temperature=$temp;$thresh_warn;$thresh_crit;0;80"
exit $STATE_CRITICAL
elif [[ "$temp" -gt "$thresh_warn" ]] && [[ "$temp" -lt "$thresh_crit" ]]; then
# Free memory is less than the warning threshold
echo "WARNING - CPU temperature is $temp °C | temperature=$temp;$thresh_warn;$thresh_crit;0;80"
exit $STATE_WARNING
else
# There's no error
echo "OK - CPU temperature is $temp °C | temperature=$temp;$thresh_warn;$thresh_crit;0;80"
exit $STATE_OK
fi
}
function get_frequency {
freq=$(($(</sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq) / 1000))
#freq=930
if [[ -z "$thresh_warn" || -z "$thresh_crit" ]]; then
# One or both thresholds were not specified
echo "$PROGNAME: Threshold not set"
print_usage
exit $STATE_UNKNOWN
elif [[ "$thresh_warn" -gt "$thresh_crit" ]]; then
# The warning threshold must be greater than the critical threshold
echo "$PROGNAME: Warning free space should be more than critical free space"
print_usage
exit $STATE_UNKNOWN
fi
if [[ "$freq" -gt "$thresh_crit" ]]; then
# Free memory is less than the critical threshold
echo "CRITICAL - $freq CPU frequency is to high | cpufreq=$freq;$thresh_warn;$thresh_crit;0;1200"
exit $STATE_CRITICAL
elif [[ "$freq" -gt "$thresh_warn" ]] && [[ "$freq" -lt "$thresh_crit" ]]; then
# Free memory is less than the warning threshold
echo "WARNING - CPU frequency is $freq | cpufreq=$freq;$thresh_warn;$thresh_crit;0;1200"
exit $STATE_WARNING
else
# There's no error
echo "OK - CPU frequency is $freq | cpufreq=$freq;$thresh_warn;$thresh_crit;0;1200"
exit $STATE_OK
fi
}
# Main #########################################################################
# Verbosity level
verbosity=0
# Warning threshold
thresh_warn=
# Critical threshold
thresh_crit=
# Parse command line options
while [ "$1" ]; do
case "$1" in
-h | --help)
print_help
exit $STATE_OK
;;
-V | --version)
print_revision
exit $STATE_OK
;;
-m | --module)
if [[ "$2" = "temperature" ]]; then
get_temperature
exit $STATE_OK
elif [[ "$2" = "frequency" ]]; then
get_frequency
exit $STATE_OK
else
# Threshold is neither a number nor a percentage
echo "$PROGNAME: Threshold must be integer or percentage"
print_usage
exit $STATE_UNKNOWN
fi
;;
-w | --warning | -c | --critical)
if [[ -z "$2" || "$2" = -* ]]; then
# Threshold not provided
echo "$PROGNAME: Option '$1' requires an argument"
print_usage
exit $STATE_UNKNOWN
elif [[ "$2" = +([0-9]) ]]; then
# Threshold is a number (MB)
thresh=$2
else
# Threshold is neither a number nor a percentage
echo "$PROGNAME: Threshold must be integer or percentage"
print_usage
exit $STATE_UNKNOWN
fi
[[ "$1" = *-w* ]] && thresh_warn=$thresh || thresh_crit=$thresh
shift 2
;;
-?)
print_usage
exit $STATE_OK
;;
*)
echo "$PROGNAME: Invalid option '$1'"
print_usage
exit $STATE_UNKNOWN
;;
esac
done