Debian Codegraph @

src/apps/inin/mediaserver/snmp/mode/cmdsrvusage.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::inin::mediaserver::snmp::mode::cmdsrvusage;
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
32 my $msg = 'status : ' . $self->{result_values}->{status};
33 return $msg;
34 }
35
36 sub custom_status_calc {
37 my ($self, %options) = @_;
38
39 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_i3MsCmdSrvStatus'};
40 $self->{result_values}->{accept_sessions} = $options{new_datas}->{$self->{instance} . '_i3MsCmdSrvAcceptSessions'};
41 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
42 return 0;
43 }
44
45 sub custom_usage_perfdata {
46 my ($self, %options) = @_;
47
48 my $label = 'used';
49 my $value_perf = $self->{result_values}->{used};
50 if (defined($self->{instance_mode}->{option_results}->{free})) {
51 $label = 'free';
52 $value_perf = $self->{result_values}->{free};
53 }
54
55 my %total_options = ();
56 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
57 $total_options{total} = $self->{result_values}->{total};
58 $total_options{cast_int} = 1;
59 }
60
61 $self->{output}->perfdata_add(
62 label => $label, unit => 'B',
63 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
64 value => $value_perf,
65 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
66 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
67 min => 0, max => $self->{result_values}->{total}
68 );
69 }
70
71 sub custom_usage_threshold {
72 my ($self, %options) = @_;
73
74 my ($exit, $threshold_value);
75 $threshold_value = $self->{result_values}->{used};
76 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
77 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
78 $threshold_value = $self->{result_values}->{prct_used};
79 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
80 }
81 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
82 return $exit;
83 }
84
85 sub custom_usage_output {
86 my ($self, %options) = @_;
87
88 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
89 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
90 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
91 my $msg = sprintf("Disk Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
92 $total_size_value . " " . $total_size_unit,
93 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
94 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
95 return $msg;
96 }
97
98 sub custom_usage_calc {
99 my ($self, %options) = @_;
100
101 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
102 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
103 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
104 $self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free};
105 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
106 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
107
108 return 0;
109 }
110
111 sub set_counters {
112 my ($self, %options) = @_;
113
114 $self->{maps_counters_type} = [
115 { name => 'cmd', type => 1, cb_prefix_output => 'prefix_cmd_output', message_multiple => 'All command servers are ok' }
116 ];
117
118 $self->{maps_counters}->{cmd} = [
119 { label => 'status', threshold => 0, set => {
120 key_values => [ { name => 'i3MsCmdSrvStatus' }, { name => 'i3MsCmdSrvAcceptSessions' }, { name => 'display' } ],
121 closure_custom_calc => $self->can('custom_status_calc'),
122 closure_custom_output => $self->can('custom_status_output'),
123 closure_custom_perfdata => sub { return 0; },
124 closure_custom_threshold_check => \&catalog_status_threshold,
125 }
126 },
127 { label => 'usage', set => {
128 key_values => [ { name => 'display' }, { name => 'free' }, { name => 'total' } ],
129 closure_custom_calc => $self->can('custom_usage_calc'),
130 closure_custom_output => $self->can('custom_usage_output'),
131 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
132 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
133 }
134 },
135 { label => 'resource-count', set => {
136 key_values => [ { name => 'i3MsCmdSrvResourceCount' }, { name => 'display' } ],
137 output_template => 'Resource Count : %s',
138 perfdatas => [
139 { label => 'resource_count', value => 'i3MsCmdSrvResourceCount', template => '%d',
140 min => 0, label_extra_instance => 1, instance_use => 'display' },
141 ],
142 }
143 },
144 ];
145 }
146
147 sub new {
148 my ($class, %options) = @_;
149 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
150 bless $self, $class;
151
152 $options{options}->add_options(arguments =>
153 {
154 "filter-client:s" => { name => 'filter_client' },
155 "warning-status:s" => { name => 'warning_status', default => '' },
156 "critical-status:s" => { name => 'critical_status', default => '%{status} !~ /^ready/i' },
157 "units:s" => { name => 'units', default => '%' },
158 "free" => { name => 'free' },
159 });
160
161 return $self;
162 }
163
164 sub check_options {
165 my ($self, %options) = @_;
166 $self->SUPER::check_options(%options);
167
168 $self->change_macros(macros => ['warning_status', 'critical_status']);
169 }
170
171 sub prefix_cmd_output {
172 my ($self, %options) = @_;
173
174 return "Command Server '" . $options{instance_value}->{display} . "' ";
175 }
176
177 my %map_status = (1 => 'unknown', 1 => 'ready', 2 => 'notready', 3 => 'error');
178 my %map_truth = (1 => 'true', 2 => 'false');
179 my $mapping = {
180 i3MsCmdSrvAcceptSessions => { oid => '.1.3.6.1.4.1.2793.8227.2.2.1.8', map => \%map_truth },
181 i3MsCmdSrvStatus => { oid => '.1.3.6.1.4.1.2793.8227.2.2.1.2', map => \%map_status },
182 i3MsCmdSrvClient => { oid => '.1.3.6.1.4.1.2793.8227.2.2.1.6' },
183 i3MsCmdSrvResourceCount => { oid => '.1.3.6.1.4.1.2793.8227.2.2.1.9' },
184 i3MsCmdSrvRecFreeDiskSpace => { oid => '.1.3.6.1.4.1.2793.8227.2.2.1.12' },
185 i3MsCmdSrvRecTotalDiskSpace => { oid => '.1.3.6.1.4.1.2793.8227.2.2.1.13' },
186 };
187
188 my $oid_i3MsCommandServerInfoTableEntry = '.1.3.6.1.4.1.2793.8227.2.2.1';
189
190 sub manage_selection {
191 my ($self, %options) = @_;
192
193 $self->{cmd} = {};
194 my $snmp_result = $options{snmp}->get_table(oid => $oid_i3MsCommandServerInfoTableEntry,
195 nothing_quit => 1);
196
197
198 foreach my $oid (keys %{$snmp_result}) {
199 next if ($oid !~ /^$mapping->{i3MsCmdSrvStatus}->{oid}\.(.*)$/);
200 my $instance = $1;
201 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
202
203 if (defined($self->{option_results}->{filter_client}) && $self->{option_results}->{filter_client} ne '' &&
204 $result->{i3MsCmdSrvClient} !~ /$self->{option_results}->{filter_client}/) {
205 $self->{output}->output_add(long_msg => "skipping '" . $result->{i3MsCmdSrvClient} . "': no matching filter.", debug => 1);
206 next;
207 }
208
209 $self->{cmd}->{$instance} = {
210 display => $result->{i3MsCmdSrvClient},
211 total => $result->{i3MsCmdSrvRecTotalDiskSpace} * 1024 * 1024,
212 free => $result->{i3MsCmdSrvRecFreeDiskSpace} * 1024 * 1024,
213 %$result
214 };
215 }
216
217 if (scalar(keys %{$self->{cmd}}) <= 0) {
218 $self->{output}->add_option_msg(short_msg => "No command server found.");
219 $self->{output}->option_exit();
220 }
221 }
222
223 1;
224
225 __END__
226
227 =head1 MODE
228
229 Check command servers usage.
230
231 =over 8
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<--filter-client>
239
240 Filter client name (can be a regexp).
241
242 =item B<--warning-status>
243
244 Define the conditions to match for the status to be WARNING.
245 You can use the following variables: %{accept_sessions}, %{status}, %{display}
246
247 =item B<--critical-status>
248
249 Define the conditions to match for the status to be CRITICAL (default: '%{status} !~ /^ready/i').
250 You can use the following variables: %{accept_sessions}, %{status}, %{display}
251
252 =item B<--warning-*>
253
254 Warning threshold.
255 Can be: 'usage', 'resource-count'.
256
257 =item B<--critical-*>
258
259 Critical threshold.
260 Can be: 'usage', 'resource-count'.
261
262 =item B<--units>
263
264 Units of thresholds (default: '%') ('%', 'B').
265
266 =item B<--free>
267
268 Thresholds are on free space left.
269
270 =back
271
272 =cut