-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_memory
More file actions
134 lines (125 loc) · 5.17 KB
/
check_memory
File metadata and controls
134 lines (125 loc) · 5.17 KB
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
#!/bin/sh
################################################################################
# Linux memory utilization nagios plugin #
# Copyright (c) 2019 Frostbyte <frostbytegr@gmail.com> #
# #
# Credits: #
# <github.com/hugme> - sample code for linux nagios checks #
# <devel@nagios-plugins.org> - plugin development guidelines #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
################################################################################
# Usage helper function
usage() {
cat << EOF
Usage:
$0 [-w util_warn_threshold] [-c util_warn_threshold] [-u friendly_unit] [-n]
Friendly units: K=Kilobytes, M=Megabytes, G=Gigabytes, T=Terabytes.
Example: $0 -w 80 -c 90 -u M
Cached memory and buffers will not be included in system's free memory when calculating utilization, with the -n argument.
EOF
exit 3
}
# Input error helper function
inputError() {
# Throw supplied error message and exit
echo "$1"
exit 3
}
# Parse user-supplied arguments
while getopts ":w:c:u:n" inputArgs; do
case "${inputArgs}" in
w) thresholdWarn="${OPTARG}" ;;
c) thresholdCrit="${OPTARG}" ;;
u) friendlyUnit="${OPTARG}" ;;
n) noCachedMemory=1 ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
# Apply any defaults if needed
[ -z "$friendlyUnit" ] && friendlyUnit=M
[ -z "$noCachedMemory" ] && noCachedMemory=0
# Validate any user-supplied arguments
[ -z "$thresholdWarn" ] || {
[ -z $(echo $thresholdWarn | tr -d [:digit:]) ] || inputError "The warning threshold value may only contain numbers."
[ $thresholdWarn -le 0 ] || [ $thresholdWarn -ge 100 ] && inputError "The warning threshold value must be between 1 and 99."
}
[ -z "$thresholdCrit" ] || {
[ -z $(echo $thresholdCrit | tr -d [:digit:]) ] || inputError "The critical threshold value may only contain numbers."
[ $thresholdCrit -le 0 ] || [ $thresholdCrit -ge 100 ] && inputError "The critical threshold value must be between 1 and 99."
}
[ ! -z "$thresholdWarn" ] && [ ! -z "$thresholdCrit" ] && [ $thresholdCrit -lt $thresholdWarn ] && inputError "The critical threshold value must be higher than or equal to the warning threshold value."
case $friendlyUnit in
k|K) unitDivider=1 ;;
m|M) unitDivider=1024 ;;
g|G) unitDivider=1048576 ;;
t|T) unitDivider=1073741824 ;;
*) inputError "The friendly unit value may only be one of the following: K, M, G, or T. (Case Insensitive)" ;;
esac
# If the memory information file is missing
memoryInfo="/proc/meminfo"
[ -f "$memoryInfo" ] || {
# Throw error and exit
echo "The memory information file $memoryInfo is missing."
exit 3
}
# Disable any non-specified thresholds
[ -z "$thresholdWarn" ] && thresholdWarn=0
[ -z "$thresholdCrit" ] && thresholdCrit=0
#Calculate and construct a standard nagios response
result=$(awk \
-v thresholdWarn=$thresholdWarn \
-v thresholdCrit=$thresholdCrit \
-v friendlyUnit=$friendlyUnit \
-v unitDivider=$unitDivider \
-v noCachedMemory=$noCachedMemory \
'/^MemTotal:/ { memTotal=$2 }
/^MemFree:/ { memFree=$2 }
/^Buffers:/ { memBuffers=$2 }
/^Cached:/ { memCached=$2 }
/^Active:/ { memActive=$2 }
/^Inactive:/ { memInactive=$2 }
END {
if (noCachedMemory == 0) {
memFree=memFree+memCached+memBuffers
}
memUsed=memTotal-memFree
memUtil=memUsed/memTotal*100
result="OK"
exitCode=0
if (memUtil > thresholdCrit && thresholdCrit > 0) {
result="CRITICAL"
exitCode=2
} else if (memUtil > thresholdWarn && thresholdWarn > 0) {
result="WARNING"
exitCode=1
}
print exitCode" "result" - \
Utilization: "memUtil"% \
Total: "memTotal/unitDivider friendlyUnit" \
Active: "memActive/unitDivider friendlyUnit" \
Inactive: "memInactive/unitDivider friendlyUnit" \
Buffers: "memBuffers/unitDivider friendlyUnit" \
Cached: "memCached/unitDivider friendlyUnit" | \
util="memUtil";"thresholdWarn";"thresholdCrit";0 \
total="memTotal";0;0;0 \
active="memActive";0;0;0 \
inactive="memInactive";0;0;0 \
buffers="memBuffers";0;0;0 \
cached="memCached";0;0;0"
}' "$memoryInfo")
# Respond and exit
echo ${result#* }
exit ${result%% *}