| 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 hardware::devices::nvidia::gpu::smi::mode::stats; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use XML::LibXML::Simple; |
| 28 |
|
| 29 |
sub custom_memory_usage_output { |
| 30 |
my ($self, %options) = @_; |
| 31 |
|
| 32 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 33 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 34 |
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); |
| 35 |
return sprintf( |
| 36 |
"memory usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)", |
| 37 |
$total_size_value . " " . $total_size_unit, |
| 38 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 39 |
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
sub device_long_output { |
| 44 |
my ($self, %options) = @_; |
| 45 |
|
| 46 |
return "checking device gpu '" . $options{instance} . "'"; |
| 47 |
} |
| 48 |
|
| 49 |
sub prefix_device_output { |
| 50 |
my ($self, %options) = @_; |
| 51 |
|
| 52 |
return "Device gpu '" . $options{instance} . "' "; |
| 53 |
} |
| 54 |
|
| 55 |
sub prefix_util_output { |
| 56 |
my ($self, %options) = @_; |
| 57 |
|
| 58 |
return 'utilization '; |
| 59 |
} |
| 60 |
|
| 61 |
sub prefix_fb_output { |
| 62 |
my ($self, %options) = @_; |
| 63 |
|
| 64 |
return 'frame buffer '; |
| 65 |
} |
| 66 |
|
| 67 |
sub prefix_bar1_output { |
| 68 |
my ($self, %options) = @_; |
| 69 |
|
| 70 |
return 'bar1 '; |
| 71 |
} |
| 72 |
|
| 73 |
sub set_counters { |
| 74 |
my ($self, %options) = @_; |
| 75 |
|
| 76 |
$self->{maps_counters_type} = [ |
| 77 |
{ name => 'global', type => 0, skipped_code => { -10 => 1 } }, |
| 78 |
{ name => 'devices', type => 3, cb_prefix_output => 'prefix_device_output', cb_long_output => 'device_long_output', indent_long_output => ' ', message_multiple => 'All devices are ok', |
| 79 |
group => [ |
| 80 |
{ name => 'util', cb_prefix_output => 'prefix_util_output', type => 0, skipped_code => { -10 => 1 } }, |
| 81 |
{ name => 'fb', type => 0, cb_prefix_output => 'prefix_fb_output', skipped_code => { -10 => 1 } }, |
| 82 |
{ name => 'bar1', type => 0, cb_prefix_output => 'prefix_bar1_output', skipped_code => { -10 => 1 } }, |
| 83 |
{ name => 'fan', type => 0, skipped_code => { -10 => 1 } }, |
| 84 |
{ name => 'temp', type => 0, skipped_code => { -10 => 1 } }, |
| 85 |
{ name => 'power', type => 0, skipped_code => { -10 => 1 } } |
| 86 |
] |
| 87 |
} |
| 88 |
]; |
| 89 |
|
| 90 |
$self->{maps_counters}->{global} = [ |
| 91 |
{ label => 'devices-gpu-total', nlabel => 'devices.gpu.total.count', display_ok => 0, set => { |
| 92 |
key_values => [ { name => 'devices'} ], |
| 93 |
output_template => 'total gpu devices: %s', |
| 94 |
perfdatas => [ |
| 95 |
{ template => '%s', min => 0 }, |
| 96 |
] |
| 97 |
} |
| 98 |
} |
| 99 |
]; |
| 100 |
|
| 101 |
$self->{maps_counters}->{util} = [ |
| 102 |
{ label => 'gpu-utilization', nlabel => 'device.gpu.utilization.percentage', set => { |
| 103 |
key_values => [ { name => 'gpu_util' } ], |
| 104 |
output_template => 'gpu: %.2f %%', |
| 105 |
perfdatas => [ |
| 106 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 107 |
] |
| 108 |
} |
| 109 |
}, |
| 110 |
{ label => 'gpu-memory-utilization', nlabel => 'device.gpu.memory.utilization.percentage', set => { |
| 111 |
key_values => [ { name => 'mem_util' } ], |
| 112 |
output_template => 'memory: %.2f %%', |
| 113 |
perfdatas => [ |
| 114 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 115 |
] |
| 116 |
} |
| 117 |
}, |
| 118 |
{ label => 'gpu-encoder-utilization', nlabel => 'device.gpu.encoder.utilization.percentage', set => { |
| 119 |
key_values => [ { name => 'encoder_util' } ], |
| 120 |
output_template => 'encoder: %.2f %%', |
| 121 |
perfdatas => [ |
| 122 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 123 |
] |
| 124 |
} |
| 125 |
}, |
| 126 |
{ label => 'gpu-decoder-utilization', nlabel => 'device.gpu.decoder.utilization.percentage', set => { |
| 127 |
key_values => [ { name => 'decoder_util' } ], |
| 128 |
output_template => 'decoder: %.2f %%', |
| 129 |
perfdatas => [ |
| 130 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 131 |
] |
| 132 |
} |
| 133 |
} |
| 134 |
]; |
| 135 |
|
| 136 |
$self->{maps_counters}->{fb} = [ |
| 137 |
{ label => 'fb-memory-usage', nlabel => 'device.gpu.frame_buffer.memory.usage.bytes', set => { |
| 138 |
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 139 |
closure_custom_output => $self->can('custom_memory_usage_output'), |
| 140 |
perfdatas => [ |
| 141 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } |
| 142 |
] |
| 143 |
} |
| 144 |
}, |
| 145 |
{ label => 'fb-memory-usage-free', display_ok => 0, nlabel => 'device.gpu.frame_buffer.memory.free.bytes', set => { |
| 146 |
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 147 |
closure_custom_output => $self->can('custom_memory_usage_output'), |
| 148 |
perfdatas => [ |
| 149 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } |
| 150 |
] |
| 151 |
} |
| 152 |
}, |
| 153 |
{ label => 'fb-memory-usage-prct', display_ok => 0, nlabel => 'device.gpu.frame_buffer.memory.usage.percentage', set => { |
| 154 |
key_values => [ { name => 'prct_used' }, { name => 'free' }, { name => 'used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 155 |
closure_custom_output => $self->can('custom_memory_usage_output'), |
| 156 |
perfdatas => [ |
| 157 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 158 |
] |
| 159 |
} |
| 160 |
} |
| 161 |
]; |
| 162 |
|
| 163 |
$self->{maps_counters}->{bar1} = [ |
| 164 |
{ label => 'bar1-memory-usage', nlabel => 'device.gpu.bar1.memory.usage.bytes', set => { |
| 165 |
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 166 |
closure_custom_output => $self->can('custom_memory_usage_output'), |
| 167 |
perfdatas => [ |
| 168 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } |
| 169 |
] |
| 170 |
} |
| 171 |
}, |
| 172 |
{ label => 'bar1-memory-usage-free', display_ok => 0, nlabel => 'device.gpu.bar1.memory.free.bytes', set => { |
| 173 |
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 174 |
closure_custom_output => $self->can('custom_memory_usage_output'), |
| 175 |
perfdatas => [ |
| 176 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } |
| 177 |
] |
| 178 |
} |
| 179 |
}, |
| 180 |
{ label => 'bar1-memory-usage-prct', display_ok => 0, nlabel => 'device.gpu.bar1.memory.usage.percentage', set => { |
| 181 |
key_values => [ { name => 'prct_used' }, { name => 'free' }, { name => 'used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 182 |
closure_custom_output => $self->can('custom_memory_usage_output'), |
| 183 |
perfdatas => [ |
| 184 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 185 |
] |
| 186 |
} |
| 187 |
} |
| 188 |
]; |
| 189 |
|
| 190 |
$self->{maps_counters}->{fan} = [ |
| 191 |
{ label => 'fan-speed', nlabel => 'device.gpu.fan.speed.percentage', set => { |
| 192 |
key_values => [ { name => 'speed' } ], |
| 193 |
output_template => 'fan speed: %.2f %%', |
| 194 |
perfdatas => [ |
| 195 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 196 |
] |
| 197 |
} |
| 198 |
} |
| 199 |
]; |
| 200 |
|
| 201 |
$self->{maps_counters}->{temp} = [ |
| 202 |
{ label => 'temperature', nlabel => 'device.gpu.temperature.celsius', set => { |
| 203 |
key_values => [ { name => 'current' } ], |
| 204 |
output_template => 'gpu temperature: %s C', |
| 205 |
perfdatas => [ |
| 206 |
{ template => '%s', unit => 'C', label_extra_instance => 1 } |
| 207 |
] |
| 208 |
} |
| 209 |
} |
| 210 |
]; |
| 211 |
|
| 212 |
$self->{maps_counters}->{power} = [ |
| 213 |
{ label => 'power', nlabel => 'device.gpu.power.consumption.watt', set => { |
| 214 |
key_values => [ { name => 'current' } ], |
| 215 |
output_template => 'power consumption: %s W', |
| 216 |
perfdatas => [ |
| 217 |
{ template => '%s', min => 0, unit => 'W', label_extra_instance => 1 } |
| 218 |
] |
| 219 |
} |
| 220 |
} |
| 221 |
]; |
| 222 |
} |
| 223 |
|
| 224 |
sub new { |
| 225 |
my ($class, %options) = @_; |
| 226 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 227 |
bless $self, $class; |
| 228 |
|
| 229 |
$options{options}->add_options(arguments => { |
| 230 |
'filter-name:s' => { name => 'filter_name' } |
| 231 |
}); |
| 232 |
|
| 233 |
return $self; |
| 234 |
} |
| 235 |
|
| 236 |
sub get_bytes { |
| 237 |
my ($self, %options) = @_; |
| 238 |
|
| 239 |
return undef if ($options{value} !~ /(\d+)\s*([a-zA-Z]+)/); |
| 240 |
my ($value, $unit) = ($1, $2); |
| 241 |
if ($unit =~ /KiB*/i) { |
| 242 |
$value = $value * 1024; |
| 243 |
} elsif ($unit =~ /MiB*/i) { |
| 244 |
$value = $value * 1024 * 1024; |
| 245 |
} elsif ($unit =~ /GiB*/i) { |
| 246 |
$value = $value * 1024 * 1024 * 1024; |
| 247 |
} elsif ($unit =~ /TiB*/i) { |
| 248 |
$value = $value * 1024 * 1024 * 1024 * 1024; |
| 249 |
} |
| 250 |
|
| 251 |
return $value; |
| 252 |
} |
| 253 |
|
| 254 |
sub manage_selection { |
| 255 |
my ($self, %options) = @_; |
| 256 |
|
| 257 |
my ($stdout) = $options{custom}->execute_command( |
| 258 |
command => 'nvidia-smi', |
| 259 |
command_options => '-q -x' |
| 260 |
); |
| 261 |
|
| 262 |
my $decoded; |
| 263 |
eval { |
| 264 |
$SIG{__WARN__} = sub {}; |
| 265 |
$decoded = XMLin($stdout, KeyAttr => [], ForceArray => ['gpu']); |
| 266 |
}; |
| 267 |
if ($@) { |
| 268 |
$self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@"); |
| 269 |
$self->{output}->option_exit(); |
| 270 |
} |
| 271 |
|
| 272 |
$self->{global} = { devices => 0 }; |
| 273 |
$self->{devices} = {}; |
| 274 |
foreach my $entry (@{$decoded->{gpu}}) { |
| 275 |
my $name = $entry->{product_name} . ':' . $entry->{id}; |
| 276 |
if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 277 |
$name !~ /$self->{option_results}->{filter_name}/) { |
| 278 |
$self->{output}->output_add(long_msg => "skipping device '" . $name . "'.", debug => 1); |
| 279 |
next; |
| 280 |
} |
| 281 |
|
| 282 |
$self->{devices}->{$name} = { util => {} }; |
| 283 |
if (defined($entry->{utilization}->{gpu_util}) && $entry->{utilization}->{gpu_util} =~ /([0-9\.]+)\s*%/) { |
| 284 |
$self->{devices}->{$name}->{util}->{gpu_util} = $1; |
| 285 |
} |
| 286 |
if (defined($entry->{utilization}->{memory_util}) && $entry->{utilization}->{memory_util} =~ /([0-9\.]+)\s*%/) { |
| 287 |
$self->{devices}->{$name}->{util}->{mem_util} = $1; |
| 288 |
} |
| 289 |
if (defined($entry->{utilization}->{encoder_util}) && $entry->{utilization}->{encoder_util} =~ /([0-9\.]+)\s*%/) { |
| 290 |
$self->{devices}->{$name}->{util}->{encoder_util} = $1; |
| 291 |
} |
| 292 |
if (defined($entry->{utilization}->{decoder_util}) && $entry->{utilization}->{decoder_util} =~ /([0-9\.]+)\s*%/) { |
| 293 |
$self->{devices}->{$name}->{util}->{decoder_util} = $1; |
| 294 |
} |
| 295 |
if (defined($entry->{fb_memory_usage})) { |
| 296 |
my $total = $self->get_bytes(value => $entry->{fb_memory_usage}->{total}); |
| 297 |
my $used = $self->get_bytes(value => $entry->{fb_memory_usage}->{used}); |
| 298 |
my $free = $self->get_bytes(value => $entry->{fb_memory_usage}->{free}); |
| 299 |
$self->{devices}->{$name}->{fb} = { |
| 300 |
total => $total, |
| 301 |
used => $used, |
| 302 |
free => $free, |
| 303 |
prct_used => $used * 100 / $total, |
| 304 |
prct_free => 100 - ($used * 100 / $total) |
| 305 |
}; |
| 306 |
} |
| 307 |
if (defined($entry->{bar1_memory_usage})) { |
| 308 |
my $total = $self->get_bytes(value => $entry->{bar1_memory_usage}->{total}); |
| 309 |
my $used = $self->get_bytes(value => $entry->{bar1_memory_usage}->{used}); |
| 310 |
my $free = $self->get_bytes(value => $entry->{bar1_memory_usage}->{free}); |
| 311 |
$self->{devices}->{$name}->{bar1} = { |
| 312 |
total => $total, |
| 313 |
used => $used, |
| 314 |
free => $free, |
| 315 |
prct_used => $used * 100 / $total, |
| 316 |
prct_free => 100 - ($used * 100 / $total) |
| 317 |
}; |
| 318 |
} |
| 319 |
if (defined($entry->{fan_speed}) && $entry->{fan_speed} =~ /([0-9\.]+)\s*%/) { |
| 320 |
$self->{devices}->{$name}->{fan} = { speed => $1 }; |
| 321 |
} |
| 322 |
if (defined($entry->{temperature}) && $entry->{temperature}->{gpu_temp} =~ /([0-9\.]+)\s*C/) { |
| 323 |
$self->{devices}->{$name}->{temp} = { current => $1 }; |
| 324 |
} |
| 325 |
if (defined($entry->{power_readings}) && $entry->{power_readings}->{power_draw} =~ /([0-9\.]+)\s*W/) { |
| 326 |
$self->{devices}->{$name}->{power} = { current => $1 }; |
| 327 |
} |
| 328 |
|
| 329 |
$self->{global}->{devices}++; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
1; |
| 334 |
|
| 335 |
__END__ |
| 336 |
|
| 337 |
=head1 MODE |
| 338 |
|
| 339 |
Check GPU statistics. |
| 340 |
|
| 341 |
Command used: nvidia-smi -q -x |
| 342 |
|
| 343 |
=over 8 |
| 344 |
|
| 345 |
=item B<--filter-name> |
| 346 |
|
| 347 |
Filter gpu devices by name (can be a regexp). |
| 348 |
|
| 349 |
=item B<--warning-*> B<--critical-*> |
| 350 |
|
| 351 |
Thresholds. |
| 352 |
Can be: 'devices-gpu-total', |
| 353 |
'bar1-memory-usage', 'bar1-memory-usage-free', 'bar1-memory-usage-prct', |
| 354 |
'fb-memory-usage', 'fb-memory-usage-free', 'fb-memory-usage-prct', |
| 355 |
'gpu-utilization', 'gpu-memory-utilization', 'gpu-encoder-utilization', 'gpu-decoder-utilization', |
| 356 |
'temperature', 'fan-speed', 'power'. |
| 357 |
|
| 358 |
=back |
| 359 |
|
| 360 |
=cut |