Personal tools
You are here: Home Software Nagiosgraph, Nagios Plugins and Extras check_clamav.pl
Document Actions

check_clamav.pl

Updated from the version at NagiosExchange to use Nagios::Plugin.

Click here to get the file

Size 7.2 kB - File type text/x-perl

File contents

#!/usr/bin/perl -w
#
# Copyright (c) 2005-2008 Darren Spruell <phatbuckett@gmail.com>
# Copyright (c) 2008 Ithaka Harbors, Inc. <alan.brenner@ithaka.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

use strict;
use Nagios::Plugin;
use Net::DNS;

# Path to installed clamd binary.
my $clamd_cmd  = "/usr/sbin/clamd";

=head1 NAME

check_clamav.pl - Nagios anti-virus database status check

=head1 SYNOPSIS

B<check_clamav.pl>
[B<-h> or B<--help>]
[B<-v> or B<--verbose>]
[B<-t> or B<--timeout>]
[B<-c> or B<--critical>=threshold]
[B<-w> or B<--warning>=threshold]

=head1 DESCRIPTION

Compare the currently available anti-virus definition with what is installed.

=head1 REQUIREMENTS

=over 4

=item B<clamd>

From http://www.clamav.net/ or your OS distribution.

=item B<Net::DNS>

check_clamav.pl uses the DNS TXT value to determine the currently available
database.

=item B<Nagios::Plugin>

This provides the return codes that Nagios wants.

=back

=head1 COMMAND LINE OPTIONS

=over 4

=item B<-s> or B<--warning>

The number of revisioins out of date to cause a Nagios warning. Defaults to 1.

=item B<-c> or B<--critical>

The number of revisioins out of date to cause a Nagios critical alert. Defaults
to 2.

=item B<-v> or B<--verbose>

Outputs debugging information on STDERR.

=item B<-h> or B<--help>

Prints usage information and exits.

=back

=head1 AUTHOR

Alan Brenner - alan.brenner@ithaka.org; I've updated this from the version
at http://www.nagiosexchange.org/cgi-bin/page.cgi?g=Detailed%2F2093.html;d=1 by
switching to Nagios::Plugin.

=head1 BUGS

Undoubtedly there are some in here. I (Alan Brenner) have endevored to keep this
simple.

=head1 COPYRIGHT

Copyright (c) 2005-2008 Darren Spruell <phatbuckett@gmail.com>
Copyright (c) 2008 Ithaka Harbors, Inc. <alan.brenner@ithaka.org>

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

=cut

# Leave the rest of this alone:
our($np,			# Nagios::Plugin object
	$debug,			# verbosity switch
	$warning,		# any value higher than this generates a warning message
	$critical,		# any value higher than this generates a critical message
);

# Looks up and returns the current CVD version information from clamav.net.
sub lookup_current() {
    my $res = Net::DNS::Resolver->new;
    my $query = $res->search("current.cvd.clamav.net", "TXT");
    if ($query) {
		my $rr;
        foreach $rr (grep { $_->type eq 'TXT' } $query->answer) {
            warn "Net::DNS found result: " . $rr->txtdata . "\n" if $debug;
            return $rr->txtdata;
        }
    } else {
        warn "query failed: ", $res->errorstring, "\n";
    }
}

# comp_sig_ver() should receive five parameters: remote signature database
# version, local signature database version, build date of local signatures
# database, the warning value and the critical value.
sub comp_sig_ver ($$$$$;) {
    my ($sig_rem, $sig_local, $sig_date, $warn_val, $crit_val) = @_;
    my ($msg, $status) = ('ClamAV ');

    if ($sig_local != $sig_rem) {
        my $diff = $sig_rem - $sig_local;
        if ($diff >= $crit_val) {
            warn "Installed daily.cvd is behind clamav.net\n" if $debug;
            $status = CRITICAL;  # Will exit with CRITICAL status
            $msg .= 'CRITICAL';
        } elsif ($diff >= $warn_val) {
            warn "Installed daily.cvd is behind clamav.net\n" if $debug;
            $status = WARNING;   # Will exit with WARNING status
            $msg .= 'WARNING';
        } else {
            warn "Installed daily.cvd is behind clamav.net\n" if $debug;
            $status = OK;  # Will exit with OK status
            $msg .= 'OK';
		}
        $msg .= ": daily.cvd $sig_local out of date by $diff " .
			(($diff == 1) ? "revision" : "revisions");
    } else {
        warn "Installed daily.cvd matches latest from clamav.net\n" if $debug;
        $status = OK;  # Will exit with OK status
        $msg .= "OK: daily.cvd $sig_local ($sig_date) is up to date";
    }
    return $status, $msg;
}

# use Nagios::Plugin::Getopt to process the @ARGV command line options:
#   --verbose, --help, --usage, --timeout and --host are defined automatically.
$np = Nagios::Plugin->new(version => 'Ithaka 1.0',
	usage => "Usage: %s [ -v|--verbose ] [-t <timeout>] "
		. "[ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ]");
$np->add_arg(spec => 'warning|w=i',
	help => '-w, --warning=INTEGER The minimum number of version difference' .
		' that generates a warning status (default 1).');
$np->add_arg(spec => 'critical|c=i',
	help => '-c, --critical=INTEGER The minimum number of version difference' .
		' that generates a critical status (default 2).');
# Parse @ARGV and process standard arguments (e.g. usage, help, version)
$np->getopts;
# Timeout, if necessary
alarm $np->opts->timeout;
$debug = $np->opts->verbose;
eval {
	$warning = $np->opts->get('warning');
	$warning = abs(int($warning)) if defined $warning;
};
$warning = 1 if ($@ or not $warning);
eval {
	$critical = $np->opts->get('critical');
	$critical = abs(int($critical)) if defined $critical;
};
$critical = 2 if ($@ or not $critical);

# Make sure the binary exists.
if (-x $clamd_cmd) {
    warn "Found clamd at $clamd_cmd\n" if $debug;
} else {
    warn "Can't execute clamd at $clamd_cmd\n" if $debug;
    $np->nagios_die("FATAL: Unable to execute $clamd_cmd");
}

warn "Threshhold values: warning=$warning, critical=$critical\n" if $debug;

# Should return something like: ClamAV 0.87.1/1205/Wed Dec  7 07:00:48 2005
chomp(my $clamd_ver = `$clamd_cmd -V`);

# Should return something like: 0.87.1:34:1206:1134072033:1
chomp(my $dnstxt_ver = lookup_current());

# Parse what we get from clamd -V and our DNS query
my @clamdresults = split(/\//, $clamd_ver);
my @txtresults   = split(/:/, $dnstxt_ver);
warn "Local daily.cvd dated $clamdresults[2]\n" if $debug;
warn "Local daily.cvd version = $clamdresults[1]\n" if $debug;
warn "Latest daily.cvd version = $txtresults[2]\n" if $debug;

my @prog_sig_res = comp_sig_ver($txtresults[2], $clamdresults[1],
	$clamdresults[2], $warning, $critical);
$np->nagios_exit($prog_sig_res[0], $prog_sig_res[1]);

Powered by Plone, the Open Source Content Management System