#!/usr/bin/perl -w ## ======================== ## ## Author: ## ## Email: ## ## Date: 13.11.2012 ## ## Version: 1.0 ## ## ======================== ## ## Info: use strict; use warnings; use File::Basename; use Getopt::Long; use LWP; use Data::Dumper; use XML::LibXML; use vars qw( $opt_hostname $opt_query_url $opt_basic_user $opt_basic_pass $opt_timeout $opt_verbose $opt_help ); my $progname = basename($0); my %ERRORS = ('UNKNOWN' => '3', 'OK' => '0', 'WARNING' => '1', 'CRITICAL' => '2'); sub print_help(); sub get_xml_value; my $opt_servicename = "HOST"; my $opt_basic_realm = "Notify XML Kerberos Access"; Getopt::Long::Configure('bundling'); GetOptions( "H=s" => \$opt_hostname, "hostname=s" => \$opt_hostname, "S=s" => \$opt_servicename, "servicename=s" => \$opt_servicename, "U=s" => \$opt_query_url, "query-url=s" => \$opt_query_url, "u=s" => \$opt_basic_user, "basic-user=s" => \$opt_basic_user, "p=s" => \$opt_basic_pass, "basic-pass=s" => \$opt_basic_pass, "r=s" => \$opt_basic_realm, "basic-realm=s" => \$opt_basic_realm, "v" => \$opt_verbose, "verbose" => \$opt_verbose, "t=s" => \$opt_timeout, "timeout=s" => \$opt_timeout, "h" => \$opt_help, "help" => \$opt_help ) || die "Try `$progname --help' for more information.\n"; local $SIG{ALRM} = sub{ print "Timeout after $opt_timeout seconds"; exit $ERRORS{'UNKNOWN'}; }; if($opt_timeout){alarm($opt_timeout);} if(!$opt_help && $opt_hostname && $opt_query_url && $opt_basic_user && $opt_basic_pass){ my $use_port = 80; my $base_url = ""; my $query_url_regex = 'http([s]*):\/\/([\w-]+(\.[\w-]+|\.[\w-]+\.[\w-]+))\/.+/'; if($opt_query_url =~ m/$query_url_regex/){ if($1){$use_port = 443;} $base_url = $2; } else{ print "ERROR - Unkonwn query url format: '$opt_query_url' != '$query_url_regex'\n"; exit $ERRORS{'UNKNOWN'}; } my $query_url = $opt_query_url.$opt_hostname."/".$opt_servicename.".xml"; if($opt_verbose){print "Attempting to query url: $query_url - Base URL: $base_url\n";} my $user_agent = LWP::UserAgent->new; $user_agent->agent("Nagios/0.1"); $user_agent->credentials( ($base_url.":".$use_port), $opt_basic_realm, $opt_basic_user => $opt_basic_pass ); my $request = HTTP::Request->new(POST => $query_url); $request->content_type('text/plain'); $request->content('query=libwww-perl&mode=dist'); my $response = $user_agent->request($request); if(!$response->is_success){ print $response->status_line."\n"; if($opt_verbose){"Dumping user agent:\n".Dumper($user_agent)."Dumping response:\n".Dumper($response);} exit $ERRORS{'UNKNOWN'}; } if($opt_verbose){print $response->content."\n";} my $document = XML::LibXML->new->parse_string($response->content); my $check_type = "HOST"; if($opt_servicename){$check_type = "SERVICE";} my $nagios_perfdata = get_xml_value($document,"NAGIOS_".$check_type."PERFDATA"); if($nagios_perfdata){$nagios_perfdata = "|$nagios_perfdata";} my $output = get_xml_value($document,"NAGIOS_".$check_type."OUTPUT").get_xml_value($document,"NAGIOS_LONG".$check_type."OUTPUT");.$nagios_perfdata; $output =~ s/\\n/\n/g; print $output; exit get_xml_value($document,"NAGIOS_".$check_type."STATEID"); } else{print print_help();exit $ERRORS{'UNKNOWN'};} sub get_xml_value{ my ($document,$value) = @_; my $returnvalue = ""; for($document->findnodes("//status/$value/text()")){$returnvalue = $_->toString();} return $returnvalue; } sub print_help () { print "\n$progname - \n\n"; print "Options are:\n"; print " -H, --hostname Nagios hostname\n"; print " -S, --servicename Nagios servicename\n"; print " -U, --query-url The URL where xml files are found\n"; print " -u, --basic-user Basic authentication user\n"; print " -p, --basic-pass Basic authentication pass\n"; print " -r, --basic-realm Basic authentication realm\n"; print " -v, --verbose verbose output - not for Nagios\n"; print " -t, --timeout timeout in seconds\n"; print " -h, --help display this help and exit\n\n"; print "Example:\n"; print " ./$progname\n\n"; } print "End of script. Should not be reached\n"; exit $ERRORS{'UNKNOWN'};