Hallo zusammen,
wer Interesse hat seine eigenen Plugins in Perl zu schreiben aber nicht wirklich lust/zeit hat sich mit den Nagios Plugin Developer Guidelines zu beschäftigen, der ist hier genau richtig.
Mit folgendem Perl Skelett darf sich der gemeine Perl Programmierer nun um seine eigentliche Arbeit kümmern… dem schreiben der Intelligenz.
Das Skelett bietet anhand von Beispielen die beliebten getopt_long Methoden zur Argumentübergaben á la
./check_hacker –host 127.0.0.1
Das Skelett wurde für den Einsatz von SNMP entworfen, daher auch noch die „Leichen“ à la community, oid etc.
#!/usr/bin/perl -w # # check_skeleton.pl - nagios plugin # # Copyright (C) 2006 AUTHOR, # # 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 2 # 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, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # # Report bugs to: nagiosplug-help@lists.sourceforge.net # # 26.10.2006 Version 0.2 # # use strict; use Getopt::Long; use vars qw($PROGNAME); use lib "/usr/local/nagios/libexec"; # Pfad zur util.pm !! use utils qw ($TIMEOUT %ERRORS &print_revision &support); sub print_help (); sub print_usage (); my ($opt_V, $opt_h, $opt_C, $opt_P, $opt_H, $opt_p, $opt_o, $opt_r, $opt_w, $opt_c, $opt_t); my ($result, $message, $age, $size, $st); $PROGNAME="check_skeleton"; $opt_C="public"; $opt_P="1"; $opt_H=""; $opt_p=161; $opt_o=""; $opt_r=""; $opt_w="5"; $opt_c="20"; $opt_t="10"; Getopt::Long::Configure('bundling'); GetOptions( "V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "C=s" => \$opt_C, "community" => \$opt_C, "P=s" => \$opt_P, "protocol=s" => \$opt_P, "H=s" => \$opt_H, "hostname" => \$opt_H, "p=i" => \$opt_p, "port" => \$opt_p, "o=s" => \$opt_o, "oid" => \$opt_o, "r=s" => \$opt_r, "regex" => \$opt_r, "w=s" => \$opt_w, "warning=s" => \$opt_w, "c=s" => \$opt_c, "critical=s" => \$opt_c, "t=i" => \$opt_t, "timeout" => \$opt_t); if ($opt_t) { $TIMEOUT=$opt_t; } # Just in case of problems, let's not hang Nagios $SIG{'ALRM'} = sub { print "UNKNOWN - Plugin Timed out\n"; exit $ERRORS{"UNKNOWN"}; }; alarm($TIMEOUT); if ($opt_V) { exit $ERRORS{'OK'}; } if ($opt_h) { print_help(); exit $ERRORS{'OK'}; } if (! $opt_H) { print "No Hostname specified\n\n"; print_usage(); exit $ERRORS{'UNKNOWN'}; } if (! $opt_o) { print "No OID specified\n\n"; print_usage(); exit $ERRORS{'UNKNOWN'}; } if (! $opt_r) { print "No Regex specified\n\n"; print_usage(); exit $ERRORS{'UNKNOWN'}; } sub print_usage () { print "Usage:\n"; print " $PROGNAME -H <hostname> -o <OID> -r <regex> [-C <COMMUNITY>] [-p <port>] [-P <snmp-version>] [-w <INTEGER>] [-c <INTEGER>]\n"; print " $PROGNAME [-h | --help]\n"; print " $PROGNAME [-V | --version]\n"; print "\n\nOptions:\n"; print " -H, --hostname\n"; print " Host name or IP Address\n"; print " -o, --oid\n"; print " Object identifier parent tree whose values you wish to query\n"; print " -r, --regex\n"; print " Regex like PoolName you want to check\n"; print " -C, --community\n"; print " Optional community string for SNMP communication\n"; print " (default is \"public\")\n"; print " -p, --port\n"; print " Port number (default: 161)\n"; print " -P, --protocol\n"; print " SNMP protocol version [1,2c]\n"; print " (SNMP Version 3 is not supported yet)\n"; print " -w, --warning\n"; print " FIXME: Some usefull explain for warning threshold\n"; print " -c, --critical\n"; print " FIXME: Some usefull explain for critical threshold\n"; print " -h, --help\n"; print " Print detailed help screen\n"; print " -V, --version\n"; print " Print version information\n\n"; } sub print_help () { print "Copyright (c) 200x SOME AUTHOR\n\n"; print_usage(); print "\n"; print " host Give me some Host to work with\n"; print " <help> This output\n"; print "\n"; support(); } print "Given Options:\n"; printf ("Hostname: %s\n",$opt_H); printf ("OID: %s\n",$opt_o); printf ("Regex: %s\n",$opt_r); printf ("Community: %s\n",$opt_C); printf ("Port: %i\n",$opt_p); printf ("SNMP Version: %s\n",$opt_P); printf ("Warning %s\n",$opt_w); printf ("Critical %s\n",$opt_c);