Debian Codegraph @

src/apps/video/zixi/restapi/mode/broadcasterlicenseusage.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::video::zixi::restapi::mode::broadcasterlicenseusage;
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);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31 my $info = $self->{result_values}->{info} eq '' ? '-' : $self->{result_values}->{info};
32 my $error = $self->{result_values}->{error} eq '' ? '-' : $self->{result_values}->{error};
33 my $msg = 'information : ' . $info . ' [error: ' . $error . ']';
34
35 return $msg;
36 }
37
38 sub custom_status_calc {
39 my ($self, %options) = @_;
40
41 $self->{result_values}->{info} = $options{new_datas}->{$self->{instance} . '_info'};
42 $self->{result_values}->{name} = $options{new_datas}->{$self->{instance} . '_name'};
43 $self->{result_values}->{error} = $options{new_datas}->{$self->{instance} . '_error'};
44
45 return 0;
46 }
47
48 sub custom_usage_perfdata {
49 my ($self, %options) = @_;
50
51 my $label = 'used';
52 my $value_perf = $self->{result_values}->{used};
53 if (defined($self->{instance_mode}->{option_results}->{free})) {
54 $label = 'free';
55 $value_perf = $self->{result_values}->{free};
56 }
57
58 my %total_options = ();
59 if ($self->{result_values}->{total} =~ /[0-9]/ && $self->{instance_mode}->{option_results}->{units} eq '%') {
60 $total_options{total} = $self->{result_values}->{total};
61 $total_options{cast_int} = 1;
62 }
63
64 $self->{output}->perfdata_add(
65 label => $label,
66 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
67 value => $value_perf,
68 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
69 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
70 min => 0, max => $self->{result_values}->{total} =~ /[0-9]/ ? $self->{result_values}->{total} : undef
71 );
72 }
73
74 sub custom_usage_threshold {
75 my ($self, %options) = @_;
76
77 my ($exit, $threshold_value);
78 $threshold_value = $self->{result_values}->{used};
79 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
80 if ($self->{result_values}->{total} =~ /[0-9]/ && $self->{instance_mode}->{option_results}->{units} eq '%') {
81 $threshold_value = $self->{result_values}->{prct_used};
82 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
83 }
84 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
85 return $exit;
86 }
87
88 sub custom_usage_output {
89 my ($self, %options) = @_;
90
91 my $msg = sprintf("Total: %s Used: %s (%s) Free: %s (%s)",
92 $self->{result_values}->{total},
93 $self->{result_values}->{used},
94 defined($self->{result_values}->{prct_used}) ? sprintf("%.2f%%", $self->{result_values}->{prct_used}) : '-',
95 defined($self->{result_values}->{free}) ? $self->{result_values}->{free} : '-',
96 defined($self->{result_values}->{prct_free}) ? sprintf("%.2f%%", $self->{result_values}->{prct_free}) : '-');
97 return $msg;
98 }
99
100 sub custom_usage_calc {
101 my ($self, %options) = @_;
102
103 return -10 if ($options{new_datas}->{$self->{instance} . '_info'} =~ /disabled/i ||
104 $options{new_datas}->{$self->{instance} . '_used'} eq '' ||
105 ($options{new_datas}->{$self->{instance} . '_used'} =~ /^0$/ && $options{new_datas}->{$self->{instance} . '_limit'} =~ /^0$/));
106
107 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_limit'};
108 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
109 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_name'};
110
111 # Can be 'unlimited'
112 if ($self->{result_values}->{total} =~ /[0-9]/) {
113 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
114 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
115 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
116 if ($self->{result_values}->{free} < 0) {
117 $self->{result_values}->{free} = 0;
118 $self->{result_values}->{prct_free} = 0;
119 }
120 }
121
122 return 0;
123 }
124
125 sub set_counters {
126 my ($self, %options) = @_;
127
128 $self->{maps_counters_type} = [
129 { name => 'license', type => 1, cb_prefix_output => 'prefix_license_output', message_multiple => 'All licenses are ok', skipped_code => { -10 => 1 } },
130 ];
131
132 $self->{maps_counters}->{license} = [
133 { label => 'status', threshold => 0, set => {
134 key_values => [ { name => 'info' }, { name => 'name' }, { name => 'error' } ],
135 closure_custom_calc => $self->can('custom_status_calc'),
136 closure_custom_output => $self->can('custom_status_output'),
137 closure_custom_perfdata => sub { return 0; },
138 closure_custom_threshold_check => \&catalog_status_threshold,
139 }
140 },
141 { label => 'usage', set => {
142 key_values => [ { name => 'used' }, { name => 'limit' }, { name => 'name' }, { name => 'info' } ],
143 closure_custom_calc => $self->can('custom_usage_calc'),
144 closure_custom_output => $self->can('custom_usage_output'),
145 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
146 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
147 }
148 },
149 { label => 'days', set => {
150 key_values => [ { name => 'days' } ],
151 output_template => '%d days remaining before expiration',
152 closure_custom_perfdata => sub { return 0; },
153 }
154 },
155 ];
156 }
157
158 sub new {
159 my ($class, %options) = @_;
160 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
161 bless $self, $class;
162
163 $options{options}->add_options(arguments =>
164 {
165 "filter-name:s" => { name => 'filter_name' },
166 "warning-status:s" => { name => 'warning_status' },
167 "critical-status:s" => { name => 'critical_status' },
168 "units:s" => { name => 'units', default => '%' },
169 "free" => { name => 'free' },
170 });
171 centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Date::Parse',
172 error_msg => "Cannot load module 'Date::Parse'.");
173 return $self;
174 }
175
176 sub check_options {
177 my ($self, %options) = @_;
178 $self->SUPER::check_options(%options);
179
180 $self->change_macros(macros => ['warning_status', 'critical_status']);
181 }
182
183 sub prefix_license_output {
184 my ($self, %options) = @_;
185
186 return "License '" . $options{instance_value}->{name} . "' ";
187 }
188
189 sub manage_selection {
190 my ($self, %options) = @_;
191
192 $self->{license} = {};
193 my $result = $options{custom}->get(path => '/licensing_info');
194
195 my $current_time = time();
196 foreach my $entry (@{$result->{lics}}) {
197 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
198 $entry->{name} !~ /$self->{option_results}->{filter_name}/) {
199 $self->{output}->output_add(long_msg => "skipping '" . $entry->{name} . "': no matching filter.", debug => 1);
200 next;
201 }
202
203 # Format 09-dec-2017. Can be also 'permanent' (so dont care)
204 my $days_remaining;
205 if ($entry->{exp} =~ /\d+-\S+-\d+/) {
206 my $create_time = Date::Parse::str2time($entry->{exp});
207 $days_remaining = ($create_time - $current_time) / 86400;
208 }
209
210 $self->{license}->{$entry->{name}} = { %{$entry}, days => $days_remaining };
211 }
212
213 if (scalar(keys %{$self->{license}}) <= 0) {
214 $self->{output}->add_option_msg(short_msg => "No license found.");
215 $self->{output}->option_exit();
216 }
217 }
218
219 1;
220
221 __END__
222
223 =head1 MODE
224
225 Check license usage.
226
227 =over 8
228
229 =item B<--filter-source>
230
231 Filter source (can be a regexp).
232
233 =item B<--filter-counters>
234
235 Only display some counters (regexp can be used).
236 Example: --filter-counters='^status$'
237
238 =item B<--warning-*>
239
240 Warning threshold.
241 Can be: 'usage', 'days'.
242
243 =item B<--critical-*>
244
245 Critical threshold.
246 Can be: 'usage', 'days'.
247
248 =item B<--units>
249
250 Units of thresholds (default: '%') ('%', 'absolute').
251
252 =item B<--free>
253
254 Thresholds are on free license left.
255
256 =item B<--warning-status>
257
258 Define the conditions to match for the status to be WARNING (default: -)
259 You can use the following variables: %{name}, %{error}, %{info}.
260
261 =item B<--critical-status>
262
263 Define the conditions to match for the status to be CRITICAL (default: -).
264 You can use the following variables: %{name}, %{error}, %{info}.
265
266 =back
267
268 =cut