Debian Codegraph @

src/network/extreme/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::extreme::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 $self->{output}->perfdata_add(
32 label => 'used', unit => 'B',
33 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
34 value => $self->{result_values}->{used},
35 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
36 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
37 min => 0, max => $self->{result_values}->{total}
38 );
39 }
40
41 sub custom_usage_threshold {
42 my ($self, %options) = @_;
43
44 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' } ]);
45 return $exit;
46 }
47
48 sub custom_usage_output {
49 my ($self, %options) = @_;
50
51 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
52 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
53 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
54
55 my $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
56 $total_size_value . " " . $total_size_unit,
57 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
58 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
59 return $msg;
60 }
61
62 sub custom_usage_calc {
63 my ($self, %options) = @_;
64
65 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
66 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
67 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_total'} - $options{new_datas}->{$self->{instance} . '_free'};
68 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
69 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
70 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
71 return 0;
72 }
73
74 sub set_counters {
75 my ($self, %options) = @_;
76
77 $self->{maps_counters_type} = [
78 { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory usages are ok' }
79 ];
80
81 $self->{maps_counters}->{memory} = [
82 { label => 'usage', set => {
83 key_values => [ { name => 'display' }, { name => 'free' }, { name => 'total' } ],
84 closure_custom_calc => $self->can('custom_usage_calc'),
85 closure_custom_output => $self->can('custom_usage_output'),
86 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
87 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
88 }
89 },
90 ];
91 }
92
93 sub prefix_memory_output {
94 my ($self, %options) = @_;
95
96 return "Memory '" . $options{instance_value}->{display} . "' ";
97 }
98
99 sub new {
100 my ($class, %options) = @_;
101 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
102 bless $self, $class;
103
104 $options{options}->add_options(arguments => {
105 });
106
107 return $self;
108 }
109
110 my $mapping = {
111 extremeMemoryMonitorSystemFree => { oid => '.1.3.6.1.4.1.1916.1.32.2.2.1.3' },
112 extremeMemoryMonitorSystemTotal => { oid => '.1.3.6.1.4.1.1916.1.32.2.2.1.2' },
113 };
114
115 sub manage_selection {
116 my ($self, %options) = @_;
117
118 $self->{memory} = {};
119 my $oid_extremeMemoryMonitorSystemEntry = '.1.3.6.1.4.1.1916.1.32.2.2.1';
120 my $snmp_result = $options{snmp}->get_table(
121 oid => $oid_extremeMemoryMonitorSystemEntry,
122 nothing_quit => 1
123 );
124 foreach my $oid (keys %$snmp_result) {
125 next if ($oid !~ /^$mapping->{extremeMemoryMonitorSystemFree}->{oid}\.(\d+)/);
126 my $instance = $1;
127 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
128
129 $self->{memory}->{$instance} = {
130 display => $instance,
131 free => $result->{extremeMemoryMonitorSystemFree} * 1024,
132 total => $result->{extremeMemoryMonitorSystemTotal} * 1024
133 };
134 }
135 }
136
137 1;
138
139 __END__
140
141 =head1 MODE
142
143 Check memory usages.
144
145 =over 8
146
147 =item B<--warning-usage>
148
149 Warning threshold (in percent).
150
151 =item B<--critical-usage>
152
153 Critical threshold (in percent).
154
155 =back
156
157 =cut