Debian Codegraph @

src/centreon/common/h3c/snmp/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 centreon::common::h3c::snmp::mode::memory;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::statefile;
28
29 sub custom_usage_perfdata {
30 my ($self, %options) = @_;
31
32 if ($self->{result_values}->{total} == 4294967295) {
33 $self->{output}->perfdata_add(
34 label => 'used', unit => '%',
35 nlabel => 'memory.usage.percentage',
36 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
37 value => $self->{result_values}->{prct_used},
38 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
39 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
40 min => 0, max => 100
41 );
42 } else {
43 $self->{output}->perfdata_add(
44 label => 'used', unit => 'B',
45 nlabel => $self->{nlabel},
46 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
47 value => int($self->{result_values}->{used}),
48 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
49 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
50 min => 0, max => $self->{result_values}->{total}
51 );
52 }
53 }
54
55 sub custom_usage_threshold {
56 my ($self, %options) = @_;
57
58 my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
59 return $exit;
60 }
61
62 sub custom_usage_output {
63 my ($self, %options) = @_;
64
65 my $msg;
66 if ($self->{result_values}->{total} == 4294967295) {
67 $msg = sprintf("Used: %.2f%% Free: %.2f%%",
68 $self->{result_values}->{prct_used},
69 $self->{result_values}->{prct_free});
70 } else {
71 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
72 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
73 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
74 $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
75 $total_size_value . " " . $total_size_unit,
76 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
77 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
78 }
79 return $msg;
80 }
81
82 sub custom_usage_calc {
83 my ($self, %options) = @_;
84
85 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
86 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
87 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_total'} * $options{new_datas}->{$self->{instance} . '_used_prct'} / 100;
88 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_total'} - $self->{result_values}->{used};
89 $self->{result_values}->{prct_free} = 100 - $options{new_datas}->{$self->{instance} . '_used_prct'};
90 $self->{result_values}->{prct_used} = $options{new_datas}->{$self->{instance} . '_used_prct'};
91 return 0;
92 }
93
94 sub set_counters {
95 my ($self, %options) = @_;
96
97 $self->{maps_counters_type} = [
98 { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory usages are ok' }
99 ];
100
101 $self->{maps_counters}->{memory} = [
102 { label => 'usage', nlabel => 'memory.usage.bytes', set => {
103 key_values => [ { name => 'display' }, { name => 'used_prct' }, { name => 'total' } ],
104 closure_custom_calc => $self->can('custom_usage_calc'),
105 closure_custom_output => $self->can('custom_usage_output'),
106 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
107 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
108 }
109 },
110 ];
111 }
112
113 sub prefix_memory_output {
114 my ($self, %options) = @_;
115
116 return "Memory '" . $options{instance_value}->{display} . "' ";
117 }
118
119 sub new {
120 my ($class, %options) = @_;
121 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
122 bless $self, $class;
123
124 $options{options}->add_options(arguments => {
125 "reload-cache-time:s" => { name => 'reload_cache_time', default => 180 },
126 "display-entity-name" => { name => 'display_entity_name' },
127 });
128
129 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
130 return $self;
131 }
132
133 sub check_options {
134 my ($self, %options) = @_;
135 $self->SUPER::check_options(%options);
136
137 $self->{statefile_cache}->check_options(%options);
138 }
139
140 my $oid_entPhysicalEntry = '.1.3.6.1.2.1.47.1.1.1.1';
141 my $oid_entPhysicalDescr = '.1.3.6.1.2.1.47.1.1.1.1.2';
142 my $oid_entPhysicalContainedIn = '.1.3.6.1.2.1.47.1.1.1.1.4';
143 my $oid_entPhysicalClass = '.1.3.6.1.2.1.47.1.1.1.1.5';
144 my $oid_entPhysicalName = '.1.3.6.1.2.1.47.1.1.1.1.7';
145
146 sub check_cache {
147 my ($self, %options) = @_;
148
149 $self->{hostname} = $self->{snmp}->get_hostname();
150 $self->{snmp_port} = $self->{snmp}->get_port();
151
152 # init cache file
153 $self->{write_cache} = 0;
154 my $has_cache_file = $self->{statefile_cache}->read(statefile => 'cache_h3c_entity_' . $self->{hostname} . '_' . $self->{snmp_port});
155 my $timestamp_cache = $self->{statefile_cache}->get(name => 'last_timestamp');
156 if ($has_cache_file == 0 ||
157 !defined($timestamp_cache) || ((time() - $timestamp_cache) > (($self->{option_results}->{reload_cache_time}) * 60))) {
158 push @{$self->{snmp_request}}, { oid => $oid_entPhysicalEntry, start => $oid_entPhysicalDescr, end => $oid_entPhysicalName };
159 $self->{write_cache} = 1;
160 }
161 }
162
163 sub write_cache {
164 my ($self, %options) = @_;
165
166 if ($self->{write_cache} == 1) {
167 my $datas = {};
168 $datas->{last_timestamp} = time();
169 $datas->{oids} = $self->{results}->{$oid_entPhysicalEntry};
170 $self->{statefile_cache}->write(data => $datas);
171 } else {
172 $self->{results}->{$oid_entPhysicalEntry} = $self->{statefile_cache}->get(name => 'oids');
173 }
174 }
175
176 sub get_long_name {
177 my ($self, %options) = @_;
178
179 my @names = ($self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $options{instance}});
180 my %loop = ($options{instance} => 1);
181 my $child = $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalContainedIn . '.' . $options{instance}};
182 while (1) {
183 last if (!defined($child) || defined($loop{$child}) || !defined($self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $child}));
184
185 unshift @names, $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalDescr . '.' . $child};
186 $loop{$child} = 1;
187 $child = $self->{results}->{$oid_entPhysicalEntry}->{$oid_entPhysicalContainedIn . '.' . $child};
188 }
189
190 return join(' > ', @names);
191 }
192
193 sub manage_selection {
194 my ($self, %options) = @_;
195
196 $self->{snmp} = $options{snmp};
197 my $oid_h3cEntityExtStateEntry = '.1.3.6.1.4.1.2011.10.2.6.1.1.1.1';
198 my $oid_hh3cEntityExtStateEntry = '.1.3.6.1.4.1.25506.2.6.1.1.1.1';
199 my $oid_hh3cEntityExtMemUsage = '.1.3.6.1.4.1.25506.2.6.1.1.1.1.8';
200 my $oid_hh3cEntityExtMemSize = '.1.3.6.1.4.1.25506.2.6.1.1.1.1.10';
201 my $oid_h3cEntityExtMemUsage = '.1.3.6.1.4.1.2011.10.2.6.1.1.1.1.8';
202 my $oid_h3cEntityExtMemSize = '.1.3.6.1.4.1.2011.10.2.6.1.1.1.1.10';
203
204 $self->{snmp_request} = [
205 { oid => $oid_hh3cEntityExtMemUsage },
206 { oid => $oid_hh3cEntityExtMemSize },
207 { oid => $oid_h3cEntityExtMemUsage },
208 { oid => $oid_h3cEntityExtMemSize },
209 ];
210 $self->check_cache() if (defined($self->{option_results}->{display_entity_name}));
211
212 $self->{results} = $self->{snmp}->get_multiple_table(oids => $self->{snmp_request},
213 nothing_quit => 1);
214 $self->write_cache() if (defined($self->{option_results}->{display_entity_name}));
215 $self->{branch} = $oid_hh3cEntityExtStateEntry;
216 if (defined($self->{results}->{$oid_h3cEntityExtMemUsage}) && scalar(keys %{$self->{results}->{$oid_h3cEntityExtMemUsage}}) > 0) {
217 $self->{branch} = $oid_h3cEntityExtStateEntry;
218 }
219
220 my $mapping = {
221 EntityExtMemUsage => { oid => $self->{branch} . '.8' },
222 };
223 my $mapping2 = {
224 EntityExtMemSize => { oid => $self->{branch} . '.10' },
225 };
226
227 $self->{memory} = {};
228 foreach my $oid (keys %{$self->{results}->{$self->{branch} . '.8'}}) {
229 next if ($oid !~ /^$self->{branch}\.8\.(\d+)/);
230 my $instance = $1;
231 my $result = $self->{snmp}->map_instance(mapping => $mapping, results => $self->{results}->{$self->{branch} . '.8'}, instance => $instance);
232 my $result2 = $self->{snmp}->map_instance(mapping => $mapping2, results => $self->{results}->{$self->{branch} . '.10'}, instance => $instance);
233
234 if (defined($result2->{EntityExtMemSize}) && $result2->{EntityExtMemSize} > 0) {
235 my $name = '-';
236
237 if (defined($self->{option_results}->{display_entity_name})) {
238 $name = $self->get_long_name(instance => $instance);
239 }
240 $self->{memory}->{$instance} = {
241 display => $instance, name => $name,
242 used_prct => $result->{EntityExtMemUsage},
243 total => $result2->{EntityExtMemSize}
244 };
245 }
246 }
247
248 if (scalar(keys %{$self->{memory}}) <= 0) {
249 $self->{output}->add_option_msg(short_msg => "No entry found.");
250 $self->{output}->option_exit();
251 }
252 }
253
254 1;
255
256 __END__
257
258 =head1 MODE
259
260 Check memory usages.
261
262 =over 8
263
264 =item B<--warning-usage>
265
266 Warning threshold (in percent).
267
268 =item B<--critical-usage>
269
270 Critical threshold (in percent).
271
272 =item B<--reload-cache-time>
273
274 Time in seconds before reloading cache file (default: 180).
275
276 =item B<--display-entity-name>
277
278 Display entity name of the component. A cache file will be used.
279
280 =back
281
282 =cut