From 646f16bf6074e5a33d2cdd344faf20956cf7632f Mon Sep 17 00:00:00 2001 From: Sylvestre Ho Date: Mon, 26 Nov 2012 12:32:23 +0000 Subject: [PATCH] refs #3944; git-svn-id: http://svn.centreon.com/trunk/plugins-2.x@13816 6bcd3966-0018-0410-8128-fd23d134de7e --- centreon-plugins/src/check_meta_service | 72 +++++++++++++++++++------ 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/centreon-plugins/src/check_meta_service b/centreon-plugins/src/check_meta_service index d16d9ce9b..0bfa6f8ae 100644 --- a/centreon-plugins/src/check_meta_service +++ b/centreon-plugins/src/check_meta_service @@ -46,10 +46,11 @@ use vars qw($mysql_user $mysql_passwd $mysql_host $mysql_database_oreon $mysql_d ######################################## my $PROGNAME = $0; -my $VERSION = "2.3"; -my $MODIF_DATE = "13/04/2011"; +my $VERSION = "2.4"; +my $MODIF_DATE = "26/11/2011"; my %ERRORS = ( "OK" => 0, "WARNING" => 1, "CRITICAL" => 2, "UNKNOWN" => 3); +my %DSTYPE = ( "0" => "g", "1" => "c", "2" => "d", "3" => "a"); require "@CENTREON_ETC@/conf.pm"; my $dbh = DBI->connect("DBI:mysql:database=$mysql_database_oreon;host=$mysql_host","$mysql_user", "$mysql_passwd",{'RaiseError' => 1}); @@ -85,6 +86,7 @@ my $display; sub print_help() { print "Usage: ".$PROGNAME."\n"; print " -i (--id) Hostname to query (required)\n"; + print " -l (--legacy) Compatible with Centreon perfdata syntax prior to v2.4\n"; print " -V (--version) Plugin version\n"; print " -h (--help) usage help\n"; } @@ -97,9 +99,10 @@ my %OPTION = ( "help" => undef, "version" => undef, "id" => undef); Getopt::Long::Configure('bundling'); GetOptions - ("h|help" => \$OPTION{'help'}, + ("h|help" => \$OPTION{'help'}, + "l|legacy" => \$OPTION{'legacy'}, "V|version" => \$OPTION{'version'}, - "i|id=s" => \$OPTION{'id'}); + "i|id=s" => \$OPTION{'id'}); if (defined($OPTION{'version'})) { print $PROGNAME." : ".$VERSION." last modification : ".$MODIF_DATE."\n"; @@ -110,6 +113,7 @@ if (defined($OPTION{'help'})) { print_help(); exit $ERRORS{'UNKNOWN'}; } + if (!defined($OPTION{'id'})) { print_help; exit $ERRORS{'UNKNOWN'}; @@ -164,6 +168,28 @@ sub calculate_meta() { return $result; } +################################################## +## +## Get Data Source Type of meta service +## +################################################## +sub getDataSourceType() { + my $sql = "SHOW COLUMNS FROM meta_service WHERE Field LIKE 'data_source_type'"; + my $res = $dbh->prepare($sql); + $res->execute; + while ($res->fetchrow_hashref()) { + my $sqlDs = "SELECT data_source_type FROM meta_service WHERE meta_id = ".$OPTION{'id'}; + my $resDs = $dbh->prepare($sqlDs); + $resDs->execute; + while ($row = $resDs->fetchrow_hashref()) { + if (defined($DSTYPE{$row->{'data_source_type'}})) { + return $DSTYPE{$row->{'data_source_type'}}; + } + } + } + return $DSTYPE{'0'}; +} + ################################################## ## ## Retrieve by regexp for Centreon Broker @@ -237,7 +263,7 @@ sub retrieve_by_list_centreon_broker() { my $query2 = "SELECT host_id, metric_id FROM `meta_service_relation` WHERE meta_id = '".$OPTION{'id'}."'"; my $DBRES2 = $dbh->prepare($query2); if (!$DBRES2->execute) { - die "Error:" . $DBRES2->errstr . "\n"; + die "Error:" . $DBRES2->errstr . "\n"; } my $i = 0; while ($row2 = $DBRES2->fetchrow_hashref()) { @@ -255,14 +281,23 @@ sub retrieve_by_list_centreon_broker() { die "Error:" . $DBRES4->errstr . "\n"; } $row4 = $DBRES4->fetchrow_hashref(); - - while ($row4->{'perfdata'} =~ m/\'?([a-zA-Z0-9\_\-\/\.\:\ ]+)\'?=([0-9\.\,\-]+)/g) { - my $value = $2; - if (defined($1) && defined($value) && $1 =~ $row3->{'metric_name'}) { - $metric_value_tab[$i] = $value; - $i++; - } - } + if (defined($OPTION{'legacy'})) { + while ($row4->{'perfdata'} =~ m/\'?([a-zA-Z0-9\_\-\/\.\:\ ]+)\'?=([0-9\.\,\-]+)/g) { + my $value = $2; + if (defined($1) && defined($value) && $1 =~ $row3->{'metric_name'}) { + $metric_value_tab[$i] = $value; + $i++; + } + } + } else { + while ($row4->{'perfdata'} =~ m/\'?([a|c|d|g]\[)?([a-zA-Z0-9\_\-\/\.\:\ ]+)(\])?\'?=([0-9\.\,\-])+/g) { + my $value = $4; + if (defined($2) && defined($value) && $2 =~ $row3->{'metric_name'}) { + $metric_value_tab[$i] = $value; + $i++; + } + } + } } } } @@ -416,11 +451,12 @@ if (defined($warning) && defined($critical)) { my $metric = ""; if (defined($metric_select)) { - $metric = $metric_select; + $metric = $metric_select; } else { - $metric = "value"; + $metric = "value"; } + if (!defined($display) || !$display) { if ($calculation eq "AVE") { $msg .= "Average - "; @@ -431,10 +467,14 @@ if (!defined($display) || !$display) { } elsif ($calculation eq "MAX") { $msg .= "Max - "; } - $msg .= $metric." : " . $res . "|".$metric."=".$res; + $msg .= $metric." : " . $res; } else { $msg = sprintf($display, $res); +} +if (defined($OPTION{'legacy'})) { $msg .= "|".$metric."=".$res; +} else { + $msg .= "|".getDataSourceType()."[".$metric."]=".$res; } ##############