This commit is contained in:
garnier-quentin 2020-04-20 15:27:44 +02:00
parent 7b50b89261
commit 9098a5adef
2 changed files with 63 additions and 50 deletions

View File

@ -26,46 +26,44 @@ use Net::DNS;
my $handle;
my %map_search_field = (
MX => 'exchange',
SOA => 'mname',
NS => 'nsdname',
A => 'address',
PTR => 'name',
CNAME => 'cname',
TXT => 'txtdata',
);
sub search {
my ($self, %options) = @_;
my $map_search_field = {
MX => 'exchange',
SOA => 'mname',
NS => 'nsdname',
A => 'address',
PTR => 'name',
CNAME => 'cname',
TXT => 'txtdata'
};
my @results = ();
my $search_type = $self->{option_results}->{search_type};
if (defined($search_type) && !defined($map_search_field{$search_type})) {
if (defined($search_type) && !defined($map_search_field->{$search_type})) {
$self->{output}->add_option_msg(short_msg => "search-type '$search_type' is unknown or unsupported");
$self->{output}->option_exit();
}
$map_search_field->{PTR} = 'ptrdname' if (defined($self->{option_results}->{use_ptr_fqdn}));
my $error_quit = defined($options{error_quit}) ? $options{error_quit} : undef;
my $reply = $handle->search($self->{option_results}->{search}, $search_type);
if ($reply) {
foreach my $rr ($reply->answer) {
if (!defined($search_type)) {
push @results, $rr->address if ($rr->type eq 'A');
push @results, $rr->name if ($rr->type eq 'PTR');
push @results, $rr->txtdata if ($rr->type eq 'TXT');
next;
}
next if ($rr->type ne $search_type);
my $search_field = $map_search_field{$search_type};
push @results, $rr->$search_field;
my $type = defined($search_type) ? $search_type : $rr->type;
next if ($type ne $rr->type);
my $attr = $map_search_field->{$type};
push @results, $rr->$attr;
}
} else {
if (defined($error_quit)) {
$self->{output}->output_add(severity => $error_quit,
short_msg => sprintf("DNS Query Failed: %s", $handle->errorstring));
$self->{output}->output_add(
severity => $error_quit,
short_msg => sprintf('DNS query failed: %s', $handle->errorstring)
);
$self->{output}->display();
$self->{output}->exit();
}

View File

@ -34,18 +34,18 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments =>
{
"nameservers:s@" => { name => 'nameservers' },
"searchlist:s@" => { name => 'searchlist' },
"dns-options:s@" => { name => 'dns_options' },
"search:s" => { name => 'search' },
"search-type:s" => { name => 'search_type' },
"expected-answer:s" => { name => 'expected_answer' },
"warning:s" => { name => 'warning' },
"critical:s" => { name => 'critical' },
"memory" => { name => 'memory' },
});
$options{options}->add_options(arguments => {
'nameservers:s@' => { name => 'nameservers' },
'searchlist:s@' => { name => 'searchlist' },
'dns-options:s@' => { name => 'dns_options' },
'search:s' => { name => 'search' },
'search-type:s' => { name => 'search_type' },
'use-ptr-fqdn' => { name => 'use_ptr_fqdn' },
'expected-answer:s' => { name => 'expected_answer' },
'warning:s' => { name => 'warning' },
'critical:s' => { name => 'critical' },
'memory' => { name => 'memory' }
});
$self->{statefile_cache} = centreon::plugins::statefile->new(%options);
return $self;
@ -96,14 +96,20 @@ sub run {
my $timeelapsed = tv_interval ($timing0, [gettimeofday]);
my $result_str = join(', ', @results);
my $exit = $self->{perfdata}->threshold_check(value => $timeelapsed,
threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
$self->{output}->output_add(severity => $exit,
short_msg => sprintf("Response time %.3f second(s) (answer: %s)", $timeelapsed, $result_str));
$self->{output}->perfdata_add(label => "time", unit => 's',
value => sprintf('%.3f', $timeelapsed),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'));
my $exit = $self->{perfdata}->threshold_check(
value => $timeelapsed,
threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]
);
$self->{output}->output_add(
severity => $exit,
short_msg => sprintf("Response time %.3f second(s) (answer: %s)", $timeelapsed, $result_str)
);
$self->{output}->perfdata_add(
label => "time", unit => 's',
value => sprintf('%.3f', $timeelapsed),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical')
);
if (defined($self->{option_results}->{expected_answer}) && $self->{option_results}->{expected_answer} ne '') {
my $match = 0;
@ -114,8 +120,11 @@ sub run {
}
if ($match == 0) {
$self->{output}->output_add(severity => 'CRITICAL',
short_msg => sprintf("No result values match expected answer (answer: %s)", $result_str));
$self->{output}->output_add(
severity => 'CRITICAL',
short_msg => sprintf("No result values match expected answer (answer: %s)", $result_str
)
);
}
}
@ -125,15 +134,17 @@ sub run {
my $old_result = $self->{statefile_cache}->get(name => "result");
if (defined($old_result)) {
if ($old_result ne $result_str) {
$self->{output}->output_add(severity => 'CRITICAL',
short_msg => sprintf("Result has changed [answer: %s] [old answer: %s]", $result_str, $old_result));
$self->{output}->output_add(
severity => 'CRITICAL',
short_msg => sprintf("Result has changed [answer: %s] [old answer: %s]", $result_str, $old_result)
);
}
} else {
$self->{output}->output_add(long_msg => 'cache file created.');
}
$self->{statefile_cache}->write(data => $datas);
}
$self->{output}->display();
$self->{output}->exit();
}
@ -169,6 +180,10 @@ Set the search value (required).
Set the search type. Can be: 'MX', 'SOA', 'NS', 'A', 'CNAME' or 'PTR'.
'A' or 'PTR' is used by default (depends if an IP or not).
=item B<--use-ptr-fqdn>
Search is done on conical names for PTR type.
=item B<--expected-answer>
What the server must answer (can be a regexp).