3 # check_mem_ucd_snmp: nagios-check testing the memory of a linux-system via SNMP
5 # see http://www.intra2net.com/en/developer/monitoring/
6 # for current version, documentation, contact information etc.
8 # (C) 2009 by Gerd v. Egidy <gerd.von.egidy@intra2net.com>
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
26 my $np = Nagios::Plugin->new(
27 plugin => "check_mem_ucd_snmp",
29 blurb => "nagios-check testing the memory of a linux-system via SNMP",
30 usage => "Usage: %s [-h|--help] -H <hostname> [-p <port>] [-t <timeout>] [-T <retries>] [-v] [-i (KB|MB|GB)]\n"
31 . "[-1 | -2] [-c <community>]\n"
32 . "[-3] [-u <username>] [-a (md5|sha)] [-A <authpasswd>] [-x (des|aes)] [-X <privpasswd>]\n"
33 . " [-E <contextengineid>] [-n <contextname>]\n"
34 . "[-t <total-threshold>] [-T <total-threshold>] [-r <real-threshold>] [-R <real-threshold>]",
35 extra => "\nAll thresholds are in nagios standard format, see\n"
36 . "http://nagiosplug.sourceforge.net/developer-guidelines.html#THRESHOLDFORMAT\n"
37 . "for a description\n"
39 . "This check does currently not support setting the security engine id.\n",
40 url => "http://www.intra2net.com/en/developer/monitoring/",
41 license => "This nagios plugin is free software, and comes with ABSOLUTELY\n"
42 . "NO WARRANTY. It may be used, redistributed and/or modified under\n"
43 . "the terms of the GNU General Public Licence; either version 2\n"
44 . "of the License, or (at your option) any later version."
49 spec => "hostname|H=s",
50 help => "hostname or IP",
59 spec => "retries|T=i",
60 help => "SNMP retries on error, default: %s",
65 help => "unit for output and thresholds (KB|MB|GB), default: %s",
72 help => "use SNMP version 1"
76 help => "use SNMP version 2c"
79 spec => "community|c=s",
80 help => "SNMPv1/SNMPv2c community name, default: %s",
87 help => "use SNMP version 3"
91 spec => "username|u=s",
92 help => "SNMPv3 username"
95 spec => "authprotocol|a=s",
96 help => "SNMPv3 authentication protocol (md5|sha), default: %s",
100 spec => "authpassword|A=s",
101 help => "SNMPv3 authentication password"
104 spec => "privprotocol|x=s",
105 help => "SNMPv3 privacy protocol (des|aes), default: %s",
109 spec => "privpassword|X=s",
110 help => "SNMPv3 privacy password"
113 spec => "contextengineid|E=s",
114 help => "SNMPv3 context engine ID"
117 spec => "contextname|n=s",
118 help => "SNMPv3 context name"
123 spec => "totalavail-warning|m=s",
124 help => "Warning threshold for the total memory (real+swap) available, default: %s",
125 label => [ "FLOAT:FLOAT" ],
129 spec => "totalavail-critical|M=s",
130 help => "Critical threshold for the total memory (real+swap) available, default: %s",
131 label => [ "FLOAT:FLOAT" ],
135 spec => "realavail-warning|r=s",
136 help => "Warning threshold for the real memory available (free+buffer+cache), default: %s",
137 label => [ "FLOAT:FLOAT" ],
141 spec => "realavail-critical|R=s",
142 help => "Critical threshold for the real memory available (free+buffer+cache), default: %s",
143 label => [ "FLOAT:FLOAT" ],
150 # care for the unit parameter
153 if (uc($np->opts->get("unit")) eq "KB")
158 elsif (uc($np->opts->get("unit")) eq "MB")
163 elsif (uc($np->opts->get("unit")) eq "GB")
170 $np->nagios_die("illegal unit requested");
173 # sanity-check the snmp-version
174 my $snmpversion=undef;
176 if ($np->opts->get("snmpv1"))
180 if ($np->opts->get("snmpv2c"))
182 if (defined $snmpversion)
184 $np->nagios_die("you can only define one SNMP version");
191 if ($np->opts->get("snmpv3"))
193 if (defined $snmpversion)
195 $np->nagios_die("you can only define one SNMP version");
202 if (not defined $snmpversion)
204 # snmp default version
208 # create the SNMP session
209 my %snmpsessionparam = (
210 -hostname => $np->opts->get("hostname"),
211 -port => $np->opts->get("port"),
212 -version => $snmpversion,
213 -timeout => $np->opts->get("timeout"),
214 -retries => $np->opts->get("retries"),
216 my %snmprequestparam = ();
218 if ($snmpversion == 1 || $snmpversion == 2)
220 $snmpsessionparam{-community}=$np->opts->get("community");
224 $snmpsessionparam{-username}=$np->opts->get("username");
226 if (defined $np->opts->get("authpassword"))
228 $snmpsessionparam{-authprotocol}=$np->opts->get("authprotocol");
229 $snmpsessionparam{-authpassword}=$np->opts->get("authpassword");
232 if (defined $np->opts->get("privpassword"))
234 $snmpsessionparam{-privprotocol}=$np->opts->get("privprotocol");
235 $snmpsessionparam{-privpassword}=$np->opts->get("privpassword");
238 if (defined $np->opts->get("contextname"))
240 $snmprequestparam{-contextname}=$np->opts->get("contextname");
242 if (defined $np->opts->get("contextengineid"))
244 $snmprequestparam{-contextengineid}=$np->opts->get("contextengineid");
248 ($np->opts->get("verbose") > 1) && print Data::Dumper->Dump([\%snmpsessionparam], [qw(snmp_session_data)]);
250 my ($session,$snmperror)=Net::SNMP->session(%snmpsessionparam);
252 if (!defined $session)
254 $np->nagios_die("Error opening SNMP session: $snmperror");
257 # prepare & execute the snmp request
258 my $oid_memTotalSwap = "1.3.6.1.4.1.2021.4.3.0";
259 my $oid_memAvailSwap = "1.3.6.1.4.1.2021.4.4.0";
260 my $oid_memTotalReal = "1.3.6.1.4.1.2021.4.5.0";
261 my $oid_memAvailReal = "1.3.6.1.4.1.2021.4.6.0";
262 my $oid_memBuffer = "1.3.6.1.4.1.2021.4.14.0";
263 my $oid_memCached = "1.3.6.1.4.1.2021.4.15.0";
264 my $oid_ssSwapIn = "1.3.6.1.4.1.2021.11.3.0";
265 my $oid_ssSwapOut = "1.3.6.1.4.1.2021.11.4.0";
266 my $oid_intranator_swap_warning = "1.3.6.1.4.1.30475.1.1.3";
268 # we really need these oids to complete the check
269 my @essential_oidlist = ($oid_memTotalSwap, $oid_memAvailSwap, $oid_memTotalReal, $oid_memAvailReal,
270 $oid_memBuffer, $oid_memCached);
271 # all oids, including the additional ones
272 my @oidlist = (@essential_oidlist, $oid_ssSwapIn, $oid_ssSwapOut, $oid_intranator_swap_warning);
274 my %thisrequest=%snmprequestparam;
275 $thisrequest{-varbindlist}=\@oidlist;
277 ($np->opts->get("verbose") > 2) && print Data::Dumper->Dump([\%thisrequest], [qw(snmp_request_data)]);
279 my $result = $session->get_request(%thisrequest);
280 if (!defined $result)
282 $snmperror=$session->error;
284 $np->nagios_die("Error in SNMP request: $snmperror");
287 ($np->opts->get("verbose") > 0) && print Data::Dumper->Dump([$result], [qw(snmp_result)]);
291 # check if the essential oids are there
292 foreach my $oid (@essential_oidlist)
294 if (!defined $result->{$oid} || $result->{$oid} eq "noSuchObject")
296 $np->nagios_die("Essential OID $oid missing in result");
300 # interpret the results: use only OIDs listed as essential here!
301 my $realmem=$result->{$oid_memTotalReal};
302 my $realavail=$result->{$oid_memAvailReal}+$result->{$oid_memBuffer}+$result->{$oid_memCached};
303 my $swap=$result->{$oid_memTotalSwap};
304 my $swapused=$swap-$result->{$oid_memAvailSwap};
305 my $totalavail=$realavail+$result->{$oid_memAvailSwap};
307 my @resultvar=(\$realmem, \$realavail, \$swap, \$swapused, \$totalavail);
308 foreach my $varref (@resultvar)
313 # check the thresholds: do this BEFORE the rounding
316 push (@results, $np->check_threshold(
317 check => $totalavail,
318 warning => $np->opts->get("totalavail-warning"),
319 critical => $np->opts->get("totalavail-critical")
321 push (@results, $np->check_threshold(
323 warning => $np->opts->get("realavail-warning"),
324 critical => $np->opts->get("realavail-critical")
327 # rounding: show decimals only if value less than limit
328 foreach my $varref (@resultvar)
332 $$varref=sprintf("%.2f",$$varref)
336 $$varref=sprintf("%.0f",$$varref)
340 # output the performance data
342 label => "total_avail",
343 value => $totalavail,
345 warning => $np->opts->get("totalavail-warning"),
346 critical => $np->opts->get("totalavail-critical"),
348 max => $swap+$realmem
351 label => "real_avail",
354 warning => $np->opts->get("realavail-warning"),
355 critical => $np->opts->get("realavail-critical"),
360 label => "swap_used",
369 $np->max_state(@results),
370 "Real av: $realavail $unitstr, Total av: $totalavail $unitstr, Swapped: $swapused $unitstr",