Debian Codegraph @

src/cloud/prometheus/exporters/nodeexporter/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 cloud::prometheus::exporters::nodeexporter::mode::storage;
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 $nlabel = 'storage.space.usage.bytes';
32 my $value_perf = $self->{result_values}->{used};
33
34 if (defined($self->{instance_mode}->{option_results}->{free})) {
35 $nlabel = 'storage.space.free.bytes';
36 $value_perf = $self->{result_values}->{free};
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 nlabel => $nlabel,
46 unit => 'B',
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 instances => [$self->{result_values}->{instance}, $self->{result_values}->{mountpoint}]
52 );
53 }
54
55 sub custom_usage_threshold {
56 my ($self, %options) = @_;
57
58 my ($exit, $threshold_value);
59 $threshold_value = $self->{result_values}->{used};
60 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
61 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
62 $threshold_value = $self->{result_values}->{prct_used};
63 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
64 }
65 $exit = $self->{perfdata}->threshold_check(
66 value => $threshold_value,
67 threshold => [
68 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
69 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' }
70 ]
71 );
72 return $exit;
73 }
74
75 sub custom_usage_output {
76 my ($self, %options) = @_;
77
78 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
79 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
80 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
81 return sprintf(
82 "Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
83 $total_size_value . " " . $total_size_unit,
84 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
85 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
86 );
87 }
88
89 sub custom_usage_calc {
90 my ($self, %options) = @_;
91
92 $self->{result_values}->{instance} = $options{new_datas}->{$self->{instance} . '_instance'};
93 $self->{result_values}->{mountpoint} = $options{new_datas}->{$self->{instance} . '_mountpoint'};
94 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_size'};
95 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
96 $self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free};
97 $self->{result_values}->{prct_used} = ($self->{result_values}->{total} > 0) ? $self->{result_values}->{used} * 100 / $self->{result_values}->{total} : 0;
98 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
99
100 # limit to 100. Better output.
101 if ($self->{result_values}->{prct_used} > 100) {
102 $self->{result_values}->{free} = 0;
103 $self->{result_values}->{prct_used} = 100;
104 $self->{result_values}->{prct_free} = 0;
105 }
106
107 return 0;
108 }
109
110 sub prefix_node_output {
111 my ($self, %options) = @_;
112
113 return "Node '" . $options{instance_value}->{instance} . "' ";
114 }
115
116 sub node_long_output {
117 my ($self, %options) = @_;
118
119 return "Checking node '" . $options{instance_value}->{instance} . "'";
120 }
121
122 sub prefix_storage_output {
123 my ($self, %options) = @_;
124
125 return "Storage '" . $options{instance_value}->{mountpoint} . "' ";
126 }
127
128 sub set_counters {
129 my ($self, %options) = @_;
130
131 $self->{maps_counters_type} = [
132 { name => 'nodes', type => 3, cb_prefix_output => 'prefix_node_output', cb_long_output => 'node_long_output',
133 message_multiple => 'All nodes storages usage are ok', indent_long_output => ' ',
134 group => [
135 { name => 'storage', display_long => 1, cb_prefix_output => 'prefix_storage_output',
136 message_multiple => 'All storages usage are ok', type => 1, skipped_code => { -10 => 1 } }
137 ]
138 }
139 ];
140
141 $self->{maps_counters}->{storage} = [
142 { label => 'usage', set => {
143 key_values => [ { name => 'free' }, { name => 'size' }, { name => 'instance' }, { name => 'mountpoint' } ],
144 closure_custom_calc => $self->can('custom_usage_calc'),
145 closure_custom_output => $self->can('custom_usage_output'),
146 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
147 closure_custom_threshold_check => $self->can('custom_usage_threshold')
148 }
149 }
150 ];
151 }
152
153 sub new {
154 my ($class, %options) = @_;
155 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
156 bless $self, $class;
157
158 $options{options}->add_options(arguments => {
159 'instance:s' => { name => 'instance', default => 'instance=~".*"' },
160 'mountpoint:s' => { name => 'mountpoint', default => 'mountpoint=~".*"' },
161 'fstype:s' => { name => 'fstype', default => 'fstype!~"linuxfs|rootfs|tmpfs"' },
162 'units:s' => { name => 'units', default => '%' },
163 'free' => { name => 'free' },
164 'extra-filter:s@' => { name => 'extra_filter' },
165 'metric-overload:s@' => { name => 'metric_overload' }
166 });
167
168 return $self;
169 }
170
171 sub check_options {
172 my ($self, %options) = @_;
173 $self->SUPER::check_options(%options);
174
175 $self->{metrics} = {
176 'free' => "^node_filesystem_free.*",
177 'size' => "^node_filesystem_size.*"
178 };
179 foreach my $metric (@{$self->{option_results}->{metric_overload}}) {
180 next if ($metric !~ /(.*),(.*)/);
181 $self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1}));
182 }
183
184 $self->{labels} = {};
185 foreach my $label (('instance', 'mountpoint', 'fstype')) {
186 if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) {
187 $self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter.");
188 $self->{output}->option_exit();
189 }
190 $self->{labels}->{$label} = $1;
191 }
192
193 $self->{extra_filter} = '';
194 foreach my $filter (@{$self->{option_results}->{extra_filter}}) {
195 $self->{extra_filter} .= ',' . $filter;
196 }
197 }
198
199 sub manage_selection {
200 my ($self, %options) = @_;
201
202 my $results = $options{custom}->query(
203 queries => [
204 'label_replace({__name__=~"' . $self->{metrics}->{free} . '",' .
205 $self->{option_results}->{instance} . ',' .
206 $self->{option_results}->{mountpoint} . ',' .
207 $self->{option_results}->{fstype} .
208 $self->{extra_filter} . '}, "__name__", "free", "", "")',
209 'label_replace({__name__=~"' . $self->{metrics}->{size} . '",' .
210 $self->{option_results}->{instance} . ',' .
211 $self->{option_results}->{mountpoint} . ',' .
212 $self->{option_results}->{fstype} .
213 $self->{extra_filter} . '}, "__name__", "size", "", "")'
214 ]
215 );
216
217 $self->{nodes} = {};
218 foreach my $result (@$results) {
219 if (!defined($self->{nodes}->{ $result->{metric}->{ $self->{labels}->{instance} } })) {
220 $self->{nodes}->{ $result->{metric}->{ $self->{labels}->{instance} } } = {
221 instance => $result->{metric}->{ $self->{labels}->{instance} },
222 storage => {}
223 };
224 }
225 if (!defined($self->{nodes}->{ $result->{metric}->{ $self->{labels}->{instance} } }->{storage}->{ $result->{metric}->{mountpoint} })) {
226 $self->{nodes}->{ $result->{metric}->{ $self->{labels}->{instance} } }->{storage}->{ $result->{metric}->{mountpoint} } = {
227 instance => $result->{metric}->{ $self->{labels}->{instance} },
228 mountpoint => $result->{metric}->{ $self->{labels}->{mountpoint} }
229 };
230 }
231 $self->{nodes}->{ $result->{metric}->{ $self->{labels}->{instance} } }->{storage}->{ $result->{metric}->{mountpoint} }->{ $result->{metric}->{__name__} } = $result->{value}->[1];
232 }
233
234 if (scalar(keys %{$self->{nodes}}) <= 0) {
235 $self->{output}->add_option_msg(short_msg => 'No nodes found.');
236 $self->{output}->option_exit();
237 }
238 }
239
240 1;
241
242 __END__
243
244 =head1 MODE
245
246 Check storages usage.
247
248 =over 8
249
250 =item B<--instance>
251
252 Filter on a specific instance (must be a PromQL filter, Default: 'instance=~".*"')
253
254 =item B<--mountpoint>
255
256 Filter on a specific mountpoint (must be a PromQL filter, Default: 'mountpoint=~".*"')
257
258 =item B<--fstype>
259
260 Filter on a specific fstype (must be a PromQL filter, Default: 'fstype!~"linuxfs|rootfs|tmpfs"')
261
262 =item B<--units>
263
264 Units of thresholds (default: '%') ('%', 'B').
265
266 =item B<--free>
267
268 Thresholds are on free space left.
269
270 =item B<--warning-usage>
271
272 Warning threshold.
273
274 =item B<--critical-usage>
275
276 Critical threshold.
277
278 =item B<--extra-filter>
279
280 Add a PromQL filter (can be defined multiple times)
281
282 Example : --extra-filter='name=~".*pretty.*"'
283
284 =item B<--metric-overload>
285
286 Overload default metrics name (can be defined multiple times)
287
288 Example : --metric-overload='metric,^my_metric_name$'
289
290 Default :
291
292 - free: ^node_filesystem_free.*
293 - size: ^node_filesystem_size.*
294
295 =back
296
297 =cut