Debian Codegraph @

src/os/freebsd/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 os::freebsd::snmp::mode::memory;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
31 bless $self, $class;
32
33 $options{options}->add_options(arguments => {
34 "warning:s" => { name => 'warning' },
35 "critical:s" => { name => 'critical' },
36 "swap" => { name => 'check_swap' },
37 "warning-swap:s" => { name => 'warning_swap' },
38 "critical-swap:s" => { name => 'critical_swap' },
39 "no-swap:s" => { name => 'no_swap' }
40 });
41
42 $self->{no_swap} = 'critical';
43 return $self;
44 }
45
46 sub check_options {
47 my ($self, %options) = @_;
48 $self->SUPER::init(%options);
49
50 if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
51 $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
52 $self->{output}->option_exit();
53 }
54 if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
55 $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
56 $self->{output}->option_exit();
57 }
58 if (defined($self->{option_results}->{check_swap})) {
59 if (($self->{perfdata}->threshold_validate(label => 'warning-swap', value => $self->{option_results}->{warning_swap})) == 0) {
60 $self->{output}->add_option_msg(short_msg => "Wrong warning-swap threshold '" . $self->{option_results}->{warning_swap} . "'.");
61 $self->{output}->option_exit();
62 }
63 if (($self->{perfdata}->threshold_validate(label => 'critical-swap', value => $self->{option_results}->{critical_swap})) == 0) {
64 $self->{output}->add_option_msg(short_msg => "Wrong critical-swap threshold '" . $self->{option_results}->{critical_swap} . "'.");
65 $self->{output}->option_exit();
66 }
67 if (defined($self->{option_results}->{no_swap}) && $self->{option_results}->{no_swap} ne '') {
68 if ($self->{output}->is_litteral_status(status => $self->{option_results}->{no_swap}) == 0) {
69 $self->{output}->add_option_msg(short_msg => "Wrong --no-swap status '" . $self->{option_results}->{no_swap} . "'.");
70 $self->{output}->option_exit();
71 }
72 $self->{no_swap} = $self->{option_results}->{no_swap};
73 }
74 }
75 }
76
77 sub run {
78 my ($self, %options) = @_;
79 $self->{snmp} = $options{snmp};
80
81 # with bsnmpd-ucd 0.4.1
82 my $oid_memTotalReal = '.1.3.6.1.4.1.2021.4.5.0'; # hw.physmem
83 my $oid_memAvailReal = '.1.3.6.1.4.1.2021.4.6.0'; # vm.stats.vm.v_free_count
84 my $oid_memShared = '.1.3.6.1.4.1.2021.4.13.0'; # not needed
85 my $oid_memBuffer = '.1.3.6.1.4.1.2021.4.14.0'; # vfs.bufspace (not needed)
86 my $oid_memCached = '.1.3.6.1.4.1.2021.4.15.0'; # vm.stats.vm.v_cache_count + vm.stats.vm.v_inactive_count
87 my $oid_memTotalSwap = '.1.3.6.1.4.1.2021.4.3.0'; # KB
88 my $oid_memAvailSwap = '.1.3.6.1.4.1.2021.4.4.0'; # KB
89
90 my $oids = [$oid_memTotalReal, $oid_memAvailReal,
91 $oid_memShared, $oid_memBuffer, $oid_memCached];
92 if (defined($self->{option_results}->{check_swap})) {
93 push @$oids, ($oid_memTotalSwap, $oid_memAvailSwap);
94 }
95
96 my $result = $self->{snmp}->get_leef(
97 oids => $oids,
98 nothing_quit => 1
99 );
100
101 my $cached_used = $result->{$oid_memCached} * 1024;
102 my $physical_used = ($result->{$oid_memTotalReal} * 1024) - ($result->{$oid_memAvailReal} * 1024);
103 # mem_available = total - mem_inactive - mem_cache - mem_free
104 # Maybe it's will be better to have have total with: total = 'active' + 'wired' + 'cache' + 'inactive' + 'free'
105 # But i can't have that with values from bsnmpd-ucd
106 my $nobuf_used = $physical_used - $cached_used;
107
108 my $total_size = $result->{$oid_memTotalReal} * 1024;
109
110 my $prct_used = $nobuf_used * 100 / $total_size;
111 my $exit = $self->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
112
113 my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_size);
114 my ($nobuf_value, $nobuf_unit) = $self->{perfdata}->change_bytes(value => $nobuf_used);
115 my ($cached_value, $cached_unit) = $self->{perfdata}->change_bytes(value => $cached_used);
116
117 $self->{output}->output_add(
118 severity => $exit,
119 short_msg => sprintf(
120 "Ram Total: %s, Used (-cache): %s (%.2f%%), Cached: %s",
121 $total_value . " " . $total_unit,
122 $nobuf_value . " " . $nobuf_unit, $prct_used,
123 $cached_value . " " . $cached_unit
124 )
125 );
126
127 $self->{output}->perfdata_add(
128 label => "cached", nlabel => 'memory.cached.bytes', unit => 'B',
129 value => $cached_used,
130 min => 0
131 );
132 $self->{output}->perfdata_add(
133 label => "used", nlabel => 'memory.usage.bytes', unit => 'B',
134 value => $nobuf_used,
135 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning', total => $total_size, cast_int => 1),
136 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical', total => $total_size, cast_int => 1),
137 min => 0, max => $total_size
138 );
139
140 if (defined($self->{option_results}->{check_swap})) {
141 if ($result->{$oid_memTotalSwap} == 0) {
142 $self->{output}->output_add(
143 severity => $self->{no_swap},
144 short_msg => 'No active swap.'
145 );
146 $self->{output}->display();
147 $self->{output}->exit();
148 }
149
150 $total_size = $result->{$oid_memTotalSwap} * 1024;
151 my $swap_used = ($result->{$oid_memTotalSwap} - $result->{$oid_memAvailSwap}) * 1024;
152
153 $prct_used = $swap_used * 100 / $total_size;
154 $exit = $self->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical-swap', 'exit_litteral' => 'critical' }, { label => 'warning-swap', exit_litteral => 'warning' } ]);
155
156 my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_size);
157 my ($swap_used_value, $swap_used_unit) = $self->{perfdata}->change_bytes(value => $swap_used);
158 my ($swap_free_value, $swap_free_unit) = $self->{perfdata}->change_bytes(value => ($total_size - $swap_used));
159
160 $self->{output}->output_add(
161 severity => $exit,
162 short_msg => sprintf(
163 "Swap Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
164 $total_value . " " . $total_unit,
165 $swap_used_value . " " . $swap_used_unit, $prct_used,
166 $swap_free_value . " " . $swap_free_unit, (100 - $prct_used)
167 )
168 );
169
170 $self->{output}->perfdata_add(
171 label => "swap", nlabel => 'swap.usage.bytes', unit => 'B',
172 value => $swap_used,
173 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-swap', total => $total_size, cast_int => 1),
174 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-swap', total => $total_size, cast_int => 1),
175 min => 0, max => $total_size
176 );
177 }
178
179 $self->{output}->display();
180 $self->{output}->exit();
181 }
182
183 1;
184
185 __END__
186
187 =head1 MODE
188
189 Check physical memory (UCD-SNMP-MIB).
190
191 =over 8
192
193 =item B<--warning>
194
195 Warning threshold in percent.
196
197 =item B<--critical>
198
199 Critical threshold in percent.
200
201 =item B<--swap>
202
203 Check swap also.
204
205 =item B<--warning-swap>
206
207 Warning threshold in percent.
208
209 =item B<--critical-swap>
210
211 Critical threshold in percent.
212
213 =item B<--no-swap>
214
215 Threshold if no active swap (default: 'critical').
216
217 =back
218
219 =cut