Use a new separator ('~' by default). (permits to use regexp for filters)

git-svn-id: http://svn.merethis.net/centreon-esxd/trunk@71 a5eaa968-4c79-4d68-970d-af6011b5b055
This commit is contained in:
Quentin Garnier 2013-09-11 11:29:51 +00:00
parent 81933cfa69
commit 3929e91a7c
2 changed files with 219 additions and 193 deletions

View File

@ -6,9 +6,10 @@ use IO::Socket;
use Getopt::Long;
my $PROGNAME = $0;
my $VERSION = "1.4.1";
my %ERRORS = ('OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3, 'DEPENDENT' => 4);
my $VERSION = "1.5.0";
my %ERRORS = (OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3, DEPENDENT => 4);
my $socket;
my $separatorin = '~';
sub print_help();
sub print_usage();
@ -59,14 +60,14 @@ GetOptions(
"critical2=i" => \$OPTION{'critical2'},
);
if (defined($OPTION{'version'})) {
if (defined($OPTION{version})) {
print_revision($PROGNAME, $VERSION);
exit $ERRORS{'OK'};
exit $ERRORS{OK};
}
if (defined($OPTION{'help'})) {
if (defined($OPTION{help})) {
print_help();
exit $ERRORS{'OK'};
exit $ERRORS{OK};
}
#############
@ -218,7 +219,7 @@ sub myconnect {
PeerAddr => $OPTION{'esxd-host'},
PeerPort => $OPTION{'esxd-port'}))) {
print "Cannot connect to on '$OPTION{'esxd-host'}': $!\n";
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
$socket->autoflush(1);
}
@ -231,116 +232,121 @@ sub maintenancehost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
return 0;
}
sub maintenancehost_get_str {
return "maintenancehost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'};
return join($separatorin,
('maintenancehost', $OPTION{vsphere}, $OPTION{'esx-host'}));
}
sub statushost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
return 0;
}
sub statushost_get_str {
return "statushost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'};
return join($separatorin,
('statushost', $OPTION{vsphere}, $OPTION{'esx-host'}));
}
sub healthhost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
return 0;
}
sub healthhost_get_str {
return "healthhost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'};
return join($separatorin,
('healthhost', $OPTION{vsphere}, $OPTION{'esx-host'}));
}
sub datastoreusage_check_arg {
if (!defined($OPTION{'datastore'})) {
if (!defined($OPTION{datastore})) {
print "Option --datastore is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 80;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 80;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 90;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 90;
}
return 0;
}
sub datastoreusage_get_str {
return "datastore-usage|" . $OPTION{'vsphere'} . "|" . $OPTION{'datastore'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('datastore-usage', $OPTION{vsphere}, $OPTION{datastore}, $OPTION{warning}, $OPTION{critical}));
}
sub datastoreio_check_arg {
if (!defined($OPTION{'datastore'})) {
print "Option --datastore is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = '';
if (!defined($OPTION{warning})) {
$OPTION{warning} = '';
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = '';
if (!defined($OPTION{critical})) {
$OPTION{critical} = '';
}
return 0;
}
sub datastoreio_get_str {
return "datastore-io|" . $OPTION{'vsphere'} . "|" . $OPTION{'datastore'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('datastore-io', $OPTION{vsphere}, $OPTION{datastore}, $OPTION{warning}, $OPTION{critical}));
}
sub datastoresnapshots_check_arg {
if (!defined($OPTION{'datastore'})) {
if (!defined($OPTION{datastore})) {
print "Option --datastore is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = '';
if (!defined($OPTION{warning})) {
$OPTION{warning} = '';
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = '';
if (!defined($OPTION{critical})) {
$OPTION{critical} = '';
}
if (!defined($OPTION{'warning2'})) {
$OPTION{'warning2'} = '';
if (!defined($OPTION{warning2})) {
$OPTION{warning2} = '';
}
if (!defined($OPTION{'critical2'})) {
$OPTION{'critical2'} = '';
if (!defined($OPTION{critical2})) {
$OPTION{critical2} = '';
}
return 0;
}
sub datastoresnapshots_get_str {
return "datastore-snapshots|" . $OPTION{'vsphere'} . "|" . $OPTION{'datastore'}
. "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'} . "|" . $OPTION{'warning2'} . "|" . $OPTION{'critical2'};
return join($separatorin,
('datastore-snapshots', $OPTION{vsphere}, $OPTION{datastore}, $OPTION{warning}, $OPTION{critical}, $OPTION{warning2}, $OPTION{critical2}));
}
sub cpuhost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 80;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 80;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 90;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 90;
}
if (!defined($OPTION{'light-perfdata'})) {
$OPTION{'light-perfdata'} = 0;
@ -349,20 +355,21 @@ sub cpuhost_check_arg {
}
sub cpuhost_get_str {
return "cpuhost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'} . "|" . $OPTION{'light-perfdata'};
return join($separatorin,
('cpuhost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{warning}, $OPTION{critical}, $OPTION{'light-perfdata'}));
}
sub datastoreshost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = '';
if (!defined($OPTION{warning})) {
$OPTION{warning} = '';
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = '';
if (!defined($OPTION{critical})) {
$OPTION{critical} = '';
}
if (!defined($OPTION{'filter-datastores'})) {
$OPTION{'filter-datastores'} = '';
@ -371,244 +378,257 @@ sub datastoreshost_check_arg {
}
sub datastoreshost_get_str {
return "datastoreshost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'} . "|" . $OPTION{'filter-datastores'};
return join($separatorin,
('datastoreshost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{warning}, $OPTION{critical} , $OPTION{'filter-datastores'}));
}
sub memhost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 80;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 80;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 90;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 90;
}
return 0;
}
sub memhost_get_str {
return "memhost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('memhost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{warning}, $OPTION{critical}));
}
sub swaphost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 0.8;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 0.8;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 1;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 1;
}
return 0;
}
sub swaphost_get_str {
return "swaphost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('swaphost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{warning}, $OPTION{critical}));
}
sub nethost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'nic'})) {
if (!defined($OPTION{nic})) {
print "Option --nic is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 80;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 80;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 90;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 90;
}
return 0;
}
sub nethost_get_str {
return "nethost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'nic'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('nethost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{nic}, $OPTION{warning}, $OPTION{critical}));
}
sub countvmhost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = '';
if (!defined($OPTION{warning})) {
$OPTION{warning} = '';
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = '';
if (!defined($OPTION{critical})) {
$OPTION{critical} = '';
}
return 0;
}
sub countvmhost_get_str {
return "countvmhost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('countvmhost', $OPTION{vsphere}, $OPTION{'esx-host'}, $OPTION{warning}, $OPTION{critical}));
}
sub uptimehost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
return 0;
}
sub uptimehost_get_str {
return "uptimehost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'};
return join($separatorin,
('uptimehost', $OPTION{vsphere}, $OPTION{'esx-host'}));
}
sub cpuvm_check_arg {
if (!defined($OPTION{'vm'})) {
if (!defined($OPTION{vm})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 80;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 80;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 90;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 90;
}
return 0;
}
sub cpuvm_get_str {
return "cpuvm|" . $OPTION{'vsphere'} . "|" . $OPTION{'vm'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('cpuvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{warning}, $OPTION{critical}));
}
sub toolsvm_check_arg {
if (!defined($OPTION{'vm'})) {
if (!defined($OPTION{vm})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
return 0;
}
sub toolsvm_get_str {
return "toolsvm|" . $OPTION{'vsphere'} . "|" . $OPTION{'vm'};
return join($separatorin,
('toolsvm', $OPTION{vsphere}, $OPTION{vm}));
}
sub snapshotvm_check_arg {
if (!defined($OPTION{'vm'})) {
if (!defined($OPTION{vm})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'older'})) {
$OPTION{'older'} = '';
if (!defined($OPTION{older})) {
$OPTION{older} = '';
}
if (!defined($OPTION{'warn'})) {
$OPTION{'warn'} = 0;
if (!defined($OPTION{warn})) {
$OPTION{warn} = 0;
} else {
$OPTION{'warn'} = 1;
$OPTION{warn} = 1;
}
if (!defined($OPTION{'crit'})) {
$OPTION{'crit'} = 0;
if (!defined($OPTION{crit})) {
$OPTION{crit} = 0;
} else {
$OPTION{'crit'} = 1;
$OPTION{crit} = 1;
}
return 0;
}
sub snapshotvm_get_str {
return "snapshotvm|" . $OPTION{'vsphere'} . "|" . $OPTION{'vm'} . "|" . $OPTION{'older'} . "|" . $OPTION{'warn'} . "|" . $OPTION{'crit'};
return join($separatorin,
('snapshotvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{older}, $OPTION{warn}, $OPTION{crit}));
}
sub datastoresvm_check_arg {
if (!defined($OPTION{'vm'})) {
if (!defined($OPTION{vm})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = '';
if (!defined($OPTION{warning})) {
$OPTION{warning} = '';
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = '';
if (!defined($OPTION{critical})) {
$OPTION{critical} = '';
}
return 0;
}
sub datastoresvm_get_str {
return "datastoresvm|" . $OPTION{'vsphere'} . "|" . $OPTION{'vm'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('datastoresvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{warning}, $OPTION{critical}));
}
sub memvm_check_arg {
if (!defined($OPTION{'vm'})) {
if (!defined($OPTION{vm})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 80;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 80;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 90;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 90;
}
return 0;
}
sub memvm_get_str {
return "memvm|" . $OPTION{'vsphere'} . "|" . $OPTION{'vm'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('memvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{warning}, $OPTION{critical}));
}
sub swapvm_check_arg {
if (!defined($OPTION{'vm'})) {
if (!defined($OPTION{vm})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = 0.8;
if (!defined($OPTION{warning})) {
$OPTION{warning} = 0.8;
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = 1;
if (!defined($OPTION{critical})) {
$OPTION{critical} = 1;
}
return 0;
}
sub swapvm_get_str {
return "swapvm|" . $OPTION{'vsphere'} . "|" . $OPTION{'vm'} . "|" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('swapvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{warning}, $OPTION{critical}));
}
sub thinprovisioningvm_check_arg {
if (!defined($OPTION{'vm'})) {
if (!defined($OPTION{vm})) {
print "Option --vm is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'on'})) {
$OPTION{'on'} = 0;
if (!defined($OPTION{on})) {
$OPTION{on} = 0;
} else {
$OPTION{'on'} = 1;
$OPTION{on} = 1;
}
if (!defined($OPTION{'warn'})) {
$OPTION{'warn'} = 0;
if (!defined($OPTION{warn})) {
$OPTION{warn} = 0;
} else {
$OPTION{'warn'} = 1;
$OPTION{warn} = 1;
}
if (!defined($OPTION{'crit'})) {
$OPTION{'crit'} = 0;
if (!defined($OPTION{crit})) {
$OPTION{crit} = 0;
} else {
$OPTION{'crit'} = 1;
$OPTION{crit} = 1;
}
return 0;
}
sub thinprovisioningvm_get_str {
return "thinprovisioningvm|" . $OPTION{'vsphere'} . "|" . $OPTION{'vm'} . "|" . $OPTION{'on'} . "|" . $OPTION{'warn'} . "|" . $OPTION{'crit'};
return join($separatorin,
('thinprovisioningvm', $OPTION{vsphere}, $OPTION{vm}, $OPTION{on}, $OPTION{warn}, $OPTION{crit}));
}
@ -617,7 +637,8 @@ sub listhost_check_arg {
}
sub listhost_get_str {
return "listhost|" . $OPTION{'vsphere'};
return join($separatorin,
('listhost', $OPTION{vsphere}));
}
sub listdatastore_check_arg {
@ -625,20 +646,22 @@ sub listdatastore_check_arg {
}
sub listdatastore_get_str {
return "listdatastore|" . $OPTION{'vsphere'};
return join($separatorin,
('listdatastore', $OPTION{vsphere}));
}
sub listnichost_check_arg {
if (!defined($OPTION{'esx-host'})) {
print "Option --esx-host is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
return 0;
}
sub listnichost_get_str {
return "listnichost|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'};
return join($separatorin,
('listnichost', $OPTION{vsphere}, $OPTION{'esx-host'}));
}
sub getmap_check_arg {
@ -649,21 +672,23 @@ sub getmap_check_arg {
}
sub getmap_get_str {
return "getmap|" . $OPTION{'vsphere'} . "|" . $OPTION{'esx-host'};
return join($separatorin,
('getmap', $OPTION{vsphere}, $OPTION{'esx-host'}));
}
sub stats_check_arg {
if (!defined($OPTION{'warning'})) {
$OPTION{'warning'} = "";
if (!defined($OPTION{warning})) {
$OPTION{warning} = "";
}
if (!defined($OPTION{'critical'})) {
$OPTION{'critical'} = "";
if (!defined($OPTION{critical})) {
$OPTION{critical} = "";
}
return 0;
}
sub stats_get_str {
return "stats||" . $OPTION{'warning'} . "|" . $OPTION{'critical'};
return join($separatorin,
('stats', $OPTION{warning}, $OPTION{critical}));
}
#################
@ -672,18 +697,18 @@ sub stats_get_str {
if (!defined($OPTION{'esxd-host'})) {
print "Option -H (--esxd-host) is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if (!defined($OPTION{'usage'})) {
if (!defined($OPTION{usage})) {
print "Option -u (--usage) is required\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
if ($OPTION{'usage'} !~ /^(healthhost|datastore-usage|datastore-io|datastore-snapshots|maintenancehost|statushost|cpuhost|datastoreshost|nethost|memhost|swaphost|countvmhost|uptimehost|cpuvm|toolsvm|snapshotvm|datastoresvm|memvm|swapvm|thinprovisioningvm|listhost|listdatastore|listnichost|getmap|stats)$/) {
if ($OPTION{usage} !~ /^(healthhost|datastore-usage|datastore-io|datastore-snapshots|maintenancehost|statushost|cpuhost|datastoreshost|nethost|memhost|swaphost|countvmhost|uptimehost|cpuvm|toolsvm|snapshotvm|datastoresvm|memvm|swapvm|thinprovisioningvm|listhost|listdatastore|listnichost|getmap|stats)$/) {
print "Usage value is unknown\n";
print_usage();
exit $ERRORS{'UNKNOWN'};
exit $ERRORS{UNKNOWN};
}
$OPTION{'usage'} =~ s/-//g;

View File

@ -20,7 +20,7 @@ BEGIN {
use base qw(centreon::script);
use vars qw(%centreonesxd_config);
my %handlers = ('TERM' => {}, 'HUP' => {}, 'CHLD' => {});
my %handlers = (TERM => {}, HUP => {}, CHLD => {});
my @load_modules = ('centreon::esxd::cmdcountvmhost',
'centreon::esxd::cmdcpuhost',
'centreon::esxd::cmdcpuvm',
@ -103,10 +103,11 @@ sub new {
$self->{counter} = 0;
$self->{global_id} = undef;
$self->{whoaim} = undef; # to know which vsphere to connect
$self->{separatorin} = '~';
$self->{filenos} = {};
$self->{module_date_parse_loaded} = 0;
$self->{modules_registry} = {};
return $self;
}
@ -214,7 +215,7 @@ sub handle_CHLD {
my $child_pid;
while (($child_pid = waitpid(-1, &WNOHANG)) > 0) {
$self->{return_child}{$child_pid} = {'status' => 1, 'rtime' => time()};
$self->{return_child}{$child_pid} = {status => 1, rtime => time()};
}
$SIG{CHLD} = \&class_handle_CHLD;
}
@ -239,15 +240,15 @@ sub load_module {
sub verify_child {
my $self = shift;
my $progress = 0;
my $handle_writer_pipe = ${$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'writer_one'}};
my $handle_writer_pipe = ${$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{writer_one}};
# Verify process
foreach (keys %{$self->{child_proc}}) {
# Check ctime
if (time() - $self->{child_proc}->{$_}->{'ctime'} > $self->{centreonesxd_config}->{timeout}) {
my $handle = ${$self->{child_proc}->{$_}->{'reading'}};
if (time() - $self->{child_proc}->{$_}->{ctime} > $self->{centreonesxd_config}->{timeout}) {
my $handle = ${$self->{child_proc}->{$_}->{reading}};
print $handle_writer_pipe "$_|-1|Timeout Process.\n";
kill('INT', $self->{child_proc}->{$_}->{'pid'});
kill('INT', $self->{child_proc}->{$_}->{pid});
$self->{read_select}->remove($handle);
close $handle;
delete $self->{child_proc}->{$_};
@ -257,7 +258,7 @@ sub verify_child {
}
# Clean old hash CHILD (security)
foreach (keys %{$self->{return_child}}) {
if (time() - $self->{return_child}->{$_}->{'rtime'} > 600) {
if (time() - $self->{return_child}->{$_}->{rtime} > 600) {
$self->{logger}->writeLogInfo("Clean Old return_child list = " . $_);
delete $self->{return_child}->{$_};
}
@ -270,9 +271,9 @@ sub vsphere_handler {
my $self = shift;
my $timeout_process;
my $handle_reader_pipe = ${$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'reader_two'}};
my $handle_reader_pipe = ${$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{reader_two}};
my $fileno_reader = fileno($handle_reader_pipe);
my $handle_writer_pipe = ${$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'writer_one'}};
my $handle_writer_pipe = ${$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{writer_one}};
$self->{read_select} = new IO::Select();
$self->{read_select}->add($handle_reader_pipe);
while (1) {
@ -284,7 +285,7 @@ sub vsphere_handler {
if ($self->{stop} && $timeout_process > $self->{centreonesxd_config}->{timeout_kill}) {
$self->{logger}->writeLogError("'" . $self->{whoaim} . "' Kill child not gently.");
foreach (keys %{$self->{child_proc}}) {
kill('INT', $self->{child_proc}->{$_}->{'pid'});
kill('INT', $self->{child_proc}->{$_}->{pid});
}
$progress = 0;
}
@ -313,9 +314,9 @@ sub vsphere_handler {
$self->{whoaim},
$self->{centreonesxd_config}->{timeout_vsphere},
\$self->{session1},
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'url'},
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'username'},
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'password'})) {
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{url},
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{username},
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{password})) {
$self->{logger}->writeLogInfo("'" . $self->{whoaim} . "' Vsphere connection ok");
$self->{logger}->writeLogInfo("'" . $self->{whoaim} . "' Create perf counters cache in progress");
if (!centreon::esxd::common::cache_perf_counters($self)) {
@ -372,7 +373,7 @@ sub vsphere_handler {
my ($id) = split(/\|/, $data_element);
if ($self->{vsphere_connected}) {
$self->{logger}->writeLogInfo("vpshere '" . $self->{whoaim} . "' handler asking: $data_element");
$self->{child_proc}->{$id} = {'ctime' => time()};
$self->{child_proc}->{$id} = {ctime => time()};
my $reader;
my $writer;
@ -380,15 +381,15 @@ sub vsphere_handler {
$writer->autoflush(1);
$self->{read_select}->add($reader);
$self->{child_proc}->{$id}->{'reading'} = \*$reader;
$self->{child_proc}->{$id}->{'pid'} = fork;
if (!$self->{child_proc}->{$id}->{'pid'}) {
$self->{child_proc}->{$id}->{reading} = \*$reader;
$self->{child_proc}->{$id}->{pid} = fork;
if (!$self->{child_proc}->{$id}->{pid}) {
# Child
close $reader;
open STDOUT, '>&', $writer;
# Can't print on stdout
$self->{logger}->{log_mode} = 1 if ($self->{logger}->{log_mode} == 0);
my ($id, $name, @args) = split /\|/, $data_element;
my ($id, $name, @args) = split /\Q$self->{separatorin}\E/, $data_element;
$self->{global_id} = $id;
$self->{modules_registry}->{$name}->initArgs(@args);
$self->{modules_registry}->{$name}->run();
@ -408,11 +409,11 @@ sub vsphere_handler {
$output =~ s/^(.*?)\|//;
my $lid = $1;
if ($output =~ /^-1/) {
$self->{last_time_check} = $self->{child_proc}->{$lid}->{'ctime'};
$self->{last_time_check} = $self->{child_proc}->{$lid}->{ctime};
}
chomp $output;
print $handle_writer_pipe "$lid|$output\n";
delete $self->{return_child}->{$self->{child_proc}->{$lid}->{'pid'}};
delete $self->{return_child}->{$self->{child_proc}->{$lid}->{pid}};
delete $self->{child_proc}->{$lid};
}
}
@ -450,27 +451,27 @@ sub run {
$writer_pipe_one->autoflush(1);
$writer_pipe_two->autoflush(1);
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'reader_one'} = \*$reader_pipe_one;
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'writer_one'} = \*$writer_pipe_one;
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'reader_two'} = \*$reader_pipe_two;
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'writer_two'} = \*$writer_pipe_two;
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{reader_one} = \*$reader_pipe_one;
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{writer_one} = \*$writer_pipe_one;
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{reader_two} = \*$reader_pipe_two;
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{writer_two} = \*$writer_pipe_two;
$self->{child_vpshere_pid} = fork();
if (!$self->{child_vpshere_pid}) {
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'reader_one'};
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'writer_two'};
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{reader_one};
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{writer_two};
$self->vsphere_handler();
exit(0);
}
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'running'} = 1;
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'writer_one'};
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{'reader_two'};
$self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{running} = 1;
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{writer_one};
close $self->{centreonesxd_config}->{vsphere_server}->{$self->{whoaim}}->{reader_two};
}
$self->{read_select} = new IO::Select();
$self->{read_select}->add($server);
foreach (keys %{$self->{centreonesxd_config}->{vsphere_server}}) {
$self->{filenos}->{fileno(${$self->{centreonesxd_config}->{vsphere_server}->{$_}->{'reader_one'}})} = 1;
$self->{read_select}->add(${$self->{centreonesxd_config}->{vsphere_server}->{$_}->{'reader_one'}});
$self->{filenos}->{fileno(${$self->{centreonesxd_config}->{vsphere_server}->{$_}->{reader_one}})} = 1;
$self->{read_select}->add(${$self->{centreonesxd_config}->{vsphere_server}->{$_}->{reader_one}});
}
my $socket_fileno = fileno($server);
$self->{logger}->writeLogInfo("[Server accepting clients]");
@ -479,7 +480,7 @@ sub run {
if ($self->{stop} == 1) {
foreach (keys %{$self->{centreonesxd_config}->{vsphere_server}}) {
$self->{logger}->writeLogInfo("Send STOP command to '$_' child.");
my $writer_handle = $self->{centreonesxd_config}->{vsphere_server}->{$_}->{'writer_two'};
my $writer_handle = $self->{centreonesxd_config}->{vsphere_server}->{$_}->{writer_two};
print $writer_handle "STOP\n";
}
$self->{stop} = 2;
@ -503,10 +504,10 @@ sub run {
# We have to wait all childs
my ($name, $which_one) = split(/\|/, $data_element);
$self->{logger}->writeLogInfo("Thread vsphere '$which_one' has stopped");
$self->{centreonesxd_config}->{vsphere_server}->{$which_one}->{'running'} = 0;
$self->{centreonesxd_config}->{vsphere_server}->{$which_one}->{running} = 0;
my $to_stop_or_not = 1;
foreach (keys %{$self->{centreonesxd_config}->{vsphere_server}}) {
$to_stop_or_not = 0 if ($self->{centreonesxd_config}->{vsphere_server}->{$_}->{'running'} == 1);
$to_stop_or_not = 0 if ($self->{centreonesxd_config}->{vsphere_server}->{$_}->{running} == 1);
}
if ($to_stop_or_not == 1) {
# We quit
@ -517,23 +518,23 @@ sub run {
}
my @results = split(/\|/, $data_element);
my ($id, $counter) = split(/\./, $results[0]);
if (!defined($self->{sockets}->{$id}) || $counter != $self->{sockets}->{$id}->{'counter'}) {
if (!defined($self->{sockets}->{$id}) || $counter != $self->{sockets}->{$id}->{counter}) {
$self->{logger}->writeLogInfo("Too much time to get response.");
next;
}
$self->{logger}->writeLogInfo("response = $data_element");
$data_element =~ s/^.*?\|//;
${$self->{sockets}->{$id}->{'obj'}}->send($data_element . "\n");
$self->{read_select}->remove(${$self->{sockets}->{$id}->{"obj"}});
close ${$self->{sockets}->{$id}->{"obj"}};
${$self->{sockets}->{$id}->{obj}}->send($data_element . "\n");
$self->{read_select}->remove(${$self->{sockets}->{$id}->{obj}});
close ${$self->{sockets}->{$id}->{obj}};
delete $self->{sockets}->{$id};
} else {
# Socket
my $line = <$rh>;
if (defined($line) && $line ne "") {
chomp $line;
my ($name, $vsphere_name, @args) = split /\|/, $line;
my ($name, $vsphere_name, @args) = split /\Q$self->{separatorin}\E/, $line;
if ($name eq 'stats') {
centreon::esxd::common::stats_info($self, $rh, $current_fileno, \@args);
@ -554,8 +555,8 @@ sub run {
next;
}
my $tmp_handle = ${$self->{centreonesxd_config}->{vsphere_server}->{$vsphere_name}->{'writer_two'}};
print $tmp_handle $current_fileno . "." . $self->{sockets}->{$current_fileno}->{'counter'} . "|$name|" . join('|', @args) . "\n";
my $tmp_handle = ${$self->{centreonesxd_config}->{vsphere_server}->{$vsphere_name}->{writer_two}};
print $tmp_handle $current_fileno . "." . $self->{sockets}->{$current_fileno}->{counter} . $self->{separatorin} . $name . $self->{separatorin} . join($self->{separatorin}, @args) . "\n";
} else {
centreon::esxd::common::response_client1($self, $rh, $current_fileno, "3|Need arguments\n");
}
@ -564,11 +565,11 @@ sub run {
# Verify socket
foreach (keys %{$self->{sockets}}) {
if (time() - $self->{sockets}->{$_}->{'ctime'} > $self->{centreonesxd_config}->{timeout}) {
if (time() - $self->{sockets}->{$_}->{ctime} > $self->{centreonesxd_config}->{timeout}) {
$self->{logger}->writeLogInfo("Timeout returns.");
${$self->{sockets}->{$_}->{'obj'}}->send("3|TIMEOUT\n");
$self->{read_select}->remove(${$self->{sockets}->{$_}->{"obj"}});
close ${$self->{sockets}->{$_}->{"obj"}};
${$self->{sockets}->{$_}->{obj}}->send("3|TIMEOUT\n");
$self->{read_select}->remove(${$self->{sockets}->{$_}->{obj}});
close ${$self->{sockets}->{$_}->{obj}};
delete $self->{sockets}->{$_};
}
}