-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_linux_memory.sh
116 lines (98 loc) · 3.67 KB
/
check_linux_memory.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
#!/bin/sh
#
# Plugin to check system memory
# by hugme ([email protected])
# You can find my checks here: https://github.com/hugme/Nag_checks
# Nagios script to check memory usage on linux server
# version 1.3.0
#
##########################################################
MEMINFO="/proc/meminfo"
##########################################################
# We call them functions because they're fun
##########################################################
print_help() {
cat << EOF
Linux Memory Plugin for Nagios
Copyright (c) hugme ([email protected])
Version: 1.2.0
Last Modified: 10-07-2014
License: This software can be used for free unless I meet you, then you owe me lunch.
Usage: check_linux_memory -w [warning %] -c [critical %]
Options:
-w [0-99] = Your warning %. 20 means 20% of your memory can remain before a warning alarm. Do not use the % sign.
-c [0-99] = Your critical %. 10 means 10% of your memory can remain before a critical alarm. Do not use the % sign.
-d [K,M,G,T] = divider K=kilobytes, M=megabytes, G=gigabytes, T=terabytes
-f = Included for backwards compatability to older verserions
-n = Don't Include cached memory as free memory when calculating your percentage free
EOF
}
invalid_type() {
echo "\nInvalid $1\n"
print_help
exit 3
}
##############################################
## Suck in the user input
##############################################
while test -n "$1"; do
case $1 in
--help) print_help ; exit 0 ;;
-h) print_help ; exit 0 ;;
-w) WARN="$2"; shift ;;
-c) CRIT="$2"; shift ;;
-d) DIV="$2"; shift ;;
-n) NC=1 ;;
esac
shift
done
##############################################
## Set the defaults if needed
##############################################
[ -z "$WARN" ] && WARN=20
[ -z "$CRIT" ] && CRIT=10
[ -z "$DIV" ] && DIV=M
[ -z "$FC" ] && FC=0
##############################################
## Check user input
##############################################
[ ! -z `echo $WARN | tr -d [:digit:]` ] && invalid_type "Warning: Warning value can only contain numbers"
[ ! -z `echo $CRIT | tr -d [:digit:]` ] && invalid_type "Critical: Critical value can only contain numbers"
[ "${WARN%.*}" -ge 100 ] && invalid_type "Warning: Warning must be smaller than 100%"
[ "${CRIT%.*}" -ge 100 ] && invalid_type "Critical: Critical must be smaller than 100%"
[ "${CRIT%.*}" -gt "${WARN%.*}" ] && invalid_type "Critical: Your Warning must be Higher than your Critical"
case $DIV in
k|K) DIVNUM=1;;
m|M) DIVNUM=1024;;
g|G) DIVNUM=1048576;;
t|T) DIVNUM=1073741824;;
*) invalid_type;;
esac
[ ! -f "$MEMINFO" ] && {
echo "Your Memory info file seems to be missing"
exit 1
}
##############################################
## Do the work
## Pull the memory file into awk
## grab the lines we need
## Print the information
##############################################
RESULT=$(awk -v warn=$WARN -v crit=$CRIT -v div=$DIV -v divnum=$DIVNUM -v nc=$NC '/^MemTotal:/ { total=$2 }
/^MemTotal:/ { tot=$2 }
/^MemFree:/ { free=$2 }
/^Buffers:/ { buff=$2 }
/^Cached:/ { cache=$2 }
/^Active:/ { active=$2 }
/^Inactive:/ { inactive=$2 }
END { if ( nc != 1 ) { free=free+cache+buff }
{ freeperct=free/tot*100 }
if ( freeperct > warn ) { result="OK" ; xit="0"}
if ( freeperct <= warn ) {
if ( freeperct > crit ) { result="WARNING" ; xit="1" }
else if ( freeperct <= crit ) { result="CRITICAL" ; xit="2" }
}
{print xit" MEMORY "result" - "freeperct"% Free - Total:"tot/divnum div" Active:"active/divnum div" Inactive:"inactive/divnum div" Buffers:"buff/divnum div" Cached:"cache/divnum div" |Free="freeperct";"warn";"crit";0 Active="active";0;0;0 Inactive="inactive";0;0;0 Buffers="buff";0;0;0 Cached="cache";0;0;0" }
}' /proc/meminfo)
echo ${RESULT#* }
exit ${RESULT%% *}