Debian Codegraph @

src/storage/hp/3par/ssh/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 storage::hp::3par::ssh::mode::diskusage;
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_ng);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 return 'status: ' . $self->{result_values}->{status};
33 }
34
35 sub custom_usage_output {
36 my ($self, %options) = @_;
37
38 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
39 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
40 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
41 return sprintf(
42 "Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
43 $total_size_value . " " . $total_size_unit,
44 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
45 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
46 );
47 }
48
49 sub prefix_disk_output {
50 my ($self, %options) = @_;
51
52 return "Disk '" . $options{instance_value}->{display} . "' ";
53 }
54
55 sub set_counters {
56 my ($self, %options) = @_;
57
58 $self->{maps_counters_type} = [
59 { name => 'disk', type => 1, cb_prefix_output => 'prefix_disk_output', message_multiple => 'All disks are ok', sort_method => 'num' }
60 ];
61
62 $self->{maps_counters}->{disk} = [
63 { label => 'status', type => 2, critical_default => '%{status} !~ /normal/i', set => {
64 key_values => [ { name => 'status' }, { name => 'display' } ],
65 closure_custom_output => $self->can('custom_status_output'),
66 closure_custom_perfdata => sub { return 0; },
67 closure_custom_threshold_check => \&catalog_status_threshold_ng
68 }
69 },
70 { label => 'usage', nlabel => 'disk.space.usage.bytes', set => {
71 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ],
72 closure_custom_output => $self->can('custom_usage_output'),
73 perfdatas => [
74 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' }
75 ]
76 }
77 },
78 { label => 'usage-free', display_ok => 0, nlabel => 'disk.space.free.bytes', set => {
79 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ],
80 closure_custom_output => $self->can('custom_usage_output'),
81 perfdatas => [
82 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' }
83 ]
84 }
85 },
86 { label => 'usage-prct', display_ok => 0, nlabel => 'disk.space.usage.percentage', set => {
87 key_values => [ { name => 'prct_used' }, { name => 'display' } ],
88 output_template => 'Used : %.2f %%',
89 perfdatas => [
90 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }
91 ]
92 }
93 }
94 ];
95 }
96
97 sub new {
98 my ($class, %options) = @_;
99 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
100 bless $self, $class;
101
102 $options{options}->add_options(arguments => {
103 'filter-name:s' => { name => 'filter_name' }
104 });
105
106 return $self;
107 }
108
109 sub manage_selection {
110 my ($self, %options) = @_;
111
112 my ($result, $exit) = $options{custom}->execute_command(commands => ['showpd -showcols Id,State,Type,Size_MB,Free_MB']);
113
114 #Id State Type Size_MB Free_MB
115 # 0 normal FC 838656 133120
116 # 1 normal FC 838656 101376
117 # 2 normal FC 838656 133120
118
119 $self->{disk} = {};
120 my @lines = split /\n/, $result;
121 foreach (@lines) {
122 next if (!/^\s*(\d+)\s+(\S+)\s+\S+\s+(\d+)\s+(\d+)/);
123 my ($disk_id, $status, $total, $free) = ($1, $2, $3 * 1024 * 1024, $4 * 1024 * 1024);
124
125 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
126 $disk_id !~ /$self->{option_results}->{filter_name}/) {
127 $self->{output}->output_add(long_msg => "skipping disk '" . $disk_id . "': no matching filter.", debug => 1);
128 next;
129 }
130
131 $self->{disk}->{$disk_id} = {
132 display => $disk_id,
133 status => $status,
134 total => $total,
135 used => $total - $free,
136 free => $free,
137 prct_used => ($total - $free) * 100 / $total,
138 prct_free => 100 - (($total - $free) * 100 / $total)
139 };
140 }
141
142 if (scalar(keys %{$self->{disk}}) <= 0) {
143 $self->{output}->add_option_msg(short_msg => "No disk found.");
144 $self->{output}->option_exit();
145 }
146 }
147
148 1;
149
150 __END__
151
152 =head1 MODE
153
154 Check disk usages.
155
156 =over 8
157
158 =item B<--filter-counters>
159
160 Only display some counters (regexp can be used).
161 Example: --filter-counters='^status$'
162
163 =item B<--filter-name>
164
165 Filter disk name (can be a regexp).
166
167 =item B<--warning-status>
168
169 Define the conditions to match for the status to be WARNING (default: '').
170 You can use the following variables: %{status}, %{display}
171
172 =item B<--critical-status>
173
174 Define the conditions to match for the status to be CRITICAL (default: '%{status} !~ /normal/i').
175 You can use the following variables: %{status}, %{display}
176
177 =item B<--warning-*> B<--critical-*>
178
179 Warning threshold.
180 Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%).
181
182 =back
183
184 =cut