--- a/check_exec.pl Tue Dec 28 15:04:51 2010 +0100
+++ b/check_exec.pl Tue Dec 28 16:38:55 2010 +0100
@@ -17,6 +17,7 @@
#
# Christian Arnold <arnold@schlittermann.de>
+use 5.010;
use strict;
use File::Basename;
use Pod::Usage;
@@ -24,6 +25,7 @@
use LWP::Simple;
use HTTP::Status;
use File::Path;
+use File::Temp;
use lib "/usr/lib/nagios/plugins";
use utils qw (%ERRORS);
@@ -40,7 +42,7 @@
sub version($$);
my $opt = {
- url => "",
+ url => undef,
path => "/var/tmp/nagios",
binary => "/usr/bin/gpg"
};
@@ -54,18 +56,15 @@
"h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) },
"m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) },
"V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
- )
- or do {
- pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
- exit $ERRORS{CRITICAL};
- };
+ ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
+
+ $opt->{url} // pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
- unless ($opt->{url}) {
- pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
- exit $ERRORS{CRITICAL};
- }
+ my $file = download($opt->{url}, $opt->{path});
+ my $run_file = verify($file);
- download($opt->{url}, $opt->{path});
+ warn "<< $run_file >>\n";
+ die `cat $run_file`;
}
sub execute($) {
@@ -101,54 +100,45 @@
unless (-d $path) {
mkpath($path, { mode => 0700, error => \my $err });
for my $diag (@$err) {
- my ($directory, $message) = each %$diag;
- print
- "EXEC CRITICAL: Can't create directory $directory: $message\n";
- exit $ERRORS{CRITICAL};
+ my ($directory, $message) = %$diag;
+ print "EXEC CRITICAL: Can't create directory $directory: $message\n";
}
+ exit $ERRORS{CRITICAL} if defined $err;
}
$file = "$path/$file";
my $rc = getstore($url, $file);
if (is_error($rc)) {
+ unlink $file;
+
if ($rc == 404) {
print "EXEC OK: $url ", status_message($rc), "\n";
- cleanup($file);
exit $ERRORS{OK};
- }
- else {
- print "EXEC CRITICAL: $url ", status_message($rc), "\n";
- cleanup($file);
- exit $ERRORS{CRITICAL};
- }
+ }
+
+ print "EXEC CRITICAL: $url ", status_message($rc), "\n";
+ exit $ERRORS{CRITICAL};
}
- verify($file);
+ return $file;
}
sub verify($) {
my $file = shift;
my $dir = dirname($file);
- my $run_file = fileparse($file, qw/\.[^.]*/);
+ my $tmp = File::Temp->new();
my $home_dir = (getpwuid($>))[7];
- my $vc = qq|$opt->{binary} --homedir $home_dir/.gnupg --verify|;
- my $dc = qq|$opt->{binary} --homedir $home_dir/.gnupg --batch --yes|;
- my @r = qx/$vc $file 2>&1/;
+ my $dc = "$opt->{binary} --output @{[$tmp->filename]} --homedir $home_dir/.gnupg --batch --yes";
+ my @r = qx($dc $file 2>&1);
if ($?) {
print "EXEC CRITICAL: @r";
exit $ERRORS{CRITICAL};
}
- @r = qx/$dc $file 2>&1/;
- if ($?) {
- print "EXEC CRITICAL: @r";
- exit $ERRORS{CRITICAL};
- }
-
- execute("$dir/$run_file");
+ return $tmp->filename;
}
sub version($$) {