[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/share/se3/scripts-alertes/ -> check_debian_packages (source)

   1  #!/usr/bin/perl -w
   2  #
   3  # check_debian_packages - nagios plugin
   4  #
   5  #
   6  # Copyright (C) 2005 Francesc Guasch
   7  #
   8  # This program is free software; you can redistribute it and/or
   9  # modify it under the terms of the GNU General Public License
  10  # as published by the Free Software Foundation; either version 2
  11  # of the License, or (at your option) any later version.
  12  #
  13  # This program is distributed in the hope that it will be useful,
  14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16  # GNU General Public License for more details.
  17  #
  18  # You should have received a copy of the GNU General Public License
  19  # along with this program; if not, write to the Free Software
  20  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21  #
  22  # Report bugs to: frankie@etsetb.upc.edu
  23  #
  24  use strict;
  25  use lib '/usr/lib/nagios/plugins';
  26  use utils qw(%ERRORS &print_revision &support &usage);
  27  use Getopt::Long;
  28  
  29  my $VERSION = '0.06';
  30  
  31  my $RET = 'OK';
  32  my $LOCK_FILE = "/var/lib/dpkg/lock";
  33  my $CMD_APT = "/usr/bin/apt-get -s upgrade";
  34  my $TIMEOUT = 60;
  35  my $DEBUG    = 0;
  36  
  37  #####################################################################
  38  #
  39  # Command line arguments
  40  #
  41  
  42  sub print_usage ();
  43  
  44  my ($help,$version);
  45  GetOptions(    help => \$help,
  46                debug => \$DEBUG,
  47              version => \$version,
  48          'timeout=s' => \$TIMEOUT
  49  );
  50  my ($PROGNAME) = $0 =~ m#.*/(.*)#;
  51  
  52  if ($help) {
  53      print_revision($PROGNAME,"\$Revision: $VERSION \$");
  54      print "Copyright (c) 2005 Francesc Guasch - Ortiz
  55  
  56      Perl Check debian packages plugin for Nagios
  57  
  58  ";
  59      print_usage();
  60      exit($ERRORS{OK});
  61  }
  62  
  63  if ($version) {
  64      print_revision($PROGNAME,"\$Revision: $VERSION \$");
  65      exit($ERRORS{OK});
  66  }
  67  
  68  #
  69  # unlikely but compliant
  70  #
  71  $SIG{'ALRM'} = sub {
  72      print ("ERROR: Timeout\n");
  73      exit $ERRORS{"UNKNOWN"};
  74  };
  75  alarm($TIMEOUT);
  76  
  77  
  78  ######################################################################
  79  #
  80  # subs
  81  #
  82  
  83  sub print_usage () {
  84      print "Usage: $PROGNAME [--debug] [--version] [--help]"
  85              ." [--timeout=$TIMEOUT]\n";
  86  }
  87  
  88  sub add_info {
  89      my ($info,$type,$pkg) = @_;
  90      $$info .= scalar(keys %$pkg)." new pkgs in $type: ";
  91      if (keys %$pkg< 5 ) {
  92          $$info .= join " ",keys %$pkg;
  93      } else {
  94          my $alguns = join " ",keys %$pkg;
  95          $alguns = substr($alguns,0,80);
  96          $alguns .= "...";
  97          $$info .= $alguns;
  98      }
  99  }
 100  
 101  sub exit_unknown {
 102      my ($info) = @_;
 103      chomp $info;
 104      $RET='UNKNOWN';
 105      print "$RET: $info\n";
 106      exit $ERRORS{$RET};
 107  };
 108  
 109  sub run_apt {
 110      my ($pkg,$ver,$type,$release);
 111      open APT,"$CMD_APT 2>&1|" or exit_unknown($!);
 112      my (%stable,%security,%other);
 113      while (<APT>) {
 114          print "APT: $_" if $DEBUG;
 115          exit_unknown($_) if /(Could not open lock file)|(Could not get lock)/;
 116          next unless /^Inst/;
 117          ($pkg,$ver,$release) = /Inst (.*?) .*\((.*?) (.*?)\)/;
 118          print "$_\npkg=$pkg ver=$ver release=$release\n" if $DEBUG;
 119          die "$_\n" unless defined $release;
 120          $release = 'stable'  
 121                  if $release =~ /stable$/ && $release !~/security/i;
 122          $release = 'security' 
 123                  if $release =~ /security/i;
 124          if ($release eq 'stable') {
 125              $stable{$pkg} = $ver;
 126          } elsif ($release eq 'security') {
 127              $security{$pkg} = $ver;
 128          } else {
 129              $other{$pkg}=$ver;
 130          }
 131      }
 132      close APT;
 133      my $info = '';
 134      if (keys (%security)) {
 135          $RET = 'CRITICAL';
 136          add_info(\$info,'security',\%security);
 137      } elsif (keys (%other) or keys(%stable)) {
 138          $RET = 'WARNING';
 139          add_info(\$info,'stable',\%stable);
 140          add_info(\$info,'other',\%other) if keys %other;
 141      }
 142      print "$RET: $info\n";
 143  }
 144  
 145  run_apt();
 146  exit $ERRORS{$RET};


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1