Debian Codegraph @

percona-nagios-plugins/nagios/bin/pmp-check-unix-memory

raw symbols SCIP
1 #!/bin/sh
2
3 # ########################################################################
4 # This program is part of $PROJECT_NAME$
5 # License: GPL License (see COPYING)
6 # Authors:
7 # Baron Schwartz, Roman Vynar
8 # ########################################################################
9
10 # ########################################################################
11 # Redirect STDERR to STDOUT; Nagios doesn't handle STDERR.
12 # ########################################################################
13 exec 2>&1
14
15 # ########################################################################
16 # Set up constants, etc.
17 # ########################################################################
18 STATE_OK=0
19 STATE_WARNING=1
20 STATE_CRITICAL=2
21 STATE_UNKNOWN=3
22 STATE_DEPENDENT=4
23
24 # ########################################################################
25 # Run the program.
26 # ########################################################################
27 main() {
28
29 # Get options
30 for o; do
31 case "${o}" in
32 -w) shift; OPT_WARN="${1}"; shift; ;;
33 -d) shift; ;; # left for backward-compatibility
34 -c) shift; OPT_CRIT="${1}"; shift; ;;
35 --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;;
36 --help) perl -00 -ne 'm/^ Usage:/ && print' "$0"; exit 0 ;;
37 -*) echo "Unknown option ${o}. Try --help."; exit 1; ;;
38 esac
39 done
40 OPT_WARN=${OPT_WARN:-90}
41 OPT_CRIT=${OPT_CRIT:-95}
42 if is_not_sourced; then
43 if [ -n "$1" ]; then
44 echo "WARN spurious command-line options: $@"
45 exit 1
46 fi
47 fi
48
49 NOTE="UNK cannot find memory statistics"
50 TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $?
51 trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT
52 if [ -e /proc/meminfo ] && cat /proc/meminfo > "${TEMP}" ; then
53 USD_PCT=$(get_used_memory_linux "${TEMP}")
54 elif which sysctl > /dev/null && sysctl -a > "${TEMP}" ; then
55 USD_PCT=$(get_used_memory_bsd "${TEMP}")
56 else
57 echo $NOTE
58 exit
59 fi
60
61 NOTE="Memory ${USD_PCT}% used"
62 if [ "${USD_PCT:-0}" -ge "${OPT_CRIT}" ]; then
63 NOTE="CRIT $NOTE. $(get_largest_process)"
64 elif [ "${USD_PCT:-0}" -ge "${OPT_WARN}" ]; then
65 NOTE="WARN $NOTE. $(get_largest_process)"
66 else
67 NOTE="OK $NOTE"
68 fi
69
70 # Build the common perf data output for graph trending
71 PERFDATA="memory_used=${USD_PCT:-0};${OPT_WARN};${OPT_CRIT};0;100"
72 echo "$NOTE | $PERFDATA"
73 }
74
75 # ########################################################################
76 # Find the largest process
77 # ########################################################################
78 get_largest_process () {
79 ALL_PROCS=$(ps axo pid,%mem,ucomm | sort -k2 -nr)
80 BIGGEST=$(echo $ALL_PROCS | head -n 1)
81 BIG_PID=$(echo $BIGGEST | cut -d' ' -f1)
82 BIG_MEM=$(echo $BIGGEST | cut -d' ' -f2)
83 BIG_COMMAND=$(echo $BIGGEST | cut -d' ' -f3)
84 echo "Largest process: ${BIG_COMMAND} (${BIG_PID}) = ${BIG_MEM}%"
85 }
86
87 # ########################################################################
88 # Get the used memory from /proc/meminfo
89 # Consider MemAvailable if available in the file (starting procps-ng 3.3.10).
90 # See https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
91 # ########################################################################
92 get_used_memory_linux() {
93 awk '/^MemTotal:/ {total = $2;}
94 /^MemFree:/ {free = $2;}
95 /^MemAvailable:/ {avail = $2;}
96 /^Buffers:/ {buffers = $2;}
97 /^Cached:/ {cached = $2;
98 if (avail == "") avail = free + buffers + cached;
99 printf "%d\n", (total - avail) / total * 100;}' "$1"
100 }
101 # ########################################################################
102 # Get the used memory from sysctl for BSD systems
103 # ########################################################################
104 get_used_memory_bsd() {
105 awk '/vm.stats.vm.v_cache_count:/ {cache = $2}
106 /vm.stats.vm.v_inactive_count:/ {inactive = $2}
107 /vm.stats.vm.v_free_count:/ {free = $2}
108 /vm.stats.vm.v_page_count:/ {
109 total = $2;
110 used = total - inactive - cache - free;
111 printf "%d\n", used / total * 100}' "$1"
112 }
113
114 # ########################################################################
115 # Determine whether this program is being executed directly, or sourced/included
116 # from another file.
117 # ########################################################################
118 is_not_sourced() {
119 [ "${0##*/}" = "pmp-check-unix-memory" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ]
120 }
121
122 # ########################################################################
123 # Execute the program if it was not included from another file.
124 # This makes it possible to include without executing, and thus test.
125 # ########################################################################
126 if is_not_sourced; then
127 OUTPUT=$(main "$@")
128 EXITSTATUS=$STATE_UNKNOWN
129 case "${OUTPUT}" in
130 UNK*) EXITSTATUS=$STATE_UNKNOWN; ;;
131 OK*) EXITSTATUS=$STATE_OK; ;;
132 WARN*) EXITSTATUS=$STATE_WARNING; ;;
133 CRIT*) EXITSTATUS=$STATE_CRITICAL; ;;
134 esac
135 echo "${OUTPUT}"
136 exit $EXITSTATUS
137 fi
138
139 # ############################################################################
140 # Documentation
141 # ############################################################################
142 : <<'DOCUMENTATION'
143 =pod
144
145 =head1 NAME
146
147 pmp-check-unix-memory - Alert on low memory.
148
149 =head1 SYNOPSIS
150
151 Usage: pmp-check-unix-memory [OPTIONS]
152 Options:
153 -c CRIT Critical threshold; default 95%.
154 -w WARN Warning threshold; default 90%.
155 --help Print help and exit.
156 --version Print version and exit.
157 Options must be given as --option value, not --option=value or -Ovalue.
158 Use perldoc to read embedded documentation with more details.
159
160 =head1 DESCRIPTION
161
162 This Nagios plugin examines C</proc/meminfo> (Linux) or the output of C<sysctl> (BSD)
163 to check whether the system is running out of memory and finds the largest
164 process in memory from C<ps> output.
165
166 The plugin is tested on GNU/Linux and FreeBSD.
167
168 =head1 PRIVILEGES
169
170 This plugin does not access MySQL.
171
172 This plugin executes the following UNIX commands that may need special privileges:
173
174 =over
175
176 =item *
177
178 cat /proc/meminfo (Linux), sysctl (BSD)
179
180 =item *
181
182 ps
183
184 =back
185
186 =head1 COPYRIGHT, LICENSE, AND WARRANTY
187
188 This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc.
189 Feedback and improvements are welcome.
190
191 THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
192 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
193 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
194
195 This program is free software; you can redistribute it and/or modify it under
196 the terms of the GNU General Public License as published by the Free Software
197 Foundation, version 2. You should have received a copy of the GNU General
198 Public License along with this program; if not, write to the Free Software
199 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
200
201 =head1 VERSION
202
203 $PROJECT_NAME$ pmp-check-unix-memory $VERSION$
204
205 =cut
206
207 DOCUMENTATION