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