Debian Codegraph @

src/cloud/prometheus/exporters/nodeexporter/mode/memory.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::memory;
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 = 'used';
32 my $value_perf = $self->{result_values}->{used};
33
34 my %total_options = ();
35 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
36 $total_options{total} = $self->{result_values}->{total};
37 $total_options{cast_int} = 1;
38 }
39
40 $self->{output}->perfdata_add(
41 label => $label, unit => 'B',
42 nlabel => 'memory.usage.bytes',
43 value => $value_perf,
44 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
45 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
46 min => 0, max => $self->{result_values}->{total},
47 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
48 );
49 }
50
51 sub custom_usage_threshold {
52 my ($self, %options) = @_;
53
54 my ($exit, $threshold_value);
55 $threshold_value = $self->{result_values}->{used};
56 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
57 $threshold_value = $self->{result_values}->{prct_used};
58 }
59 $exit = $self->{perfdata}->threshold_check(value => $threshold_value,
60 threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
61 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
62 return $exit;
63 }
64
65 sub custom_usage_output {
66 my ($self, %options) = @_;
67
68 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
69 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
70 my $msg = sprintf("Ram Total: %s, Used (-buffers/cache): %s (%.2f%%)",
71 $total_size_value . " " . $total_size_unit,
72 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used});
73 return $msg;
74 }
75
76 sub custom_usage_calc {
77 my ($self, %options) = @_;
78
79 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
80 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
81 $self->{result_values}->{available} = $options{new_datas}->{$self->{instance} . '_available'};
82 $self->{result_values}->{buffer} = $options{new_datas}->{$self->{instance} . '_buffer'};
83 $self->{result_values}->{cached} = $options{new_datas}->{$self->{instance} . '_cached'};
84 $self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{available} - $self->{result_values}->{buffer} - $self->{result_values}->{cached};
85 $self->{result_values}->{prct_used} = ($self->{result_values}->{total} > 0) ? $self->{result_values}->{used} * 100 / $self->{result_values}->{total} : 0;
86
87 return 0;
88 }
89
90 sub set_counters {
91 my ($self, %options) = @_;
92
93 $self->{maps_counters_type} = [
94 { name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes memory usage are ok' },
95 ];
96
97 $self->{maps_counters}->{nodes} = [
98 { label => 'usage', set => {
99 key_values => [ { name => 'total' }, { name => 'available' },
100 { name => 'buffer' }, { name => 'cached' }, { name => 'display' } ],
101 closure_custom_calc => $self->can('custom_usage_calc'),
102 closure_custom_output => $self->can('custom_usage_output'),
103 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
104 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
105 }
106 },
107 { label => 'buffer', nlabel => 'buffer.usage.bytes', set => {
108 key_values => [ { name => 'buffer' }, { name => 'display' } ],
109 output_template => 'Buffer: %.2f %s',
110 output_change_bytes => 1,
111 perfdatas => [
112 { label => 'buffer', value => 'buffer', template => '%s',
113 min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' },
114 ],
115 }
116 },
117 { label => 'cached', nlabel => 'cache.usage.bytes', set => {
118 key_values => [ { name => 'cached' }, { name => 'display' } ],
119 output_template => 'Cached: %.2f %s',
120 output_change_bytes => 1,
121 perfdatas => [
122 { label => 'cached', value => 'cached', template => '%s',
123 min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' },
124 ],
125 }
126 },
127 ];
128 }
129
130 sub prefix_nodes_output {
131 my ($self, %options) = @_;
132
133 return "Node '" . $options{instance_value}->{display} . "' ";
134 }
135
136 sub new {
137 my ($class, %options) = @_;
138 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
139 bless $self, $class;
140
141 $options{options}->add_options(arguments => {
142 "instance:s" => { name => 'instance', default => 'instance=~".*"' },
143 "units:s" => { name => 'units', default => '%' },
144 "extra-filter:s@" => { name => 'extra_filter' },
145 "metric-overload:s@" => { name => 'metric_overload' },
146 });
147
148 return $self;
149 }
150
151 sub check_options {
152 my ($self, %options) = @_;
153 $self->SUPER::check_options(%options);
154
155 $self->{metrics} = {
156 'total' => "^node_memory_MemTotal.*",
157 'available' => "^node_memory_MemFree.*",
158 'cached' => "^node_memory_Cached.*",
159 'buffer' => "^node_memory_Buffers.*",
160 };
161 foreach my $metric (@{$self->{option_results}->{metric_overload}}) {
162 next if ($metric !~ /(.*),(.*)/);
163 $self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1}));
164 }
165
166 $self->{labels} = {};
167 foreach my $label (('instance')) {
168 if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) {
169 $self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter.");
170 $self->{output}->option_exit();
171 }
172 $self->{labels}->{$label} = $1;
173 }
174
175 $self->{extra_filter} = '';
176 foreach my $filter (@{$self->{option_results}->{extra_filter}}) {
177 $self->{extra_filter} .= ',' . $filter;
178 }
179 }
180
181 sub manage_selection {
182 my ($self, %options) = @_;
183
184 $self->{nodes} = {};
185
186 my $results = $options{custom}->query(
187 queries => [
188 'label_replace({__name__=~"' . $self->{metrics}->{total} . '",' .
189 $self->{option_results}->{instance} .
190 $self->{extra_filter} . '}, "__name__", "total", "", "")',
191 'label_replace({__name__=~"' . $self->{metrics}->{available} . '",' .
192 $self->{option_results}->{instance} .
193 $self->{extra_filter} . '}, "__name__", "available", "", "")',
194 'label_replace({__name__=~"' . $self->{metrics}->{cached} . '",' .
195 $self->{option_results}->{instance} .
196 $self->{extra_filter} . '}, "__name__", "cached", "", "")',
197 'label_replace({__name__=~"' . $self->{metrics}->{buffer} . '",' .
198 $self->{option_results}->{instance} .
199 $self->{extra_filter} . '}, "__name__", "buffer", "", "")'
200 ]
201 );
202
203 foreach my $result (@{$results}) {
204 $self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{display} = $result->{metric}->{$self->{labels}->{instance}};
205 $self->{nodes}->{$result->{metric}->{$self->{labels}->{instance}}}->{$result->{metric}->{__name__}} = ${$result->{value}}[1];
206 }
207
208 if (scalar(keys %{$self->{nodes}}) <= 0) {
209 $self->{output}->add_option_msg(short_msg => "No nodes found.");
210 $self->{output}->option_exit();
211 }
212 }
213
214 1;
215
216 __END__
217
218 =head1 MODE
219
220 Check memory usage.
221
222 =over 8
223
224 =item B<--instance>
225
226 Filter on a specific instance (must be a PromQL filter, Default: 'instance=~".*"')
227
228 =item B<--warning-*>
229
230 Warning threshold.
231 Can be: 'usage', 'buffer', 'cached'.
232
233 =item B<--critical-*>
234
235 Critical threshold.
236 Can be: 'usage', 'buffer', 'cached'.
237
238 =item B<--units>
239
240 Units of thresholds (default: '%') ('%', 'B').
241
242 =item B<--extra-filter>
243
244 Add a PromQL filter (can be defined multiple times)
245
246 Example : --extra-filter='name=~".*pretty.*"'
247
248 =item B<--metric-overload>
249
250 Overload default metrics name (can be defined multiple times)
251
252 Example : --metric-overload='metric,^my_metric_name$'
253
254 By default, 'node_memory_MemFree' node's metric will be used for 'available' metric as it is
255 more commonly used for now. The best being to use 'node_memory_MemAvailable' in the future.
256
257 Default :
258
259 - total: ^node_memory_MemTotal.*
260 - available: ^node_memory_MemFree.*
261 - cached: ^node_memory_Cached.*
262 - buffer: ^node_memory_Buffers.*
263
264 =item B<--filter-counters>
265
266 Only display some counters (regexp can be used).
267 Example: --filter-counters='usage'
268
269 =back
270
271 =cut