| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package apps::antivirus::clamav::local::mode::updatestatus; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::misc; |
| 28 |
use Net::DNS; |
| 29 |
use DateTime; |
| 30 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 31 |
|
| 32 |
sub custom_engine_status_output { |
| 33 |
my ($self, %options) = @_; |
| 34 |
|
| 35 |
return sprintf( |
| 36 |
"clamav engine version '%s/%s'", |
| 37 |
$self->{result_values}->{current_engine_version}, |
| 38 |
$self->{result_values}->{last_engine_version} |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub custom_maindb_status_output { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
return sprintf( |
| 46 |
"main.cvd version '%s/%s', last update %s", |
| 47 |
$self->{result_values}->{current_maindb_version}, |
| 48 |
$self->{result_values}->{last_maindb_version}, |
| 49 |
centreon::plugins::misc::change_seconds( |
| 50 |
value => $self->{result_values}->{current_maindb_timediff} |
| 51 |
) |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
sub custom_dailydb_status_output { |
| 56 |
my ($self, %options) = @_; |
| 57 |
|
| 58 |
return sprintf( |
| 59 |
"daily.cvd version '%s/%s', last update %s", |
| 60 |
$self->{result_values}->{current_dailydb_version}, |
| 61 |
$self->{result_values}->{last_dailydb_version}, |
| 62 |
centreon::plugins::misc::change_seconds( |
| 63 |
value => $self->{result_values}->{current_dailydb_timediff} |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
sub set_counters { |
| 69 |
my ($self, %options) = @_; |
| 70 |
|
| 71 |
$self->{maps_counters_type} = [ |
| 72 |
{ name => 'update', type => 0, message_separator => ' - ' } |
| 73 |
]; |
| 74 |
|
| 75 |
$self->{maps_counters}->{update} = [ |
| 76 |
{ label => 'engine-status', type => 2, critical_default => '%{last_engine_version} ne %{current_engine_version}', set => { |
| 77 |
key_values => [ { name => 'last_engine_version' }, { name => 'current_engine_version' } ], |
| 78 |
closure_custom_output => $self->can('custom_engine_status_output'), |
| 79 |
closure_custom_perfdata => sub { return 0; }, |
| 80 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 81 |
} |
| 82 |
}, |
| 83 |
{ label => 'maindb-status', type => 2, critical_default => '%{last_maindb_version} ne %{current_maindb_version}', set => { |
| 84 |
key_values => [ { name => 'last_maindb_version' }, { name => 'current_maindb_version' }, { name => 'current_maindb_timediff' } ], |
| 85 |
closure_custom_output => $self->can('custom_maindb_status_output'), |
| 86 |
closure_custom_perfdata => sub { return 0; }, |
| 87 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 88 |
} |
| 89 |
}, |
| 90 |
{ label => 'dailydb-status', type => 2, critical_default => '%{last_dailydb_version} ne %{current_dailydb_version} || %{current_dailydb_timediff} > 432000', set => { |
| 91 |
key_values => [ { name => 'last_dailydb_version' }, { name => 'current_dailydb_version' }, { name => 'current_dailydb_timediff' } ], |
| 92 |
closure_custom_output => $self->can('custom_dailydb_status_output'), |
| 93 |
closure_custom_perfdata => sub { return 0; }, |
| 94 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 95 |
} |
| 96 |
} |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
sub new { |
| 101 |
my ($class, %options) = @_; |
| 102 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 103 |
bless $self, $class; |
| 104 |
|
| 105 |
$options{options}->add_options(arguments => { |
| 106 |
'nameservers:s@' => { name => 'nameservers' }, |
| 107 |
'maindb-file:s' => { name => 'maindb_file', default => '/var/lib/clamav/main.cvd' }, |
| 108 |
'dailydb-file:s' => { name => 'dailydb_file', default => '/var/lib/clamav/daily.cvd' } |
| 109 |
}); |
| 110 |
|
| 111 |
return $self; |
| 112 |
} |
| 113 |
|
| 114 |
sub check_options { |
| 115 |
my ($self, %options) = @_; |
| 116 |
$self->SUPER::check_options(%options); |
| 117 |
|
| 118 |
$self->{option_results}->{maindb_file} = centreon::plugins::misc::sanitize_command_param(value => $self->{option_results}->{maindb_file}); |
| 119 |
$self->{option_results}->{dailydb_file} = centreon::plugins::misc::sanitize_command_param(value => $self->{option_results}->{dailydb_file}); |
| 120 |
} |
| 121 |
|
| 122 |
sub get_clamav_last_update { |
| 123 |
my ($self, %options) = @_; |
| 124 |
|
| 125 |
#0.99.2:57:23114:1487851834:1:63:45614:290 |
| 126 |
# field 2 = main.cvd version number |
| 127 |
# field 3 = daily.cvd version number |
| 128 |
my %dns_options = (); |
| 129 |
if (defined($self->{option_results}->{nameservers})) { |
| 130 |
foreach my $dns (@{$self->{option_results}->{nameservers}}) { |
| 131 |
next if ($dns !~ /[a-zA-Z0-9]/); |
| 132 |
$dns_options{nameservers} = [] if (!defined($dns_options{nameservers})); |
| 133 |
push @{$dns_options{nameservers}}, $dns; |
| 134 |
} |
| 135 |
} |
| 136 |
my $handle = Net::DNS::Resolver->new(%dns_options); |
| 137 |
my $txt_query = $handle->query("current.cvd.clamav.net", "TXT"); |
| 138 |
if (!$txt_query) { |
| 139 |
$self->{output}->add_option_msg(short_msg => "Unable to get TXT Record : " . $handle->errorstring . "."); |
| 140 |
$self->{output}->option_exit(); |
| 141 |
} |
| 142 |
|
| 143 |
my @fields = split /:/, ($txt_query->answer)[0]->txtdata; |
| 144 |
($self->{last_engine_version}, $self->{last_maindb_version}, $self->{last_dailydb_version}) = |
| 145 |
($fields[0], $fields[1], $fields[2]); |
| 146 |
} |
| 147 |
|
| 148 |
sub get_clamav_current_signature_info { |
| 149 |
my ($self, %options) = @_; |
| 150 |
|
| 151 |
if ($options{content} !~ /====\s+$options{label}.*?Build\s+time:\s+(.*?)\n.*?Version:\s+(\d+)/msi) { |
| 152 |
return ; |
| 153 |
} |
| 154 |
|
| 155 |
$self->{'current_' . $options{label} . 'db_version'} = $2; |
| 156 |
#13 Jun 2016 09:53 -0400 |
| 157 |
my $time = $1; |
| 158 |
if ($time =~ /^\s*(\d+)\s+(\S+)\s+(\d+)\s+(\d+):(\d+)\s+(\S+)/) { |
| 159 |
my %months = ("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, "May" => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12); |
| 160 |
my $dt = DateTime->new( |
| 161 |
year => $3, |
| 162 |
month => $months{$2}, |
| 163 |
day => $1, |
| 164 |
hour => $4, |
| 165 |
minute => $5, |
| 166 |
second => 0, |
| 167 |
time_zone => $6, |
| 168 |
); |
| 169 |
$self->{'current_' . $options{label} . 'db_timediff'} = time() - $dt->epoch(); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
sub manage_selection { |
| 174 |
my ($self, %options) = @_; |
| 175 |
|
| 176 |
$self->get_clamav_last_update(); |
| 177 |
|
| 178 |
my ($stdout) = $options{custom}->execute_command( |
| 179 |
command => 'echo "==== CLAMD ===" ; clamd -V ; echo "==== DAILY ===="; sigtool --info ' . $self->{option_results}->{dailydb_file} . '; echo "==== MAIN ====" ; sigtool --info ' . $self->{option_results}->{maindb_file} |
| 180 |
); |
| 181 |
|
| 182 |
#==== CLAMD === |
| 183 |
#ClamAV 0.99.2/21723/Mon Jun 13 14:53:00 2016 |
| 184 |
#==== DAILY ==== |
| 185 |
#File: /var/lib/clamav/daily.cvd |
| 186 |
#Build time: 13 Jun 2016 09:53 -0400 |
| 187 |
#Version: 21723 |
| 188 |
#Signatures: 276682 |
| 189 |
#Functionality level: 63 |
| 190 |
#Builder: neo |
| 191 |
#MD5: 280928f25d175359e6e6a0270d9d4fb2 |
| 192 |
#Digital signature: yLfcgb9dbgKO2rWpXGa238252jqH7VDsAjnqQsHc+9cbIwiM9wnz3fqyl33G15S4YsMbRR6CYbSTxccKXBJvvxRhgZQJmpCYiThslUKBPo5QhIFcI1QBMfoHKCpf8riB2/xAgI401UkZVJip+6eWFpUJ9aeaFai+Mvinif5BRzi |
| 193 |
#LibClamAV Warning: ************************************************** |
| 194 |
#LibClamAV Warning: *** The virus database is older than 7 days! *** |
| 195 |
#LibClamAV Warning: *** Please update it as soon as possible. *** |
| 196 |
#LibClamAV Warning: ************************************************** |
| 197 |
#Verification OK. |
| 198 |
#==== MAIN ==== |
| 199 |
#File: /var/lib/clamav/main.cvd |
| 200 |
#Build time: 16 Mar 2016 23:17 +0000 |
| 201 |
#Version: 57 |
| 202 |
#Signatures: 4218790 |
| 203 |
#Functionality level: 60 |
| 204 |
#Builder: amishhammer |
| 205 |
#MD5: 06386f34a16ebeea2733ab037f0536be |
| 206 |
#Digital signature: AIzk/LYbX8K9OEbR5GMyJ6LWTqSu9ffa5bONcA0FN3+onMlZ2BMRzuyvVURBvAZvOaGPdtMBcgDJSl7fGxDfcxRWhIrQ98f8FPdAQaFPgWu3EX46ufw+IRZnM4irKKYuh1GdCIbsGs6jejWo9iNErsbDqkFSobVBkUJYxBgvqfd |
| 207 |
#Verification OK. |
| 208 |
|
| 209 |
$self->get_clamav_current_signature_info(label => 'daily', content => $stdout); |
| 210 |
$self->get_clamav_current_signature_info(label => 'main', content => $stdout); |
| 211 |
if ($stdout =~ /==== CLAMD.*?ClamAV (.*?)\//msi) { |
| 212 |
$self->{current_engine_version} = $1; |
| 213 |
} |
| 214 |
|
| 215 |
$self->{update} = { |
| 216 |
last_engine_version => $self->{last_engine_version}, last_maindb_version => $self->{last_maindb_version}, last_dailydb_version => $self->{last_dailydb_version}, |
| 217 |
current_engine_version => $self->{current_engine_version}, |
| 218 |
current_maindb_version => $self->{current_maindb_version}, current_maindb_timediff => $self->{current_maindb_timediff}, |
| 219 |
current_dailydb_version => $self->{current_dailydb_version}, current_dailydb_timediff => $self->{current_dailydb_timediff}, |
| 220 |
}; |
| 221 |
} |
| 222 |
|
| 223 |
1; |
| 224 |
|
| 225 |
__END__ |
| 226 |
|
| 227 |
=head1 MODE |
| 228 |
|
| 229 |
Check antivirus update status. |
| 230 |
|
| 231 |
Command used: 'clamd -V ; sigtool --info %(dailydb-file) ; sigtool --info %(maindb-file)' |
| 232 |
|
| 233 |
=over 8 |
| 234 |
|
| 235 |
=item B<--nameservers> |
| 236 |
|
| 237 |
Set nameserver to query (can be multiple). |
| 238 |
The system configuration is used by default. |
| 239 |
|
| 240 |
=item B<--maindb-file> |
| 241 |
|
| 242 |
Antivirus main.cvd file (default: '/var/lib/clamav/main.cvd'). |
| 243 |
|
| 244 |
=item B<--dailydb-file> |
| 245 |
|
| 246 |
Antivirus daily.cvd file (default: '/var/lib/clamav/daily.cvd'). |
| 247 |
|
| 248 |
=item B<--warning-engine-status> |
| 249 |
|
| 250 |
Define the conditions to match for the status to be WARNING (default: '') |
| 251 |
You can use the following variables: %{last_engine_version}, %{current_engine_version} |
| 252 |
|
| 253 |
=item B<--critical-engine-status> |
| 254 |
|
| 255 |
Define the conditions to match for the status to be CRITICAL (default: '%{last_engine_version} ne %{current_engine_version}'). |
| 256 |
You can use the following variables: %{last_engine_version}, %{current_engine_version} |
| 257 |
|
| 258 |
=item B<--warning-maindb-status> |
| 259 |
|
| 260 |
Define the conditions to match for the status to be WARNING (default: '') |
| 261 |
You can use the following variables: %{last_maindb_version}, %{current_maindb_version}, %{current_maindb_timediff} |
| 262 |
|
| 263 |
=item B<--critical-maindb-status> |
| 264 |
|
| 265 |
Define the conditions to match for the status to be CRITICAL (default: '%{last_maindb_version} ne %{current_maindb_version}'). |
| 266 |
You can use the following variables: %{last_maindb_version}, %{current_maindb_version}, %{current_maindb_timediff} |
| 267 |
|
| 268 |
=item B<--warning-dailydb-status> |
| 269 |
|
| 270 |
Define the conditions to match for the status to be WARNING (default: '') |
| 271 |
You can use the following variables: %{last_dailydb_version}, %{current_dailydb_version}, %{current_dailydb_timediff} |
| 272 |
|
| 273 |
=item B<--critical-dailydb-status> |
| 274 |
|
| 275 |
Define the conditions to match for the status to be CRITICAL (default: '%{last_dailydb_version} ne %{current_dailydb_version} || %{current_dailydb_timediff} > 432000'). |
| 276 |
You can use the following variables: %{last_dailydb_version}, %{current_dailydb_version}, %{current_dailydb_timediff} |
| 277 |
|
| 278 |
=back |
| 279 |
|
| 280 |
=cut |