#!/usr/bin/perl -w

############################################################################
# Nagios Check script to see if apt has updates waiting
#
# Copyright (C)2005 Guy Van Sanden <nocturn00@gmail.com> - http://nocturn.vsbnet.be
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
# USAGE:
# check_apt
#
############################################################################

use strict;
use warnings;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_t $opt_F $opt_m $PROGNAME);
use lib '/usr/lib/nagios/plugins/';
use utils qw(%ERRORS &print_revision &support);
use Net::Ping;

$PROGNAME="check_apt";
my ( $updates, $msg, $status, $state );

sub print_help ();
sub print_usage ();

#Option checking
Getopt::Long::Configure('bundling');
$status = GetOptions(
                'V'   		=> \$opt_V, 
		'version'    	=> \$opt_V,
                'h'   		=> \$opt_h, 
		'help'       	=> \$opt_h);

# Version
if ($opt_V) {
        print_revision($PROGNAME,'$Revision: 0.1 $');
        exit $ERRORS{'OK'};
}

# Help
if ($opt_h) {
        print_help();
        exit $ERRORS{'OK'};
}


my $aptitude_out = `aptitude -sy upgrade`;

$aptitude_out =~ /(\d+) packages upgraded/;
$updates=$1;

if($updates > 0){
	$state = $ERRORS{'WARNING'};
	$msg = "$updates updates waiting";
} else {
	$state = $ERRORS{'OK'};
	$msg = "There are no updates waiting";
}


if ( $state == $ERRORS{'WARNING'} ) {
        print "WARNING - $msg\n";
} elsif ( $state == $ERRORS{'OK'} )
         { print "OK - $msg\n"; }
exit $state;


sub print_usage () {
        print "Usage: $PROGNAME -F <filename>\n";
}

sub print_help () {
        print_revision($PROGNAME,'$Revision: 0.1 $');
        print "Copyright (c) 2005 Guy Van Sanden\n";
        print "\n";
        print_usage();
        print "Checks if there are updates waiting in apt\n\n";
support();
}


