Debian Codegraph @

src/storage/oracle/zs/snmp/mode/shareusage.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::oracle::zs::snmp::mode::shareusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_usage_perfdata {
29 my ($self, %options) = @_;
30
31 my $label = $self->{result_values}->{label} . '_used';
32 my $value_perf = $self->{result_values}->{used};
33 if (defined($self->{instance_mode}->{option_results}->{free})) {
34 $label = $self->{result_values}->{label} . '_free';
35 $value_perf = $self->{result_values}->{free};
36 }
37
38 my %total_options = ();
39 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
40 $total_options{total} = $self->{result_values}->{total};
41 $total_options{cast_int} = 1;
42 }
43
44 $self->{output}->perfdata_add(
45 label => $label, unit => 'B',
46 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
47 value => $value_perf,
48 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
49 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
50 min => 0, max => $self->{result_values}->{total}
51 );
52 }
53
54 sub custom_usage_threshold {
55 my ($self, %options) = @_;
56
57 my ($exit, $threshold_value);
58 $threshold_value = $self->{result_values}->{used};
59 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
60 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
61 $threshold_value = $self->{result_values}->{prct_used};
62 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
63 }
64 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
65 return $exit;
66 }
67
68 sub custom_usage_output {
69 my ($self, %options) = @_;
70
71 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
72 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
73 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
74 my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
75 $total_size_value . " " . $total_size_unit,
76 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
77 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
78 return $msg;
79 }
80
81 sub custom_usage_calc {
82 my ($self, %options) = @_;
83
84 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
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 => 'project', type => 1, cb_prefix_output => 'prefix_project_output', message_multiple => 'All projects are ok' },
100 { name => 'share', type => 1, cb_prefix_output => 'prefix_share_output', message_multiple => 'All shares are ok' }
101 ];
102
103 $self->{maps_counters}->{share} = [
104 { label => 'share-usage', set => {
105 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
106 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'share' },
107 closure_custom_output => $self->can('custom_usage_output'),
108 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
109 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
110 }
111 },
112 ];
113 $self->{maps_counters}->{project} = [
114 { label => 'project-usage', set => {
115 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
116 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'project' },
117 closure_custom_output => $self->can('custom_usage_output'),
118 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
119 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
120 }
121 },
122 ];
123 }
124
125 sub new {
126 my ($class, %options) = @_;
127 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
128 bless $self, $class;
129
130 $options{options}->add_options(arguments => {
131 "filter-name:s" => { name => 'filter_name' },
132 "filter-project:s" => { name => 'filter_project' },
133 "units:s" => { name => 'units', default => '%' },
134 "free" => { name => 'free' },
135 });
136
137 return $self;
138 }
139
140 sub prefix_share_output {
141 my ($self, %options) = @_;
142
143 return "Share '" . $options{instance_value}->{display} . "' ";
144 }
145
146 sub prefix_project_output {
147 my ($self, %options) = @_;
148
149 return "Project '" . $options{instance_value}->{display} . "' ";
150 }
151
152 my $mapping = {
153 sunAkShareName => { oid => '.1.3.6.1.4.1.42.2.225.1.6.1.2' },
154 sunAkShareProject => { oid => '.1.3.6.1.4.1.42.2.225.1.6.1.4' },
155 sunAkShareSizeB => { oid => '.1.3.6.1.4.1.42.2.225.1.6.1.10' },
156 sunAkShareUsedB => { oid => '.1.3.6.1.4.1.42.2.225.1.6.1.11' },
157 };
158
159 my $oid_sunAkShareEntry = '.1.3.6.1.4.1.42.2.225.1.6.1';
160
161 sub manage_selection {
162 my ($self, %options) = @_;
163
164 if ($options{snmp}->is_snmpv1()) {
165 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
166 $self->{output}->option_exit();
167 }
168
169 $self->{share} = {};
170 $self->{project} = {};
171 my $snmp_result = $options{snmp}->get_table(oid => $oid_sunAkShareEntry,
172 nothing_quit => 1);
173
174
175 foreach my $oid (keys %{$snmp_result}) {
176 next if ($oid !~ /^$mapping->{sunAkShareName}->{oid}\.(.*)$/);
177 my $instance = $1;
178 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
179
180 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
181 $result->{sunAkShareName} !~ /$self->{option_results}->{filter_name}/) {
182 $self->{output}->output_add(long_msg => "skipping '" . $result->{sunAkShareName} . "': no matching filter.", debug => 1);
183 next;
184 }
185 if (defined($self->{option_results}->{filter_project}) && $self->{option_results}->{filter_project} ne '' &&
186 $result->{sunAkShareProject} !~ /$self->{option_results}->{filter_project}/) {
187 $self->{output}->output_add(long_msg => "skipping '" . $result->{sunAkShareName} . "': no matching filter.", debug => 1);
188 next;
189 }
190
191 $self->{project}->{$result->{sunAkShareProject}} = { total => 0, used => 0, display => $result->{sunAkShareProject} }
192 if (!defined($self->{project}->{$result->{sunAkShareProject}}));
193 $self->{share}->{$instance} = {
194 display => $result->{sunAkShareName},
195 total => $result->{sunAkShareSizeB},
196 used => $result->{sunAkShareUsedB},
197 };
198 $self->{project}->{$result->{sunAkShareProject}}->{total} += $result->{sunAkShareSizeB};
199 $self->{project}->{$result->{sunAkShareProject}}->{used} += $result->{sunAkShareUsedB};
200 }
201
202 if (scalar(keys %{$self->{share}}) <= 0) {
203 $self->{output}->add_option_msg(short_msg => "No share found.");
204 $self->{output}->option_exit();
205 }
206 }
207
208 1;
209
210 __END__
211
212 =head1 MODE
213
214 Check shares.
215
216 =over 8
217
218 =item B<--filter-name>
219
220 Filter share name (can be a regexp).
221
222 =item B<--filter-project>
223
224 Filter project name (can be a regexp).
225
226 =item B<--warning-*>
227
228 Warning threshold.
229 Can be: 'share-usage', 'project-usage'.
230
231 =item B<--critical-*>
232
233 Critical threshold.
234 Can be: 'share-usage', 'project-usage'.
235
236 =item B<--units>
237
238 Units of thresholds (default: '%') ('%', 'B').
239
240 =item B<--free>
241
242 Thresholds are on free space left.
243
244 =back
245
246 =cut