Debian Codegraph @

src/network/cyberoam/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::cyberoam::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 => $self->{result_values}->{label} . '_used', unit => 'B',
33 value => $self->{result_values}->{used},
34 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1),
35 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1),
36 min => 0, max => $self->{result_values}->{total}
37 );
38 }
39
40 sub custom_usage_output {
41 my ($self, %options) = @_;
42
43 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
44 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
45 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
46
47 return sprintf(
48 "Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
49 $total_size_value . " " . $total_size_unit,
50 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
51 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free}
52 );
53 }
54
55 sub custom_usage_calc {
56 my ($self, %options) = @_;
57
58 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
59 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_used'} + $options{new_datas}->{$self->{instance} . '_free'};
60 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
61 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
62 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
63 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
64 return 0;
65 }
66
67 sub prefix_memory_output {
68 my ($self, %options) = @_;
69
70 return "Physical memory ";
71 }
72
73 sub prefix_swap_output {
74 my ($self, %options) = @_;
75
76 return "Swap memory ";
77 }
78
79 sub set_counters {
80 my ($self, %options) = @_;
81
82 $self->{maps_counters_type} = [
83 { name => 'memory', type => 0, cb_prefix_output => 'prefix_memory_output' },
84 { name => 'swap', type => 0, cb_prefix_output => 'prefix_swap_output' }
85 ];
86
87 $self->{maps_counters}->{memory} = [
88 { label => 'physical-usage', set => {
89 key_values => [ { name => 'free' }, { name => 'used' } ],
90 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'physical' },
91 closure_custom_output => $self->can('custom_usage_output'),
92 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
93 threshold_use => 'prct_used'
94 }
95 }
96 ];
97
98 $self->{maps_counters}->{swap} = [
99 { label => 'swap-usage', set => {
100 key_values => [ { name => 'free' }, { name => 'used' } ],
101 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'swap' },
102 closure_custom_output => $self->can('custom_usage_output'),
103 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
104 threshold_use => 'prct_used'
105 }
106 }
107 ];
108 }
109
110 sub new {
111 my ($class, %options) = @_;
112 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
113 bless $self, $class;
114
115 $options{options}->add_options(arguments => {});
116
117 return $self;
118 }
119
120 sub add_counters {
121 my ($self, %options) = @_;
122
123 return if (!defined($options{result}->{memory_total}));
124
125 my $memory_used = $options{result}->{memory_used_prct} * $options{result}->{memory_total} * 1024 * 1024 / 100;
126 my $swap_used = $options{result}->{swap_used_prct} * $options{result}->{swap_total} * 1024 * 1024 / 100;
127 $self->{memory} = {
128 free => $options{result}->{memory_total} * 1024 * 1024 - $memory_used,
129 used => $memory_used
130 };
131 $self->{swap} = {
132 free => $options{result}->{swap_total} * 1024 * 1024 - $swap_used,
133 used => $swap_used
134 };
135 }
136
137 sub manage_selection {
138 my ($self, %options) = @_;
139
140 my $mapping = {
141 v17 => {
142 memory_total => { oid => '.1.3.6.1.4.1.21067.2.1.2.4.1' }, # memoryCapacity - MB
143 memory_used_prct => { oid => '.1.3.6.1.4.1.21067.2.1.2.4.2' }, # memoryPercentUsage
144 swap_total => { oid => '.1.3.6.1.4.1.21067.2.1.2.4.3' }, # swapCapacity - MB
145 swap_used_prct => { oid => '.1.3.6.1.4.1.21067.2.1.2.4.4' } # swapPercentUsage
146 },
147 v18 => {
148 memory_total => { oid => '.1.3.6.1.4.1.2604.5.1.2.5.1' }, # sfosMemoryCapacity - MB
149 memory_used_prct => { oid => '.1.3.6.1.4.1.2604.5.1.2.5.2' }, # sfosMemoryPercentUsage
150 swap_total => { oid => '.1.3.6.1.4.1.2604.5.1.2.5.3' }, # sfosSwapCapacity - MB
151 swap_used_prct => { oid => '.1.3.6.1.4.1.2604.5.1.2.5.4' } # sfosSwapPercentUsage
152 }
153 };
154
155 my $snmp_result = $options{snmp}->get_leef(
156 oids => [ map($_->{oid} . '.0', values(%{$mapping->{v17}}), values(%{$mapping->{v18}})) ],
157 nothing_quit => 1
158 );
159
160 my $result = $options{snmp}->map_instance(mapping => $mapping->{v17}, results => $snmp_result, instance => 0);
161 $self->add_counters(result => $result);
162 $result = $options{snmp}->map_instance(mapping => $mapping->{v18}, results => $snmp_result, instance => 0);
163 $self->add_counters(result => $result);
164 }
165
166 1;
167
168 __END__
169
170 =head1 MODE
171
172 Check memory usages.
173
174 =over 8
175
176 =item B<--filter-counters>
177
178 Only display some counters (regexp can be used).
179 Example : --filter-counters='^physical-usage$'
180
181 =item B<--warning-*> B<--critical-*>
182
183 Thresholds.
184 Can be: 'physical-usage' (%), 'swap-usage' (%).
185
186 =back
187
188 =cut