| 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::memorydetailed; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
my %mapping_memory = ( |
| 29 |
'Eden Space' => 'eden', |
| 30 |
'Par Eden Space' => 'eden', |
| 31 |
'PS Eden Space' => 'eden', |
| 32 |
'Survivor Space' => 'survivor', |
| 33 |
'Par Survivor Space' => 'survivor', |
| 34 |
'PS Survivor Space' => 'survivor', |
| 35 |
'CMS Perm Gen' => 'permanent', |
| 36 |
'PS Perm Gen' => 'permanent', |
| 37 |
'Perm Gen' => 'permanent', |
| 38 |
'Metaspace' => 'permanent', |
| 39 |
'JIT data cache' => 'permanent', |
| 40 |
'Code Cache' => 'code', |
| 41 |
'JIT Code Cache' => 'code', |
| 42 |
'CMS Old Gen' => 'tenured', |
| 43 |
'PS Old Gen' => 'tenured', |
| 44 |
'Tenured Gen' => 'tenured' |
| 45 |
); |
| 46 |
|
| 47 |
sub custom_usage_perfdata { |
| 48 |
my ($self, %options) = @_; |
| 49 |
|
| 50 |
my $use_th = 1; |
| 51 |
$use_th = 0 if ($self->{instance_mode}->{option_results}->{units} eq '%' && $self->{result_values}->{max} <= 0); |
| 52 |
|
| 53 |
my $value_perf = $self->{result_values}->{used}; |
| 54 |
my %total_options = (); |
| 55 |
if ($self->{instance_mode}->{option_results}->{units} eq '%' && $self->{result_values}->{max} > 0) { |
| 56 |
$total_options{total} = $self->{result_values}->{max}; |
| 57 |
$total_options{cast_int} = 1; |
| 58 |
} |
| 59 |
|
| 60 |
$self->{output}->perfdata_add( |
| 61 |
nlabel => $self->{nlabel}, |
| 62 |
unit => 'B', |
| 63 |
value => $value_perf, |
| 64 |
warning => $use_th == 1 ? $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options) : undef, |
| 65 |
critical => $use_th == 1 ? $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options) : undef, |
| 66 |
min => 0, max => $self->{result_values}->{max} > 0 ? $self->{result_values}->{max} : undef |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
sub custom_usage_threshold { |
| 71 |
my ($self, %options) = @_; |
| 72 |
|
| 73 |
# Cannot use percent without total |
| 74 |
return 'ok' if ($self->{result_values}->{max} <= 0 && $self->{instance_mode}->{option_results}->{units} eq '%'); |
| 75 |
my ($exit, $threshold_value); |
| 76 |
$threshold_value = $self->{result_values}->{used}; |
| 77 |
if ($self->{instance_mode}->{option_results}->{units} eq '%') { |
| 78 |
$threshold_value = $self->{result_values}->{prct_used}; |
| 79 |
} |
| 80 |
$exit = $self->{perfdata}->threshold_check( |
| 81 |
value => $threshold_value, threshold => [ |
| 82 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 83 |
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } |
| 84 |
] |
| 85 |
); |
| 86 |
return $exit; |
| 87 |
} |
| 88 |
|
| 89 |
sub custom_usage_output { |
| 90 |
my ($self, %options) = @_; |
| 91 |
|
| 92 |
my $msg; |
| 93 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 94 |
if ($self->{result_values}->{max} > 0) { |
| 95 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{max}); |
| 96 |
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{max} - $self->{result_values}->{used}); |
| 97 |
$msg = sprintf( |
| 98 |
"Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)", |
| 99 |
$total_size_value . " " . $total_size_unit, |
| 100 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 101 |
$total_free_value . " " . $total_free_unit, 100 - $self->{result_values}->{prct_used} |
| 102 |
); |
| 103 |
} else { |
| 104 |
$msg = sprintf("Used: %s", $total_used_value . " " . $total_used_unit); |
| 105 |
} |
| 106 |
return $msg; |
| 107 |
} |
| 108 |
|
| 109 |
sub custom_usage_calc { |
| 110 |
my ($self, %options) = @_; |
| 111 |
|
| 112 |
if ($mapping_memory{$options{new_datas}->{$self->{instance} . '_display'}} ne $self->{label}) { |
| 113 |
return -12; |
| 114 |
} |
| 115 |
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; |
| 116 |
$self->{result_values}->{max} = $options{new_datas}->{$self->{instance} . '_max'}; |
| 117 |
$self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'}; |
| 118 |
if ($self->{result_values}->{max} > 0) { |
| 119 |
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{max}; |
| 120 |
} |
| 121 |
|
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
sub prefix_mem_output { |
| 126 |
my ($self, %options) = @_; |
| 127 |
|
| 128 |
return "Memory '" . $options{instance_value}->{display} . "' "; |
| 129 |
} |
| 130 |
|
| 131 |
sub set_counters { |
| 132 |
my ($self, %options) = @_; |
| 133 |
|
| 134 |
$self->{maps_counters_type} = [ |
| 135 |
{ name => 'mem', type => 1, cb_prefix_output => 'prefix_mem_output', message_multiple => 'All memories within bounds', skipped_code => { -12 => 1 } } |
| 136 |
]; |
| 137 |
|
| 138 |
$self->{maps_counters}->{mem} = [ |
| 139 |
{ label => 'eden', nlabel => 'memory.eden.usage.bytes', set => { |
| 140 |
key_values => [ { name => 'used' }, { name => 'max' }, { name => 'display' } ], |
| 141 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 142 |
closure_custom_output => $self->can('custom_usage_output'), |
| 143 |
closure_custom_threshold_check => $self->can('custom_usage_threshold'), |
| 144 |
closure_custom_perfdata => $self->can('custom_usage_perfdata') |
| 145 |
} |
| 146 |
}, |
| 147 |
{ label => 'tenured', nlabel => 'memory.tenured.usage.bytes', set => { |
| 148 |
key_values => [ { name => 'used' }, { name => 'max' }, { name => 'display' } ], |
| 149 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 150 |
closure_custom_output => $self->can('custom_usage_output'), |
| 151 |
closure_custom_threshold_check => $self->can('custom_usage_threshold'), |
| 152 |
closure_custom_perfdata => $self->can('custom_usage_perfdata') |
| 153 |
} |
| 154 |
}, |
| 155 |
{ label => 'survivor', nlabel => 'memory.survivor.usage.bytes', set => { |
| 156 |
key_values => [ { name => 'used' }, { name => 'max' }, { name => 'display' } ], |
| 157 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 158 |
closure_custom_output => $self->can('custom_usage_output'), |
| 159 |
closure_custom_threshold_check => $self->can('custom_usage_threshold'), |
| 160 |
closure_custom_perfdata => $self->can('custom_usage_perfdata') |
| 161 |
} |
| 162 |
}, |
| 163 |
{ label => 'permanent', nlabel => 'memory.permanent.usage.bytes', set => { |
| 164 |
key_values => [ { name => 'used' }, { name => 'max' }, { name => 'display' } ], |
| 165 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 166 |
closure_custom_output => $self->can('custom_usage_output'), |
| 167 |
closure_custom_threshold_check => $self->can('custom_usage_threshold'), |
| 168 |
closure_custom_perfdata => $self->can('custom_usage_perfdata') |
| 169 |
} |
| 170 |
}, |
| 171 |
{ label => 'code', nlabel => 'memory.code.usage.bytes', set => { |
| 172 |
key_values => [ { name => 'used' }, { name => 'max' }, { name => 'display' } ], |
| 173 |
closure_custom_calc => $self->can('custom_usage_calc'), |
| 174 |
closure_custom_output => $self->can('custom_usage_output'), |
| 175 |
closure_custom_threshold_check => $self->can('custom_usage_threshold'), |
| 176 |
closure_custom_perfdata => $self->can('custom_usage_perfdata') |
| 177 |
} |
| 178 |
} |
| 179 |
]; |
| 180 |
} |
| 181 |
|
| 182 |
sub new { |
| 183 |
my ($class, %options) = @_; |
| 184 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 185 |
bless $self, $class; |
| 186 |
|
| 187 |
$options{options}->add_options(arguments => { |
| 188 |
'units:s' => { name => 'units', default => '%' } |
| 189 |
}); |
| 190 |
|
| 191 |
return $self; |
| 192 |
} |
| 193 |
|
| 194 |
sub manage_selection { |
| 195 |
my ($self, %options) = @_; |
| 196 |
|
| 197 |
$self->{request} = [ |
| 198 |
{ mbean => 'java.lang:type=MemoryPool,name=*', attributes => [ { name => 'Usage' } ] } |
| 199 |
]; |
| 200 |
|
| 201 |
my $result = $options{custom}->get_attributes(request => $self->{request}, nothing_quit => 1); |
| 202 |
|
| 203 |
$self->{mem} = {}; |
| 204 |
foreach my $key (keys %$result) { |
| 205 |
$key =~ /(?:[:,])name=(.*?)(?:,|$)/; |
| 206 |
my $memtype = $1; |
| 207 |
|
| 208 |
if (!defined($mapping_memory{$memtype})) { |
| 209 |
$self->{output}->output_add(long_msg => "unknown memory type: " . $memtype, debug => 1); |
| 210 |
next; |
| 211 |
} |
| 212 |
|
| 213 |
$self->{mem}->{$memtype} = { |
| 214 |
display => $memtype, |
| 215 |
used => $result->{'java.lang:name=' . $memtype . ',type=MemoryPool'}->{Usage}->{used}, |
| 216 |
max => $result->{'java.lang:name=' . $memtype . ',type=MemoryPool'}->{Usage}->{max} |
| 217 |
}; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
1; |
| 222 |
|
| 223 |
__END__ |
| 224 |
|
| 225 |
=head1 MODE |
| 226 |
|
| 227 |
Check JVM Memory Pools : |
| 228 |
|
| 229 |
Eden Space (heap) (-eden) : The pool from which memory is initially allocated for most objects. |
| 230 |
Survivor Space (heap) (-survivor) : The pool containing objects that have survived the garbage collection of the Eden space. |
| 231 |
Tenured Generation (heap) (-tenured) : The pool containing objects that have existed for some time in the survivor space. |
| 232 |
Permanent Generation (non-heap) (-permanent) : The pool containing all the reflective data of the virtual machine itself, such as class and method objects. |
| 233 |
Code Cache (non-heap) (-code) : The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code. |
| 234 |
|
| 235 |
Example: |
| 236 |
perl centreon_plugins.pl --plugin=apps::tomcat::jmx::plugin --custommode=jolokia --url=http://10.30.2.22:00/jolokia-war --mode=memory-detailed --warning-eden 60 --critical-eden 75 --warning-survivor 65 --critical-survivor 75 |
| 237 |
|
| 238 |
=over 8 |
| 239 |
|
| 240 |
=item B<--warning-eden> |
| 241 |
|
| 242 |
Warning threshold of Heap 'Eden Space' memory usage |
| 243 |
|
| 244 |
=item B<--critical-eden> |
| 245 |
|
| 246 |
Critical threshold of Heap 'Survivor Space' memory usage |
| 247 |
|
| 248 |
=item B<--warning-tenured> |
| 249 |
|
| 250 |
Warning threshold of Heap 'Tenured Generation' memory usage |
| 251 |
|
| 252 |
=item B<--critical-tenured> |
| 253 |
|
| 254 |
Critical threshold of Heap 'Tenured Generation' memory usage |
| 255 |
|
| 256 |
=item B<--warning-survivor> |
| 257 |
|
| 258 |
Warning threshold of Heap 'Survivor Space' memory usage |
| 259 |
|
| 260 |
=item B<--critical-survivor> |
| 261 |
|
| 262 |
Critical threshold of Heap 'Survivor Space' memory usage |
| 263 |
|
| 264 |
=item B<--warning-permanent> |
| 265 |
|
| 266 |
Warning threshold of NonHeap 'Permanent Generation' memory usage |
| 267 |
|
| 268 |
=item B<--critical-permanent> |
| 269 |
|
| 270 |
Critical threshold of NonHeap 'Permanent Generation' memory usage |
| 271 |
|
| 272 |
=item B<--warning-code> |
| 273 |
|
| 274 |
Warning threshold of NonHeap 'Code Cache' memory usage |
| 275 |
|
| 276 |
=item B<--critical-code> |
| 277 |
|
| 278 |
Critical threshold of NonHeap 'Code Cache' memory usage |
| 279 |
|
| 280 |
=item B<--units> |
| 281 |
|
| 282 |
Units of thresholds (default: '%') ('%', 'B'). |
| 283 |
|
| 284 |
=back |
| 285 |
|
| 286 |
=cut |