Debian Codegraph @

src/os/windows/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::windows::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) = '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,
46 nlabel => $self->{nlabel},
47 value => $value_perf, unit => 'B',
48 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
49 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
50 min => 0, max => $self->{result_values}->{total}
51 );
52 }
53
54 sub custom_usage_threshold {
55 my ($self, %options) = @_;
56
57 my ($exit, $threshold_value);
58 $threshold_value = $self->{result_values}->{used};
59 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
60 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
61 $threshold_value = $self->{result_values}->{prct_used};
62 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
63 }
64 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
65 return $exit;
66 }
67
68 sub custom_usage_output {
69 my ($self, %options) = @_;
70
71 return sprintf(
72 "Total: %s%s Used: %s%s (%.2f%%) Free: %s%s (%.2f%%)",
73 $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}),
74 $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}), $self->{result_values}->{prct_used},
75 $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}), $self->{result_values}->{prct_free}
76 );
77 }
78
79 sub custom_usage_calc {
80 my ($self, %options) = @_;
81
82 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
83 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
84 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
85 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
86 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
87
88 return 0;
89 }
90
91 sub prefix_memory_output {
92 my ($self, %options) = @_;
93
94 return 'Ram ';
95 }
96
97 sub set_counters {
98 my ($self, %options) = @_;
99
100 $self->{maps_counters_type} = [
101 { name => 'memory', type => 0, cb_prefix_output => 'prefix_memory_output' }
102 ];
103
104 $self->{maps_counters}->{memory} = [
105 { label => 'memory', nlabel => 'memory.usage.bytes', set => {
106 key_values => [ { name => 'used' }, { name => 'total' } ],
107 closure_custom_calc => \&custom_usage_calc,
108 closure_custom_output => \&custom_usage_output,
109 closure_custom_perfdata => \&custom_usage_perfdata,
110 closure_custom_threshold_check => \&custom_usage_threshold
111 }
112 }
113 ];
114 }
115
116 sub new {
117 my ($class, %options) = @_;
118 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
119 bless $self, $class;
120
121 $options{options}->add_options(arguments => {
122 'units:s' => { name => 'units', default => '%' },
123 'free' => { name => 'free' }
124 });
125
126 return $self;
127 }
128
129 sub manage_selection {
130 my ($self, %options) = @_;
131
132 my ($total_bytes, $used_bytes, $free_bytes);
133
134 my $oid_hrStorageDescr = '.1.3.6.1.2.1.25.2.3.1.3';
135
136 my $result = $options{snmp}->get_table(oid => $oid_hrStorageDescr);
137
138 foreach my $key (keys %$result) {
139 next if ($key !~ /\.([0-9]+)$/);
140 my $oid = $1;
141 if ($result->{$key} =~ /^Physical (memory|RAM)$/i) {
142 $self->{physical_memory_id} = $oid;
143 }
144 }
145
146 if (!defined($self->{physical_memory_id})) {
147 $self->{output}->add_option_msg(short_msg => 'Cannot find physical memory informations.');
148 $self->{output}->option_exit();
149 }
150
151 my $oid_hrStorageAllocationUnits = '.1.3.6.1.2.1.25.2.3.1.4';
152 my $oid_hrStorageSize = '.1.3.6.1.2.1.25.2.3.1.5';
153 my $oid_hrStorageUsed = '.1.3.6.1.2.1.25.2.3.1.6';
154
155 $options{snmp}->load(
156 oids => [$oid_hrStorageAllocationUnits, $oid_hrStorageSize, $oid_hrStorageUsed],
157 instances => [$self->{physical_memory_id}]
158 );
159 $result = $options{snmp}->get_leef();
160
161 $used_bytes = $result->{$oid_hrStorageUsed . "." . $self->{physical_memory_id}} * $result->{$oid_hrStorageAllocationUnits . "." . $self->{physical_memory_id}};
162 $total_bytes = $result->{$oid_hrStorageSize . "." . $self->{physical_memory_id}} * $result->{$oid_hrStorageAllocationUnits . "." . $self->{physical_memory_id}};
163 $free_bytes = $total_bytes - $used_bytes;
164
165 $self->{memory} = { used => $used_bytes, total => $total_bytes };
166
167 }
168
169 1;
170
171 __END__
172
173 =head1 MODE
174
175 Check memory usage
176
177 =over 8
178
179 =item B<--units>
180
181 Units of thresholds (default: '%') ('%', 'absolute').
182
183 =item B<--free>
184
185 Thresholds are on free space left.
186
187 =item B<--warning-*>
188
189 Warning threshold.
190 Can be: 'memory'.
191
192 =item B<--critical-*>
193
194 Critical threshold.
195 Can be: 'memory'.
196
197 =back
198
199 =cut