-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_io
80 lines (69 loc) · 2.24 KB
/
check_io
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
#!/bin/bash
#Get script name
scrName=`basename ${BASH_SOURCE[0]}`
# help function
function showHelp {
echo -e \\n"Help page for $scrName."\\n
echo -e "Basic test: \\\bin\\\bash $scrName -w {warning_point} -c {critical_point}"\\n
echo "Warning and critical points are optional, default values for warning and critical points are 94% and critical is 97% respectively."
echo "-w : Sets warning point in percentage for Total CPU Usage. Default is 94%."
echo "-c : Sets critical point in percentage for for Total CPU Usage. Default is 97%."
echo -e "-h : Displays the help page."\\n
echo -e "Example: /bin/bash $scrName -w 92 -c 96"\\n
echo -e "Result: [CPU_INFO: 15.70% ] User: 9.60% - System: 3.60% - Idle: 84.30% - IOwait: 2.50%"\\n
echo -e \\n"Author: [email protected]"\\n
exit 1
}
#default values for warning and critical
valCW=25
valCC=40
#Regular expression to check positive float number input
pRex='^[0-9]+\.?[0-9]*$'
while getopts :w:c:h FLAG; do
case $FLAG in
w)
if ! [[ $OPTARG =~ $pRex ]] ; then
echo "error: Use positive digits only" >&2; exit 1
else
valCW=$OPTARG
fi
;;
c)
if ! [[ $OPTARG =~ $pRex ]] ; then
echo "error: Use positive digits only" >&2; exit 1
else
valCC=$OPTARG
fi
;;
h)
showHelp
;;
\?)
echo -e \\n"No option as -$OPTARG is present!!"
showHelp
exit 2
;;
esac
done
shift $((OPTIND-1))
array=($(top -bn 2 -d 1 | grep '^%Cpu' | tail -n 1 | sed 's/,/, /g' | tr -d ':,' | awk '{ printf ( "%.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f", $2, $4, $6, $8, $10, $12, $14, $16) }'))
cpuUser=${array[0]}
cpuSystem=${array[1]}
cpuNice=${array[2]}
cpuIdle=${array[3]}
cpuUsed_t=$(echo 100 - ${array[3]} | bc)
cpuWA=${array[4]}
cpuHI=${array[5]}
cpuSI=${array[6]}
cpuST=${array[7]}
message="[CPU_INFO: $cpuUsed_t% ] User: $cpuUser% - System: $cpuSystem% - Idle: $cpuIdle% - IOwait: $cpuWA% | - Nice: $cpuNice% - Hardware_interrupts: $cpuHI% - Software_interrupts: $cpuSI% - Steal: $cpuST% "
if (( $(echo "$cpuWA >= $valCC" | bc -l) )); then
echo "$cpuWA | Count=$cpuWA"
$(exit 2)
elif (( $(echo "$cpuWA >= $valCW" | bc -l) )); then
echo "$cpuWA | Count=$cpuWA"
$(exit 1)
else
echo "$cpuWA | Count=$cpuWA"
$(exit 0)
fi