Debian Codegraph @

src/storage/hp/primera/restapi/mode/licenses.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 storage::hp::primera::restapi::mode::licenses;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_license_output {
29 my ($self, %options) = @_;
30
31 my $message;
32 if (!defined($self->{result_values}->{expires_seconds})) {
33 $message = $self->{result_values}->{name} . ' has permanent license';
34 } elsif ($self->{result_values}->{expires_seconds} == 0) {
35 $message = sprintf(
36 "%s license has expired.",
37 $self->{result_values}->{name}
38 );
39 } else {
40 $message = sprintf(
41 "%s license expires in %s.",
42 $self->{result_values}->{name},
43 centreon::plugins::misc::change_seconds(value => $self->{result_values}->{expires_seconds})
44 );
45 }
46
47 return $message;
48 }
49
50 sub custom_license_perfdata {
51 my ($self, %options) = @_;
52
53 return if ($self->{result_values}->{expires_seconds} eq 'permanent');
54
55 $self->{output}->perfdata_add(
56 nlabel => $self->{nlabel},
57 unit => 's',
58 instances => $self->{result_values}->{name},
59 value => $self->{result_values}->{expires_seconds},
60 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
61 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
62 min => 0
63 );
64 }
65
66 sub custom_license_threshold {
67 my ($self, %options) = @_;
68
69 return 'ok' if (!defined($self->{result_values}->{expires_seconds}));
70 return $self->{perfdata}->threshold_check(
71 value => $self->{result_values}->{expires_seconds},
72 threshold => [
73 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
74 { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' }
75 ]
76 );
77 }
78
79 sub prefix_license_output {
80 my ($self, %options) = @_;
81
82 return "License '" . $options{instance_value}->{name} . "' expires: " . $options{instance_value}->{expiration_human} . ". ";
83 }
84 sub set_counters {
85 my ($self, %options) = @_;
86
87 $self->{maps_counters_type} = [
88 { name => 'global', type => 0 },
89 { name => 'license_expiration', type => 1, cb_prefix_output => 'prefix_license_output', message_multiple => 'All licenses are ok'} #, cb_prefix_output => 'prefix_license_output'
90 ];
91
92 $self->{maps_counters}->{global} = [
93 {
94 label => 'total',
95 nlabel => 'licenses.total.count',
96 set => {
97 key_values => [ { name => 'total' } ],
98 output_template => 'Number of licenses: %s',
99 perfdatas => [
100 { template => '%s', min => 0 }
101 ]
102 }
103 },
104 {
105 label => 'expired',
106 nlabel => 'licenses.expired.count',
107 set => {
108 key_values => [ { name => 'expired' }, { name => 'total' } ],
109 output_template => 'Number of expired licenses: %s',
110 perfdatas => [
111 { template => '%s', min => 0, max => 'total' }
112 ]
113 }
114 }
115 ];
116 $self->{maps_counters}->{license_expiration} = [
117 { label => 'license-expiration', nlabel => 'license.expiration.seconds', set => {
118 key_values => [ { name => 'name' }, { name => 'expires_seconds' }, { name => 'expiration_status' }, { name => 'expiration_human' }],
119 closure_custom_output => $self->can('custom_license_output'),
120 closure_custom_perfdata => $self->can('custom_license_perfdata'),
121 closure_custom_threshold_check => $self->can('custom_license_threshold')
122 }
123 }
124 ];
125 }
126
127 sub new {
128 my ($class, %options) = @_;
129 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
130 bless $self, $class;
131
132 $options{options}->add_options(arguments => {
133 'filter-name:s' => { name => 'filter_name' }
134 });
135
136 return $self;
137 }
138
139 sub manage_selection {
140 my ($self, %options) = @_;
141
142 my $response = $options{custom}->request_api(
143 endpoint => '/api/v1/system'
144 );
145 my $licenses = $response->{licenseInfo}->{licenses};
146
147 my ($total_licenses, $expired_licenses) = (0, 0);
148 for my $license_item (@{$licenses}) {
149 # skip if filter does not match
150 next if (defined($self->{option_results}->{filter_name})
151 and $self->{option_results}->{filter_name} ne ''
152 and $license_item->{name} !~ /$self->{option_results}->{filter_name}/);
153
154 $total_licenses = $total_licenses + 1;
155 $self->{license_expiration}->{$license_item->{name}} = {
156 name => $license_item->{name},
157 expiration_human => defined($license_item->{expiryTime8601}) ? $license_item->{expiryTime8601} : 'never'
158 };
159
160 my $license_status = 'valid';
161 if (defined($license_item->{expiryTimeSec})) {
162 if ($license_item->{expiryTimeSec} > time()) {
163 $self->{license_expiration}->{$license_item->{name}}->{expires_seconds} = $license_item->{expiryTimeSec} - time();
164 } else {
165 $self->{license_expiration}->{$license_item->{name}}->{expires_seconds} = 0;
166 $license_status = 'expired';
167 $expired_licenses = $expired_licenses + 1;
168 }
169 }
170 $self->{license_expiration}->{$license_item->{name}}->{expiration_status} = $license_status;
171 }
172 $self->{global} = {
173 total => $total_licenses,
174 expired => $expired_licenses
175 };
176
177 if (scalar(keys %{$self->{license_expiration}}) <= 0) {
178 $self->{output}->add_option_msg(short_msg => "Couldn't get information about licenses");
179 $self->{output}->option_exit();
180 }
181 }
182
183 1;
184
185 __END__
186
187 =head1 MODE
188
189 Check storage capacities.
190
191 =over 8
192
193 =item B<--filter-name>
194
195 Filter licenses by name (regular expression).
196
197 =item B<--warning-*> B<--critical-*>
198
199 Thresholds for counters and license validity remaining time in seconds.
200 * may be replaced with:
201
202 'total': applies to the total number of licenses.
203 'expired': applies to the number of expired licenses.
204 'license-expiration': applies to the remaining time in seconds until the licenses will expire.
205
206 =back
207
208 =cut