Debian Codegraph @

src/network/nokia/isam/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::nokia::isam::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 my ($label, $nlabel) = ('used', $self->{nlabel});
32 my $value_perf = $self->{result_values}->{used};
33 if (defined($self->{instance_mode}->{option_results}->{free})) {
34 ($label, $nlabel) = ('free', 'memory.free.bytes');
35 $value_perf = $self->{result_values}->{free};
36 }
37
38 my %total_options = ();
39 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
40 $total_options{total} = $self->{result_values}->{total};
41 $total_options{cast_int} = 1;
42 }
43
44 $self->{output}->perfdata_add(
45 label => $label, unit => 'B',
46 nlabel => $nlabel,
47 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
48 value => $value_perf,
49 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
50 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
51 min => 0, max => $self->{result_values}->{total}
52 );
53 }
54
55 sub custom_usage_threshold {
56 my ($self, %options) = @_;
57
58 my ($exit, $threshold_value);
59 $threshold_value = $self->{result_values}->{used};
60 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
61 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
62 $threshold_value = $self->{result_values}->{prct_used};
63 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
64 }
65 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
66 return $exit;
67 }
68
69 sub custom_usage_output {
70 my ($self, %options) = @_;
71
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 my $msg = sprintf("Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
76 $total_size_value . " " . $total_size_unit,
77 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
78 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
79 return $msg;
80 }
81
82 sub custom_usage_calc {
83 my ($self, %options) = @_;
84
85 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
86 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
87 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
88 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
89 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
90 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
91
92 return 0;
93 }
94
95 sub set_counters {
96 my ($self, %options) = @_;
97
98 $self->{maps_counters_type} = [
99 { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memories are ok' },
100 ];
101
102 $self->{maps_counters}->{memory} = [
103 { label => 'usage', nlabel => 'memory.usage.bytes', set => {
104 key_values => [ { name => 'display' }, { name => 'used' }, { name => 'total' } ],
105 closure_custom_calc => $self->can('custom_usage_calc'),
106 closure_custom_output => $self->can('custom_usage_output'),
107 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
108 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
109 }
110 },
111 ];
112 }
113
114 sub new {
115 my ($class, %options) = @_;
116 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
117 bless $self, $class;
118
119 $options{options}->add_options(arguments => {
120 "filter-name:s" => { name => 'filter_name' },
121 "units:s" => { name => 'units', default => '%' },
122 "free" => { name => 'free' },
123 });
124
125 return $self;
126 }
127
128 sub prefix_memory_output {
129 my ($self, %options) = @_;
130
131 return "Memory '" . $options{instance_value}->{display} . "' ";
132 }
133
134 my $mapping = {
135 totalMemSize => { oid => '.1.3.6.1.4.1.637.61.1.9.29.2.1.1' },
136 memAbsoluteUsage => { oid => '.1.3.6.1.4.1.637.61.1.9.29.2.1.2' },
137 eqptSlotActualType => { oid => '.1.3.6.1.4.1.637.61.1.23.3.1.3' },
138 eqptBoardInventorySerialNumber => { oid => '.1.3.6.1.4.1.637.61.1.23.3.1.19' },
139 };
140
141 my $oid_asamSwmTotalSpaceOnFileDisk = '.1.3.6.1.4.1.637.61.1.24.1.6.0';
142 my $oid_asamSwmFreeSpaceOnFileDisk = '.1.3.6.1.4.1.637.61.1.24.1.7.0';
143
144 sub manage_selection {
145 my ($self, %options) = @_;
146
147 my $snmp_result = $options{snmp}->get_multiple_table(oids => [
148 { oid => $mapping->{totalMemSize}->{oid} },
149 { oid => $mapping->{memAbsoluteUsage}->{oid} },
150 { oid => $mapping->{eqptSlotActualType}->{oid} },
151 { oid => $mapping->{eqptBoardInventorySerialNumber}->{oid} },
152 ], return_type => 1, nothing_quit => 1);
153
154 foreach my $oid (keys %{$snmp_result}) {
155 next if ($oid !~ /^$mapping->{memAbsoluteUsage}->{oid}\.(.*)$/);
156 my $instance = $1;
157 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
158
159 my $name = $result->{eqptBoardInventorySerialNumber} . '_' . $result->{eqptSlotActualType};
160 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
161 $name !~ /$self->{option_results}->{filter_name}/) {
162 $self->{output}->output_add(long_msg => "skipping '" . $name . "': not matching filter.", debug => 1);
163 next;
164 }
165 $self->{memory}->{$name} = { display => $name, total => $result->{totalMemSize} * 1024 * 1024, used => $result->{memAbsoluteUsage} * 1024 * 1024 };
166 }
167
168 $snmp_result = $options{snmp}->get_leef(oids => [$oid_asamSwmTotalSpaceOnFileDisk, $oid_asamSwmFreeSpaceOnFileDisk]);
169 if (defined($snmp_result->{$oid_asamSwmFreeSpaceOnFileDisk})) {
170 my $name = 'SD Card';
171 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
172 $name !~ /$self->{option_results}->{filter_name}/) {
173 $self->{output}->output_add(long_msg => "skipping '" . $name . "': not matching filter.", debug => 1);
174 } else {
175 $self->{memory}->{$name} = { display => $name, total => $snmp_result->{$oid_asamSwmTotalSpaceOnFileDisk},
176 used => $snmp_result->{$oid_asamSwmTotalSpaceOnFileDisk} - $snmp_result->{$oid_asamSwmFreeSpaceOnFileDisk} };
177 }
178 }
179
180 if (scalar(keys %{$self->{memory}}) <= 0) {
181 $self->{output}->add_option_msg(short_msg => "No memory found.");
182 $self->{output}->option_exit();
183 }
184 }
185
186 1;
187
188 __END__
189
190 =head1 MODE
191
192 Monitor memory usage.
193
194 =over 8
195
196 =item B<--filter-name>
197
198 Define which memory component to monitor based on their name.
199 This option will be treated as a regular expression.
200
201 =item B<--warning-usage>
202
203 Warning threshold.
204
205 =item B<--critical-usage>
206
207 Critical threshold.
208
209 =item B<--units>
210
211 Define the unit to use to apply to thresholds (default: '%') ('%', 'B').
212
213 =item B<--free>
214
215 Apply the thresholds on free space left instead of on used space.
216
217 =back
218
219 =cut