Debian Codegraph @

src/centreon/common/jvm/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 centreon::common::jvm::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 $use_th = 1;
32 $use_th = 0 if ($self->{instance_mode}->{option_results}->{units} eq '%' && $self->{result_values}->{max} <= 0);
33
34 my $value_perf = $self->{result_values}->{used};
35 my %total_options = ();
36 if ($self->{instance_mode}->{option_results}->{units} eq '%' && $self->{result_values}->{max} > 0) {
37 $total_options{total} = $self->{result_values}->{max};
38 $total_options{cast_int} = 1;
39 }
40
41 $self->{output}->perfdata_add(
42 nlabel => $self->{nlabel},
43 unit => 'B',
44 value => $value_perf,
45 warning => $use_th == 1 ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options) : undef,
46 critical => $use_th == 1 ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options) : undef,
47 min => 0, max => $self->{result_values}->{max} > 0 ? $self->{result_values}->{max} : undef
48 );
49 }
50
51 sub custom_usage_threshold {
52 my ($self, %options) = @_;
53
54 # Cannot use percent without total
55 return 'ok' if ($self->{result_values}->{max} <= 0 && $self->{instance_mode}->{option_results}->{units} eq '%');
56 my ($exit, $threshold_value);
57 $threshold_value = $self->{result_values}->{used};
58 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
59 $threshold_value = $self->{result_values}->{prct_used};
60 }
61 $exit = $self->{perfdata}->threshold_check(
62 value => $threshold_value, threshold => [
63 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
64 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' }
65 ]
66 );
67 return $exit;
68 }
69
70 sub custom_usage_output {
71 my ($self, %options) = @_;
72
73 my $msg;
74 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
75 if ($self->{result_values}->{max} > 0) {
76 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{max});
77 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{max} - $self->{result_values}->{used});
78 $msg = sprintf(
79 "%s Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", $self->{result_values}->{label},
80 $total_size_value . " " . $total_size_unit,
81 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
82 $total_free_value . " " . $total_free_unit, 100 - $self->{result_values}->{prct_used}
83 );
84 } else {
85 $msg = sprintf(
86 "%s Used: %s",
87 $self->{result_values}->{label},
88 $total_used_value . " " . $total_used_unit
89 );
90 }
91
92 return $msg;
93 }
94
95 sub custom_usage_calc {
96 my ($self, %options) = @_;
97
98 $self->{result_values}->{label} = $options{new_datas}->{$self->{label} . '_label'};
99 $self->{result_values}->{max} = $options{new_datas}->{$self->{instance} . '_max'};
100 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
101
102 if ($self->{result_values}->{max} > 0) {
103 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{max};
104 }
105
106 return 0;
107 }
108
109 sub set_counters {
110 my ($self, %options) = @_;
111
112 $self->{maps_counters_type} = [
113 { name => 'heap', type => 0 },
114 { name => 'nonheap', type => 0 }
115 ];
116
117 $self->{maps_counters}->{heap} = [
118 { label => 'heap', nlabel => 'memory.heap.usage.bytes', set => {
119 key_values => [ { name => 'used' }, { name => 'max' }, { name => 'label' } ],
120 closure_custom_calc => $self->can('custom_usage_calc'),
121 closure_custom_output => $self->can('custom_usage_output'),
122 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
123 closure_custom_threshold_check => $self->can('custom_usage_threshold')
124 }
125 },
126 ];
127
128 $self->{maps_counters}->{nonheap} = [
129 { label => 'nonheap', nlabel => 'memory.nonheap.usage.bytes', set => {
130 key_values => [ { name => 'used' }, { name => 'max' }, { name => 'label' } ],
131 closure_custom_calc => $self->can('custom_usage_calc'),
132 closure_custom_output => $self->can('custom_usage_output'),
133 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
134 closure_custom_threshold_check => $self->can('custom_usage_threshold')
135 }
136 }
137 ];
138 }
139
140 sub new {
141 my ($class, %options) = @_;
142 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
143 bless $self, $class;
144
145 $options{options}->add_options(arguments => {
146 'units:s' => { name => 'units', default => '%' }
147 });
148
149 return $self;
150 }
151
152 sub manage_selection {
153 my ($self, %options) = @_;
154
155 $self->{request} = [
156 { mbean => 'java.lang:type=Memory' }
157 ];
158
159 my $result = $options{custom}->get_attributes(request => $self->{request}, nothing_quit => 1);
160
161 $self->{heap} = { label => 'HeapMemory', used => $result->{'java.lang:type=Memory'}->{HeapMemoryUsage}->{used}, max => $result->{'java.lang:type=Memory'}->{HeapMemoryUsage}->{max} };
162 $self->{nonheap} = { label => 'NonHeapMemoryUsage', used => $result->{'java.lang:type=Memory'}->{NonHeapMemoryUsage}->{used}, max => $result->{'java.lang:type=Memory'}->{NonHeapMemoryUsage}->{max} };
163 }
164
165 1;
166
167 __END__
168
169 =head1 MODE
170
171 Check Java Heap and NonHeap Memory usage (Mbean java.lang:type=Memory).
172
173 Example:
174 perl centreon_plugins.pl --plugin=apps::tomcat::jmx::plugin --custommode=jolokia --url=http://10.30.2.22:8080/jolokia-war --mode=memory --warning-heap 60 --critical-heap 75 --warning-nonheap 65 --critical-nonheap 75
175
176 =over 8
177
178 =item B<--warning-heap>
179
180 Warning threshold of Heap memory usage
181
182 =item B<--critical-heap>
183
184 Critical threshold of Heap memory usage
185
186 =item B<--warning-nonheap>
187
188 Warning threshold of NonHeap memory usage
189
190 =item B<--critical-nonheap>
191
192 Critical threshold of NonHeap memory usage
193
194 =item B<--units>
195
196 Units of thresholds (default: '%') ('%', 'B').
197
198 =back
199
200 =cut