Debian Codegraph @

src/hardware/devices/hikvision/nvr/snmp/mode/disks.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 hardware::devices::hikvision::nvr::snmp::mode::disks;
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 sprintf(
33 'status: %s',
34 $self->{result_values}->{status}
35 );
36 }
37
38 sub custom_space_usage_output {
39 my ($self, %options) = @_;
40
41 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total_space});
42 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used_space});
43 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free_space});
44 return sprintf(
45 'space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
46 $total_size_value . " " . $total_size_unit,
47 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used_space},
48 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free_space}
49 );
50 }
51
52 sub prefix_disk_output {
53 my ($self, %options) = @_;
54
55 return "Disk '" . $options{instance}. "' ";
56 }
57
58 sub prefix_global_output {
59 my ($self, %options) = @_;
60
61 return 'Disks ';
62 }
63
64 sub set_counters {
65 my ($self, %options) = @_;
66
67 $self->{maps_counters_type} = [
68 { name => 'global', cb_prefix_output => 'prefix_global_output', type => 0, skipped_code => { -10 => 1 } },
69 { name => 'disks', type => 1, cb_prefix_output => 'prefix_disk_output', message_multiple => 'All disks are ok' }
70 ];
71
72 $self->{maps_counters}->{global} = [
73 { label => 'disks-total', nlabel => 'disks.total.count', set => {
74 key_values => [ { name => 'total' } ],
75 output_template => 'total: %s',
76 perfdatas => [
77 { template => '%s', min => 0 }
78 ]
79 }
80 },
81 { label => 'disks-errors', nlabel => 'disks.errors.count', set => {
82 key_values => [ { name => 'errors' }, { name => 'total' } ],
83 output_template => 'errors: %s',
84 perfdatas => [
85 { template => '%s', min => 0, max => 'total' }
86 ]
87 }
88 }
89 ];
90
91 $self->{maps_counters}->{disks} = [
92 {
93 label => 'status',
94 type => 2,
95 warning_default => '%{status} =~ /reparing|formatting/i',
96 critical_default => '%{status} =~ /abnormal|smartfailed/i',
97 set => {
98 key_values => [ { name => 'status' }, { name => 'name' } ],
99 closure_custom_output => $self->can('custom_status_output'),
100 closure_custom_perfdata => sub { return 0; },
101 closure_custom_threshold_check => \&catalog_status_threshold_ng
102 }
103 },
104 { label => 'space-usage', nlabel => 'disk.space.usage.bytes', set => {
105 key_values => [ { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' } ],
106 closure_custom_output => $self->can('custom_space_usage_output'),
107 perfdatas => [
108 { template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 }
109 ]
110 }
111 },
112 { label => 'space-usage-free', nlabel => 'disk.space.free.bytes', display_ok => 0, set => {
113 key_values => [ { name => 'free_space' }, { name => 'used_space' }, { name => 'prct_used_space' }, { name => 'prct_free_space' }, { name => 'total_space' } ],
114 closure_custom_output => $self->can('custom_space_usage_output'),
115 perfdatas => [
116 { template => '%d', min => 0, max => 'total_space', unit => 'B', cast_int => 1, label_extra_instance => 1 }
117 ]
118 }
119 },
120 { label => 'space-usage-prct', nlabel => 'disk.space.usage.percentage', display_ok => 0, set => {
121 key_values => [ { name => 'prct_used_space' }, { name => 'used_space' }, { name => 'free_space' }, { name => 'prct_free_space' }, { name => 'total_space' } ],
122 closure_custom_output => $self->can('custom_space_usage_output'),
123 perfdatas => [
124 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
125 ]
126 }
127 }
128 ];
129 }
130
131 sub new {
132 my ($class, %options) = @_;
133 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
134 bless $self, $class;
135
136 $options{options}->add_options(arguments => {
137 'filter-name:s' => { name => 'filter_name' }
138 });
139
140 return $self;
141 }
142
143 sub manage_selection {
144 my ($self, %options) = @_;
145
146 my $map_status = {
147 0 => 'normal', 1 => 'unformatted', 2 => 'abnormal',
148 3 => 'smartfailed', 4 => 'mismatch', 5 => 'idle',
149 6 => 'notOnline', 10 => 'reparing', 11 => 'formatting'
150 };
151 my $mapping = {
152 name => { oid => '.1.3.6.1.4.1.50001.1.241.1.2' }, # hikDiskVolume
153 status => { oid => '.1.3.6.1.4.1.50001.1.241.1.3', map => $map_status }, # hikDiskStatus
154 free => { oid => '.1.3.6.1.4.1.50001.1.241.1.4' }, # hikDiskFreeSpace
155 total => { oid => '.1.3.6.1.4.1.50001.1.241.1.5' } # hikDiskCapability
156 };
157 my $oid_diskEntry = '.1.3.6.1.4.1.50001.1.241.1';
158
159 my $snmp_result = $options{snmp}->get_table(oid => $oid_diskEntry, nothing_quit => 1);
160
161 $self->{global} = { total => 0, errors => 0 };
162 $self->{disks} = {};
163 foreach my $oid (keys %$snmp_result) {
164 next if ($oid !~ /^$mapping->{status}->{oid}\.(.*)$/);
165 my $instance = $1;
166 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
167
168 my $name = defined($result->{name}) && $result->{name} ne '' ? $result->{name} : $instance;
169 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
170 $name !~ /$self->{option_results}->{filter_name}/) {
171 $self->{output}->output_add(long_msg => "skipping disk '" . $name . "': no matching filter.", debug => 1);
172 next;
173 }
174
175 $result->{total} *= 1024 * 1024;
176 $result->{free} *= 1024 * 1024;
177 $self->{disks}->{$name} = {
178 name => $name,
179 status => $result->{status},
180 free_space => $result->{free},
181 total_space => $result->{total},
182 used_space => $result->{total} - $result->{free},
183 prct_used_space => ($result->{total} - $result->{free}) * 100 / $result->{total},
184 prct_free_space => $result->{free} * 100 / $result->{total},
185 };
186
187 if ($result->{status} =~ /abnormal|smartfailed/) {
188 $self->{global}->{errors}++;
189 }
190 $self->{global}->{total}++;
191 }
192 }
193
194 1;
195
196 __END__
197
198 =head1 MODE
199
200 Check disks.
201
202 =over 8
203
204 =item B<--filter-name>
205
206 Filter disks by name (can be a regexp).
207
208 =item B<--unknown-status>
209
210 Define the conditions to match for the status to be UNKNOWN.
211 You can use the following variables: %{status}, %{name}
212
213 =item B<--warning-status>
214
215 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /reparing|formatting/i').
216 You can use the following variables: %{status}, %{name}
217
218 =item B<--critical-status>
219
220 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /abnormal|smartfailed/i').
221 You can use the following variables: %{status}, %{name}
222
223 =item B<--warning-*> B<--critical-*>
224
225 Thresholds.
226 Can be: 'space-usage', 'space-usage-free', 'space-usage-prct',
227 'disks-total', 'disks-errors'.
228
229 =back
230
231 =cut