~ubuntu-branches/ubuntu/trusty/nagios-plugins-contrib/trusty-proposed

« back to all changes in this revision

Viewing changes to check_clamav/check_clamav

  • Committer: Package Import Robot
  • Author(s): Bernd Zeimetz
  • Date: 2013-05-21 22:11:50 UTC
  • mfrom: (5.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130521221150-k5bda5v5euvt7wg9
Tags: 6.20130521
* [e68c82e1] check_raid: do not run hpacucli if cciss_vol_status is available.
* [4a1c57e8] Also support tw-cli as additional name for the 3ware binary.
  Thanks to Dennis Hoppe
* [eb5e1c7c] Add /run/ to the check_libs ignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
#
 
3
# Copyright (c) 2005-2008 Darren Spruell <phatbuckett@gmail.com>
 
4
#
 
5
# Permission to use, copy, modify, and distribute this software for any
 
6
# purpose with or without fee is hereby granted, provided that the above
 
7
# copyright notice and this permission notice appear in all copies.
 
8
#
 
9
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 
10
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
11
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 
12
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
13
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
14
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 
15
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
#
 
17
################################################################################
 
18
# This script is used to compare the version and signature level of the
 
19
# currently running clamd daemon with the latest available versions listed in
 
20
# the TXT record for current.cvd.clamav.net.
 
21
#
 
22
# In order to use this script, you might need to make the following adjustments:
 
23
#  - Set the "use lib" path correctly (where utils.pm is located.)
 
24
#  - Set the path to your clamd binary in $clamd_cmd.
 
25
#
 
26
# This plugin requires the Net::DNS Perl module.
 
27
################################################################################
 
28
 
 
29
# Plugin directory / home of utils.pm.
 
30
use lib "/usr/local/libexec/nagios";
 
31
use utils qw(%ERRORS &print_revision &support &usage);
 
32
use Getopt::Long qw(:config no_ignore_case bundling);
 
33
use File::Basename;
 
34
use Net::DNS;
 
35
 
 
36
use strict;
 
37
 
 
38
# Path to installed clamd binary.
 
39
my $clamd_cmd  = "/usr/local/sbin/clamd";
 
40
 
 
41
# Leave the rest of this alone:
 
42
my $prog_name  = basename $0;
 
43
my $prog_ver   = "1.2";
 
44
 
 
45
my $warn_val = 1;  # Default - override with -w arg
 
46
my $crit_val = 2;  # Default - override with -c arg
 
47
my $help_val = 0;  # Off unless -h arg
 
48
my $verb_val = 0;  # Off unless -v arg
 
49
my $vers_val = 0;  # Off unless -V arg
 
50
 
 
51
my ($msg, $rev_word, $rr, $status, $status_print);
 
52
 
 
53
# Gives us a way to print out verbose debug information to the screen when user
 
54
# passes in a -v argument.
 
55
# print_debug() should receive one parameter: a text string to print out.
 
56
sub print_debug() {
 
57
    my $message = shift;
 
58
    if ($verb_val == 1) {
 
59
        print "DEBUG: " . $message . "\n";
 
60
    }
 
61
}
 
62
 
 
63
# Looks up and returns the current CVD version information from 
 
64
# clamav.net.
 
65
sub lookup_current() {
 
66
    my $res = Net::DNS::Resolver->new;
 
67
    my $query = $res->search("current.cvd.clamav.net", "TXT");
 
68
    if ($query) {
 
69
        foreach $rr (grep { $_->type eq 'TXT' } $query->answer) {
 
70
            &print_debug("Net::DNS found result: (TXT) " . $rr->txtdata);
 
71
            return $rr->txtdata;
 
72
        }
 
73
    } else {
 
74
        warn "query failed: ", $res->errorstring, "\n";
 
75
    }
 
76
}
 
77
 
 
78
# comp_sig_ver() should receive three parameters: remote signature database
 
79
# version, local signature database version, and build date of local
 
80
# signatures database.
 
81
sub comp_sig_ver() {
 
82
    my $sig_rem   = shift;
 
83
    my $sig_local = shift;
 
84
    my $sig_date  = shift;
 
85
    my $diff = 0;
 
86
    my $msg = "";
 
87
 
 
88
    if ($sig_local != $sig_rem) {
 
89
        $diff = $sig_rem - $sig_local;
 
90
        $rev_word = ($diff == 1) ? "revision" : "revisions";
 
91
        if ($diff >= $crit_val) {
 
92
            &print_debug("Installed daily.cvd is behind clamav.net");
 
93
            $status = $ERRORS{'CRITICAL'};  # Will exit with CRITICAL status
 
94
            $status_print = "CRITICAL";
 
95
        } elsif ($diff >= $warn_val) {
 
96
            &print_debug("Installed daily.cvd is behind clamav.net");
 
97
            $status = $ERRORS{'WARNING'};   # Will exit with WARNING status
 
98
            $status_print = "WARNING";
 
99
        } else {
 
100
            &print_debug("Installed daily.cvd is behind clamav.net");
 
101
            $status = $ERRORS{'OK'};  # Will exit with OK status
 
102
            $status_print = "OK";
 
103
        }
 
104
        $msg  = "ClamAV " . $status_print . ": daily.cvd " . $sig_local .
 
105
            " out of date by " . $diff . " " . $rev_word;
 
106
    } else {
 
107
        &print_debug("Installed daily.cvd matches latest from clamav.net");
 
108
        $status = $ERRORS{'OK'};  # Will exit with OK status
 
109
        $msg    = "ClamAV OK: daily.cvd " . $sig_local . " (" . $sig_date .
 
110
            ") is up to date";
 
111
    }
 
112
    return $msg, $status;
 
113
}
 
114
 
 
115
# Show usage information
 
116
sub show_help() {
 
117
    print <<END;
 
118
$prog_name Nagios plugin $prog_ver (c) 2005-2008 Darren Spruell <phatbuckett\@gmail.com>
 
119
 
 
120
Perl Check ClamAV daily.cvd plugin for Nagios
 
121
 
 
122
Usage: $prog_name [-w <warn>] [-c <crit>] [-V] [-v] [-h]
 
123
 
 
124
-w, --warning=INTEGER
 
125
   Number of revisions behind current daily.cvd to generate a warning state (Default: 1)
 
126
-c, --critical=INTEGER
 
127
   Number of revisions behind current daily.cvd to generate a critical state (Default: 2)
 
128
-V, --version
 
129
   Output version information for the plugin
 
130
-v, --verbose
 
131
   Enable verbose output
 
132
-h, --help
 
133
   Show this help
 
134
END
 
135
}
 
136
 
 
137
GetOptions (
 
138
    "w=i" => \$warn_val, "warning=i" => \$warn_val,
 
139
    "c=i" => \$crit_val, "critical=i" => \$crit_val,
 
140
    "h" => \$help_val, "help" => \$help_val,
 
141
    "V" => \$vers_val, "version" => \$vers_val,
 
142
    "v" => \$verb_val, "verbose" => \$verb_val,
 
143
);
 
144
 
 
145
if ($help_val != 0) {
 
146
    &show_help;     
 
147
    exit $ERRORS{'OK'};
 
148
}
 
149
 
 
150
if ($vers_val != 0) {
 
151
    &print_revision($prog_name,$prog_ver);
 
152
    exit $ERRORS{'OK'};
 
153
}
 
154
 
 
155
# Make sure the binary exists.
 
156
if (-x $clamd_cmd) {
 
157
    &print_debug("Found clamd at $clamd_cmd");
 
158
} else {
 
159
    &print_debug("Can't execute clamd at $clamd_cmd");
 
160
    die("FATAL: Unable to execute $clamd_cmd");
 
161
}
 
162
 
 
163
&print_debug("Threshhold values: warning=$warn_val, critical=$crit_val");
 
164
 
 
165
# Should return something like: ClamAV 0.87.1/1205/Wed Dec  7 07:00:48 2005
 
166
chomp(my $clamd_ver = `$clamd_cmd -V`);
 
167
 
 
168
# Should return something like: 0.87.1:34:1206:1134072033:1
 
169
chomp(my $dnstxt_ver = &lookup_current());
 
170
 
 
171
# Parse what we get from clamd -V and our DNS query
 
172
my @clamdresults = split(/\//,$clamd_ver);
 
173
my @txtresults   = split(/:/,$dnstxt_ver);
 
174
 
 
175
# Get the currently running ClamAV sig level and cvd date out of this
 
176
my $local_latest_daily   = $clamdresults[1];
 
177
my $local_latest_date    = $clamdresults[2];
 
178
 
 
179
&print_debug("Local daily.cvd dated $local_latest_date");
 
180
&print_debug("Local daily.cvd version = $local_latest_daily");
 
181
 
 
182
# Get the latest ClamAV daily signatures version out of this
 
183
my $clamav_latest_daily   = $txtresults[2];
 
184
&print_debug("Latest daily.cvd version = $clamav_latest_daily");
 
185
 
 
186
my @prog_sig_res = &comp_sig_ver($clamav_latest_daily, $local_latest_daily,
 
187
    $local_latest_date);
 
188
 
 
189
print $prog_sig_res[0] . "\n";
 
190
exit $prog_sig_res[1];