Debian Codegraph @

src/apps/eclipse/mosquitto/mqtt/mode/stringvalue.pm

raw
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::stringvalue;
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 'format-custom' => { name => 'format_custom' },
39
40 'warning-regexp:s' => { name => 'warning_regexp' },
41 'critical-regexp:s' => { name => 'critical_regexp' },
42 'unknown-regexp:s' => { name => 'unknown_regexp' },
43 'regexp-insensitive' => { name => 'use_iregexp' },
44
45 'format-ok:s' => { name => 'format_ok', default => 'value: %{value}' },
46 'format-warning:s' => { name => 'format_warning', default => 'value: %{value}' },
47 'format-critical:s' => { name => 'format_critical', default => 'value: %{value}' },
48 'format-unknown:s' => { name => 'format_unknown', default => 'value: %{value}' },
49 });
50
51 return $self;
52 }
53
54 sub custom_stringvalue_output {
55 my ($self, %options) = @_;
56
57 my $value = $self->{result_values}->{stringvalue};
58
59 if (!centreon::plugins::misc::is_empty($self->{instance_mode}->{option_results}->{'format_' . $self->{severity}})) {
60 my $format_value = $self->{instance_mode}->{option_results}->{'format_' . $self->{severity}};
61 $format_value =~ s/%\{value\}/$value/g;
62 $format_value =~ s/%\{(.*?)\}/$format_value->{$1}/g;
63 $value = $format_value;
64 }
65
66 return $value;
67 }
68
69 sub custom_stringvalue_threshold {
70 my ($self, %options) = @_;
71
72 my $severity = 'ok';
73 my $value = $self->{result_values}->{stringvalue};
74 my $option_results = $self->{instance_mode}->{option_results};
75 foreach my $check_severity (('critical', 'warning', 'unknown')) {
76 next if (centreon::plugins::misc::is_empty($option_results->{$check_severity . '_regexp'}));
77 my $regexp = $option_results->{$check_severity . '_regexp'};
78 if (defined($option_results->{use_iregexp}) && $value =~ /$regexp/i) {
79 $severity = $check_severity;
80 } elsif (!defined($option_results->{use_iregexp}) && $value =~ /$regexp/) {
81 $severity = $check_severity;
82 }
83 }
84 $self->{severity} = $severity;
85 return $severity;
86 }
87
88 sub set_counters {
89 my ($self, %options) = @_;
90
91 $self->{maps_counters_type} = [
92 { name => 'global', type => 0 }
93 ];
94
95 $self->{maps_counters}->{global} = [
96 { label => 'generic',
97 set => {
98 key_values => [ { name => 'stringvalue' } ],
99 closure_custom_output => $self->can('custom_stringvalue_output'),
100 closure_custom_threshold_check => \&custom_stringvalue_threshold
101 }
102 }
103 ];
104 }
105
106 sub check_options {
107 my ($self, %options) = @_;
108 $self->SUPER::check_options(%options);
109
110 if (centreon::plugins::misc::is_empty($options{option_results}->{topic})) {
111 $self->{output}->add_option_msg(short_msg => 'Missing parameter --topic.');
112 $self->{output}->option_exit();
113 }
114 }
115
116 sub manage_selection {
117 my ($self, %options) = @_;
118
119 my $value = $options{mqtt}->query(
120 topic => $self->{option_results}->{topic}
121 );
122
123 if (!centreon::plugins::misc::is_empty($self->{option_results}->{format_custom})) {
124 if ($value =~ /$self->{option_results}->{format_custom}/ && defined($1)) {
125 $value = $1;
126 }
127 }
128
129 if (!defined($value)) {
130 $self->{output}->add_option_msg(short_msg => "Cannot find information");
131 $self->{output}->option_exit();
132 }
133
134 $self->{global} = { stringvalue => $value };
135 }
136
137 1;
138
139 __END__
140
141 =head1 MODE
142
143 Check an Eclipse Mosquitto MQTT topic value against regular expression.
144
145 =over 8
146
147 =item B<--topic>
148
149 Topic value to check.
150
151 =item B<--format-custom>
152
153 Apply a custom change on the value.
154
155 =item B<--warning-regexp>
156
157 Return Warning if the topic value match the regexp.
158
159 =item B<--critical-regexp>
160
161 Return Critical if the topic value match the regexp.
162
163 =item B<--regexp-insensitive>
164
165 Allows to use case-insensitive regexp.
166
167 =item B<--format-*>
168
169 Output format according to the threshold.
170 Can be:
171 'ok' (default: 'value: %{value}'),
172 'warning' (default: 'value: %{value}'),
173 'critical' (default: 'value: %{value}'),
174 'unknown' (default: 'value: %{value}').
175
176 =item B<--format-custom>
177
178 Apply a custom change on the value.
179
180 =back
181
182 =cut