git-svn-id: http://svn.centreon.com/trunk/plugins-2.x@13816 6bcd3966-0018-0410-8128-fd23d134de7e
This commit is contained in:
Sylvestre Ho 2012-11-26 12:32:23 +00:00
parent ef7dbc69f5
commit 646f16bf60
1 changed files with 56 additions and 16 deletions

View File

@ -46,10 +46,11 @@ use vars qw($mysql_user $mysql_passwd $mysql_host $mysql_database_oreon $mysql_d
######################################## ########################################
my $PROGNAME = $0; my $PROGNAME = $0;
my $VERSION = "2.3"; my $VERSION = "2.4";
my $MODIF_DATE = "13/04/2011"; my $MODIF_DATE = "26/11/2011";
my %ERRORS = ( "OK" => 0, "WARNING" => 1, "CRITICAL" => 2, "UNKNOWN" => 3); 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"; require "@CENTREON_ETC@/conf.pm";
my $dbh = DBI->connect("DBI:mysql:database=$mysql_database_oreon;host=$mysql_host","$mysql_user", "$mysql_passwd",{'RaiseError' => 1}); 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() { sub print_help() {
print "Usage: ".$PROGNAME."\n"; print "Usage: ".$PROGNAME."\n";
print " -i (--id) Hostname to query (required)\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 " -V (--version) Plugin version\n";
print " -h (--help) usage help\n"; print " -h (--help) usage help\n";
} }
@ -97,9 +99,10 @@ my %OPTION = ( "help" => undef, "version" => undef, "id" => undef);
Getopt::Long::Configure('bundling'); Getopt::Long::Configure('bundling');
GetOptions GetOptions
("h|help" => \$OPTION{'help'}, ("h|help" => \$OPTION{'help'},
"l|legacy" => \$OPTION{'legacy'},
"V|version" => \$OPTION{'version'}, "V|version" => \$OPTION{'version'},
"i|id=s" => \$OPTION{'id'}); "i|id=s" => \$OPTION{'id'});
if (defined($OPTION{'version'})) { if (defined($OPTION{'version'})) {
print $PROGNAME." : ".$VERSION." last modification : ".$MODIF_DATE."\n"; print $PROGNAME." : ".$VERSION." last modification : ".$MODIF_DATE."\n";
@ -110,6 +113,7 @@ if (defined($OPTION{'help'})) {
print_help(); print_help();
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
} }
if (!defined($OPTION{'id'})) { if (!defined($OPTION{'id'})) {
print_help; print_help;
exit $ERRORS{'UNKNOWN'}; exit $ERRORS{'UNKNOWN'};
@ -164,6 +168,28 @@ sub calculate_meta() {
return $result; 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 ## 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 $query2 = "SELECT host_id, metric_id FROM `meta_service_relation` WHERE meta_id = '".$OPTION{'id'}."'";
my $DBRES2 = $dbh->prepare($query2); my $DBRES2 = $dbh->prepare($query2);
if (!$DBRES2->execute) { if (!$DBRES2->execute) {
die "Error:" . $DBRES2->errstr . "\n"; die "Error:" . $DBRES2->errstr . "\n";
} }
my $i = 0; my $i = 0;
while ($row2 = $DBRES2->fetchrow_hashref()) { while ($row2 = $DBRES2->fetchrow_hashref()) {
@ -255,14 +281,23 @@ sub retrieve_by_list_centreon_broker() {
die "Error:" . $DBRES4->errstr . "\n"; die "Error:" . $DBRES4->errstr . "\n";
} }
$row4 = $DBRES4->fetchrow_hashref(); $row4 = $DBRES4->fetchrow_hashref();
if (defined($OPTION{'legacy'})) {
while ($row4->{'perfdata'} =~ m/\'?([a-zA-Z0-9\_\-\/\.\:\ ]+)\'?=([0-9\.\,\-]+)/g) { while ($row4->{'perfdata'} =~ m/\'?([a-zA-Z0-9\_\-\/\.\:\ ]+)\'?=([0-9\.\,\-]+)/g) {
my $value = $2; my $value = $2;
if (defined($1) && defined($value) && $1 =~ $row3->{'metric_name'}) { if (defined($1) && defined($value) && $1 =~ $row3->{'metric_name'}) {
$metric_value_tab[$i] = $value; $metric_value_tab[$i] = $value;
$i++; $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 = ""; my $metric = "";
if (defined($metric_select)) { if (defined($metric_select)) {
$metric = $metric_select; $metric = $metric_select;
} else { } else {
$metric = "value"; $metric = "value";
} }
if (!defined($display) || !$display) { if (!defined($display) || !$display) {
if ($calculation eq "AVE") { if ($calculation eq "AVE") {
$msg .= "Average - "; $msg .= "Average - ";
@ -431,10 +467,14 @@ if (!defined($display) || !$display) {
} elsif ($calculation eq "MAX") { } elsif ($calculation eq "MAX") {
$msg .= "Max - "; $msg .= "Max - ";
} }
$msg .= $metric." : " . $res . "|".$metric."=".$res; $msg .= $metric." : " . $res;
} else { } else {
$msg = sprintf($display, $res); $msg = sprintf($display, $res);
}
if (defined($OPTION{'legacy'})) {
$msg .= "|".$metric."=".$res; $msg .= "|".$metric."=".$res;
} else {
$msg .= "|".getDataSourceType()."[".$metric."]=".$res;
} }
############## ##############