This commit is contained in:
garnier-quentin 2020-03-27 15:10:40 +01:00 committed by Lotfi zaouche
parent 780694ddff
commit 043878a7f7
7 changed files with 56 additions and 71 deletions

View File

@ -40,23 +40,24 @@ sub new {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
"hostname:s" => { name => 'hostname' },
"port:s" => { name => 'port' },
"protocol:s" => { name => 'protocol' },
"username:s" => { name => 'username' },
"password:s" => { name => 'password' },
"timeout:s" => { name => 'timeout' },
"ssl-opt:s@" => { name => 'ssl_opt' },
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
'protocol:s' => { name => 'protocol' },
'username:s' => { name => 'username' },
'password:s' => { name => 'password' },
'timeout:s' => { name => 'timeout' },
'ssl-opt:s@' => { name => 'ssl_opt' },
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'DRIVER OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{mode} = $options{mode};
return $self;
}
@ -127,10 +128,10 @@ sub connect {
$uri = $self->{protocol} . '://';
$uri .= $encoded_username . ':' . $encoded_password . '@' if ($encoded_username ne '' && $encoded_password ne '');
$uri .= $self->{hostname} if ($self->{hostname} ne '');
$uri .= ':' . $self->{port} if ($self->{port} ne '');
$uri .= ':' . $self->{port} if ($self->{port} ne '' && $self->{protocol} eq 'mongodb+srv');
$self->{output}->output_add(long_msg => 'Connection URI: ' . $uri, debug => 1);
my $ssl = (defined($self->{ssl_opts})) ? $self->{ssl_opts} : 0;
$self->{client} = MongoDB::MongoClient->new(host => $uri, ssl => $ssl);
$self->{client}->connect();
@ -163,7 +164,7 @@ sub run_command {
if (!defined($self->{client})) {
$self->connect();
}
my $db = $self->{client}->get_database($options{database});
return $db->run_command($options{command});
}

View File

@ -105,25 +105,22 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
my $databases = $options{custom}->list_databases();
$self->{databases} = {};
my $databases = $self->{custom}->list_databases();
foreach my $database (sort @{$databases}) {
next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne ''
&& $database !~ /$self->{option_results}->{filter_database}/);
my $collections = $self->{custom}->list_collections(database => $database);
my $collections = $options{custom}->list_collections(database => $database);
$self->{databases}->{$database}->{display} = $database;
foreach my $collection (sort @{$collections}) {
my $cl_stats = $self->{custom}->run_command(
my $cl_stats = $options{custom}->run_command(
database => $database,
command => $self->{custom}->ordered_hash(collStats => $collection),
command => $options{custom}->ordered_hash(collStats => $collection),
);
$self->{databases}->{$database}->{collections}->{$collection} = {

View File

@ -88,14 +88,12 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
$self->{global} = {};
my $server_stats = $self->{custom}->run_command(
my $server_stats = $options{custom}->run_command(
database => 'admin',
command => $self->{custom}->ordered_hash(serverStatus => 1),
command => $options{custom}->ordered_hash(serverStatus => 1),
);
$self->{global}->{active} = $server_stats->{connections}->{active};
@ -103,7 +101,7 @@ sub manage_selection {
$self->{global}->{usage} = $server_stats->{connections}->{current} / ($server_stats->{connections}->{current} + $server_stats->{connections}->{available});
$self->{global}->{totalCreated} = $server_stats->{connections}->{totalCreated};
$self->{cache_name} = "mongodb_" . $self->{mode} . '_' . $self->{custom}->get_hostname() . '_' . $self->{custom}->get_port() . '_' .
$self->{cache_name} = "mongodb_" . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
}

View File

@ -50,7 +50,7 @@ sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {});
return $self;
@ -58,13 +58,11 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
my $start = Time::HiRes::time();
$self->{custom}->connect();
$options{custom}->connect();
my $end = Time::HiRes::time();
$self->{global}->{connection_time} = ($end - $start) * 1000;
}

View File

@ -115,29 +115,26 @@ sub new {
bless $self, $class;
$options{options}->add_options(arguments => {
"filter-database:s" => { name => 'filter_database' },
'filter-database:s' => { name => 'filter_database' }
});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
my $databases = $options{custom}->list_databases();
$self->{databases} = {};
my $databases = $self->{custom}->list_databases();
foreach my $database (sort @{$databases}) {
next if (defined($self->{option_results}->{filter_database}) && $self->{option_results}->{filter_database} ne ''
&& $database !~ /$self->{option_results}->{filter_database}/);
my $db_stats = $self->{custom}->run_command(
my $db_stats = $options{custom}->run_command(
database => $database,
command => $self->{custom}->ordered_hash('dbStats' => 1),
command => $options{custom}->ordered_hash(dbStats => 1),
);
$self->{databases}->{$db_stats->{db}} = {
display => $db_stats->{db},
collections => $db_stats->{collections},

View File

@ -28,11 +28,11 @@ use Digest::MD5 qw(md5_hex);
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_output' },
];
$self->{maps_counters}->{global} = [
{ label => 'total', nlabel => 'queries.total.persecond', set => {
key_values => [ { name => 'total', diff => 1 } ],
@ -44,7 +44,7 @@ sub set_counters {
}
},
];
foreach ('insert', 'query', 'update', 'delete', 'getmore', 'command') {
push @{$self->{maps_counters}->{global}}, {
label => $_, nlabel => 'queries.' . $_ . '.persecond', display_ok => 0, set => {
@ -71,7 +71,7 @@ sub set_counters {
sub prefix_output {
my ($self, %options) = @_;
return "Requests ";
return 'Requests ';
}
sub new {
@ -86,14 +86,11 @@ sub new {
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
$self->{global} = {};
my $server_stats = $self->{custom}->run_command(
my $server_stats = $options{custom}->run_command(
database => 'admin',
command => $self->{custom}->ordered_hash(serverStatus => 1),
command => $options{custom}->ordered_hash(serverStatus => 1),
);
foreach my $querie (keys %{$server_stats->{opcounters}}) {
@ -101,7 +98,7 @@ sub manage_selection {
$self->{global}->{total} += $server_stats->{opcounters}->{$querie};
}
$self->{cache_name} = "mongodb_" . $self->{mode} . '_' . $self->{custom}->get_hostname() . '_' . $self->{custom}->get_port() . '_' .
$self->{cache_name} = 'mongodb_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' .
(defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
}

View File

@ -41,7 +41,6 @@ sub custom_status_output {
my $msg = sprintf("Current member state is '%s'", $self->{result_values}->{state});
$msg .= sprintf(", syncing to '%s'", $self->{result_values}->{sync_host}) if ($self->{result_values}->{state} ne 'PRIMARY');
return $msg;
}
@ -50,20 +49,18 @@ sub custom_status_calc {
$self->{result_values}->{state} = $mapping_states{$options{new_datas}->{$self->{instance} . '_myState'}};
$self->{result_values}->{sync_host} = $options{new_datas}->{$self->{instance} . '_syncSourceHost'};
return 0;
}
sub custom_member_status_output {
my ($self, %options) = @_;
my $msg = sprintf("state is '%s' and health is '%s' [slave delay: %s] [priority: %s]",
return sprintf("state is '%s' and health is '%s' [slave delay: %s] [priority: %s]",
$self->{result_values}->{state},
$self->{result_values}->{health},
$self->{result_values}->{slave_delay},
$self->{result_values}->{priority});
return $msg;
$self->{result_values}->{priority}
);
}
sub custom_member_status_calc {
@ -131,11 +128,12 @@ sub new {
bless $self, $class;
$options{options}->add_options(arguments => {
"warning-status:s" => { name => 'warning_status', default => '' },
"critical-status:s" => { name => 'critical_status', default => '' },
"warning-member-status:s" => { name => 'warning_member_status', default => '%{state} !~ /PRIMARY|SECONDARY/' },
"critical-member-status:s" => { name => 'critical_member_status', default => '%{health} !~ /up/' },
'warning-status:s' => { name => 'warning_status', default => '' },
'critical-status:s' => { name => 'critical_status', default => '' },
'warning-member-status:s' => { name => 'warning_member_status', default => '%{state} !~ /PRIMARY|SECONDARY/' },
'critical-member-status:s' => { name => 'critical_member_status', default => '%{health} !~ /up/' },
});
return $self;
}
@ -143,18 +141,18 @@ sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
$self->change_macros(macros => ['warning_status', 'critical_status',
'warning_member_status', 'critical_member_status']);
$self->change_macros(macros => [
'warning_status', 'critical_status',
'warning_member_status', 'critical_member_status'
]);
}
sub manage_selection {
my ($self, %options) = @_;
$self->{custom} = $options{custom};
my $ismaster = $self->{custom}->run_command(
my $ismaster = $options{custom}->run_command(
database => 'admin',
command => $self->{custom}->ordered_hash('ismaster' => 1),
command => $options{custom}->ordered_hash(ismaster => 1),
);
if (!defined($ismaster->{me})) {
@ -164,10 +162,9 @@ sub manage_selection {
$self->{global} = {};
$self->{members} = {};
my $repl_conf = $self->{custom}->run_command(
my $repl_conf = $options{custom}->run_command(
database => 'admin',
command => $self->{custom}->ordered_hash('replSetGetConfig' => 1),
command => $options{custom}->ordered_hash(replSetGetConfig => 1),
);
my %config;
@ -175,9 +172,9 @@ sub manage_selection {
$config{$member->{host}} = { priority => $member->{priority}, slaveDelay => $member->{slaveDelay} }
}
my $repl_status = $self->{custom}->run_command(
my $repl_status = $options{custom}->run_command(
database => 'admin',
command => $self->{custom}->ordered_hash('replSetGetStatus' => 1),
command => $options{custom}->ordered_hash(replSetGetStatus => 1),
);
$self->{global}->{myState} = $repl_status->{myState};
@ -191,7 +188,7 @@ sub manage_selection {
health => $member->{health},
optimeDate => $member->{optime}->{ts}->{seconds},
slaveDelay => $config{$member->{name}}->{slaveDelay},
priority => $config{$member->{name}}->{priority},
priority => $config{$member->{name}}->{priority}
}
}