check_exec.pl
changeset 9 69c1005df8ce
parent 7 31b228cc3b89
child 11 92120bd108c9
equal deleted inserted replaced
8:d2d76379179e 9:69c1005df8ce
    15 #    You should have received a copy of the GNU General Public License
    15 #    You should have received a copy of the GNU General Public License
    16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17 #
    17 #
    18 #    Christian Arnold <arnold@schlittermann.de>
    18 #    Christian Arnold <arnold@schlittermann.de>
    19 
    19 
       
    20 use 5.010;
    20 use strict;
    21 use strict;
    21 use File::Basename;
    22 use File::Basename;
    22 use Pod::Usage;
    23 use Pod::Usage;
    23 use Getopt::Long;
    24 use Getopt::Long;
    24 use LWP::Simple;
    25 use LWP::Simple;
    25 use HTTP::Status;
    26 use HTTP::Status;
    26 use File::Path;
    27 use File::Path;
       
    28 use File::Temp;
    27 
    29 
    28 use lib "/usr/lib/nagios/plugins";
    30 use lib "/usr/lib/nagios/plugins";
    29 use utils qw (%ERRORS);
    31 use utils qw (%ERRORS);
    30 
    32 
    31 $ENV{LANG} = "POSIX";
    33 $ENV{LANG} = "POSIX";
    38 sub cleanup($);
    40 sub cleanup($);
    39 sub execute($);
    41 sub execute($);
    40 sub version($$);
    42 sub version($$);
    41 
    43 
    42 my $opt = {
    44 my $opt = {
    43     url    => "",
    45     url    => undef,
    44     path   => "/var/tmp/nagios",
    46     path   => "/var/tmp/nagios",
    45     binary => "/usr/bin/gpg"
    47     binary => "/usr/bin/gpg"
    46 };
    48 };
    47 
    49 
    48 MAIN: {
    50 MAIN: {
    52         "b|binary=s" => \$opt->{binary},
    54         "b|binary=s" => \$opt->{binary},
    53         "p|path=s"   => \$opt->{path},
    55         "p|path=s"   => \$opt->{path},
    54         "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) },
    56         "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) },
    55         "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) },
    57         "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) },
    56         "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
    58         "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
    57       )
    59     ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    58       or do {
    60 
    59         pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    61     $opt->{url} // pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    60         exit $ERRORS{CRITICAL};
    62 
    61       };
    63     my $file = download($opt->{url}, $opt->{path});
    62 
    64     my $run_file = verify($file);
    63     unless ($opt->{url}) {
    65 
    64         pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    66     warn "<< $run_file >>\n";
    65         exit $ERRORS{CRITICAL};
    67     die `cat $run_file`;
    66     }
       
    67 
       
    68     download($opt->{url}, $opt->{path});
       
    69 }
    68 }
    70 
    69 
    71 sub execute($) {
    70 sub execute($) {
    72     my $run_file = shift;
    71     my $run_file = shift;
    73     chmod 0755, $run_file or do {
    72     chmod 0755, $run_file or do {
    99     my $file = basename $url;
    98     my $file = basename $url;
   100 
    99 
   101     unless (-d $path) {
   100     unless (-d $path) {
   102         mkpath($path, { mode => 0700, error => \my $err });
   101         mkpath($path, { mode => 0700, error => \my $err });
   103         for my $diag (@$err) {
   102         for my $diag (@$err) {
   104             my ($directory, $message) = each %$diag;
   103             my ($directory, $message) = %$diag;
   105             print
   104             print "EXEC CRITICAL: Can't create directory $directory: $message\n";
   106               "EXEC CRITICAL: Can't create directory $directory: $message\n";
       
   107             exit $ERRORS{CRITICAL};
       
   108         }
   105         }
       
   106 	exit $ERRORS{CRITICAL} if defined $err;
   109     }
   107     }
   110 
   108 
   111     $file = "$path/$file";
   109     $file = "$path/$file";
   112 
   110 
   113     my $rc = getstore($url, $file);
   111     my $rc = getstore($url, $file);
   114     if (is_error($rc)) {
   112     if (is_error($rc)) {
       
   113 	unlink $file;
       
   114 
   115         if ($rc == 404) {
   115         if ($rc == 404) {
   116             print "EXEC OK: $url ", status_message($rc), "\n";
   116             print "EXEC OK: $url ", status_message($rc), "\n";
   117             cleanup($file);
       
   118             exit $ERRORS{OK};
   117             exit $ERRORS{OK};
   119         }
   118         } 
   120         else {
   119 
   121             print "EXEC CRITICAL: $url ", status_message($rc), "\n";
   120 	print "EXEC CRITICAL: $url ", status_message($rc), "\n";
   122             cleanup($file);
   121 	exit $ERRORS{CRITICAL};
   123             exit $ERRORS{CRITICAL};
   122     }
   124         }
   123 
   125     }
   124     return $file;
   126 
       
   127     verify($file);
       
   128 }
   125 }
   129 
   126 
   130 sub verify($) {
   127 sub verify($) {
   131     my $file     = shift;
   128     my $file     = shift;
   132     my $dir      = dirname($file);
   129     my $dir      = dirname($file);
   133     my $run_file = fileparse($file, qw/\.[^.]*/);
   130     my $tmp = File::Temp->new();
   134     my $home_dir = (getpwuid($>))[7];
   131     my $home_dir = (getpwuid($>))[7];
   135 
   132 
   136     my $vc = qq|$opt->{binary} --homedir $home_dir/.gnupg --verify|;
   133 
   137     my $dc = qq|$opt->{binary} --homedir $home_dir/.gnupg --batch --yes|;
   134     my $dc = "$opt->{binary} --output @{[$tmp->filename]} --homedir $home_dir/.gnupg --batch --yes";
   138 
   135     my @r = qx($dc $file 2>&1);
   139     my @r = qx/$vc $file 2>&1/;
       
   140     if ($?) {
   136     if ($?) {
   141         print "EXEC CRITICAL: @r";
   137         print "EXEC CRITICAL: @r";
   142         exit $ERRORS{CRITICAL};
   138         exit $ERRORS{CRITICAL};
   143     }
   139     }
   144 
   140 
   145     @r = qx/$dc $file 2>&1/;
   141     return $tmp->filename;
   146     if ($?) {
       
   147         print "EXEC CRITICAL: @r";
       
   148         exit $ERRORS{CRITICAL};
       
   149     }
       
   150 
       
   151     execute("$dir/$run_file");
       
   152 }
   142 }
   153 
   143 
   154 sub version($$) {
   144 sub version($$) {
   155     my $progname = shift;
   145     my $progname = shift;
   156     my $version  = shift;
   146     my $version  = shift;