Debian Codegraph @

src/network/citrix/sdx/snmp/mode/diskusage.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::citrix::sdx::snmp::mode::diskusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28
29 sub custom_usage_perfdata {
30 my ($self, %options) = @_;
31
32 my $label = 'used';
33 my $value_perf = $self->{result_values}->{used};
34 if (defined($self->{instance_mode}->{option_results}->{free})) {
35 $label = 'free';
36 $value_perf = $self->{result_values}->{free};
37 }
38
39 my %total_options = ();
40 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
41 $total_options{total} = $self->{result_values}->{total};
42 $total_options{cast_int} = 1;
43 }
44
45 $self->{output}->perfdata_add(
46 label => $label, unit => 'B',
47 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
48 value => $value_perf,
49 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
50 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
51 min => 0, max => $self->{result_values}->{total}
52 );
53 }
54
55 sub custom_usage_threshold {
56 my ($self, %options) = @_;
57
58 my ($exit, $threshold_value);
59 $threshold_value = $self->{result_values}->{used};
60 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
61 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
62 $threshold_value = $self->{result_values}->{prct_used};
63 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
64 }
65 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
66 return $exit;
67 }
68
69 sub custom_usage_output {
70 my ($self, %options) = @_;
71
72 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
73 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
74 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
75 my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
76 $total_size_value . " " . $total_size_unit,
77 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
78 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
79 return $msg;
80 }
81
82 sub custom_usage_calc {
83 my ($self, %options) = @_;
84
85 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
86 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
87 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
88 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
89 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
90 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
91
92 return 0;
93 }
94
95 sub set_counters {
96 my ($self, %options) = @_;
97
98 $self->{maps_counters_type} = [
99 { name => 'disk', type => 1, cb_prefix_output => 'prefix_disk_output', message_multiple => 'All disks are ok' }
100 ];
101
102 $self->{maps_counters}->{disk} = [
103 { label => 'usage', set => {
104 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
105 closure_custom_calc => $self->can('custom_usage_calc'),
106 closure_custom_output => $self->can('custom_usage_output'),
107 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
108 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
109 }
110 },
111 { label => 'read-iops', set => {
112 key_values => [ { name => 'diskTotalBlocksRead', per_second => 1 }, { name => 'display' } ],
113 output_template => 'Read IOPs : %.2f', output_error_template => "Read IOPs : %s",
114 perfdatas => [
115 { label => 'read_iops', template => '%.2f',
116 unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' },
117 ],
118 }
119 },
120 { label => 'write-iops', set => {
121 key_values => [ { name => 'diskTotalBlocksWritten', per_second => 1 }, { name => 'display' } ],
122 output_template => 'Write IOPs : %.2f', output_error_template => "Write IOPs : %s",
123 perfdatas => [
124 { label => 'write_iops', template => '%.2f',
125 unit => 'iops', min => 0, label_extra_instance => 1, instance_use => 'display' },
126 ],
127 }
128 },
129 ];
130 }
131
132 sub new {
133 my ($class, %options) = @_;
134 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
135 bless $self, $class;
136
137 $options{options}->add_options(arguments => {
138 'filter-name:s' => { name => 'filter_name' },
139 'units:s' => { name => 'units', default => '%' },
140 'free' => { name => 'free' },
141 });
142
143 return $self;
144 }
145
146 sub prefix_disk_output {
147 my ($self, %options) = @_;
148
149 return "Disk '" . $options{instance_value}->{display} . "' ";
150 }
151
152 my $mapping = {
153 diskName => { oid => '.1.3.6.1.4.1.5951.6.2.1000.3.1.1' },
154 diskTotalBlocksRead => { oid => '.1.3.6.1.4.1.5951.6.2.1000.3.1.7' },
155 diskTotalBlocksWritten => { oid => '.1.3.6.1.4.1.5951.6.2.1000.3.1.8' },
156 diskUtilized => { oid => '.1.3.6.1.4.1.5951.6.2.1000.3.1.9' },
157 diskSize => { oid => '.1.3.6.1.4.1.5951.6.2.1000.3.1.10' },
158 };
159
160 my $oid_diskEntry = '.1.3.6.1.4.1.5951.6.2.1000.3.1';
161
162 sub manage_selection {
163 my ($self, %options) = @_;
164
165 $self->{disk} = {};
166 my $snmp_result = $options{snmp}->get_table(oid => $oid_diskEntry,
167 nothing_quit => 1);
168
169
170 foreach my $oid (keys %{$snmp_result}) {
171 next if ($oid !~ /^$mapping->{diskName}->{oid}\.(.*)$/);
172 my $instance = $1;
173 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
174
175 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
176 $result->{diskName} !~ /$self->{option_results}->{filter_name}/) {
177 $self->{output}->output_add(long_msg => "skipping '" . $result->{diskName} . "': no matching filter.", debug => 1);
178 next;
179 }
180
181 $self->{disk}->{$instance} = {
182 display => $result->{diskName},
183 total => $result->{diskSize},
184 used => $result->{diskUtilized},
185 %$result
186 };
187 }
188
189 if (scalar(keys %{$self->{disk}}) <= 0) {
190 $self->{output}->add_option_msg(short_msg => "No disk found.");
191 $self->{output}->option_exit();
192 }
193
194 $self->{cache_name} = "citrix_sdx_" . $self->{mode} . '_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' .
195 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
196 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
197 }
198
199 1;
200
201 __END__
202
203 =head1 MODE
204
205 Check disks.
206
207 =over 8
208
209 =item B<--filter-counters>
210
211 Only display some counters (regexp can be used).
212 Example: --filter-counters='^usage$'
213
214 =item B<--filter-name>
215
216 Filter disk name (can be a regexp).
217
218 =item B<--warning-*>
219
220 Warning threshold.
221 Can be: 'read-iops', 'write-iops', 'usage'.
222
223 =item B<--critical-*>
224
225 Critical threshold.
226 Can be: 'read-iops', 'write-iops', 'usage'.
227
228 =item B<--units>
229
230 Units of thresholds (default: '%') ('%', 'B').
231
232 =item B<--free>
233
234 Thresholds are on free space left.
235
236 =back
237
238 =cut