Debian Codegraph @

src/os/hpux/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::hpux::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 $self->{output}->perfdata_add(label => $self->{result_values}->{label} . '_used', unit => 'B',
32 value => $self->{result_values}->{used},
33 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1),
34 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1),
35 min => 0, max => $self->{result_values}->{total});
36 }
37
38 sub custom_usage_threshold {
39 my ($self, %options) = @_;
40
41 my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, threshold => [ { label => 'critical-' . $self->{result_values}->{label} . '-usage', exit_litteral => 'critical' }, { label => 'warning-' . $self->{result_values}->{label} . '-usage', exit_litteral => 'warning' } ]);
42 return $exit;
43 }
44
45 sub custom_usage_output {
46 my ($self, %options) = @_;
47
48 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
49 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
50 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
51
52 my $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
53 $total_size_value . " " . $total_size_unit,
54 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
55 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
56 return $msg;
57 }
58
59 sub custom_usage_calc {
60 my ($self, %options) = @_;
61
62 $self->{result_values}->{label} = $options{extra_options}->{label_ref};
63 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_used'} + $options{new_datas}->{$self->{instance} . '_free'};
64 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
65 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
66 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
67 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
68 return 0;
69 }
70
71 sub set_counters {
72 my ($self, %options) = @_;
73
74 $self->{maps_counters_type} = [
75 { name => 'memory', type => 0, cb_prefix_output => 'prefix_memory_output' },
76 { name => 'swap', type => 0, cb_prefix_output => 'prefix_swap_output' }
77 ];
78
79 $self->{maps_counters}->{memory} = [
80 { label => 'physical-usage', set => {
81 key_values => [ { name => 'free' }, { name => 'used' } ],
82 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'physical' },
83 closure_custom_output => $self->can('custom_usage_output'),
84 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
85 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
86 }
87 },
88 ];
89 $self->{maps_counters}->{swap} = [
90 { label => 'swap-usage', set => {
91 key_values => [ { name => 'free' }, { name => 'used' } ],
92 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'swap' },
93 closure_custom_output => $self->can('custom_usage_output'),
94 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
95 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
96 }
97 },
98 ];
99 }
100
101 sub prefix_memory_output {
102 my ($self, %options) = @_;
103
104 return "Physical memory ";
105 }
106
107 sub prefix_swap_output {
108 my ($self, %options) = @_;
109
110 return "Swap memory ";
111 }
112
113 sub new {
114 my ($class, %options) = @_;
115 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
116 bless $self, $class;
117
118 $options{options}->add_options(arguments =>
119 {
120 "filter-counters:s" => { name => 'filter_counters' },
121 });
122
123 return $self;
124 }
125
126 sub manage_selection {
127 my ($self, %options) = @_;
128
129 my $oid_computerSystemFreeMemory = '.1.3.6.1.4.1.11.2.3.1.1.7.0'; # in KB
130 my $oid_computerSystemPhysMemory = '.1.3.6.1.4.1.11.2.3.1.1.8.0'; # in KB
131 my $oid_computerSystemSwapConfig = '.1.3.6.1.4.1.11.2.3.1.1.10.0'; # in KB
132 my $oid_computerSystemFreeSwap = '.1.3.6.1.4.1.11.2.3.1.1.12.0'; # in KB
133
134 my $result = $options{snmp}->get_leef(oids => [$oid_computerSystemFreeMemory, $oid_computerSystemPhysMemory,
135 $oid_computerSystemSwapConfig, $oid_computerSystemFreeSwap],
136 nothing_quit => 1);
137 $self->{memory} = {
138 free => $result->{$oid_computerSystemFreeMemory} * 1024,
139 used => $result->{$oid_computerSystemPhysMemory} * 1024 - $result->{$oid_computerSystemFreeMemory} * 1024,
140 };
141 $self->{swap} = {
142 free => $result->{$oid_computerSystemFreeSwap} * 1024,
143 used => $result->{$oid_computerSystemSwapConfig} * 1024 - $result->{$oid_computerSystemFreeSwap} * 1024,
144 };
145 }
146
147 1;
148
149 __END__
150
151 =head1 MODE
152
153 Check memory usages.
154
155 =over 8
156
157 =item B<--filter-counters>
158
159 Only display some counters (regexp can be used).
160 Example : --filter-counters='^physical-usage$'
161
162 =item B<--warning-*>
163
164 Warning threshold.
165 Can be: 'physical-usage' (%), 'swap-usage' (%).
166
167 =item B<--critical-*>
168
169 Critical threshold.
170 Can be: 'physical-usage' (%), 'swap-usage' (%).
171
172 =back
173
174 =cut