Debian Codegraph @

src/os/windows/snmp/mode/swap.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::windows::snmp::mode::swap;
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 'real-swap' => { name => 'real_swap' },
37 });
38
39 $self->{swap_memory_id} = undef;
40 $self->{physical_memory_id} = undef;
41
42 return $self;
43 }
44
45 sub check_options {
46 my ($self, %options) = @_;
47 $self->SUPER::init(%options);
48
49 if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
50 $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
51 $self->{output}->option_exit();
52 }
53 if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
54 $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
55 $self->{output}->option_exit();
56 }
57 }
58
59 sub run {
60 my ($self, %options) = @_;
61 $self->{snmp} = $options{snmp};
62
63 my $oid_hrStorageDescr = '.1.3.6.1.2.1.25.2.3.1.3';
64
65 my $result = $self->{snmp}->get_table(oid => $oid_hrStorageDescr);
66
67 foreach my $key (keys %$result) {
68 next if ($key !~ /\.([0-9]+)$/);
69 my $oid = $1;
70 if ($result->{$key} =~ /^Virtual memory$/i) {
71 $self->{swap_memory_id} = $oid;
72 }
73 if ($result->{$key} =~ /^Physical (memory|RAM)$/i) {
74 $self->{physical_memory_id} = $oid;
75 }
76 }
77
78 if (!defined($self->{swap_memory_id})) {
79 $self->{output}->add_option_msg(short_msg => "Cannot find virtual memory informations.");
80 $self->{output}->option_exit();
81 }
82 if (defined($self->{option_results}->{real_swap}) && !defined($self->{physical_memory_id})) {
83 $self->{output}->add_option_msg(short_msg => "Cannot find physical memory informations.");
84 $self->{output}->option_exit();
85 }
86
87 my $oid_hrStorageAllocationUnits = '.1.3.6.1.2.1.25.2.3.1.4';
88 my $oid_hrStorageSize = '.1.3.6.1.2.1.25.2.3.1.5';
89 my $oid_hrStorageUsed = '.1.3.6.1.2.1.25.2.3.1.6';
90
91 my ($physicalSize, $physicalUsed, $physicalUnits) = (0, 0, 0);
92 if (defined($self->{option_results}->{real_swap})) {
93 $self->{snmp}->load(oids => [$oid_hrStorageAllocationUnits, $oid_hrStorageSize, $oid_hrStorageUsed],
94 instances => [$self->{physical_memory_id}]);
95 $result = $self->{snmp}->get_leef();
96 $physicalSize = $result->{$oid_hrStorageSize . "." . $self->{physical_memory_id}};
97 $physicalUsed = $result->{$oid_hrStorageUsed . "." . $self->{physical_memory_id}};
98 $physicalUnits = $result->{$oid_hrStorageAllocationUnits . "." . $self->{physical_memory_id}};
99 }
100
101 $self->{snmp}->load(oids => [$oid_hrStorageAllocationUnits, $oid_hrStorageSize, $oid_hrStorageUsed],
102 instances => [$self->{swap_memory_id}]);
103 $result = $self->{snmp}->get_leef();
104
105 my $swap_used = ($result->{$oid_hrStorageUsed . "." . $self->{swap_memory_id}} * $result->{$oid_hrStorageAllocationUnits . "." . $self->{swap_memory_id}})
106 - ($physicalUsed * $physicalUnits);
107 my $total_size = ($result->{$oid_hrStorageSize . "." . $self->{swap_memory_id}} * $result->{$oid_hrStorageAllocationUnits . "." . $self->{swap_memory_id}})
108 - ($physicalSize * $physicalUnits);
109
110 my $prct_used = $swap_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 ($swap_used_value, $swap_used_unit) = $self->{perfdata}->change_bytes(value => $swap_used);
114 my ($swap_free_value, $swap_free_unit) = $self->{perfdata}->change_bytes(value => $total_size - $swap_used);
115 my ($total_value, $total_unit) = $self->{perfdata}->change_bytes(value => $total_size);
116
117 $self->{output}->output_add(severity => $exit,
118 short_msg => sprintf("Swap Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
119 $total_value . " " . $total_unit,
120 $swap_used_value . " " . $swap_used_unit, $prct_used,
121 $swap_free_value . " " . $swap_free_unit, 100 - $prct_used));
122
123 $self->{output}->perfdata_add(label => "used", unit => 'B',
124 nlabel => 'swap.usage.bytes',
125 value => $swap_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 $self->{output}->display();
131 $self->{output}->exit();
132 }
133
134 1;
135
136 __END__
137
138 =head1 MODE
139
140 Check Windows swap memory.
141
142 =over 8
143
144 =item B<--warning>
145
146 Warning threshold in percent.
147
148 =item B<--critical>
149
150 Critical threshold in percent.
151
152 =item B<--real-swap>
153
154 Use this option to remove physical memory from Windows SNMP swap values.
155 Using that option can give wrong values (incoherent or negative).
156
157 =back
158
159 =cut