| 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::numericvalue; |
| 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 |
sub new { |
| 32 |
my ($class, %options) = @_; |
| 33 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 34 |
bless $self, $class; |
| 35 |
|
| 36 |
$options{options}->add_options(arguments => { |
| 37 |
'topic:s' => { name => 'topic' }, |
| 38 |
'warning:s' => { name => 'warning' }, |
| 39 |
'critical:s' => { name => 'critical' }, |
| 40 |
'extracted-pattern:s' => { name => 'extracted_pattern' }, |
| 41 |
'format:s' => { name => 'format' }, |
| 42 |
'format-custom:s' => { name => 'format_custom' }, |
| 43 |
'perfdata-unit:s' => { name => 'perfdata_unit' }, |
| 44 |
'perfdata-name:s' => { name => 'perfdata_name', default => 'value' }, |
| 45 |
'perfdata-min:s' => { name => 'perfdata_min' }, |
| 46 |
'perfdata-max:s' => { name => 'perfdata_max' }, |
| 47 |
}); |
| 48 |
|
| 49 |
return $self; |
| 50 |
} |
| 51 |
|
| 52 |
sub custom_generic_output { |
| 53 |
my ($self, %options) = @_; |
| 54 |
|
| 55 |
my $format = $self->{instance_mode}{option_results}->{perfdata_name} . ' is: %s'; |
| 56 |
if (defined($self->{instance_mode}{option_results}->{format})) { |
| 57 |
$format = $self->{instance_mode}{option_results}->{format}; |
| 58 |
} |
| 59 |
|
| 60 |
my $value = $self->{result_values}->{numericvalue}; |
| 61 |
if (!centreon::plugins::misc::is_empty($self->{instance_mode}{option_results}->{format_custom})) { |
| 62 |
$value = eval "$value $self->{instance_mode}{option_results}->{format_custom}"; |
| 63 |
} |
| 64 |
|
| 65 |
return sprintf($format, $value); |
| 66 |
} |
| 67 |
|
| 68 |
sub custom_generic_perfdata { |
| 69 |
my ($self, %options) = @_; |
| 70 |
|
| 71 |
$self->{output}->perfdata_add( |
| 72 |
label => $options{option_results}->{perfdata_name}, |
| 73 |
unit => $options{option_results}->{perfdata_unit}, |
| 74 |
value => $self->{result_values}->{numericvalue}, |
| 75 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 76 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 77 |
min => $options{option_results}->{perfdata_min}, |
| 78 |
max => $options{option_results}->{perfdata_max} |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
sub custom_generic_threshold { |
| 83 |
my ($self, %options) = @_; |
| 84 |
|
| 85 |
return $self->{perfdata}->threshold_check( |
| 86 |
value => $self->{result_values}->{numericvalue}, |
| 87 |
threshold => [ |
| 88 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 89 |
{ label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' }, |
| 90 |
{ label => 'unknown-' . $self->{thlabel}, exit_litteral => 'unknown' } |
| 91 |
] |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
sub set_counters { |
| 96 |
my ($self, %options) = @_; |
| 97 |
|
| 98 |
$self->{maps_counters_type} = [ |
| 99 |
{ name => 'global', type => 0 } |
| 100 |
]; |
| 101 |
|
| 102 |
$self->{maps_counters}->{global} = [ |
| 103 |
{ label => 'generic', |
| 104 |
set => { |
| 105 |
key_values => [{ name => 'numericvalue' }], |
| 106 |
closure_custom_output => $self->can('custom_generic_output'), |
| 107 |
closure_custom_perfdata => $self->can('custom_generic_perfdata'), |
| 108 |
closure_custom_threshold_check => $self->can('custom_generic_threshold') |
| 109 |
} |
| 110 |
} |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
sub check_options { |
| 115 |
my ($self, %options) = @_; |
| 116 |
$self->SUPER::check_options(%options); |
| 117 |
|
| 118 |
if (centreon::plugins::misc::is_empty($options{option_results}->{topic})) { |
| 119 |
$self->{output}->add_option_msg(short_msg => 'Missing parameter --topic.'); |
| 120 |
$self->{output}->option_exit(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
sub manage_selection { |
| 125 |
my ($self, %options) = @_; |
| 126 |
|
| 127 |
my $value = $options{mqtt}->query( |
| 128 |
topic => $self->{option_results}->{topic} |
| 129 |
); |
| 130 |
|
| 131 |
if (!centreon::plugins::misc::is_empty($self->{option_results}->{extracted_pattern})) { |
| 132 |
if ($value =~ /$self->{option_results}->{extracted_pattern}/ && defined($1)) { |
| 133 |
$value = $1; |
| 134 |
} |
| 135 |
} |
| 136 |
if ($value !~ /^-?\d+(?:\.\d+)?$/) { |
| 137 |
$self->{output}->output_add( |
| 138 |
severity => 'UNKNOWN', |
| 139 |
short_msg => 'topic value is not numeric (' . $value . ')' |
| 140 |
); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if (!defined($value)) { |
| 145 |
$self->{output}->add_option_msg(short_msg => "Cannot find information"); |
| 146 |
$self->{output}->option_exit(); |
| 147 |
} |
| 148 |
|
| 149 |
$self->{global} = { numericvalue => $value }; |
| 150 |
} |
| 151 |
|
| 152 |
1; |
| 153 |
|
| 154 |
__END__ |
| 155 |
|
| 156 |
=head1 MODE |
| 157 |
|
| 158 |
Check an Eclipse Mosquitto MQTT topic numeric value. |
| 159 |
|
| 160 |
=over 8 |
| 161 |
|
| 162 |
=item B<--topic> |
| 163 |
|
| 164 |
Topic value to check. |
| 165 |
|
| 166 |
=item B<--warning> |
| 167 |
|
| 168 |
Warning threshold. |
| 169 |
|
| 170 |
=item B<--critical> |
| 171 |
|
| 172 |
Critical threshold. |
| 173 |
|
| 174 |
=item B<--extracted-pattern> |
| 175 |
|
| 176 |
Define a pattern to extract a number from the returned string. |
| 177 |
|
| 178 |
=item B<--format> |
| 179 |
|
| 180 |
Output format (default: 'current value is %s') |
| 181 |
|
| 182 |
=item B<--format-custom> |
| 183 |
|
| 184 |
Apply a custom change on the value |
| 185 |
(example to multiply the value: --format-custom='* 8'). |
| 186 |
|
| 187 |
=item B<--perfdata-unit> |
| 188 |
|
| 189 |
Perfdata unit in perfdata output (default: '') |
| 190 |
|
| 191 |
=item B<--perfdata-name> |
| 192 |
|
| 193 |
Perfdata name in perfdata output (default: 'value') |
| 194 |
|
| 195 |
=item B<--perfdata-min> |
| 196 |
|
| 197 |
Minimum value to add in perfdata output (default: '') |
| 198 |
|
| 199 |
=item B<--perfdata-max> |
| 200 |
|
| 201 |
Maximum value to add in perfdata output (default: '') |
| 202 |
|
| 203 |
=back |
| 204 |
|
| 205 |
=cut |