#!/usr/bin/perl
#Description:
#      Check the update of rpm, comparing the remote host with opbakup rpms
#
#Author:
#       Ricardo Marashini (ricardo.marashini@opservices.com.br)
#
#Contributions:
#	Fernando Rocha (fernando.rocha@opservices.com.br)
#
#Version:
#       1.0: 		Initial release
#       1.1: 20-03-2009 Added the so support EL5/FC3
#       1.2: 09-04-2009 Fixed Regex at populate_hash function

use Data::Dumper;
use strict;

our ( $host, $nrpe_version, $ssl, $so ,$port ) = @ARGV;

# Global conf
our $version = "1.2";
our $libexec_path = "/usr/local/opmon/libexec";
our $current_version = "rpm_versions";
our $last_version = "last_rpm_versions$so";
our $opbackup = "192.168.10.3";

our (%last_hash, %current_hash);

our @ignored_packages = (	"opmon-nrpe"	,
				"opmon-ntop",
				"opmon-nagios-plugins"	);

sub main {

	# Parse args
	parse_args();

	# Adjust destination port
	$port = 5666 if (not defined $port);

	# Adjust so
	if ($so !~ /EL5|FC3/){
		$so = "EL5";
		$last_version = "last_rpm_versions$so";
	}

	# Get remote rpm versions
	my $remote_versions = check_nrpe( $host, $current_version, $nrpe_version, $ssl, $port );
	populate_hash( \%current_hash, $remote_versions );
	
	# Get last rpm verions
	my $last_versions = check_nrpe( $opbackup, $last_version, 2, 1, 5666 );
	populate_hash( \%last_hash, $last_versions );


	my $error = 0;
	my $error_msg;
	foreach my $package ( sort keys %last_hash ) {
		if (not defined $current_hash{$package}) {
			$error_msg .= "$package NOT INSTALLED <br>";
			#$error_msg .= "Package: $package NOT INSTALLED \n";
			$error++;
		}elsif ($last_hash{$package} ne $current_hash{$package}) {
			$error_msg .= "$package CUR: $current_hash{$package} LAST: $last_hash{$package}<br>";
			#$error_msg .= "Package: $package CUR: $current_hash{$package} LAST: $last_hash{$package} \n";
			$error++;
		}

	}

	if (not $error) {
		print "Ok - RPMs up-to-date!\n";
		exit(0);
	} else {
		print "Critical - $error mismatch(es):<br>$error_msg";
		exit(2);
	}

}

# Create an nrpe request
sub check_nrpe {

	# Args
	my ( $target, $command, $version, $crypt, $dport ) = @_;

	# Create command
	my $run = "$libexec_path/check_nrpe";

	# Nrpe version
	if ($version == 2) {
		$run .= "2 -a '' -t 60 ";
	} else {
		$run .= " -to 60 ";
	}

	# Command
	$run .= " -H $target -p $dport -c $command ";

	# Use ssl?
	$run .= " -n" if ($crypt == 0 && $version == 2);

	my $return = `$run`;
	my $code = $?;
	chomp($return);

	if ($code) {
		print "Critical - Can't connect! Host: $target Port: $dport NRPE: $version SSL: $crypt\n";
		exit(2);
	}

	return $return;

}

sub populate_hash {

	my ($string) = $_[1];

	foreach(split(" ", $string)) {
		m/([a-zA-Z0-9\-]+)-(.*)$/;
		next if (ignored($1));
		${$_[0]}{$1} = "$2";
		
	}

}

sub parse_args {

	if ($#ARGV<2) {

		print "Usage:\n\t$0 [host] [check_nrpe version] [use_ssl] [EL5|FC3] [port]\n";
		exit(1);

	}

}

sub ignored {

	my $package = shift;

	foreach(@ignored_packages) {
		return 1 if ( $package =~ $_ );
	}

	return 0;

}

&main();
