Debian Codegraph @

src/network/hirschmann/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 network::hirschmann::standard::snmp::mode::memory;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_ram_usage_output {
29 my ($self, %options) = @_;
30
31 return sprintf(
32 'Memory total: %s %s used: %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 => 'memory-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_ram_usage_output'),
52 perfdatas => [
53 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
54 ]
55 }
56 },
57 { label => 'memory-usage-free', display_ok => 0, nlabel => 'memory.free.bytes', set => {
58 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ],
59 closure_custom_output => $self->can('custom_ram_usage_output'),
60 perfdatas => [
61 { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 }
62 ]
63 }
64 },
65 { label => 'memory-usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => {
66 key_values => [ { name => 'prct_used' }, { name => 'used' }, { name => 'free' }, { name => 'prct_free' }, { name => 'total' } ],
67 closure_custom_output => $self->can('custom_ram_usage_output'),
68 perfdatas => [
69 { template => '%.2f', min => 0, max => 100, unit => '%' }
70 ]
71 }
72 }
73 ];
74 }
75
76 sub new {
77 my ($class, %options) = @_;
78 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
79 bless $self, $class;
80
81 return $self;
82 }
83
84 my $map_enable = {
85 1 => 'enable', 2 => 'disable'
86 };
87
88 my $mapping = {
89 hios => {
90 measure_enable => { oid => '.1.3.6.1.4.1.248.11.22.1.8.1', map => $map_enable }, # hm2DiagEnableMeasurement
91 ram_used => { oid => '.1.3.6.1.4.1.248.11.22.1.8.11.1' }, # hm2DiagMemoryRamAllocated
92 ram_free => { oid => '.1.3.6.1.4.1.248.11.22.1.8.11.2' } # hm2DiagMemoryRamFree
93 },
94 classic => {
95 measure_enable => { oid => '.1.3.6.1.4.1.248.14.2.15.1', map => $map_enable }, # hmEnableMeasurement
96 ram_used => { oid => '.1.3.6.1.4.1.248.14.2.15.3.1' }, # hmMemoryAllocated
97 ram_free => { oid => '.1.3.6.1.4.1.248.14.2.15.3.2' } # hmMemoryFree
98 }
99 };
100
101 sub check_memory {
102 my ($self, %options) = @_;
103
104 my $result = $options{snmp}->map_instance(mapping => $mapping->{ $options{type} }, results => $options{snmp_result}, instance => 0);
105 return 0 if (!defined($result->{ram_free}));
106
107 if ($result->{measure_enable} eq 'disable') {
108 $self->{output}->add_option_msg(short_msg => 'resource measurement is disabled');
109 $self->{output}->option_exit();
110 }
111
112 $result->{ram_used} *= 1024;
113 $result->{ram_free} *= 1024;
114 $self->{ram} = {
115 total => $result->{ram_used} + $result->{ram_free},
116 used => $result->{ram_used},
117 free => $result->{ram_free},
118 prct_used => $result->{ram_used} * 100 / ($result->{ram_used} + $result->{ram_free}),
119 prct_free => $result->{ram_free} * 100 / ($result->{ram_used} + $result->{ram_free})
120 };
121 }
122
123 sub manage_selection {
124 my ($self, %options) = @_;
125
126 my $snmp_result = $options{snmp}->get_leef(
127 oids => [ map($_->{oid} . '.0', values(%{$mapping->{hios}}), values(%{$mapping->{classic}})) ],
128 nothing_quit => 1
129 );
130 if ($self->check_memory(snmp => $options{snmp}, type => 'hios', snmp_result => $snmp_result) == 0) {
131 $self->check_memory(snmp => $options{snmp}, type => 'classic', snmp_result => $snmp_result);
132 }
133 }
134
135 1;
136
137 __END__
138
139 =head1 MODE
140
141 Check memory.
142
143 =over 8
144
145 =item B<--warning-*> B<--critical-*>
146
147 Thresholds.
148 Can be: 'memory-usage' (B), 'memory-usage-free' (B), 'memory-usage-prct' (%).
149
150 =back
151
152 =cut