diff options
author | Dmitry Sivachenko <demon@FreeBSD.org> | 2011-12-07 14:24:20 +0000 |
---|---|---|
committer | Dmitry Sivachenko <demon@FreeBSD.org> | 2011-12-07 14:24:20 +0000 |
commit | f561562808c167c1fce604d4ced66902670ff5ca (patch) | |
tree | 9d1922402f5ad39cb52703ef60e234083bc9fcf8 /net-mgmt/nagios-check_netsnmp | |
parent | e63d4e62a87064075dc5b7eafa1d3ca11aead109 (diff) | |
download | ports-f561562808c167c1fce604d4ced66902670ff5ca.tar.gz ports-f561562808c167c1fce604d4ced66902670ff5ca.zip |
Notes
Diffstat (limited to 'net-mgmt/nagios-check_netsnmp')
-rw-r--r-- | net-mgmt/nagios-check_netsnmp/Makefile | 31 | ||||
-rw-r--r-- | net-mgmt/nagios-check_netsnmp/files/check_netsnmp | 242 | ||||
-rw-r--r-- | net-mgmt/nagios-check_netsnmp/files/check_netsnmp.html | 106 | ||||
-rw-r--r-- | net-mgmt/nagios-check_netsnmp/pkg-descr | 9 |
4 files changed, 388 insertions, 0 deletions
diff --git a/net-mgmt/nagios-check_netsnmp/Makefile b/net-mgmt/nagios-check_netsnmp/Makefile new file mode 100644 index 000000000000..3a45d16ace4f --- /dev/null +++ b/net-mgmt/nagios-check_netsnmp/Makefile @@ -0,0 +1,31 @@ +# New ports collection makefile for: nagios-check_netsnmp +# Date created: 7 December 2011 +# Whom: Dmitry Sivachenko <demon@FreeBSD.org> +# +# $FreeBSD$ +# + +PORTNAME= check_netsnmp +PORTVERSION= 1.0 +CATEGORIES= net-mgmt +MASTER_SITES= # none +PKGNAMEPREFIX= nagios- +DISTFILES= # none + +MAINTAINER= demon@FreeBSD.org +COMMENT= Nagios plug-in to get some common NetSNMP-exported information + +RUN_DEPENDS= ${LOCALBASE}/libexec/nagios/utils.pm:${PORTSDIR}/net-mgmt/nagios-plugins \ + ${SITE_PERL}/Net/SNMP.pm:${PORTSDIR}/net-mgmt/p5-Net-SNMP + +USE_PERL5_RUN= yes +NO_BUILD= yes +PLIST_FILES= libexec/nagios/check_netsnmp %%DOCSDIR%%/check_netsnmp.html +PLIST_DIRS= %%DOCSDIR%% + +do-install: + ${INSTALL_SCRIPT} ${FILESDIR}/check_netsnmp ${PREFIX}/libexec/nagios + ${MKDIR} ${DOCSDIR} + ${INSTALL_DATA} ${FILESDIR}/check_netsnmp.html ${DOCSDIR} + +.include <bsd.port.mk> diff --git a/net-mgmt/nagios-check_netsnmp/files/check_netsnmp b/net-mgmt/nagios-check_netsnmp/files/check_netsnmp new file mode 100644 index 000000000000..42f6eb250b25 --- /dev/null +++ b/net-mgmt/nagios-check_netsnmp/files/check_netsnmp @@ -0,0 +1,242 @@ +#! /usr/bin/perl -w + +# ---------------------------------------------------------------------------- +# "THE BEER-WARE LICENSE" (Revision 42, (c) Poul-Henning Kamp): +# Dmitry Sivachenko <mitya@cavia.pp.ru> wrote this file. As long as you +# retain this notice you can do whatever you want with this stuff. If we +# meet some day, and you think this stuff is worth it, you can buy me a +# beer in return. +# +# Dmitry Sivachenko +# ---------------------------------------------------------------------------- + +use strict; + +use Getopt::Long; +use Net::SNMP; +use lib "/usr/local/libexec/nagios"; +use utils qw($TIMEOUT %ERRORS); + +my $load = '.1.3.6.1.4.1.2021.10.1.'; +my $proc = '.1.3.6.1.4.1.2021.2.1.'; +my $disk = '.1.3.6.1.4.1.2021.9.1.'; +my $file = '.1.3.6.1.4.1.2021.15.1.'; +my $swap = '.1.3.6.1.4.1.2021.4.'; +my $exec = '.1.3.6.1.4.1.2021.8.'; +my $errorFlag = '100'; +my $errorMsg = '101'; +my $TotalSwap = '.1.3.6.1.4.1.2021.4.3.0'; +my $AvailSwap = '.1.3.6.1.4.1.2021.4.4.0'; +my $laLoad1 = '.1.3.6.1.4.1.2021.10.1.3.1'; +my $laLoad5 = '.1.3.6.1.4.1.2021.10.1.3.2'; +my $laLoad15 = '.1.3.6.1.4.1.2021.10.1.3.3'; + +my $errflag = $ERRORS{'OK'}; + +my ($opt_h, $opt_C, $opt_H, $opt_o, $opt_p, $opt_t); +Getopt::Long::Configure('bundling'); +GetOptions( + "h" => \$opt_h, "help" => \$opt_h, + "C=s" => \$opt_C, "community=s" => \$opt_C, + "H=s" => \$opt_H, "hostname=s" => \$opt_H, + "o=s" => \$opt_o, "oid=s" => \$opt_o, + "p=i" => \$opt_p, "port=i" => \$opt_p, + "t=i" => \$opt_t, "timeout=i" => \$opt_t +); + +if ($opt_h) { + print_help(); + exit $ERRORS{'OK'}; +} + +if (! utils::is_hostname($opt_H)){ + print_help(); + exit $ERRORS{"UNKNOWN"}; +} + +if ($opt_o ne 'disk' && $opt_o ne 'swap' && $opt_o ne 'proc' && + $opt_o ne 'load' && $opt_o ne 'file' && + $opt_o !~ /^\d+$/ && $opt_o !~ /^(\.\d+)+\.$/) { + print_help(); + exit $ERRORS{"UNKNOWN"}; +} + +# Just in case of problems, let's not hang Nagios +$SIG{'ALRM'} = sub { + print ("ERROR: No response from SNMP server (alarm)\n"); + exit $ERRORS{"UNKNOWN"}; +}; +alarm($TIMEOUT * 6); # 1.5 minutes + +my ($session, $error) = Net::SNMP->session( + -timeout => $opt_t || $TIMEOUT, + -hostname => $opt_H, + -community => $opt_C || 'public', + -port => $opt_p || 161 +); + +if (!defined($session)) { + printf("ERROR: %s.\n", $error); + exit $ERRORS{'UNKNOWN'}; +} + +if (!defined($opt_o)) { + print("Please specify OID to query (-c).\n"); + exit $ERRORS{'UNKNOWN'}; +} + +checkOID($opt_o); + +$session->close; +exit $errflag; + + +sub getOID { + my $result = $session->get_request(-varbindlist => [$_[0]]); + if (defined($result)) { + return $result->{$_[0]}; + } else { + return undef; + } +} + +sub checkOID { + my $baseOID; my $i; my $first_query = 1; my $first_error = 1; + if ($_[0] eq 'swap') { + $i = 0; + } else { + $i = 1; + } + if ($_[0] eq 'load') { + $baseOID = $load; + } elsif ($_[0] eq 'proc') { + $baseOID = $proc; + } elsif ($_[0] eq 'disk') { + $baseOID = $disk; + } elsif ($_[0] eq 'swap') { + $baseOID = $swap; + } elsif ($_[0] eq 'file') { + $baseOID = $file; + } else { + if ($_[0] =~ /^\d+$/) { + $baseOID = $exec . $_[0] . '.'; + } else { + $baseOID = $_[0]; + } + } + + my $tmpOID = $baseOID . $errorFlag . ".$i"; + my $res = getOID($tmpOID); + while (defined($res)) { + if ($res != 0) { + $errflag = $ERRORS{'CRITICAL'}; + $tmpOID = $baseOID . $errorMsg . ".$i"; + my $res1 = getOID($tmpOID); + if (defined($res1)) { + if (!$first_error) { + print ", "; + } + print $res1; + } else { + print 'Unable to get error message!'; + } + $first_error = 0; + } + $first_query = 0; + $i++; $tmpOID = $baseOID . $errorFlag . ".$i"; + $res = getOID($tmpOID); + } + if ($errflag == $ERRORS{'OK'} && !$first_query) { + if ($_[0] eq 'load') { + my $tmp; + $tmp = getOID($laLoad1); + if (defined($tmp)) { + print "Load-1: $tmp, "; + } else { + $errflag = $ERRORS{'UNKNOWN'}; + print "Error obtaining OID $laLoad1!"; + } + $tmp = getOID($laLoad5); + if (defined($tmp)) { + print "Load-5: $tmp, "; + } else { + $errflag = $ERRORS{'UNKNOWN'}; + print "Error obtaining OID $laLoad5!"; + } + $tmp = getOID($laLoad15); + if (defined($tmp)) { + print "Load-15: $tmp"; + } else { + $errflag = $ERRORS{'UNKNOWN'}; + print "Error obtaining OID $laLoad15!"; + } + } elsif ($_[0] eq 'proc') { + print "Processes ok"; + } elsif ($_[0] eq 'disk') { + print "Disks ok"; + } elsif ($_[0] eq 'file') { + print "File sizes ok"; + } elsif ($_[0] eq 'swap') { + my $tmp; + $tmp = getOID($TotalSwap); + if (defined($tmp)) { + my $tmp1 = int($tmp/1024); + print "Total: $tmp1" . "Mb, "; + } else { + $errflag = $ERRORS{'UNKNOWN'}; + print "Error obtaining OID $TotalSwap!"; + } + $tmp = getOID($AvailSwap); + if (defined($tmp)) { + my $tmp1 = int($tmp/1024); + print "Free: $tmp1" . "Mb"; + } else { + $errflag = $ERRORS{'UNKNOWN'}; + print "Error obtaining OID $AvailSwap!"; + } + } else { + my $tmp = getOID("$baseOID$errorMsg.1"); + if (defined($tmp)) { + print $tmp; + } else { + $errflag = $ERRORS{'UNKNOWN'}; + print "Error obtaining OID $baseOID$errorMsg.1!"; + } + } + } elsif ($first_query) { + print "Error obtaining OID $tmpOID! (no response?)"; + $errflag = $ERRORS{'UNKNOWN'}; + } +} + +sub print_help { + print "Usage: $0 [-C COMMUNITY] [-p port] -H HOSTNAME -o swap|proc|load|disk|file|<OID>|<exec num>\n"; + print " +-C, --community + Community name to use in queries. (default: public) +-H, --hostname + Hostname to send SNMP queries to. +-p, --port + Port number to send SNMP requests to. (default: 161) +-t, --timeout=INTEGER + Seconds before connection times out (default: 15) +-o, --oid + OID to query. $errorFlag is queried to check for errors and $errorMsg + is queried for error message set by snmpd. Shortcuts are: + swap (.1.3.6.1.4.1.2021.4). For use in conjunction with + 'swap' keyword in snmpd.conf. + proc (.1.3.6.1.4.1.2021.2.1). For use in conjunction with + 'proc' keyword in snmpd.conf. + load (.1.3.6.1.4.1.2021.10.1). For use in conjunction with + 'load' keyword in snmpd.conf. + disk (.1.3.6.1.4.1.2021.9.1). For use in conjunction with + 'disk' keyword in snmpd.conf. + <OID>. Query the specified OID. For use in conjunction with + 'exec' keyword in snmpd.conf. + file (.1.3.6.1.4.1.2021.15.1). For use in conjunction with + 'file' keyword in snmpd.conf. + <exec num>. Query the specified number in the 'exec' OID tree. + Equivalent to '.1.3.6.1.4.1.2021.8.<exec num>'. + +"; +} diff --git a/net-mgmt/nagios-check_netsnmp/files/check_netsnmp.html b/net-mgmt/nagios-check_netsnmp/files/check_netsnmp.html new file mode 100644 index 000000000000..23128f830008 --- /dev/null +++ b/net-mgmt/nagios-check_netsnmp/files/check_netsnmp.html @@ -0,0 +1,106 @@ +<html> +<head><title>check_netsnmp plugin for Nagios</title> +</head> +<body> +<center><h1>check_netsnmp plugin for Nagios</h1>(<a +href="check_netsnmp">download</a>)</center> +<p><b>Motivation.</b> There are several common parameters we want to +monitor for (almost) every computer in our network: +<ul> +<li>Free disk space +<li>Free swap space +<li>Average loads +<li>That important processes are running on the machine +<li>Total number of processes and a number of processes in +<em>zombie</em> state +<li>A number of free mbuf clusters available (most our servers run +FreeBSD, thus the notion of mbuf clusters) +</ul> +<code>snmpd</code>, provided by free <a +href="http://net-snmp.sourceforge.net/">net-snmp</a> package, provides +a convenient way to collect this information and export it by means of +SNMP protocol. It also allows one to specify the critical values for +the above parameters and automatically sets error flag to indicate the +error occured and provides error message which describes it. +Using <code>snmpd</code> it is also possible to execute custom scripts +which allows to monitor parameters which are not available in standard +mibs. +<p>check_netsnmp plugin for <a +href="http://www.nagios.org/">Nagios</a> is written in perl, uses no +external commands and +requires <code>utils</code> module, available from <a +href="http://www.nagios.org/">Naguios-plugins</a> +distribution, and <code>Net::SNMP</code> module, available from <a +href="http://www.cpan.org">CPAN</a>. +<p><b>Usage.</b> Put <code>check_netsnmp</code> plugin to the +directory in which you installed nagios-plugins (typically $USER1$ +variable points to it). +<ol> +<li><b>Monitoring free disk space.</b> Used in conjunction with +<code>disk</code> keyword in <code>snmpd.conf</code>. Imagine that +you have two mount points on the monitored machine: <code>/</code> and +<code>/var</code> and you want to be sure <code>/</code> has more than +<code>20%</code> free space and <code>/var</code> has more that +<code>30%</code> free space. Put the following lines to the +<code>snmpd.conf</code>: +<pre> +disk / 20% +disk /var 30% +</pre> +Next, define the following command in your Nagios configuration: +<pre> +define command{ + command_name check_disk + command_line $USER1$/check_netsnmp -H $HOSTADDRESS$ -o disk +} +</pre> +and add the relevant service: +<pre> +define service{ + use generic-service + host_name host.name.ru + service_description disks + check_command check_disk +} +</pre> + +The same procedure applies to checking swap, processes and load averages +(use with <code>swap</code>, <code>proc</code> and <code>load</code> +keywords in <code>snmpd.conf</code>). + +<li><b>Monitoring free mbuf clusters.</b> This is a bit harder since +there are no corresponding mibs in snmpd. Here is where snmpd's +<code>exec</code> keyword may be used. Write your own script which +checks for mbuf clusters and returns 0 if there are enough of them +free, and 1 if there are not. An example may be found <a +href="mbufmon.sh">here</a>. Then put the folowing line in your +snmpd.conf: +<pre> +exec .1.3.6.1.4.1.2021.8.3 MBufMon /usr/local/share/snmp/exec/mbufmon.sh +</pre> + +Define the following command in your Nagios configuration: +<pre> +define command{ + command_name check_mbufmon + command_line $USER1$/check_netsnmp -H $HOSTADDRESS$ -o 3 +} +</pre> +You may use the full OID instead of just 3 here, if you export the +result of mbufmon.sh under nonstandard OID. +<code>.1.3.6.1.4.1.2021.8.</code> is prepended to 3 in this example +automatically. Finally, add the service to check for mbufs: +<pre> +define service{ + use generic-service + host_name host.name.ru + service_description MBufMon + check_command check_mbufmon +} +</pre> +</ol> +<hr> +Please send your comments and suggestions to <a +href="mailto:trtrmitya@gmail.com">me</a>. +</body> +</html> diff --git a/net-mgmt/nagios-check_netsnmp/pkg-descr b/net-mgmt/nagios-check_netsnmp/pkg-descr new file mode 100644 index 000000000000..624e5e087b9a --- /dev/null +++ b/net-mgmt/nagios-check_netsnmp/pkg-descr @@ -0,0 +1,9 @@ +A plugin for Nagios to query common NetSNMP-exported information: + +-- disk status; +-- processes status; +-- swap status; +-- load averages; +-- anything else exported via NetSNMP's exec or extend directives. + +Author: trtrmitya@gmail.com |