-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellscript.sh
74 lines (66 loc) · 1.55 KB
/
shellscript.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
#!/bin/sh
readonly PROG="`basename $0`"
usage_and_exit() {
cat << EOF
Usage: $PROG
description
Options:
-w Warning
-c Critical
-h, --help Display this help and exit
EOF
exit 1
}
ARGS=`getopt -n $PROG -o w:c:h -l help -- "$@"`
[ $? -ne 0 ] && { echo; usage_and_exit; }
eval set -- "${ARGS}"
while true; do
case "$1" in
-w)
WARNING_PERCENT=$2
expr match "$WARNING_PERCENT" "^[1-9][0-9]\?%\|100%\|0%$" > /dev/null
if [ $? -ne 0 ]; then
echo "Invalid argument: \"-w $WARNING_PERCENT\""
exit 1
fi
shift 2
;;
-c)
CRITICAL_PERCENT=$2
expr match "$CRITICAL_PERCENT" "^[1-9][0-9]\?%\|100%\|0%$" > /dev/null
if [ $? -ne 0 ]; then
echo "Invalid argument: \"-c $CRITICAL_PERCENT\""
exit 1
fi
shift 2
;;
-h|--help)
usage_and_exit
exit 1
;;
--)
shift
break
;;
esac
done
if [ -z "$CRITICAL_PERCENT" ] || [ -z "$WARNING_PERCENT" ]; then
usage_and_exit
fi
free | sed -n 2p | awk -v c="$CRITICAL_PERCENT" -v w="$WARNING_PERCENT" '{
sub(/%/, "", c);
sub(/%/, "", w);
if ($4 / $2 <= c / 100) {
status = "CRITICAL";
retval = 2;
} else if ($4 / $2 <= w / 100) {
status = "WARNING";
retval = 1;
} else {
status = "OK";
retval = 0;
}
printf("%s, %.2f%% - total: %d, used: %d, free: %d, shared: %d, buff/cache: %d, available: %d (kB)\n",
status, ($4/$2)*100, $2, $3, $4, $5 ,$6, $7);
exit retval;
}'