forked from inuits/monitoring-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_mem.pl
executable file
·197 lines (172 loc) · 6.49 KB
/
check_mem.pl
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#! /usr/bin/perl -w
#
# $Id: check_mem.pl 8 2008-08-23 08:59:52Z rhomann $
#
# check_mem v1.7 plugin for nagios
#
# uses the output of `free` to find the percentage of memory used
#
# Copyright Notice: GPL
#
# History:
# v1.8 Rouven Homann - [email protected]
# + added findbin patch from Duane Toler
# + added backward compatibility patch from Timour Ezeev
#
# v1.7 Ingo Lantschner - ingo AT boxbe DOT com
# + adapted for systems with no swap (avoiding divison through 0)
#
# v1.6 Cedric Temple - cedric DOT temple AT cedrictemple DOT info
# + add swap monitoring
# + if warning and critical threshold are 0, exit with OK
# + add a directive to exclude/include buffers
#
# v1.5 Rouven Homann - [email protected]
# + perfomance tweak with free -mt (just one sub process started instead of 7)
# + more code cleanup
#
# v1.4 Garrett Honeycutt - [email protected]
# + Fixed PerfData output to adhere to standards and show crit/warn values
#
# v1.3 Rouven Homann - [email protected]
# + Memory installed, used and free displayed in verbose mode
# + Bit Code Cleanup
#
# v1.2 Rouven Homann - [email protected]
# + Bug fixed where verbose output was required (nrpe2)
# + Bug fixed where perfomance data was not displayed at verbose output
# + FindBin Module used for the nagios plugin path of the utils.pm
#
# v1.1 Rouven Homann - [email protected]
# + Status Support (-c, -w)
# + Syntax Help Informations (-h)
# + Version Informations Output (-V)
# + Verbose Output (-v)
# + Better Error Code Output (as described in plugin guideline)
#
# v1.0 Garrett Honeycutt - [email protected]
# + Initial Release
#
use strict;
use FindBin;
FindBin::again();
use lib $FindBin::Bin;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME $PROGVER);
use Getopt::Long;
use vars qw($opt_V $opt_h $verbose $opt_w $opt_c);
$PROGNAME = "check_mem";
$PROGVER = "1.8";
# add a directive to exclude buffers:
my $DONT_INCLUDE_BUFFERS = 0;
sub print_help ();
sub print_usage ();
Getopt::Long::Configure('bundling');
GetOptions ("V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"v" => \$verbose, "verbose" => \$verbose,
"w=s" => \$opt_w, "warning=s" => \$opt_w,
"c=s" => \$opt_c, "critical=s" => \$opt_c);
if ($opt_V) {
print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
exit $ERRORS{'UNKNOWN'};
}
if ($opt_h) {
print_help();
exit $ERRORS{'UNKNOWN'};
}
print_usage() unless (($opt_c) && ($opt_w));
my ($mem_critical, $swap_critical);
my ($mem_warning, $swap_warning);
($mem_critical, $swap_critical) = ($1,$2) if ($opt_c =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);
($mem_warning, $swap_warning) = ($1,$2) if ($opt_w =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);
# Check if swap params were supplied
$swap_critical ||= 100;
$swap_warning ||= 100;
# print threshold in output message
my $mem_threshold_output = " (";
my $swap_threshold_output = " (";
if ( $mem_warning > 0 && $mem_critical > 0) {
$mem_threshold_output .= "W> $mem_warning, C> $mem_critical";
}
elsif ( $mem_warning > 0 ) {
$mem_threshold_output .= "W> $mem_warning";
}
elsif ( $mem_critical > 0 ) {
$mem_threshold_output .= "C> $mem_critical";
}
if ( $swap_warning > 0 && $swap_critical > 0) {
$swap_threshold_output .= "W> $swap_warning, C> $swap_critical";
}
elsif ( $swap_warning > 0 ) {
$swap_threshold_output .= "W> $swap_warning";
}
elsif ( $swap_critical > 0 ) {
$swap_threshold_output .= "C> $swap_critical";
}
$mem_threshold_output .= ")";
$swap_threshold_output .= ")";
my $verbose = $verbose;
my ($mem_percent, $mem_total, $mem_used, $swap_percent, $swap_total, $swap_used) = &sys_stats();
my $free_mem = $mem_total - $mem_used;
my $free_swap = $swap_total - $swap_used;
# set output message
my $output = "Memory Usage".$mem_threshold_output.": ". $mem_percent.'% <br>';
$output .= "Swap Usage".$swap_threshold_output.": ". $swap_percent.'%';
# set verbose output message
my $verbose_output = "Memory Usage:".$mem_threshold_output.": ". $mem_percent.'% '."- Total: $mem_total MB, used: $mem_used MB, free: $free_mem MB<br>";
$verbose_output .= "Swap Usage:".$swap_threshold_output.": ". $swap_percent.'% '."- Total: $swap_total MB, used: $swap_used MB, free: $free_swap MB<br>";
# set perfdata message
my $perfdata_output = "MemUsed=$mem_percent\%;$mem_warning;$mem_critical";
$perfdata_output .= " SwapUsed=$swap_percent\%;$swap_warning;$swap_critical";
# if threshold are 0, exit with OK
if ( $mem_warning == 0 ) { $mem_warning = 101 };
if ( $swap_warning == 0 ) { $swap_warning = 101 };
if ( $mem_critical == 0 ) { $mem_critical = 101 };
if ( $swap_critical == 0 ) { $swap_critical = 101 };
if ($mem_percent>$mem_critical || $swap_percent>$swap_critical) {
if ($verbose) { print "<b>CRITICAL: ".$verbose_output."</b>|".$perfdata_output."\n";}
else { print "<b>CRITICAL: ".$output."</b>|".$perfdata_output."\n";}
exit $ERRORS{'CRITICAL'};
} elsif ($mem_percent>$mem_warning || $swap_percent>$swap_warning) {
if ($verbose) { print "<b>WARNING: ".$verbose_output."</b>|".$perfdata_output."\n";}
else { print "<b>WARNING: ".$output."</b>|".$perfdata_output."\n";}
exit $ERRORS{'WARNING'};
} else {
if ($verbose) { print "OK: ".$verbose_output."|".$perfdata_output."\n";}
else { print "OK: ".$output."|".$perfdata_output."\n";}
exit $ERRORS{'OK'};
}
sub sys_stats {
my @memory = split(" ", `free -mt`);
my $mem_total = $memory[7];
my $mem_used;
if ( $DONT_INCLUDE_BUFFERS) { $mem_used = $memory[15]; }
else { $mem_used = $memory[8];}
my $swap_total = $memory[18];
my $swap_used = $memory[19];
my $mem_percent = ($mem_used / $mem_total) * 100;
my $swap_percent;
if ($swap_total == 0) {
$swap_percent = 0;
} else {
$swap_percent = ($swap_used / $swap_total) * 100;
}
return (sprintf("%.0f",$mem_percent),$mem_total,$mem_used, sprintf("%.0f",$swap_percent),$swap_total,$swap_used);
}
sub print_usage () {
print "Usage: $PROGNAME -w <warn> -c <crit> [-v] [-h]\n";
exit $ERRORS{'UNKNOWN'} unless ($opt_h);
}
sub print_help () {
print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
print "Copyright (c) 2005 Garrett Honeycutt/Rouven Homann/Cedric Temple\n";
print "\n";
print_usage();
print "\n";
print "-w <MemoryWarn>,<SwapWarn> = Memory and Swap usage to activate a warning message (eg: -w 90,25 ) .\n";
print "-c <MemoryCrit>,<SwapCrit> = Memory and Swap usage to activate a critical message (eg: -c 95,50 ).\n";
print "-v = Verbose Output.\n";
print "-h = This screen.\n\n";
support();
}