Debian Codegraph @

src/centreon/common/huawei/standard/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::huawei::standard::snmp::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 if ($self->{result_values}->{total} > 0) {
32 $self->{output}->perfdata_add(
33 label => 'used', unit => 'B',
34 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
35 value => $self->{result_values}->{used},
36 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
37 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
38 min => 0,
39 max => $self->{result_values}->{total}
40 );
41 } else {
42 $self->{output}->perfdata_add(
43 label => 'used', unit => '%',
44 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
45 value => $self->{result_values}->{prct_used},
46 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
47 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
48 min => 0,
49 max => 100
50 );
51 }
52 }
53
54 sub custom_usage_threshold {
55 my ($self, %options) = @_;
56
57 my $exit = $self->{perfdata}->threshold_check(
58 value => $self->{result_values}->{prct_used},
59 threshold => [
60 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
61 { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' }
62 ]
63 );
64 return $exit;
65 }
66
67 sub custom_usage_output {
68 my ($self, %options) = @_;
69
70 my $msg = 'Used: ' . $self->{result_values}->{prct_used} . '%';
71 if ($self->{result_values}->{total} > 0) {
72 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
73 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
74 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
75
76 $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
77 $total_size_value . " " . $total_size_unit,
78 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
79 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
80 );
81 }
82
83 return $msg;
84 }
85
86 sub custom_usage_calc {
87 my ($self, %options) = @_;
88
89 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
90 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
91 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
92 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_total'} - $self->{result_values}->{free};
93
94 if ($self->{result_values}->{total} > 0) {
95 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
96 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
97 } else {
98 $self->{result_values}->{prct_used} = $options{new_datas}->{$self->{instance} . '_used_prct'};
99 }
100
101 return 0;
102 }
103
104 sub set_counters {
105 my ($self, %options) = @_;
106
107 $self->{maps_counters_type} = [
108 { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory usages are ok' }
109 ];
110
111 $self->{maps_counters}->{memory} = [
112 { label => 'usage', set => {
113 key_values => [ { name => 'display' }, { name => 'used_prct' }, { name => 'free' }, { name => 'total' } ],
114 closure_custom_calc => $self->can('custom_usage_calc'),
115 closure_custom_output => $self->can('custom_usage_output'),
116 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
117 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
118 }
119 },
120 ];
121 }
122
123 sub prefix_memory_output {
124 my ($self, %options) = @_;
125
126 return "Memory '" . $options{instance_value}->{display} . "' ";
127 }
128
129 sub new {
130 my ($class, %options) = @_;
131 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
132 bless $self, $class;
133
134 $options{options}->add_options(arguments => {});
135
136 return $self;
137 }
138
139 my $oid_hwEntityMemUsage = '.1.3.6.1.4.1.2011.5.25.31.1.1.1.1.7'; # prct
140 my $oid_hwMemoryDevEntry = '.1.3.6.1.4.1.2011.6.3.5.1.1';
141 my $oid_hwResOccupancy = '.1.3.6.1.4.1.2011.6.3.17.1.1.3';
142 my $map_type = { 1 => 'memory', 2 => 'messageUnits', 3 => 'cpu' };
143
144 my $mapping = {
145 hwMemoryDevSize => { oid => '.1.3.6.1.4.1.2011.6.3.5.1.1.2' }, # B
146 hwMemoryDevFree => { oid => '.1.3.6.1.4.1.2011.6.3.5.1.1.3' }, # B
147 };
148
149 sub manage_selection {
150 my ($self, %options) = @_;
151
152 $self->{memory} = {};
153 my $results = $options{snmp}->get_multiple_table(oids => [
154 { oid => $oid_hwEntityMemUsage },
155 { oid => $oid_hwMemoryDevEntry, end => $mapping->{hwMemoryDevFree}->{oid} },
156 { oid => $oid_hwResOccupancy },
157 ], nothing_quit => 1);
158
159 my $num = 1;
160 if (defined($results->{$oid_hwMemoryDevEntry}) && scalar(keys %{$results->{$oid_hwMemoryDevEntry}}) > 0) {
161 foreach (sort keys %{$results->{$oid_hwMemoryDevEntry}}) {
162 next if (!/^$mapping->{hwMemoryDevFree}->{oid}\.(.*)$/);
163 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $results->{$oid_hwMemoryDevEntry}, instance => $1);
164 $self->{memory}->{$num} = { display => $num, used_prct => -1, free => $result->{hwMemoryDevFree}, total => $result->{hwMemoryDevSize} };
165 $num++;
166 }
167 } elsif (defined($results->{$oid_hwEntityMemUsage}) && scalar(keys %{$results->{$oid_hwEntityMemUsage}}) > 0) {
168 foreach (sort keys %{$results->{$oid_hwEntityMemUsage}}) {
169 $self->{memory}->{$num} = { display => $num, used_prct => $results->{$oid_hwEntityMemUsage}->{$_}, free => 0, total => 0 };
170 $num++;
171 }
172 } else {
173 foreach (sort keys %{$results->{$oid_hwResOccupancy}}) {
174 /\.([0-9]*?)$/;
175 next if (!defined($map_type->{$1}) || $map_type->{$1} ne 'memory');
176 $self->{memory}->{$num} = { display => $num, used_prct => $results->{$oid_hwResOccupancy}->{$_}, free => 0, total => 0 };
177 $num++;
178 }
179 }
180 }
181
182 1;
183
184 __END__
185
186 =head1 MODE
187
188 Check memory usage.
189
190 =over 8
191
192 =item B<--warning-usage>
193
194 Warning threshold (in percent).
195
196 =item B<--critical-usage>
197
198 Critical threshold (in percent).
199
200 =back
201
202 =cut