| 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::kaspersky::snmp::mode::updates; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::misc; |
| 28 |
use DateTime; |
| 29 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); |
| 30 |
|
| 31 |
sub custom_status_output { |
| 32 |
my ($self, %options) = @_; |
| 33 |
|
| 34 |
return sprintf("Updates status is '%s'", $self->{result_values}->{status}); |
| 35 |
} |
| 36 |
|
| 37 |
sub custom_status_calc { |
| 38 |
my ($self, %options) = @_; |
| 39 |
|
| 40 |
$self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_updatesStatus'}; |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
sub custom_last_perfdata { |
| 45 |
my ($self, %options) = @_; |
| 46 |
|
| 47 |
$self->{output}->perfdata_add( |
| 48 |
label => 'last_server_update', |
| 49 |
nlabel => $self->{nlabel}, |
| 50 |
value => $self->{result_values}->{diff}, |
| 51 |
unit => 's', |
| 52 |
min => 0 |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
sub custom_last_threshold { |
| 57 |
my ($self, %options) = @_; |
| 58 |
|
| 59 |
my $exit = $self->{perfdata}->threshold_check( |
| 60 |
value => $self->{result_values}->{diff}, |
| 61 |
threshold => [ |
| 62 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 63 |
{ label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } |
| 64 |
] |
| 65 |
); |
| 66 |
return $exit; |
| 67 |
} |
| 68 |
|
| 69 |
sub custom_last_output { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
return sprintf( |
| 73 |
'Last server update: %s [%s]', |
| 74 |
centreon::plugins::misc::change_seconds(value => $self->{result_values}->{diff}), $self->{result_values}->{date_time} |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
sub custom_last_calc { |
| 79 |
my ($self, %options) = @_; |
| 80 |
|
| 81 |
my $time = $options{new_datas}->{$self->{instance} . '_lastServerUpdateTime'}; |
| 82 |
#2018-3-30,7:43:58.0 |
| 83 |
if ($time =~ /^\s*(\d+)-(\d+)-(\d+),(\d+):(\d+):(\d+)\.(\d+)/) { |
| 84 |
my $dt = DateTime->new( |
| 85 |
year => $1, |
| 86 |
month => $2, |
| 87 |
day => $3, |
| 88 |
hour => $4, |
| 89 |
minute => $5, |
| 90 |
second => $6, |
| 91 |
%{$self->{instance_mode}->{tz}} |
| 92 |
); |
| 93 |
$self->{result_values}->{diff} = time() - $dt->epoch; |
| 94 |
$self->{result_values}->{date_time} = $dt->datetime(); |
| 95 |
} |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
sub set_counters { |
| 101 |
my ($self, %options) = @_; |
| 102 |
|
| 103 |
$self->{maps_counters_type} = [ |
| 104 |
{ name => 'global', type => 0, message_separator => ' - ' } |
| 105 |
]; |
| 106 |
|
| 107 |
$self->{maps_counters}->{global} = [ |
| 108 |
{ |
| 109 |
label => 'status', |
| 110 |
type => 2, |
| 111 |
warning_default => '%{status} =~ /Warning/i', |
| 112 |
critical_default => '%{status} =~ /Critical/i', |
| 113 |
set => { |
| 114 |
key_values => [ { name => 'updatesStatus' } ], |
| 115 |
closure_custom_calc => $self->can('custom_status_calc'), |
| 116 |
closure_custom_output => $self->can('custom_status_output'), |
| 117 |
closure_custom_perfdata => sub { return 0; }, |
| 118 |
closure_custom_threshold_check => \&catalog_status_threshold_ng |
| 119 |
} |
| 120 |
}, |
| 121 |
{ label => 'last-server-update', nlabel => 'update.server.freshness.seconds', set => { |
| 122 |
key_values => [ { name => 'lastServerUpdateTime' } ], |
| 123 |
closure_custom_calc => $self->can('custom_last_calc'), |
| 124 |
closure_custom_output => $self->can('custom_last_output'), |
| 125 |
closure_custom_threshold_check => $self->can('custom_last_threshold'), |
| 126 |
closure_custom_perfdata => $self->can('custom_last_perfdata') |
| 127 |
} |
| 128 |
}, |
| 129 |
{ label => 'not-updated', nlabel => 'update.hosts.outdated.count', set => { |
| 130 |
key_values => [ { name => 'hostsNotUpdated' } ], |
| 131 |
output_template => '%d host(s) not up to date', |
| 132 |
perfdatas => [ |
| 133 |
{ label => 'not_updated', template => '%d', min => 0 } |
| 134 |
] |
| 135 |
} |
| 136 |
} |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
sub check_options { |
| 141 |
my ($self, %options) = @_; |
| 142 |
$self->SUPER::check_options(%options); |
| 143 |
|
| 144 |
$self->{tz} = centreon::plugins::misc::set_timezone(name => $self->{option_results}->{timezone}); |
| 145 |
} |
| 146 |
|
| 147 |
sub new { |
| 148 |
my ($class, %options) = @_; |
| 149 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 150 |
bless $self, $class; |
| 151 |
|
| 152 |
$options{options}->add_options(arguments => { |
| 153 |
'timezone:s' => { name => 'timezone', default => 'GMT' } |
| 154 |
}); |
| 155 |
|
| 156 |
return $self; |
| 157 |
} |
| 158 |
|
| 159 |
my $map_status = { |
| 160 |
0 => 'OK', |
| 161 |
1 => 'Info', |
| 162 |
2 => 'Warning', |
| 163 |
3 => 'Critical' |
| 164 |
}; |
| 165 |
|
| 166 |
my $oid_updatesStatus = '.1.3.6.1.4.1.23668.1093.1.2.1'; |
| 167 |
my $oid_lastServerUpdateTime = '.1.3.6.1.4.1.23668.1093.1.2.3'; |
| 168 |
my $oid_hostsNotUpdated = '.1.3.6.1.4.1.23668.1093.1.2.4'; |
| 169 |
|
| 170 |
sub manage_selection { |
| 171 |
my ($self, %options) = @_; |
| 172 |
|
| 173 |
my $snmp_result = $options{snmp}->get_leef( |
| 174 |
oids => [ |
| 175 |
$oid_updatesStatus, $oid_lastServerUpdateTime, |
| 176 |
$oid_hostsNotUpdated |
| 177 |
], |
| 178 |
nothing_quit => 1 |
| 179 |
); |
| 180 |
|
| 181 |
$self->{global} = { |
| 182 |
updatesStatus => $map_status->{$snmp_result->{$oid_updatesStatus}}, |
| 183 |
lastServerUpdateTime => $snmp_result->{$oid_lastServerUpdateTime}, |
| 184 |
hostsNotUpdated => $snmp_result->{$oid_hostsNotUpdated}, |
| 185 |
}; |
| 186 |
} |
| 187 |
|
| 188 |
1; |
| 189 |
|
| 190 |
__END__ |
| 191 |
|
| 192 |
=head1 MODE |
| 193 |
|
| 194 |
Check updates status. |
| 195 |
|
| 196 |
=over 8 |
| 197 |
|
| 198 |
=item B<--warning-status> |
| 199 |
|
| 200 |
Define the conditions to match for the status to be WARNING. (default: '%{status} =~ /Warning/i'). |
| 201 |
Can use special variables like: %{status} |
| 202 |
|
| 203 |
=item B<--critical-status> |
| 204 |
|
| 205 |
Define the conditions to match for the status to be CRITICAL. (default: '%{status} =~ /Critical/i'). |
| 206 |
Can use special variables like: %{status} |
| 207 |
|
| 208 |
=item B<--warning-*> |
| 209 |
|
| 210 |
Warning threshold. |
| 211 |
Can be: 'last-server-update', 'not-updated'. |
| 212 |
|
| 213 |
=item B<--critical-*> |
| 214 |
|
| 215 |
Critical threshold. |
| 216 |
Can be: 'last-server-update', 'not-updated'. |
| 217 |
|
| 218 |
=item B<--timezone> |
| 219 |
|
| 220 |
Timezone options. Default is 'GMT'. |
| 221 |
|
| 222 |
=back |
| 223 |
|
| 224 |
=cut |