Debian Codegraph @

src/centreon/common/aruba/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 centreon::common::aruba::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 Time::Local;
29
30 sub custom_status_output {
31 my ($self, %options) = @_;
32
33 my $msg = "status is '" . $self->{result_values}->{flag} . "'";
34 if ($self->{result_values}->{expires_date} =~ /never/i) {
35 $msg .= ', never expires';
36 } elsif ($self->{result_values}->{expires_date} =~ /expired/i) {
37 $msg .= ', expired';
38 } else {
39 $msg .= sprintf(
40 "expires in %s [%s]",
41 $self->{result_values}->{expires_human},
42 $self->{result_values}->{expires_date}
43 );
44 }
45
46 return $msg;
47 }
48
49 sub custom_status_calc {
50 my ($self, %options) = @_;
51
52 $self->{result_values}->{key} = $options{new_datas}->{$self->{instance} . '_sysExtLicenseKey'};
53 $self->{result_values}->{flag} = $options{new_datas}->{$self->{instance} . '_sysExtLicenseFlags'};
54 $self->{result_values}->{service} = $options{new_datas}->{$self->{instance} . '_sysExtLicenseService'};
55 $self->{result_values}->{expires} = $options{new_datas}->{$self->{instance} . '_sysExtLicenseExpires'};
56 $self->{result_values}->{expires_date} = $options{new_datas}->{$self->{instance} . '_sysExtLicenseExpires'};
57
58 $self->{result_values}->{expires_human} = 'never';
59 if ($self->{result_values}->{expires_date} =~ /Expired/) {
60 $self->{result_values}->{expires_human} = 'expired';
61 $self->{result_values}->{expires} = 0;
62 }
63
64 if ($self->{result_values}->{expires_date} !~ /Never|Expired/) {
65 my ($year, $mon, $mday, $hour, $min, $sec) = split(/[\s\-:]+/, $self->{result_values}->{expires});
66 $self->{result_values}->{expires} = timelocal($sec, $min, $hour, $mday, $mon - 1, $year) - time();
67 $self->{result_values}->{expires_human} = centreon::plugins::misc::change_seconds(value => $self->{result_values}->{expires});
68 $self->{result_values}->{expires_human} = $self->{result_values}->{expires} = 0 if ($self->{result_values}->{expires} < 0);
69 }
70
71 return 0;
72 }
73
74 sub prefix_output {
75 my ($self, %options) = @_;
76
77 return "License '" . $options{instance_value}->{sysExtLicenseService} . "' ";
78 }
79
80 sub set_counters {
81 my ($self, %options) = @_;
82
83 $self->{maps_counters_type} = [
84 { name => 'license', type => 1, cb_prefix_output => 'prefix_output', message_multiple => 'All licenses status are ok' }
85 ];
86
87 $self->{maps_counters}->{license} = [
88 { label => 'status', type => 2, critical_default => '%{flag} !~ /enabled/i || (%{expires} ne "Never" && %{expires} < 86400)', set => {
89 key_values => [
90 { name => 'sysExtLicenseKey' }, { name => 'sysExtLicenseFlags' },
91 { name => 'sysExtLicenseService' }, { name => 'sysExtLicenseExpires' }
92 ],
93 closure_custom_calc => $self->can('custom_status_calc'),
94 closure_custom_output => $self->can('custom_status_output'),
95 closure_custom_perfdata => sub { return 0; },
96 closure_custom_threshold_check => \&catalog_status_threshold_ng
97 }
98 }
99 ];
100 }
101
102 sub new {
103 my ($class, %options) = @_;
104 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
105 bless $self, $class;
106
107 $options{options}->add_options(arguments => {
108 });
109
110 return $self;
111 }
112
113 my %map_flags = (
114 'E' => 'enabled', 'A' => 'auto-generated', 'R' => 'reboot-required'
115 );
116
117 my $oid_wlsxSysExtSwitchLicenseTable = '.1.3.6.1.4.1.14823.2.2.1.2.1.20.1';
118
119 my $mapping = {
120 sysExtLicenseKey => { oid => '.1.3.6.1.4.1.14823.2.2.1.2.1.20.1.2' },
121 sysExtLicenseInstalled => { oid => '.1.3.6.1.4.1.14823.2.2.1.2.1.20.1.3' },
122 sysExtLicenseExpires => { oid => '.1.3.6.1.4.1.14823.2.2.1.2.1.20.1.4' },
123 sysExtLicenseFlags => { oid => '.1.3.6.1.4.1.14823.2.2.1.2.1.20.1.5', map => \%map_flags },
124 sysExtLicenseService => { oid => '.1.3.6.1.4.1.14823.2.2.1.2.1.20.1.6' }
125 };
126
127 sub manage_selection {
128 my ($self, %options) = @_;
129
130 my $snmp_result = $options{snmp}->get_table(
131 oid => $oid_wlsxSysExtSwitchLicenseTable,
132 start => $mapping->{sysExtLicenseKey}->{oid},
133 end => $mapping->{sysExtLicenseService}->{oid},
134 nothing_quit => 1
135 );
136
137 foreach my $oid (keys %{$snmp_result}) {
138 $snmp_result->{$oid} = centreon::plugins::misc::trim($snmp_result->{$oid});
139 }
140
141 $self->{license} = {};
142 foreach my $oid (keys %{$snmp_result}) {
143 next if ($oid !~ /^$mapping->{sysExtLicenseKey}->{oid}\.(.*)/);
144 my $instance = $1;
145
146 my $result = $options{snmp}->map_instance(
147 mapping => $mapping,
148 results => $snmp_result,
149 instance => $instance
150 );
151
152 $self->{license}->{$result->{sysExtLicenseService}} = { %{$result} };
153 }
154
155 if (scalar(keys %{$self->{license}}) <= 0) {
156 $self->{output}->add_option_msg(short_msg => "No license found.");
157 $self->{output}->option_exit();
158 }
159 }
160
161 1;
162
163 __END__
164
165 =head1 MODE
166
167 Check license (WLSX-SYSTEMEXT-MIB).
168
169 =over 8
170
171 =item B<--warning-status>
172
173 Warning threshold.
174 Can use special variables like:%{key},
175 %{service}, %{flag}, %{expires} (default: '')
176
177 =item B<--critical-status>
178
179 Critical threshold.
180 Can use special variables like: %{key},
181 %{service}, %{flag}, %{expires} (default: '%{flag} !~ /enabled/i || (%{expires} ne "Never" && %{expires} < 86400)')
182
183 =back
184
185 =cut