#! /usr/bin/perl -w
# $Id: check_avwebgate.pl 4100 2008-06-11 10:41:42Z arnold $
# $URL: https://svn.schlittermann.de/is/nagios-plugins/check-avwebgate/trunk/check_avwebgate.pl $

use strict;
use File::Basename;
use Getopt::Long;
use LWP::Simple;

use lib "/usr/lib/nagios/plugins";
use utils qw (%ERRORS &print_revision &support);

sub print_help();
sub print_usage();

my $ME = basename $0;
my ( $opt_u, $opt_s, $opt_V, $opt_h, $opt_b );
my ( $result, $found, $message, $version, $current_version );
my ( @webside );

$opt_u = "http://www.avira.com/de/downloads/avira_antivir_unix_webgate.html";
$opt_s = ">Avira AntiVir WebGate / Avira WebGate Suite<";
$opt_b = "/usr/lib/AntiVir/avwebgate.bin";

Getopt::Long::Configure('bundling');
GetOptions(
    "V"              => \$opt_V,
    "version"        => \$opt_V,
    "h"              => \$opt_h,
    "help"           => \$opt_h,
    "b=s"            => \$opt_b,
    "binary"         => \$opt_b,
    "u=s"            => \$opt_u,
    "url=s"          => \$opt_u,
    "s=s"            => \$opt_s,
    "search=s"       => \$opt_s
);

if ($opt_V) {
    print_revision( $ME, "0.1" );
    exit $ERRORS{"OK"};
}

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

# check avwebgate binary
unless ( -x $opt_b ) {
    print "AVWEBGATE CRITICAL: AntiVir WebGate not found or not executable - $opt_b\n";
    exit $ERRORS{"CRITICAL"};
}

# download the webside
push @webside, split("\n", get($opt_u));

foreach (@webside) {
	unless ($found) {
		/$opt_s/ or next;
		$found = 1;
		next;
	}
	/Version:\s([\d\.-]+).*/ or next;
	$version = $1 and last;
}

unless ($version) {
	print "AVWEBGATE CRITICAL: cannot get version info from avira server\n";
	exit $ERRORS{"CRITICAL"};
}

$_ = `$opt_b --version`;
if ($_ =~ /product version:\s+(\S+)/) { $current_version = $1; }

$result = ($current_version eq $version) ? "OK" : "WARNING";

if ($result eq "WARNING") {
	$message = "new version available: $version";
	print "AVWEBGATE $result: $message\n";
	exit $ERRORS{$result};
} else {
	$message = "current version: $current_version";
	print "AVWEBGATE $result: $message\n";
	exit $ERRORS{$result};
}

sub print_usage() {
    print "Usage:\n";
    print "  $ME [-b <binary>] [-u <url>] [-s <search>\n";
    print "  $ME [-h | --help]\n";
    print "  $ME [-V | --version]\n";
}

sub print_help() {
    print_revision( $ME, "0.1" );
    print "Copyright (c) 2008 Christian Arnold\n\n";
    print "This plugin checks for new versions of Avira AntiVir WebGate.\n\n";
    print_usage();
    print "\n";
    print "  -b, --binary <binary>\n";
    print "     Path of AntiVir WebGate binary (default: /usr/lib/AntiVir/avwebgate.bin)\n";
    print "  -u, --url <url>\n";
    print "     URL where will be look for the search string (default: http://www.avira.com/de/downloads/avira_antivir_unix_webgate.html)\n";
    print "  -s, --search <search>\n";
    print "     Search string on the url (default: >Avira AntiVir UNIX WebGate<)\n";
    print "\n";
    support();
}
