Debian Codegraph @

src/os/mac/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::mac::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 {
35 "warning:s" => { name => 'warning' },
36 "critical:s" => { name => 'critical' },
37 "swap" => { name => 'check_swap' },
38 "warning-swap:s" => { name => 'warning_swap' },
39 "critical-swap:s" => { name => 'critical_swap' },
40 "no-swap:s" => { name => 'no_swap' },
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(oids => $oids,
97 nothing_quit => 1);
98
99 my $cached_used = $result->{$oid_memCached} * 1024;
100 my $physical_used = ($result->{$oid_memTotalReal} * 1024) - ($result->{$oid_memAvailReal} * 1024);
101 # mem_available = total - mem_inactive - mem_cache - mem_free
102 # Maybe it's will be better to have have total with: total = 'active' + 'wired' + 'cache' + 'inactive' + 'free'
103 # But i can't have that with values from bsnmpd-ucd
104 my $nobuf_used = $physical_used - $cached_used;
105
106 my $total_size = $result->{$oid_memTotalReal} * 1024;
107
108 my $prct_used = $nobuf_used * 100 / $total_size;
109 my $exit = $self->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
110
111 my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_size);
112 my ($nobuf_value, $nobuf_unit) = $self->{perfdata}->change_bytes(value => $nobuf_used);
113 my ($cached_value, $cached_unit) = $self->{perfdata}->change_bytes(value => $cached_used);
114
115 $self->{output}->output_add(severity => $exit,
116 short_msg => sprintf("Ram Total: %s, Used (-cache): %s (%.2f%%), Cached: %s",
117 $total_value . " " . $total_unit,
118 $nobuf_value . " " . $nobuf_unit, $prct_used,
119 $cached_value . " " . $cached_unit));
120
121 $self->{output}->perfdata_add(label => "cached", unit => 'B',
122 value => $cached_used,
123 min => 0);
124 $self->{output}->perfdata_add(label => "used", unit => 'B',
125 value => $nobuf_used,
126 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning', total => $total_size, cast_int => 1),
127 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical', total => $total_size, cast_int => 1),
128 min => 0, max => $total_size);
129
130 if (defined($self->{option_results}->{check_swap})) {
131 if ($result->{$oid_memTotalSwap} == 0) {
132 $self->{output}->output_add(severity => $self->{no_swap},
133 short_msg => 'No active swap.');
134 $self->{output}->display();
135 $self->{output}->exit();
136 }
137
138 $total_size = $result->{$oid_memTotalSwap} * 1024;
139 my $swap_used = ($result->{$oid_memTotalSwap} - $result->{$oid_memAvailSwap}) * 1024;
140
141 $prct_used = $swap_used * 100 / $total_size;
142 $exit = $self->{perfdata}->threshold_check(value => $prct_used, threshold => [ { label => 'critical-swap', 'exit_litteral' => 'critical' }, { label => 'warning-swap', exit_litteral => 'warning' } ]);
143
144 my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_size);
145 my ($swap_used_value, $swap_used_unit) = $self->{perfdata}->change_bytes(value => $swap_used);
146 my ($swap_free_value, $swap_free_unit) = $self->{perfdata}->change_bytes(value => ($total_size - $swap_used));
147
148 $self->{output}->output_add(severity => $exit,
149 short_msg => sprintf("Swap Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
150 $total_value . " " . $total_unit,
151 $swap_used_value . " " . $swap_used_unit, $prct_used,
152 $swap_free_value . " " . $swap_free_unit, (100 - $prct_used)));
153
154 $self->{output}->perfdata_add(label => "swap", unit => 'B',
155 value => $swap_used,
156 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-swap', total => $total_size, cast_int => 1),
157 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-swap', total => $total_size, cast_int => 1),
158 min => 0, max => $total_size);
159 }
160
161 $self->{output}->display();
162 $self->{output}->exit();
163 }
164
165 1;
166
167 __END__
168
169 =head1 MODE
170
171 Check physical memory (UCD-SNMP-MIB).
172
173 =over 8
174
175 =item B<--warning>
176
177 Warning threshold in percent.
178
179 =item B<--critical>
180
181 Critical threshold in percent.
182
183 =item B<--swap>
184
185 Check swap also.
186
187 =item B<--warning-swap>
188
189 Warning threshold in percent.
190
191 =item B<--critical-swap>
192
193 Critical threshold in percent.
194
195 =item B<--no-swap>
196
197 Threshold if no active swap (default: 'critical').
198
199 =back
200
201 =cut