| 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 hardware::devices::hikvision::nvr::isapi::mode::uptime; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use POSIX; |
| 28 |
use centreon::plugins::misc; |
| 29 |
|
| 30 |
my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 }; |
| 31 |
my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' }; |
| 32 |
|
| 33 |
sub custom_uptime_output { |
| 34 |
my ($self, %options) = @_; |
| 35 |
|
| 36 |
return sprintf( |
| 37 |
'System uptime is: %s', |
| 38 |
centreon::plugins::misc::change_seconds(value => $self->{result_values}->{uptime}, start => 'd') |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub custom_uptime_perfdata { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
$self->{output}->perfdata_add( |
| 46 |
nlabel => 'system.uptime.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} }, |
| 47 |
value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), |
| 48 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 49 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 50 |
min => 0 |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
sub custom_uptime_threshold { |
| 55 |
my ($self, %options) = @_; |
| 56 |
|
| 57 |
return $self->{perfdata}->threshold_check( |
| 58 |
value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), |
| 59 |
threshold => [ |
| 60 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 61 |
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' }, |
| 62 |
{ label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' } |
| 63 |
] |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
sub set_counters { |
| 68 |
my ($self, %options) = @_; |
| 69 |
|
| 70 |
$self->{maps_counters_type} = [ |
| 71 |
{ name => 'global', type => 0 } |
| 72 |
]; |
| 73 |
|
| 74 |
$self->{maps_counters}->{global} = [ |
| 75 |
{ label => 'uptime', set => { |
| 76 |
key_values => [ { name => 'uptime' } ], |
| 77 |
closure_custom_output => $self->can('custom_uptime_output'), |
| 78 |
closure_custom_perfdata => $self->can('custom_uptime_perfdata'), |
| 79 |
closure_custom_threshold_check => $self->can('custom_uptime_threshold') |
| 80 |
} |
| 81 |
} |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
sub new { |
| 86 |
my ($class, %options) = @_; |
| 87 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 88 |
bless $self, $class; |
| 89 |
|
| 90 |
$options{options}->add_options(arguments => { |
| 91 |
'unit:s' => { name => 'unit', default => 's' } |
| 92 |
}); |
| 93 |
|
| 94 |
return $self; |
| 95 |
} |
| 96 |
|
| 97 |
sub check_options { |
| 98 |
my ($self, %options) = @_; |
| 99 |
$self->SUPER::check_options(%options); |
| 100 |
|
| 101 |
if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) { |
| 102 |
$self->{option_results}->{unit} = 's'; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
sub manage_selection { |
| 107 |
my ($self, %options) = @_; |
| 108 |
|
| 109 |
my $result = $options{custom}->request_api(endpoint => '/ISAPI/System/status'); |
| 110 |
if (!defined($result->{deviceUpTime})) { |
| 111 |
$self->{output}->add_option_msg(short_msg => "Cannot find uptime information"); |
| 112 |
$self->{output}->option_exit(); |
| 113 |
} |
| 114 |
|
| 115 |
$self->{global} = { uptime => $result->{deviceUpTime} }; |
| 116 |
} |
| 117 |
|
| 118 |
1; |
| 119 |
|
| 120 |
__END__ |
| 121 |
|
| 122 |
=head1 MODE |
| 123 |
|
| 124 |
Check system uptime. |
| 125 |
|
| 126 |
=over 8 |
| 127 |
|
| 128 |
=item B<--warning-uptime> |
| 129 |
|
| 130 |
Warning threshold. |
| 131 |
|
| 132 |
=item B<--critical-uptime> |
| 133 |
|
| 134 |
Critical threshold. |
| 135 |
|
| 136 |
=item B<--unit> |
| 137 |
|
| 138 |
Select the time unit for the performance data and thresholds. May be 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days, 'w' for weeks. Default is seconds. |
| 139 |
|
| 140 |
=back |
| 141 |
|
| 142 |
=cut |