Debian Codegraph @

src/hardware/devices/hikvision/nvr/isapi/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::isapi::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_space_usage_output {
30 my ($self, %options) = @_;
31
32 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
33 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
34 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
35 return sprintf(
36 'space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)',
37 $total_size_value . " " . $total_size_unit,
38 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
39 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
40 );
41 }
42
43 sub disk_long_output {
44 my ($self, %options) = @_;
45
46 return sprintf(
47 "checking disk '%s'",
48 $options{instance}
49 );
50 }
51
52 sub prefix_disk_output {
53 my ($self, %options) = @_;
54
55 return sprintf(
56 "disk '%s' ",
57 $options{instance}
58 );
59 }
60
61 sub set_counters {
62 my ($self, %options) = @_;
63
64 $self->{maps_counters_type} = [
65 { name => 'disks', type => 3, cb_prefix_output => 'prefix_disk_output', cb_long_output => 'disk_long_output',
66 indent_long_output => ' ', message_multiple => 'All disks are ok',
67 group => [
68 { name => 'status', type => 0, skipped_code => { -10 => 1 } },
69 { name => 'space', type => 0, skipped_code => { -10 => 1 } },
70 { name => 'temperature', type => 0, skipped_code => { -10 => 1 } }
71 ]
72 }
73 ];
74
75 # status: ok,unformatted,error,idle,mismatch,offline,smartFailed,reparing,formating,notexist,unRecordHostFormatted
76 $self->{maps_counters}->{status} = [
77 {
78 label => 'status',
79 type => 2,
80 warning_default => '%{status} =~ /reparing/i',
81 critical_default => '%{status} =~ /error|smartFailed/i',
82 set => {
83 key_values => [ { name => 'status' }, { name => 'name' } ],
84 output_template => 'status: %s',
85 closure_custom_perfdata => sub { return 0; },
86 closure_custom_threshold_check => \&catalog_status_threshold_ng
87 }
88 }
89 ];
90
91 $self->{maps_counters}->{space} = [
92 { label => 'space-usage', nlabel => 'disk.space.usage.bytes', set => {
93 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
94 closure_custom_output => $self->can('custom_space_usage_output'),
95 perfdatas => [
96 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 }
97 ]
98 }
99 },
100 { label => 'space-usage-free', nlabel => 'disk.space.free.bytes', display_ok => 0, set => {
101 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
102 closure_custom_output => $self->can('custom_space_usage_output'),
103 perfdatas => [
104 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 }
105 ]
106 }
107 },
108 { label => 'space-usage-prct', nlabel => 'disk.space.usage.percentage', display_ok => 0, set => {
109 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
110 closure_custom_output => $self->can('custom_space_usage_output'),
111 perfdatas => [
112 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
113 ]
114 }
115 }
116 ];
117
118 $self->{maps_counters}->{temperature} = [
119 { label => 'temperature', nlabel => 'disk.temperature.celsius', set => {
120 key_values => [ { name => 'value' } ],
121 output_template => 'temperature: %s C',
122 perfdatas => [
123 { template => '%s', unit => 'C', label_extra_instance => 1 }
124 ]
125 }
126 }
127 ];
128 }
129
130 sub new {
131 my ($class, %options) = @_;
132 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
133 bless $self, $class;
134
135 $options{options}->add_options(arguments => {
136 'filter-name:s' => { name => 'filter_name' }
137 });
138
139 return $self;
140 }
141
142 sub manage_selection {
143 my ($self, %options) = @_;
144
145 my $result = $options{custom}->request_api(endpoint => '/ISAPI/ContentMgmt/Storage/hdd', force_array => ['hdd']);
146 if (!defined($result->{hdd})) {
147 $self->{output}->add_option_msg(short_msg => "Cannot find disk informations");
148 $self->{output}->option_exit();
149 }
150
151 $self->{disks} = {};
152 foreach my $hdd (@{$result->{hdd}}) {
153 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
154 $hdd->{hddName} !~ /$self->{option_results}->{filter_name}/) {
155 $self->{output}->output_add(long_msg => "skipping disk '" . $hdd->{hddName} . "'.", debug => 1);
156 next;
157 }
158
159 $hdd->{capacity} *= 1024 * 1024;
160 $hdd->{freeSpace} *= 1024 * 1024;
161 $self->{disks}->{ $hdd->{hddName} } = {
162 status => { name => $hdd->{hddName}, status => $hdd->{status} },
163 space => {
164 total => $hdd->{capacity},
165 used => $hdd->{capacity} - $hdd->{freeSpace},
166 free => $hdd->{freeSpace},
167 prct_used => 100 - ($hdd->{freeSpace} * 100 / $hdd->{capacity}),
168 prct_free => ($hdd->{freeSpace} * 100 / $hdd->{capacity})
169 }
170 };
171
172 my $smart = $options{custom}->request_api(endpoint => '/ISAPI/ContentMgmt/Storage/hdd/' . $hdd->{id} . '/SMARTTest/status');
173 $self->{disks}->{ $hdd->{hddName} }->{temperature} = { value => $smart->{temprature} };
174 }
175 }
176
177 1;
178
179 __END__
180
181 =head1 MODE
182
183 Check disks.
184
185 =over 8
186
187 =item B<--filter-name>
188
189 Filter disks by name (can be a regexp).
190
191 =item B<--unknown-status>
192
193 Define the conditions to match for the status to be UNKNOWN.
194 You can use the following variables: %{status}, %{name}
195
196 =item B<--warning-status>
197
198 Define the conditions to match for the status to be WARNING (default: '%{status} =~ /reparing/i').
199 You can use the following variables: %{status}, %{name}
200
201 =item B<--critical-status>
202
203 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /error|smartFailed/i').
204 You can use the following variables: %{status}, %{name}
205
206 =item B<--warning-*> B<--critical-*>
207
208 Thresholds.
209 Can be: 'space-usage', 'space-usage-free', 'space-usage-prct',
210 'temperature'.
211
212 =back
213
214 =cut