Debian Codegraph @

src/network/citrix/netscaler/snmp/mode/storage.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::netscaler::snmp::mode::storage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub set_counters {
29 my ($self, %options) = @_;
30
31 $self->{maps_counters_type} = [
32 { name => 'storage', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages are ok' }
33 ];
34
35 $self->{maps_counters}->{storage} = [
36 { label => 'usage', set => {
37 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
38 closure_custom_calc => $self->can('custom_usage_calc'),
39 closure_custom_output => $self->can('custom_usage_output'),
40 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
41 closure_custom_threshold_check => $self->can('custom_usage_threshold')
42 }
43 }
44 ];
45 }
46
47 sub custom_usage_perfdata {
48 my ($self, %options) = @_;
49
50 my $label = 'used';
51 my $value_perf = $self->{result_values}->{used};
52 if (defined($self->{instance_mode}->{option_results}->{free})) {
53 $label = 'free';
54 $value_perf = $self->{result_values}->{free};
55 }
56
57 my %total_options = ();
58 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
59 $total_options{total} = $self->{result_values}->{total};
60 $total_options{cast_int} = 1;
61 }
62
63 $self->{output}->perfdata_add(
64 label => $label, unit => 'B',
65 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
66 value => $value_perf,
67 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
68 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
69 min => 0, max => $self->{result_values}->{total}
70 );
71 }
72
73 sub custom_usage_threshold {
74 my ($self, %options) = @_;
75
76 my ($exit, $threshold_value);
77 $threshold_value = $self->{result_values}->{used};
78 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
79 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
80 $threshold_value = $self->{result_values}->{prct_used};
81 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
82 }
83 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
84 return $exit;
85 }
86
87 sub custom_usage_output {
88 my ($self, %options) = @_;
89
90 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
91 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
92 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
93 my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
94 $total_size_value . " " . $total_size_unit,
95 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
96 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
97 return $msg;
98 }
99
100 sub custom_usage_calc {
101 my ($self, %options) = @_;
102
103 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
104 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
105 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
106 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
107 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
108 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
109
110 return 0;
111 }
112
113 sub prefix_storage_output {
114 my ($self, %options) = @_;
115
116 return "Storage '" . $options{instance_value}->{display} . "' ";
117 }
118
119 sub new {
120 my ($class, %options) = @_;
121 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
122 bless $self, $class;
123
124 $options{options}->add_options(arguments => {
125 'filter-name:s' => { name => 'filter_name' },
126 'units:s' => { name => 'units', default => '%' },
127 'free' => { name => 'free' }
128 });
129
130 return $self;
131 }
132
133 my $mapping = {
134 sysHealthDiskName => { oid => '.1.3.6.1.4.1.5951.4.1.1.41.8.1.1' },
135 sysHealthDiskSize => { oid => '.1.3.6.1.4.1.5951.4.1.1.41.8.1.2' }, # in MB
136 sysHealthDiskUsed => { oid => '.1.3.6.1.4.1.5951.4.1.1.41.8.1.4' }, # in MB
137 };
138 my $oid_nsSysHealthDiskEntry = '.1.3.6.1.4.1.5951.4.1.1.41.8.1';
139
140 sub manage_selection {
141 my ($self, %options) = @_;
142
143 $self->{storage} = {};
144 my $snmp_result = $options{snmp}->get_table(
145 oid => $oid_nsSysHealthDiskEntry,
146 nothing_quit => 1
147 );
148
149 foreach my $oid (keys %{$snmp_result}) {
150 next if ($oid !~ /^$mapping->{sysHealthDiskName}->{oid}\.(.*)$/);
151 my $instance = $1;
152 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
153
154 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
155 $result->{sysHealthDiskName} !~ /$self->{option_results}->{filter_name}/) {
156 $self->{output}->output_add(long_msg => "skipping '" . $result->{sysHealthDiskName} . "': no matching filter.", debug => 1);
157 next;
158 }
159
160 $result->{sysHealthDiskSize} *= 1024 * 1024;
161 $result->{sysHealthDiskUsed} *= 1024 * 1024;
162 $self->{storage}->{$instance} = {
163 display => $result->{sysHealthDiskName},
164 total => $result->{sysHealthDiskSize},
165 used => $result->{sysHealthDiskUsed}
166 };
167 }
168
169 if (scalar(keys %{$self->{storage}}) <= 0) {
170 $self->{output}->add_option_msg(short_msg => 'No storage found.');
171 $self->{output}->option_exit();
172 }
173 }
174
175 1;
176
177 __END__
178
179 =head1 MODE
180
181 Check disk usages (NS-MIB-smiv2).
182
183 =over 8
184
185 =item B<--filter-name>
186
187 Filter storage name (can be a regexp).
188
189 =item B<--warning-usage>
190
191 Warning threshold.
192
193 =item B<--critical-usage>
194
195 Critical threshold.
196
197 =item B<--units>
198
199 Units of thresholds (default: '%') ('%', 'B').
200
201 =item B<--free>
202
203 Thresholds are on free space left.
204
205 =back
206
207 =cut
208