#!/usr/bin/perl -w

# Author: Jeff Falgout (jtfalgout@users.sourceforge.net)
# Date; Jan 06, 2006
# This plugin simply runs the command online_update on SuSE and SLES 
# machines and makes the output Nagios friendly.
# A good portion of code ripped from the check_ntp plugin version 1.29
#
# Known issues:
#    If the online_update command fails due to syntax error, a 1 (256) is returned
#     - the same return code as non-security updates available.
# 
# Change the 'use lib' line to suit your needs
#
#
# Change Log:
# 06 Jan 2006: Jeff Falgout - code cleanup.

use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_u $opt_t $update_status $update_cmd $PROGNAME $PROGVERS);
use lib "/usr/lib/nagios/plugins";
use utils qw(%ERRORS &print_revision &support &usage);

$PROGNAME = "check_suse_updates";
$PROGVERS = "0.3";

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';
 
$update_cmd = "/usr/bin/online_update";

sub print_help ();
sub print_usage ();

Getopt::Long::Configure('bundling');
GetOptions
	("V"   => \$opt_V, "version"    => \$opt_V,
	 "h"   => \$opt_h, "help"       => \$opt_h,
	 "u=s" => \$opt_u, "url=s" 		=> \$opt_u,
	 "t=s" => \$opt_t, "type=s" 	=> \$opt_t);

if ($opt_V) {
	print_revision("$PROGNAME $PROGVERS", '$Revision:  $');
	exit $ERRORS{'OK'};
}

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

($opt_u) || usage("Update url not specified\n");
unless ( $opt_u =~ /[http|ftp|nfs|file|cd|dvd|smb]:\/\/[-.A-Za-z0-9]+/ ) { 
	usage("Invalid url: $opt_u\n");
}

($opt_t) || ($opt_t = "security");
unless ( $opt_t =~ /\bsecurity\b|\brecommended\b|\boptional\b/ ) {
	usage("Invalid patch type: $opt_t\n");
}

# Getting codes as multiples of 256 using system() - According to the 
# online_update source, the return codes should be 0 1 2 3. I'm 
# getting 0 for no updates, 256 if recommended updates available, 
# and 512 if security updates are available (multiples of 256).
# If the "-n" is eliminated, the script fails with "Invalid Signature." 

$update_status = system("$update_cmd -k -n -u $opt_u $opt_t > /dev/null");

# Things look good
if ( $update_status == 0 ) { 
	print "UPDATES OK - $opt_t patches up to date"; 
	exit $ERRORS{"OK"}; 
}

# 256 == return code of 1
if ( $update_status == 256 ) {
	print "UPDATES WARNING - $opt_t patches available";
	exit $ERRORS{"WARNING"};
}

# 512 == return code of 2
if ( $update_status == 512 ) { 
	print "UPDATES CRITICAL - $opt_t patches available"; 
	exit $ERRORS{"CRITICAL"}; 
}

# If neither 0, 256, or 512 is returned, exit with an unknown
exit $ERRORS{"UNKNOWN"};

sub print_usage () {
	print "Usage: $PROGNAME -u <url> [-t <patch type>]\n";
}

sub print_help () {
	print_revision("$PROGNAME $PROGVERS",'$Revision:  $');
	print "Copyright (c) 2006 Jeff Falgout

This plugin reports the update status of SuSE/SLES Linux machines

";
	print_usage();
	print "
-h, --help
   This message
-u, --url=update url
   Base URL of directory tree from which to get patches
   Supported protocols: http, ftp, smb, nfs, cd, dvd, file
-t, --type=patch type
   Type of patches to check. security | recommended | optional
   (Default: security)
-V, --version
   Print version number

";
	support();
}
