Debian Codegraph @

src/apps/wallix/bastion/snmp/mode/license.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::wallix::bastion::snmp::mode::license;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28 use JSON::XS;
29 use DateTime;
30 use POSIX;
31
32 my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
33 my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
34
35 sub custom_expires_perfdata {
36 my ($self, %options) = @_;
37
38 $self->{output}->perfdata_add(
39 nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
40 unit => $self->{instance_mode}->{option_results}->{unit},
41 value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
42 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
43 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
44 min => 0
45 );
46 }
47
48 sub custom_expires_threshold {
49 my ($self, %options) = @_;
50
51 return $self->{perfdata}->threshold_check(
52 value => floor($self->{result_values}->{expires_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
53 threshold => [
54 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
55 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
56 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
57 ]
58 );
59 }
60
61 sub custom_status_output {
62 my ($self, %options) = @_;
63
64 return 'status: ' . $self->{result_values}->{status};
65 }
66
67 sub custom_license_output {
68 my ($self, %options) = @_;
69
70 my $msg;
71 if ($self->{result_values}->{total} != -1) {
72 $msg = sprintf(
73 'total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
74 $self->{result_values}->{total},
75 $self->{result_values}->{used},
76 $self->{result_values}->{prct_used},
77 $self->{result_values}->{free},
78 $self->{result_values}->{prct_free}
79 );
80 } else {
81 $msg = sprintf(
82 'used: %s',
83 $self->{result_values}->{used}
84 );
85 }
86
87 return $msg;
88 }
89
90 sub custom_license_usage_perfdata {
91 my ($self, %options) = @_;
92
93 $self->{output}->perfdata_add(
94 nlabel => $self->{nlabel},
95 instances => $self->{result_values}->{name},
96 value => $self->{result_values}->{used},
97 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
98 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
99 min => 0,
100 max => $self->{result_values}->{total} != -1 ? $self->{result_values}->{total} : undef
101 );
102 }
103
104 sub custom_license_calc {
105 my ($self, %options) = @_;
106
107 return -10 if ($options{new_datas}->{$self->{instance} . '_total'} == -1);
108 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
109 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
110 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
111 $self->{result_values}->{prct_free} = $options{new_datas}->{$self->{instance} . '_prct_free'};
112 $self->{result_values}->{prct_used} = $options{new_datas}->{$self->{instance} . '_prct_used'};
113 return 0;
114 }
115
116 sub prefix_license_output {
117 my ($self, %options) = @_;
118
119 return sprintf(
120 "License '%s' ",
121 $options{instance_value}->{name},
122 );
123 }
124
125 sub prefix_global_output {
126 my ($self, %options) = @_;
127
128 return 'License ';
129 }
130
131 sub set_counters {
132 my ($self, %options) = @_;
133
134 $self->{maps_counters_type} = [
135 { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
136 { name => 'licenses', type => 1, cb_prefix_output => 'prefix_license_output', message_multiple => 'All license usages are ok', skipped_code => { -10 => 1 } }
137 ];
138
139 $self->{maps_counters}->{global} = [
140 { label => 'status', type => 2, critical_default => '%{status} eq "expired"', set => {
141 key_values => [ { name => 'status' } ],
142 closure_custom_output => $self->can('custom_status_output'),
143 closure_custom_perfdata => sub { return 0; },
144 closure_custom_threshold_check => \&catalog_status_threshold_ng
145 }
146 },
147 { label => 'expires', nlabel => 'license.expires', set => {
148 key_values => [ { name => 'expires_seconds' }, { name => 'expires_human' } ],
149 output_template => 'expires in %s',
150 output_use => 'expires_human',
151 closure_custom_perfdata => $self->can('custom_expires_perfdata'),
152 closure_custom_threshold_check => $self->can('custom_expires_threshold')
153 }
154 }
155 ];
156
157 $self->{maps_counters}->{licenses} = [
158 { label => 'license-usage', nlabel => 'license.usage.count', set => {
159 key_values => [
160 { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' },
161 { name => 'total' }, { name => 'name' }
162 ],
163 closure_custom_output => $self->can('custom_license_output'),
164 closure_custom_perfdata => $self->can('custom_license_usage_perfdata'),
165 }
166 },
167 { label => 'license-free', display_ok => 0, nlabel => 'license.free.count', set => {
168 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
169 closure_custom_calc => $self->can('custom_license_calc'),
170 closure_custom_output => $self->can('custom_license_output'),
171 perfdatas => [
172 { template => '%d', min => 0, max => 'total', label_extra_instance => 1 }
173 ]
174 }
175 },
176 { label => 'license-usage-prct', display_ok => 0, nlabel => 'license.usage.percentage', set => {
177 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
178 closure_custom_calc => $self->can('custom_license_calc'),
179 closure_custom_output => $self->can('custom_license_output'),
180 perfdatas => [
181 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
182 ]
183 }
184 }
185 ];
186 }
187
188 sub new {
189 my ($class, %options) = @_;
190 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
191 bless $self, $class;
192
193 $options{options}->add_options(arguments => {
194 'filter-category:s' => { name => 'filter_category' },
195 'unit:s' => { name => 'unit', default => 's' }
196 });
197
198 return $self;
199 }
200
201 sub check_options {
202 my ($self, %options) = @_;
203 $self->SUPER::check_options(%options);
204
205 if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
206 $self->{option_results}->{unit} = 's';
207 }
208 }
209
210 sub add_license {
211 my ($self, %options) = @_;
212
213 return if (defined($self->{option_results}->{filter_category}) && $self->{option_results}->{filter_category} ne '' &&
214 $options{name} !~ /$self->{option_results}->{filter_category}/);
215
216 $self->{licenses}->{ $options{name} } = {
217 name => $options{name},
218 used => $options{used}
219 };
220 if (defined($options{max}) && $options{max} > 0) {
221 $self->{licenses}->{ $options{name} }->{total} = $options{max};
222 $self->{licenses}->{ $options{name} }->{free} = $options{max} - $options{used};
223 $self->{licenses}->{ $options{name} }->{prct_used} = $options{used} * 100 / $options{max};
224 $self->{licenses}->{ $options{name} }->{prct_free} = 100 - $self->{licenses}->{ $options{name} }->{prct_used};
225 } else {
226 $self->{licenses}->{ $options{name} }->{total} = -1;
227 $self->{licenses}->{ $options{name} }->{free} = -1;
228 $self->{licenses}->{ $options{name} }->{prct_used} = -1;
229 $self->{licenses}->{ $options{name} }->{prct_free} = -1;
230 }
231 }
232
233 sub manage_selection {
234 my ($self, %options) = @_;
235
236 my $oid_licenseInfo = '.1.3.6.1.4.1.30373.1.5.0';
237 my $snmp_result = $options{snmp}->get_leef(
238 oids => [ $oid_licenseInfo ],
239 nothing_quit => 1
240 );
241
242 #{
243 # "resource_max": -1,
244 # "primary": 7,
245 # "waapm": 0,
246 # "externvault_enabled": true,
247 # "secondary_max": -1,
248 # "siem_enabled": true,
249 # "secondary": 6,
250 # "evaluation": false,
251 # "resource": 1063,
252 # "session_manager": true,
253 # "expiration_date": "2021-12-31",
254 # "waapm_max": -1,
255 # "primary_max": -1,
256 # "password_manager": true,
257 # "enterprise": false,
258 # "is_expired": false
259 #}
260
261 my $decoded;
262 eval {
263 $snmp_result->{ $oid_licenseInfo } =~ s/\\//g;
264 $decoded = JSON::XS->new->decode($snmp_result->{ $oid_licenseInfo });
265 };
266 if ($@) {
267 $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
268 $self->{output}->option_exit();
269 }
270
271 $self->{global} = {
272 status => $decoded->{is_expired} =~ /true|1/i ? 'expired' : 'ok'
273 };
274 if (defined($decoded->{expiration_date}) && $decoded->{expiration_date} =~ /^(\d+)-(\d+)-(\d+)$/) {
275 my $dt = DateTime->new(
276 year => $1,
277 month => $2,
278 day => $3,
279 hour => 0,
280 minute => 0,
281 second => 0
282 );
283 my $expiration = $dt->epoch();
284
285 $self->{global}->{expires_seconds} = $expiration - time();
286 $self->{global}->{expires_human} = centreon::plugins::misc::change_seconds(
287 value => $self->{global}->{expires_seconds}
288 );
289 }
290
291 $self->{licenses} = {};
292 $self->add_license(name => 'primary', used => $decoded->{primary}, max => $decoded->{primary_max});
293 $self->add_license(name => 'secondary', used => $decoded->{secondary}, max => $decoded->{secondary_max});
294 $self->add_license(name => 'resource', used => $decoded->{resource}, max => $decoded->{resource_max});
295 }
296
297 1;
298
299 __END__
300
301 =head1 MODE
302
303 Check license.
304
305 =over 8
306
307 =item B<--filter-category>
308
309 Filter licenses by category ('primary', 'secondary', 'resource').
310
311 =item B<--unit>
312
313 Select the time unit for the expired license thresholds. May be 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days, 'w' for weeks. Default is seconds.
314
315 =item B<--warning-status>
316
317 Define the conditions to match for the status to be WARNING.
318 You can use the following variables: %{status}
319
320 =item B<--critical-status>
321
322 Define the conditions to match for the status to be CRITICAL (default: '%{status} eq "expired"').
323 You can use the following variables: %{status}
324
325 =item B<--warning-*> B<--critical-*>
326
327 Thresholds.
328 Can be: 'total', 'expires', 'license-usage', 'license-free', 'license-usage-prct'.
329
330 =back
331
332 =cut