Debian Codegraph @

src/network/arista/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 network::arista::snmp::mode::memory;
22
23 use base qw(snmp_standard::mode::storage);
24
25 use strict;
26 use warnings;
27
28 sub custom_usage_output {
29 my ($self, %options) = @_;
30
31 return sprintf(
32 'Ram Total: %s %s Used (-buffers/cache): %s %s (%.2f%%) Free: %s %s (%.2f%%)',
33 $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}),
34 $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}),
35 $self->{result_values}->{prct_used},
36 $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}),
37 $self->{result_values}->{prct_free}
38 );
39 }
40
41 sub set_counters {
42 my ($self, %options) = @_;
43
44 $self->{maps_counters_type} = [
45 { name => 'ram', type => 0, skipped_code => { -10 => 1 } }
46 ];
47
48 $self->{maps_counters}->{ram} = [
49 { label => 'usage', nlabel => 'memory.usage.bytes', set => {
50 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
51 closure_custom_output => $self->can('custom_usage_output'),
52 perfdatas => [
53 { value => 'used', template => '%d', min => 0, max => 'total',
54 unit => 'B', cast_int => 1 }
55 ]
56 }
57 },
58 { label => 'usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => {
59 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
60 closure_custom_output => $self->can('custom_usage_output'),
61 perfdatas => [
62 { value => 'free', template => '%d', min => 0, max => 'total',
63 unit => 'B', cast_int => 1 }
64 ]
65 }
66 },
67 { label => 'usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => {
68 key_values => [ { name => 'prct_used' } ],
69 output_template => 'Ram Used : %.2f %%',
70 perfdatas => [
71 { value => 'prct_used', template => '%.2f', min => 0, max => 100,
72 unit => '%' }
73 ]
74 }
75 },
76 { label => 'buffer', nlabel => 'memory.buffer.bytes', set => {
77 key_values => [ { name => 'buffer' } ],
78 output_template => 'Buffer: %s %s',
79 output_change_bytes => 1,
80 perfdatas => [
81 { value => 'buffer', template => '%d',
82 min => 0, unit => 'B' }
83 ]
84 }
85 },
86 { label => 'cached', nlabel => 'memory.cached.bytes', set => {
87 key_values => [ { name => 'cached' } ],
88 output_template => 'Cached: %s %s',
89 output_change_bytes => 1,
90 perfdatas => [
91 { value => 'cached', template => '%d',
92 min => 0, unit => 'B' }
93 ]
94 }
95 }
96 ];
97 }
98
99 sub new {
100 my ($class, %options) = @_;
101 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
102 bless $self, $class;
103
104 return $self;
105 }
106
107 my $mapping = {
108 hrStorageDescr => { oid => '.1.3.6.1.2.1.25.2.3.1.3' },
109 hrStorageAllocationUnits => { oid => '.1.3.6.1.2.1.25.2.3.1.4' },
110 hrStorageSize => { oid => '.1.3.6.1.2.1.25.2.3.1.5' },
111 hrStorageUsed => { oid => '.1.3.6.1.2.1.25.2.3.1.6' }
112 };
113
114 sub manage_selection {
115 my ($self, %options) = @_;
116
117 my $storage_type_ram = '.1.3.6.1.2.1.25.2.1.2';
118 my $oid_hrstoragetype = '.1.3.6.1.2.1.25.2.3.1.2';
119
120 my $snmp_result = $options{snmp}->get_table(oid => $oid_hrstoragetype, nothing_quit => 1);
121 my $storages = [];
122 foreach (keys %$snmp_result) {
123 next if ($snmp_result->{$_} ne $storage_type_ram);
124 /^$oid_hrstoragetype\.(.*)$/;
125 push @$storages, $1;
126 }
127
128 $options{snmp}->load(
129 oids => [map($_->{oid}, values(%$mapping))],
130 instances => $storages,
131 nothing_quit => 1
132 );
133 $snmp_result = $options{snmp}->get_leef();
134
135 my ($total, $used, $cached, $buffer, $unavailable);
136 #.1.3.6.1.2.1.25.2.3.1.3.1 = STRING: RAM
137 #.1.3.6.1.2.1.25.2.3.1.3.2 = STRING: RAM (Buffers)
138 #.1.3.6.1.2.1.25.2.3.1.3.3 = STRING: RAM (Cache)
139 #.1.3.6.1.2.1.25.2.3.1.3.100 = STRING: RAM (Unavailable)
140 foreach (@$storages) {
141 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_);
142 my $current = $result->{hrStorageUsed} * $result->{hrStorageAllocationUnits};
143 next if ($current < 0);
144
145 if ($result->{hrStorageDescr} =~ /RAM.*?Cache/i) {
146 $cached = $current;
147 } elsif ($result->{hrStorageDescr} =~ /RAM.*?Buffers/i) {
148 $buffer = $current;
149 } elsif ($result->{hrStorageDescr} =~ /RAM.*?Unavailable/i) {
150 $unavailable = $current;
151 } elsif ($result->{hrStorageDescr} =~ /RAM/i) {
152 $used = $current;
153 $total = $result->{hrStorageSize} * $result->{hrStorageAllocationUnits};
154 }
155 }
156
157 if (defined($unavailable)) {
158 $used = $unavailable;
159 } else {
160 $used -= (defined($cached) ? $cached : 0) - (defined($buffer) ? $buffer : 0);
161 }
162 $self->{ram} = {
163 total => $total,
164 cached => $cached,
165 buffer => $buffer,
166 used => $used,
167 free => $total - $used,
168 prct_used => $used * 100 / $total,
169 prct_free => 100 - ($used * 100 / $total)
170 };
171 }
172
173 1;
174
175 __END__
176
177 =head1 MODE
178
179 Check memory.
180
181 =over 8
182
183 =item B<--warning-*> B<--critical-*>
184
185 Thresholds.
186 Can be: 'usage' (B), 'usage-free' (B), 'usage-prct' (%),
187 'buffer' (B), 'cached' (B).
188
189 =back
190
191 =cut