Check updates
From M@Wiki

Main Page | About | Help | FAQ | Special pages | Log in

Categories: Nagios | Perl
Printable version | Disclaimers | Privacy policy

This is a Nagios check utility to check if the local system has updates available. It's obviously meant to be used via NRPE or NSCA.

Check_updates "detects" and uses (in order) Tach, RedCarpet, or Yum if you must.

By default it reports if any packages are available (except when using Tach, then it only reports if any unlocked packages are available), and trips Nagios' warning facility for the host. If you specify a list of packages via -L, it will only report if those packages are available. This list requires full package names, not pattern.

#!/usr/bin/perl -w

use strict;
use lib "/usr/local/nagios/libexec" ;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME);
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_L $opt_H);

$PROGNAME = "check_apc";
sub print_help ();
sub print_usage ();

#$ENV{'PATH'}='';
#$ENV{'BASH_ENV'}='';
#$ENV{'ENV'}='';

Getopt::Long::Configure('bundling');
GetOptions
        ("V"   => \$opt_V, "version"    => \$opt_V,
         "h"   => \$opt_h, "help"       => \$opt_h,
         "L=s"   => \$opt_L, "Lock=s"  => \$opt_L,
         "H=s" => \$opt_H, "hostname=s" => \$opt_H);

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

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

$opt_H = shift unless ($opt_H);
print_usage() unless ($opt_H);
my $host = $1 if ($opt_H =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0]+(\.[a-zA-Z][-a-zA-Z0]+)*)$/);
print_usage() unless ($host);

$opt_L = shift unless ($opt_L);

my $TACH="/usr/local/sbin/tach";
my $RUG="/usr/bin/rug";
my $YUM="/usr/bin/yum";

my $LIST=0;

my %packages;
if($opt_L) {
	$LIST=1;
	for(split(/\,/,$opt_L)) {
		$packages{$_}++;
	}
}

my $updates=0;
if(-e $TACH) {
	$updates=count_tach();
} elsif(-e $RUG) {
	$updates=count_rug();
} elsif(-e $YUM) {
	$updates=count_yum();
} else {
	# WTF?
	printf("UPDATES CRITICAL - No updater available!");
	exit(2);
}


if($updates) {
        printf("UPDATES Warning - $updates Available");
        exit(2);
} else {
        printf("Updates OK - None Available");
        exit(0);
} 

sub IsListed {
	my $package=shift;
	if(exists $packages{$package}) { return 1;}
	else { return 0; }
}

sub count_rug {
	my @lines=`$RUG lu`;
	my $line=$lines[@lines-1];
	if($line =~ /^No updates are available/) { return 0; }
	else {
		my $n=0;
		my $c=5;
		while($c < @lines) {
			if($LIST) {
				#fea | gnupg               | 1.2.4-2.1             | 1.2.4-2.3.legacy
				$lines[$c] =~ /^\w+\s+\|\s+(.*?)\s+\|/;
				my $package=$1;
				if(IsListed($package)) { $n++; }
			} elsif($lines[$c] =~ /\w/) {
				$n++;
			}
			$c++;
		}
		return $c;
	}
}

sub count_tach {
	my @lines=`$TACH lu`;
	if($lines[0] =~ /^No updates are currently available/) { return 0; }
	else {
		my $n=0;
		my $c=1;
		while($c < @lines) {
			if($LIST) {
				#| cups          | 1:1.2.2-1.8    | 1:1.2.3-1.2    | Fedora Core 5 | N       |
				$lines[$c] =~ /^\|\s+(.*?)\s+\|/;
				my $package=$1;
				if(IsListed($package)) { $n++; }
			} else {
				if($lines[$c] =~ /\|\s(\w)\s+\|$/) {
					if($1 eq "N") { $n++; } # Not locked
				}
			}
			$c++;
		}
		return $n;
	}
}

sub count_yum {
	my @lines=`$YUM check-update`;
	my $n=0;
	my $found=0;
	for(@lines) {
		my $line=$_;
		#cups.i386                                1:1.2.3-1.2            updates-released
		if($line !~ /\S/) { $found=1; next; }
		if($found) {
			if($LIST) {
				$line =~ /^(.*?)\s/;
				my $package=$1;
				$package =~ s/\..*$//; # strip the damned architecture
				if(IsListed($package)) { $n++; }
			} else {
				$n++;
			} 
		}
	}
	return $n;
}

sub print_usage () {
        print "Usage: $PROGNAME -H <host> [-L <package list>]\n";
        exit(0);
}

sub print_help () {
        print_revision($PROGNAME,'$Revision: 1.0 $');
        print "Check updates on your system using Tach, RedCarpet or ... I guess... Yum\n";
        print "Copyright (c) 2006 Matthew Keller\n";
        print "\n";
        print_usage();
        print "\n";
        print "<package list> = list of packages to LOOK FOR\n\n";
        support();
}

Retrieved from "http://mattwork.potsdam.edu/projects/wiki/index.php/Check_updates"

This page has been accessed 750 times. This page was last modified 02:59, 7 September 2006.