check_exec.pl
changeset 11 92120bd108c9
parent 9 69c1005df8ce
child 14 a74319812ee1
equal deleted inserted replaced
9:69c1005df8ce 11:92120bd108c9
    34 
    34 
    35 my $ME      = basename $0;
    35 my $ME      = basename $0;
    36 my $VERSION = "0.3";
    36 my $VERSION = "0.3";
    37 
    37 
    38 sub download($$);
    38 sub download($$);
    39 sub verify($);
    39 sub verify($$);
    40 sub cleanup($);
    40 sub cleanup($);
    41 sub execute($);
    41 sub execute($);
    42 sub version($$);
    42 sub version($$);
    43 
    43 
    44 my $opt = {
    44 my $opt = {
    58         "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
    58         "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
    59     ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    59     ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    60 
    60 
    61     $opt->{url} // pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    61     $opt->{url} // pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    62 
    62 
    63     my $file = download($opt->{url}, $opt->{path});
    63     my $tmp      = File::Temp->new();
    64     my $run_file = verify($file);
    64     my $file     = download($opt->{url}, $opt->{path});
       
    65     my $run_file = verify($file => $tmp);
    65 
    66 
    66     warn "<< $run_file >>\n";
    67     warn "<< $run_file >>\n";
    67     die `cat $run_file`;
    68     die `cat $run_file`;
    68 }
    69 }
    69 
    70 
    99 
   100 
   100     unless (-d $path) {
   101     unless (-d $path) {
   101         mkpath($path, { mode => 0700, error => \my $err });
   102         mkpath($path, { mode => 0700, error => \my $err });
   102         for my $diag (@$err) {
   103         for my $diag (@$err) {
   103             my ($directory, $message) = %$diag;
   104             my ($directory, $message) = %$diag;
   104             print "EXEC CRITICAL: Can't create directory $directory: $message\n";
   105             print
       
   106               "EXEC CRITICAL: Can't create directory $directory: $message\n";
   105         }
   107         }
   106 	exit $ERRORS{CRITICAL} if defined $err;
   108         exit $ERRORS{CRITICAL} if defined $err;
   107     }
   109     }
   108 
   110 
   109     $file = "$path/$file";
   111     $file = "$path/$file";
   110 
   112 
   111     my $rc = getstore($url, $file);
   113     my $rc = getstore($url, $file);
   112     if (is_error($rc)) {
   114     if (is_error($rc)) {
   113 	unlink $file;
   115         unlink $file;
   114 
   116 
   115         if ($rc == 404) {
   117         if ($rc == 404) {
   116             print "EXEC OK: $url ", status_message($rc), "\n";
   118             print "EXEC OK: $url ", status_message($rc), "\n";
   117             exit $ERRORS{OK};
   119             exit $ERRORS{OK};
   118         } 
   120         }
   119 
   121 
   120 	print "EXEC CRITICAL: $url ", status_message($rc), "\n";
   122         print "EXEC CRITICAL: $url ", status_message($rc), "\n";
   121 	exit $ERRORS{CRITICAL};
   123         exit $ERRORS{CRITICAL};
   122     }
   124     }
   123 
   125 
   124     return $file;
   126     return $file;
   125 }
   127 }
   126 
   128 
   127 sub verify($) {
   129 sub verify($$) {
   128     my $file     = shift;
   130     my ($file, $tmp) = @_;
   129     my $dir      = dirname($file);
   131     my $dir      = dirname($file);
   130     my $tmp = File::Temp->new();
       
   131     my $home_dir = (getpwuid($>))[7];
   132     my $home_dir = (getpwuid($>))[7];
   132 
   133 
   133 
   134     my $dc =
   134     my $dc = "$opt->{binary} --output @{[$tmp->filename]} --homedir $home_dir/.gnupg --batch --yes";
   135       "$opt->{binary} --output $tmp --homedir $home_dir/.gnupg --batch --yes";
   135     my @r = qx($dc $file 2>&1);
   136     my @r = qx($dc $file 2>&1);
   136     if ($?) {
   137     if ($?) {
   137         print "EXEC CRITICAL: @r";
   138         print "EXEC CRITICAL: @r";
   138         exit $ERRORS{CRITICAL};
   139         exit $ERRORS{CRITICAL};
   139     }
   140     }