From 6bf3b23a0c219ebf7b46c9cf3036d71fae042bc1 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Fri, 31 May 2019 17:41:30 +0200 Subject: [PATCH] enhance oracle datafile status: add total traffic io --- database/oracle/mode/datafilesstatus.pm | 97 +++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 7 deletions(-) diff --git a/database/oracle/mode/datafilesstatus.pm b/database/oracle/mode/datafilesstatus.pm index 9b2625c2b..24862642e 100644 --- a/database/oracle/mode/datafilesstatus.pm +++ b/database/oracle/mode/datafilesstatus.pm @@ -25,14 +25,61 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); +use Digest::MD5 qw(md5_hex); + +sub custom_traffic_calc { + my ($self, %options) = @_; + + my $total_traffic = -1; + foreach (keys %{$options{new_datas}}) { + if (/^(.*)_(phyrds|phywrts)$/) { + my $new_total = $options{new_datas}->{$_}; + next if (!defined($options{old_datas}->{$_})); + $total_traffic = 0 if ($total_traffic == -1); + + my $old_total = $options{old_datas}->{$_}; + if ($old_total > $new_total) { + $options{old_datas}->{$_} = 0; + $old_total = 0; + } + + my $diff_total = $new_total - $old_total; + $total_traffic += $diff_total; + } + } + + if ($total_traffic == -1) { + $self->{error_msg} = "Buffer creation"; + return -1; + } + + $self->{result_values}->{traffic} = $total_traffic / $options{delta_time}; + return 0; +} + sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, { name => 'df', type => 1, cb_prefix_output => 'prefix_df_output', message_multiple => 'All data files are ok', skipped_code => { -10 => 1 } }, ]; + $self->{maps_counters}->{global} = [ + { label => 'total-traffic', nlabel => 'datafiles.traffic.io.usage.iops', set => { + key_values => [], + closure_custom_calc => $self->can('custom_traffic_calc'), + per_second => 1, manual_keys => 1, + threshold_use => 'traffic', output_use => 'traffic', + output_template => 'Total Traffic IOPs %.2f', + perfdatas => [ + { label => 'total_traffic', value => 'traffic', template => '%.2f', min => 0 , unit => 'iops' }, + ], + } + }, + ]; + $self->{maps_counters}->{df} = [ { label => 'status', threshold => 0, set => { key_values => [ { name => 'status' }, { name => 'display' } ], @@ -82,7 +129,7 @@ sub custom_online_status_calc { sub new { my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); bless $self, $class; $self->{version} = '1.0'; @@ -117,13 +164,30 @@ sub manage_selection { $options{sql}->connect(); if ($options{sql}->is_version_minimum(version => '10')) { - $options{sql}->query(query => "SELECT file_name, tablespace_name, status, online_status FROM dba_data_files"); + $options{sql}->query(query => q{ + SELECT a.file_name, a.tablespace_name, a.status, b.phyrds, b.phywrts, a.online_status + FROM dba_data_files a, v$filestat b + WHERE a.file_id = b.file# + UNION + SELECT a.name, c.name, a.status, b.phyrds, b.phywrts, NULL + FROM v$tempfile a, v$tablespace c, v$tempstat b + WHERE a.ts#= c.ts# AND a.file# = b.file# + }); } else { - $options{sql}->query(query => "SELECT file_name, tablespace_name, status FROM dba_data_files"); + $options{sql}->query(query => q{ + SELECT a.file_name, a.tablespace_name, a.status, b.phyrds, b.phywrts + FROM dba_data_files a, v$filestat b + WHERE a.file_id = b.file# + UNION + SELECT a.name, c.name, a.status, b.phyrds, b.phywrts + FROM v$tempfile a, v$tablespace c, v$tempstat b + WHERE a.ts#= c.ts# AND a.file# = b.file# + }); } my $result = $options{sql}->fetchall_arrayref(); $options{sql}->disconnect(); - + + $self->{global} = {}; $self->{df} = {}; foreach my $row (@$result) { if (defined($self->{option_results}->{filter_data_file}) && $self->{option_results}->{filter_data_file} ne '' && @@ -136,12 +200,26 @@ sub manage_selection { $self->{output}->output_add(long_msg => "skipping '" . $$row[1] . "': no matching filter.", debug => 1); next } - $self->{df}->{$$row[1] . '/' . $$row[0]} = { + + my $name = $$row[1] . '/' . $$row[0]; + $self->{df}->{$name} = { status => $$row[2], - online_status => defined($$row[3]) ? $$row[3] : undef, - display => $$row[1] . '/' . $$row[0] + online_status => defined($$row[5]) ? $$row[5] : undef, + display => $name }; + $self->{global}->{$name . '_phyrds'} = $$row[3]; + $self->{global}->{$name . '_phywrts'} = $$row[4]; } + + if (scalar(keys %{$self->{df}}) <= 0) { + $self->{output}->add_option_msg(short_msg => "No data file found"); + $self->{output}->option_exit(); + } + + $self->{cache_name} = "oracle_" . $self->{mode} . '_' . $options{sql}->get_unique_id4save() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_tablespace}) ? md5_hex($self->{option_results}->{filter_tablespace}) : md5_hex('all')) . '_' . + (defined($self->{option_results}->{filter_data_file}) ? md5_hex($self->{option_results}->{filter_data_file}) : md5_hex('all')); } 1; @@ -186,6 +264,11 @@ Can used special variables like: %{display}, %{online_status} Set critical threshold for online status (Default: '%{online_status} =~ /offline|recover/i'). Can used special variables like: %{display}, %{online_status} +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'total-traffic'. + =back =cut