Debian Codegraph @

src/network/athonet/epc/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 network::athonet::epc::snmp::mode::license;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28 use DateTime;
29 use POSIX;
30 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
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 sprintf('status is %s', $self->{result_values}->{status});
65 }
66
67 sub custom_license_users_output {
68 my ($self, %options) = @_;
69
70 return sprintf(
71 'active users total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
72 $self->{result_values}->{total},
73 $self->{result_values}->{used},
74 $self->{result_values}->{prct_used},
75 $self->{result_values}->{free},
76 $self->{result_values}->{prct_free}
77 );
78 }
79
80 sub custom_license_sessions_output {
81 my ($self, %options) = @_;
82
83 return sprintf(
84 'active sessions total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
85 $self->{result_values}->{total},
86 $self->{result_values}->{used},
87 $self->{result_values}->{prct_used},
88 $self->{result_values}->{free},
89 $self->{result_values}->{prct_free}
90 );
91 }
92
93 sub custom_license_usim_output {
94 my ($self, %options) = @_;
95
96 return sprintf(
97 'provisioned usim total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
98 $self->{result_values}->{total},
99 $self->{result_values}->{used},
100 $self->{result_values}->{prct_used},
101 $self->{result_values}->{free},
102 $self->{result_values}->{prct_free}
103 );
104 }
105
106 sub license_long_output {
107 my ($self, %options) = @_;
108
109 return 'checking license';
110 }
111
112 sub prefix_license_output {
113 my ($self, %options) = @_;
114
115 return 'license ';
116 }
117
118 sub set_counters {
119 my ($self, %options) = @_;
120
121 $self->{maps_counters_type} = [
122 { name => 'license', type => 3, cb_long_output => 'license_long_output', indent_long_output => ' ',
123 group => [
124 { name => 'expire', type => 0, display_short => 0, cb_prefix_output => 'prefix_license_output', skipped_code => { -10 => 1 } },
125 { name => 'users', type => 0, display_short => 0, skipped_code => { -10 => 1 } },
126 { name => 'sessions', type => 0, display_short => 0, skipped_code => { -10 => 1 } },
127 { name => 'usim', type => 0, display_short => 0, skipped_code => { -10 => 1 } }
128 ]
129 }
130 ];
131
132 $self->{maps_counters}->{expire} = [
133 {
134 label => 'status',
135 type => 2,
136 critical_default => '%{status} =~ /expired|invalid/i',
137 set => {
138 key_values => [ { name => 'status' } ],
139 closure_custom_output => $self->can('custom_status_output'),
140 closure_custom_perfdata => sub { return 0; },
141 closure_custom_threshold_check => \&catalog_status_threshold_ng
142 }
143 },
144 { label => 'expires', nlabel => 'license.expires', set => {
145 key_values => [ { name => 'expires_seconds' }, { name => 'expires_human' } ],
146 output_template => 'expires in %s',
147 output_use => 'expires_human',
148 closure_custom_perfdata => $self->can('custom_expires_perfdata'),
149 closure_custom_threshold_check => $self->can('custom_expires_threshold')
150 }
151 }
152 ];
153
154 $self->{maps_counters}->{users} = [
155 { label => 'license-users-usage', nlabel => 'license.users.active.usage.count', set => {
156 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
157 closure_custom_output => $self->can('custom_license_users_output'),
158 perfdatas => [
159 { template => '%d', min => 0, max => 'total' }
160 ]
161 }
162 },
163 { label => 'license-users-free', display_ok => 0, nlabel => 'license.users.active.free.count', set => {
164 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
165 closure_custom_output => $self->can('custom_license_users_output'),
166 perfdatas => [
167 { template => '%d', min => 0, max => 'total' }
168 ]
169 }
170 },
171 { label => 'license-users-usage-prct', display_ok => 0, nlabel => 'license.users.active.usage.percentage', set => {
172 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
173 closure_custom_output => $self->can('custom_license_users_output'),
174 perfdatas => [
175 { template => '%.2f', min => 0, max => 100, unit => '%' }
176 ]
177 }
178 }
179 ];
180
181 $self->{maps_counters}->{sessions} = [
182 { label => 'license-sessions-usage', nlabel => 'license.sessions.active.usage.count', set => {
183 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
184 closure_custom_output => $self->can('custom_license_sessions_output'),
185 perfdatas => [
186 { template => '%d', min => 0, max => 'total' }
187 ]
188 }
189 },
190 { label => 'license-sessions-free', display_ok => 0, nlabel => 'license.sessions.active.free.count', set => {
191 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
192 closure_custom_output => $self->can('custom_license_sessions_output'),
193 perfdatas => [
194 { template => '%d', min => 0, max => 'total' }
195 ]
196 }
197 },
198 { label => 'license-sessions-usage-prct', display_ok => 0, nlabel => 'license.sessions.active.usage.percentage', set => {
199 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
200 closure_custom_output => $self->can('custom_license_sessions_output'),
201 perfdatas => [
202 { template => '%.2f', min => 0, max => 100, unit => '%' }
203 ]
204 }
205 }
206 ];
207
208 $self->{maps_counters}->{usim} = [
209 { label => 'license-usim-usage', nlabel => 'license.usim.usage.count', set => {
210 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
211 closure_custom_output => $self->can('custom_license_usim_output'),
212 perfdatas => [
213 { template => '%d', min => 0, max => 'total' }
214 ]
215 }
216 },
217 { label => 'license-usim-free', display_ok => 0, nlabel => 'license.usim.free.count', set => {
218 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
219 closure_custom_output => $self->can('custom_license_usim_output'),
220 perfdatas => [
221 { template => '%d', min => 0, max => 'total' }
222 ]
223 }
224 },
225 { label => 'license-usim-usage-prct', display_ok => 0, nlabel => 'license.usim.usage.percentage', set => {
226 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
227 closure_custom_output => $self->can('custom_license_usim_output'),
228 perfdatas => [
229 { template => '%.2f', min => 0, max => 100, unit => '%' }
230 ]
231 }
232 }
233 ];
234 }
235
236 sub new {
237 my ($class, %options) = @_;
238 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
239 bless $self, $class;
240
241 $options{options}->add_options(arguments => {
242 'unit:s' => { name => 'unit', default => 'd' }
243 });
244
245 return $self;
246 }
247
248 sub check_options {
249 my ($self, %options) = @_;
250 $self->SUPER::check_options(%options);
251
252 if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
253 $self->{option_results}->{unit} = 'd';
254 }
255 }
256
257 my $map_status = { 0 => 'ok', 1 => 'expired', 2 => 'invalid' };
258
259 my $mapping = {
260 users_connected => { oid => '.1.3.6.1.4.1.35805.10.2.99.1' }, # usersConnected
261 users_idle => { oid => '.1.3.6.1.4.1.35805.10.2.99.2' }, # usersIdle
262 active_connections => { oid => '.1.3.6.1.4.1.35805.10.2.99.4' }, # activeConnections
263 hss_provisioned_users => { oid => '.1.3.6.1.4.1.35805.10.2.99.7' }, # hssProvisionedUsers
264 max_active_users => { oid => '.1.3.6.1.4.1.35805.10.4.1' }, # maxActiveUsers
265 max_active_sessions => { oid => '.1.3.6.1.4.1.35805.10.4.2' }, # maxActiveSessions
266 expire_time => { oid => '.1.3.6.1.4.1.35805.10.4.4' }, # licenseExpireTime
267 max_provisioned_usim => { oid => '.1.3.6.1.4.1.35805.10.4.6' }, # maxProvisionedUSIM
268 status => { oid => '.1.3.6.1.4.1.35805.10.4.5', map => $map_status } # licenseStatus
269 };
270
271 sub manage_selection {
272 my ($self, %options) = @_;
273
274 my $snmp_result = $options{snmp}->get_leef(
275 oids => [ map($_->{oid} . '.0', values(%$mapping)) ],
276 nothing_quit => 1
277 );
278 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => 0);
279
280 $self->{output}->output_add(short_msg => 'License is ok');
281
282 $self->{license} = {
283 global => {
284 expire => { status => $result->{status} },
285 users => {},
286 sessions => {},
287 usim => {}
288 }
289 };
290
291 if (defined($result->{expire_time})) {
292 my @date = unpack('n C6 a C2', $result->{expire_time});
293 my $tz;
294 if (defined($date[7])) {
295 $tz = sprintf('%s%02d%02d', $date[7], $date[8], $date[9]);
296 }
297 my $dt = DateTime->new(
298 year => $date[0],
299 month => $date[1],
300 day => $date[2],
301 hour => $date[3],
302 minute => $date[4],
303 second => $date[5],
304 time_zone => $tz
305 );
306 $self->{license}->{global}->{expire}->{expires_seconds} = $dt->epoch() - time();
307 $self->{license}->{global}->{expire}->{expires_human} = centreon::plugins::misc::change_seconds(
308 value => $self->{license}->{global}->{expire}->{expires_seconds}
309 );
310 }
311
312 if ($result->{max_active_users} > 0) {
313 $self->{license}->{global}->{users}->{used} = $result->{users_connected} + $result->{users_idle};
314 $self->{license}->{global}->{users}->{total} = $result->{max_active_users};
315 $self->{license}->{global}->{users}->{free} = $result->{max_active_users} - $self->{license}->{global}->{users}->{used};
316 $self->{license}->{global}->{users}->{prct_used} = $self->{license}->{global}->{users}->{used} * 100 / $result->{max_active_users};
317 $self->{license}->{global}->{users}->{prct_free} = 100 - $self->{license}->{global}->{users}->{prct_used};
318 }
319
320 if ($result->{max_active_sessions} > 0) {
321 $self->{license}->{global}->{sessions}->{used} = $result->{active_connections};
322 $self->{license}->{global}->{sessions}->{total} = $result->{max_active_sessions};
323 $self->{license}->{global}->{sessions}->{free} = $result->{max_active_sessions} - $self->{license}->{global}->{sessions}->{used};
324 $self->{license}->{global}->{sessions}->{prct_used} = $self->{license}->{global}->{sessions}->{used} * 100 / $result->{max_active_sessions};
325 $self->{license}->{global}->{sessions}->{prct_free} = 100 - $self->{license}->{global}->{sessions}->{prct_used};
326 }
327
328 if ($result->{max_provisioned_usim} > 0) {
329 $self->{license}->{global}->{usim}->{used} = $result->{hss_provisioned_users};
330 $self->{license}->{global}->{usim}->{total} = $result->{max_provisioned_usim};
331 $self->{license}->{global}->{usim}->{free} = $result->{max_provisioned_usim} - $result->{hss_provisioned_users};
332 $self->{license}->{global}->{usim}->{prct_used} = $result->{hss_provisioned_users} * 100 / $result->{max_provisioned_usim};
333 $self->{license}->{global}->{usim}->{prct_free} = 100 - $self->{license}->{global}->{users}->{prct_used};
334 }
335 }
336
337 1;
338
339 __END__
340
341 =head1 MODE
342
343 Check license.
344
345 =over 8
346
347 =item B<--filter-counters>
348
349 Only display some counters (regexp can be used).
350 Example: --filter-counters='users'
351
352 =item B<--warning-status>
353
354 Define the conditions to match for the status to be WARNING.
355 Can use special variables like: %{status}
356
357 =item B<--critical-status>
358
359 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /expired|invalid/i').
360 Can use special variables like: %{status}
361
362 =item B<--unit>
363
364 Select the time unit for the expiration threshold. May be 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days, 'w' for weeks. Default is days.
365
366 =item B<--warning-*> B<--critical-*>
367
368 Thresholds.
369 Can be: 'expires', 'license-users-usage', 'license-users-free', 'license-users-usage-prct',
370 'license-sessions-usage', 'license-sessions-free', 'license-sessions-usage-prct',
371 'license-usim-usage', 'license-usim-free', 'license-usim-usage-prct'.
372
373 =back
374
375 =cut