# HG changeset patch # User Christian Arnold # Date 1292334706 -3600 # Node ID 9732a762d17c85127ac3ef546a0719a469d9ba97 Initial commit diff -r 000000000000 -r 9732a762d17c .perltidyrc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.perltidyrc Tue Dec 14 14:51:46 2010 +0100 @@ -0,0 +1,4 @@ +--paren-tightness=2 +--square-bracket-tightness=2 + + diff -r 000000000000 -r 9732a762d17c check_generic.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/check_generic.pl Tue Dec 14 14:51:46 2010 +0100 @@ -0,0 +1,206 @@ +#!/usr/bin/perl -w + +use strict; +use File::Basename; +use Getopt::Long; +use LWP::Simple; +use HTTP::Status; +use GnuPG qw( :algo ); + +use lib "/usr/lib/nagios/plugins"; +use utils qw (%ERRORS &print_revision &support); + +my $ME = basename $0; +my $VERSION = "0.1"; +my $USAGE = < -s + $ME [ -h | --help ] + $ME [ -V | --version ] +EOF + +sub print_help(); +sub print_usage(); + +sub download(); +sub verify($$); +sub cleanup($$); +sub execute($); + +my $opt_dl_file = ""; +my $opt_dl_signature_file = ""; + +my $dlpath = "/var/tmp/nagios"; +my ($file, $signature); + +MAIN: { + Getopt::Long::Configure('bundling'); + GetOptions( + "f|file=s" => \$opt_dl_file, + "s|signature=s" => \$opt_dl_signature_file, + "h|help" => sub { print_help(); exit $ERRORS{OK}; }, + "V|version" => sub { print_revision($ME, $VERSION); exit $ERRORS{OK}; } + ); + + unless ($opt_dl_file) { + print $USAGE; + exit $ERRORS{"CRITICAL"}; + } + + unless ($opt_dl_signature_file) { + print $USAGE; + exit $ERRORS{"CRITICAL"}; + } + + download(); +} + +sub execute($) { + my $file = shift; + chmod 0755, $file or print print "GENERIC WARNING: can't chmod $file\n"; + my @cmd = ("$file"); + + open(OUTPUT, "-|") or do { + open(STDERR, ">&STDOUT"); + system(@cmd); + }; + + my $result = ; + + close(OUTPUT); + + if ($? == -1) { + print "GENERIC CRITICAL: failed to execute: $!\n"; + cleanup($file, $signature), exit $ERRORS{"CRITICAL"}; + } + elsif ($? & 127) { + printf "GENERIC CRITICAL: child died with signal %d, %s coredump\n", + ($? & 127), ($? & 128) ? 'with' : 'without'; + cleanup($file, $signature), exit $ERRORS{"CRITICAL"}; + } + else { + my $rc = $? >> 8; + if ($rc == $ERRORS{"OK"}) { + print "GENERIC OK: $result"; + cleanup($file, $signature), exit $ERRORS{"OK"}; + } + elsif ($rc == $ERRORS{"WARNING"}) { + print "GENERIC WARNING: $result"; + cleanup($file, $signature), exit $ERRORS{"WARNING"}; + } + elsif ($rc == $ERRORS{"CRITICAL"}) { + print "GENERIC CRITICAL: $result"; + cleanup($file, $signature), exit $ERRORS{"CRITICAL"}; + } + elsif ($rc == $ERRORS{"UNKNOWN"}) { + print "GENERIC UNKNOWN: $result"; + cleanup($file, $signature), exit $ERRORS{"UNKNOWN"}; + } + elsif ($rc == $ERRORS{"DEPENDENT"}) { + print "GENERIC DEPENDENT: $result"; + cleanup($file, $signature), exit $ERRORS{"DEPENDENT"}; + } + } +} + +sub cleanup($$) { + my $file = shift; + my $signature = shift; + if (-f $file) { + unlink $file or do { + print "GENERIC WARNING: can't remove $file\n"; + exit $ERRORS{"WARNING"}; + } + } + if (-f $signature) { + unlink $signature or do { + print "GENERIC CRITICAL: can't remove $signature\n"; + exit $ERRORS{"WARNING"}; + } + } +} + +sub download() { + my $dl_file = basename $opt_dl_file; + my $dl_signature_file = basename $opt_dl_signature_file; + + unless (-d $dlpath) { + mkdir $dlpath or do { + print "GENERIC CRITICAL: can't create directory $dlpath\n"; + exit $ERRORS{"CRITICAL"}; + } + } + + $file = "$dlpath/$dl_file"; + $signature = "$dlpath/$dl_signature_file"; + + # get script file + my $rc = getstore($opt_dl_file, "$file"); + if (is_error($rc)) { + if ($rc == "404") { + print "GENERIC OK: $opt_dl_file ", status_message($rc), "\n"; + cleanup($file, $signature), exit $ERRORS{"OK"}; + } + else { + print "GENERIC CRITICAL: SCRIPT $opt_dl_file ", status_message($rc), + "\n"; + cleanup($file, $signature), exit $ERRORS{"CRITICAL"}; + } + } + + # get script signature file + $rc = getstore($opt_dl_signature_file, "$signature"); + if (is_error($rc)) { + if ($rc == "404") { + print "GENERIC OK: $opt_dl_signature_file ", status_message($rc), + "\n"; + cleanup($file, $signature), exit $ERRORS{"OK"}; + } + else { + print "GENERIC CRITICAL: SIGNATURE $opt_dl_signature_file ", + status_message($rc), "\n"; + cleanup($file, $signature), exit $ERRORS{"CRITICAL"}; + } + } + + verify($file, $signature); + execute($file); +} + +sub verify($$) { + my $file = shift; + my $signature = shift; + + my $gpg = new GnuPG(); + eval { $gpg->verify(signature => $signature, file => $file); }; + + # formating error output + if ($@) { + $@ =~ /^(.*)\sfrom\s+at.*/; + print "GENERIC CRITICAL: $1\n"; + cleanup($file, $signature), exit $ERRORS{"CRITICAL"}; + } +} + +sub print_usage() { print $USAGE } + +sub print_help() { + print_revision($ME, $VERSION); + print <