check_exec.pl
changeset 4 67e8bd9a87c5
parent 3 211e08d0d2b8
child 5 03bee3138913
equal deleted inserted replaced
3:211e08d0d2b8 4:67e8bd9a87c5
     1 #!/usr/bin/perl -w
     1 #!/usr/bin/perl -w
       
     2 
       
     3 #    Copyright (C) 2010  Christian Arnold
       
     4 #
       
     5 #    This program is free software: you can redistribute it and/or modify
       
     6 #    it under the terms of the GNU General Public License as published by
       
     7 #    the Free Software Foundation, either version 3 of the License, or
       
     8 #    (at your option) any later version.
       
     9 #
       
    10 #    This program is distributed in the hope that it will be useful,
       
    11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 #    GNU General Public License for more details.
       
    14 #
       
    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/>.
       
    17 #
       
    18 #    Chrisitan Arnold <arnold@schlittermann.de>
     2 
    19 
     3 use strict;
    20 use strict;
     4 use File::Basename;
    21 use File::Basename;
       
    22 use Pod::Usage;
     5 use Getopt::Long;
    23 use Getopt::Long;
     6 use LWP::Simple;
    24 use LWP::Simple;
     7 use HTTP::Status;
    25 use HTTP::Status;
     8 use File::Path;
    26 use File::Path;
     9 
    27 
    10 use lib "/usr/lib/nagios/plugins";
    28 use lib "/usr/lib/nagios/plugins";
    11 use utils qw (%ERRORS &print_revision &support);
    29 use utils qw (%ERRORS);
    12 
    30 
    13 $ENV{LANG} = "POSIX";
    31 $ENV{LANG} = "POSIX";
    14 
    32 
    15 my $ME      = basename $0;
    33 my $ME      = basename $0;
    16 my $VERSION = "0.2";
    34 my $VERSION = "0.2";
    17 my $USAGE   = <<EOF;
       
    18 Usage: $ME -u | --url
       
    19        $ME [ -b | --binary ]
       
    20        $ME [ -p | --path ]
       
    21        $ME [ -h | --help ]
       
    22        $ME [ -V | --version ]
       
    23 EOF
       
    24 
       
    25 sub print_help();
       
    26 sub print_usage();
       
    27 
    35 
    28 sub download($$);
    36 sub download($$);
    29 sub verify($);
    37 sub verify($);
    30 sub cleanup($);
    38 sub cleanup($);
    31 sub execute($);
    39 sub execute($);
    32 
    40 sub version($$);
    33 my $opt_url;
    41 
    34 my $opt_path   = "/var/tmp/nagios";
    42 my $opt = {
    35 my $opt_binary = "/usr/bin/gpg";
    43     url    => "",
    36 
    44     path   => "/var/tmp/nagios",
    37 my $home_dir = (getpwuid($>))[7];
    45     binary => "/usr/bin/gpg"
       
    46 };
    38 
    47 
    39 MAIN: {
    48 MAIN: {
    40     Getopt::Long::Configure('bundling');
    49     Getopt::Long::Configure('bundling');
    41     GetOptions(
    50     GetOptions(
    42         "u|url=s"    => \$opt_url,
    51         "u|url=s"    => \$opt->{url},
    43         "b|binary=s" => \$opt_binary,
    52         "b|binary=s" => \$opt->{binary},
    44         "p|path=s"   => \$opt_path,
    53         "p|path=s"   => \$opt->{path},
    45         "h|help"     => sub { print_help(); exit $ERRORS{OK}; },
    54         "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) },
    46         "V|version"  => sub { print_revision($ME, $VERSION); exit $ERRORS{OK}; }
    55         "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) },
       
    56         "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
    47       )
    57       )
    48       or do {
    58       or do {
    49         print $USAGE;
    59         pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    50         exit $ERRORS{CRITICAL};
    60         exit $ERRORS{CRITICAL};
    51       };
    61       };
    52 
    62 
    53     unless ($opt_url) {
    63     unless ($opt->{url}) {
    54         print $USAGE;
    64         pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    55         exit $ERRORS{CRITICAL};
    65         exit $ERRORS{CRITICAL};
    56     }
    66     }
    57 
    67 
    58     download($opt_url, $opt_path);
    68     download($opt->{url}, $opt->{path});
    59 }
    69 }
    60 
    70 
    61 sub execute($) {
    71 sub execute($) {
    62     my $run_file = shift;
    72     my $run_file = shift;
    63     chmod 0755, $run_file or do {
    73     chmod 0755, $run_file or do {
   119 
   129 
   120 sub verify($) {
   130 sub verify($) {
   121     my $file     = shift;
   131     my $file     = shift;
   122     my $dir      = dirname($file);
   132     my $dir      = dirname($file);
   123     my $run_file = fileparse($file, qw/\.[^.]*/);
   133     my $run_file = fileparse($file, qw/\.[^.]*/);
   124 
   134     my $home_dir = (getpwuid($>))[7];
   125     my $vc = qq{$opt_binary --homedir $home_dir/.gnupg --verify};
   135 
   126     my $dc = qq{$opt_binary --homedir $home_dir/.gnupg --batch --yes};
   136     my $vc = qq|$opt->{binary} --homedir $home_dir/.gnupg --verify|;
       
   137     my $dc = qq|$opt->{binary} --homedir $home_dir/.gnupg --batch --yes|;
   127 
   138 
   128     my @r = qx/$vc $file 2>&1/;
   139     my @r = qx/$vc $file 2>&1/;
   129     if ($?) {
   140     if ($?) {
   130         print "EXEC CRITICAL: @r";
   141         print "EXEC CRITICAL: @r";
   131         exit $ERRORS{CRITICAL};
   142         exit $ERRORS{CRITICAL};
   138     }
   149     }
   139 
   150 
   140     execute("$dir/$run_file");
   151     execute("$dir/$run_file");
   141 }
   152 }
   142 
   153 
   143 sub print_usage() { print $USAGE }
   154 sub version($$) {
   144 
   155     my $progname = shift;
   145 sub print_help() {
   156     my $version  = shift;
   146     print_revision($ME, $VERSION);
   157 
   147     print <<EOF;
   158     print <<_VERSION;
   148 Copyright (c) 2010 Christian Arnold
   159 $progname version $version
       
   160 Copyright (C) 2010 by Christian Arnold and Schlittermann internet & unix support.
       
   161 
       
   162 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   163 and you are welcome to redistribute it under certain conditions.
       
   164 See the GNU General Public Licence for details.
       
   165 _VERSION
       
   166 }
       
   167 
       
   168 =head1 NAME
       
   169 
       
   170 check_exec - nagios plugin to download/verify/execute a program file
       
   171 
       
   172 =head1 SYNOPSIS
       
   173 
       
   174 check_exec -u|--url
       
   175           [-b|--binary path]
       
   176           [-p|--path path]
       
   177 
       
   178 check_exec [-h|--help]
       
   179 
       
   180 check_exec [-m|--man]
       
   181 
       
   182 check_exec [-v|--version]
       
   183 
       
   184 =head1 OPTIONS
       
   185 
       
   186 =over
       
   187 
       
   188 =item B<-u>|B<--url> url
       
   189 
       
   190 Download url for generic script.
       
   191 
       
   192 =item B<-b>|B<--binary> path
       
   193 
       
   194 Path to gpg binary program (default: /usr/bin/gpg)
       
   195 
       
   196 =item B<-p>|B<--path> path
       
   197 
       
   198 Location for store download script (default: /var/tmp/nagios)
       
   199 
       
   200 =item B<-h>|B<--help>
       
   201 
       
   202 Print detailed help screen.
       
   203 
       
   204 =item B<-m>|B<--man>
       
   205 
       
   206 Print manual page.
       
   207 
       
   208 =item B<-V>|B<--version>
       
   209 
       
   210 Print version information.
       
   211 
       
   212 =back
       
   213 
       
   214 =head1 DESCRIPTION
   149 
   215 
   150 This plugin loads a program file via http or https from a
   216 This plugin loads a program file via http or https from a
   151 server and verifies its validity based on a gpg key.
   217 server and verifies its validity based on a gpg key.
   152 
   218 
   153 $USAGE
   219 =head1 VERSION
   154     -u, --url
   220 
   155         download url for generic script
   221 This man page is current for version 0.2 of check_exec.
   156     -b, --binary
   222 
   157         path to gpg (default: /usr/bin/gpg)
   223 =head1 AUTHOR
   158     -p, --path
   224 
   159         location for store download script (default: /var/tmp/nagios)
   225 Written by Christian Arnold <arnold@schlittermann.de>
   160     -h, --help
   226 
   161         print detailed help screen
   227 =head1 COPYRIGHT
   162     -V, --version
   228 
   163         print version information
   229 Copyright (C) 2010 by Christian Arnold and Schlittermann internet & unix support.
   164 
   230 This is free software, and you are welcome to redistribute it under certain conditions.
   165 EOF
   231 See the GNU General Public Licence for details.
   166     support();
   232 
   167 }
   233 =cut