Fix #1834
This commit is contained in:
parent
88ebc9773a
commit
df7ab970d2
|
@ -23,6 +23,7 @@ package apps::protocols::imap::lib::imap;
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use Net::IMAP::Simple;
|
use Net::IMAP::Simple;
|
||||||
|
use IO::Socket::SSL;
|
||||||
|
|
||||||
my $imap_handle;
|
my $imap_handle;
|
||||||
|
|
||||||
|
@ -32,63 +33,72 @@ sub quit {
|
||||||
|
|
||||||
sub search {
|
sub search {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
if (!defined($imap_handle->select($self->{option_results}->{folder}))) {
|
if (!defined($imap_handle->select($self->{option_results}->{folder}))) {
|
||||||
my $output = $imap_handle->errstr;
|
my $output = $imap_handle->errstr;
|
||||||
$output =~ s/\r//g;
|
$output =~ s/\r//g;
|
||||||
$self->{output}->output_add(severity => 'UNKNOWN',
|
$self->{output}->output_add(
|
||||||
short_msg => 'Folder Select Error: ' . $output);
|
severity => 'UNKNOWN',
|
||||||
|
short_msg => 'Folder Select Error: ' . $output
|
||||||
|
);
|
||||||
quit();
|
quit();
|
||||||
$self->{output}->display();
|
$self->{output}->display();
|
||||||
$self->{output}->exit();
|
$self->{output}->exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
my @ids = $imap_handle->search($self->{option_results}->{search});
|
my @ids = $imap_handle->search($self->{option_results}->{search});
|
||||||
|
|
||||||
if (defined($self->{option_results}->{delete})) {
|
if (defined($self->{option_results}->{delete})) {
|
||||||
foreach my $msg_num (@ids) {
|
foreach my $msg_num (@ids) {
|
||||||
$imap_handle->delete($msg_num);
|
$imap_handle->delete($msg_num);
|
||||||
}
|
}
|
||||||
$imap_handle->expunge_mailbox();
|
$imap_handle->expunge_mailbox();
|
||||||
}
|
}
|
||||||
|
|
||||||
return scalar(@ids);
|
return scalar(@ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub connect {
|
sub connect {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
my %imap_options = ();
|
my %imap_options = ();
|
||||||
|
|
||||||
my $connection_exit = defined($options{connection_exit}) ? $options{connection_exit} : 'unknown';
|
my $connection_exit = defined($options{connection_exit}) ? $options{connection_exit} : 'unknown';
|
||||||
$imap_options{port} = $self->{option_results}->{port} if (defined($self->{option_results}->{port}));
|
$imap_options{port} = $self->{option_results}->{port} if (defined($self->{option_results}->{port}));
|
||||||
$imap_options{use_ssl} = 1 if (defined($self->{option_results}->{use_ssl}));
|
$imap_options{use_ssl} = 1 if (defined($self->{option_results}->{use_ssl}));
|
||||||
$imap_options{timeout} = $self->{option_results}->{timeout} if (defined($self->{option_results}->{timeout}));
|
$imap_options{timeout} = $self->{option_results}->{timeout} if (defined($self->{option_results}->{timeout}));
|
||||||
|
if ($self->{ssl_options} ne '') {
|
||||||
|
$imap_options{ssl_options} = [ eval $self->{ssl_options} ];
|
||||||
|
}
|
||||||
|
|
||||||
if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '' &&
|
if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '' &&
|
||||||
!defined($self->{option_results}->{password})) {
|
!defined($self->{option_results}->{password})) {
|
||||||
$self->{output}->add_option_msg(short_msg => "Please set --password option.");
|
$self->{output}->add_option_msg(short_msg => 'Please set --password option.');
|
||||||
$self->{output}->option_exit();
|
$self->{output}->option_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$imap_handle = Net::IMAP::Simple->new($self->{option_results}->{hostname},
|
$imap_handle = Net::IMAP::Simple->new(
|
||||||
|
$self->{option_results}->{hostname},
|
||||||
%imap_options
|
%imap_options
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
if (!defined($imap_handle)) {
|
if (!defined($imap_handle)) {
|
||||||
$self->{output}->output_add(severity => $connection_exit,
|
$self->{output}->output_add(
|
||||||
short_msg => 'Unable to connect to IMAP: ' . $Net::IMAP::Simple::errstr);
|
severity => $connection_exit,
|
||||||
|
short_msg => 'Unable to connect to IMAP: ' . $Net::IMAP::Simple::errstr
|
||||||
|
);
|
||||||
$self->{output}->display();
|
$self->{output}->display();
|
||||||
$self->{output}->exit();
|
$self->{output}->exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '') {
|
if (defined($self->{option_results}->{username}) && $self->{option_results}->{username} ne '') {
|
||||||
if (!$imap_handle->login($self->{option_results}->{username}, $self->{option_results}->{password})) {
|
if (!$imap_handle->login($self->{option_results}->{username}, $self->{option_results}->{password})) {
|
||||||
# Exchange put '\r'...
|
# Exchange put '\r'...
|
||||||
my $output = $imap_handle->errstr;
|
my $output = $imap_handle->errstr;
|
||||||
$output =~ s/\r//g;
|
$output =~ s/\r//g;
|
||||||
$self->{output}->output_add(severity => $connection_exit,
|
$self->{output}->output_add(
|
||||||
short_msg => 'Login failed: ' . $output);
|
severity => $connection_exit,
|
||||||
|
short_msg => 'Login failed: ' . $output
|
||||||
|
);
|
||||||
quit();
|
quit();
|
||||||
$self->{output}->display();
|
$self->{output}->display();
|
||||||
$self->{output}->exit();
|
$self->{output}->exit();
|
||||||
|
|
|
@ -32,17 +32,18 @@ sub new {
|
||||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||||
bless $self, $class;
|
bless $self, $class;
|
||||||
|
|
||||||
$options{options}->add_options(arguments =>
|
$options{options}->add_options(arguments => {
|
||||||
{
|
'hostname:s' => { name => 'hostname' },
|
||||||
"hostname:s" => { name => 'hostname' },
|
'port:s' => { name => 'port', },
|
||||||
"port:s" => { name => 'port', },
|
'ssl' => { name => 'use_ssl' },
|
||||||
"ssl" => { name => 'use_ssl' },
|
'ssl-opt:s@' => { name => 'ssl_opt' },
|
||||||
"username:s" => { name => 'username' },
|
'username:s' => { name => 'username' },
|
||||||
"password:s" => { name => 'password' },
|
'password:s' => { name => 'password' },
|
||||||
"warning:s" => { name => 'warning' },
|
'warning:s' => { name => 'warning' },
|
||||||
"critical:s" => { name => 'critical' },
|
'critical:s' => { name => 'critical' },
|
||||||
"timeout:s" => { name => 'timeout', default => '30' },
|
'timeout:s' => { name => 'timeout', default => '30' }
|
||||||
});
|
});
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,26 +64,40 @@ sub check_options {
|
||||||
$self->{output}->add_option_msg(short_msg => "Please set the hostname option");
|
$self->{output}->add_option_msg(short_msg => "Please set the hostname option");
|
||||||
$self->{output}->option_exit();
|
$self->{output}->option_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my $append = '';
|
||||||
|
$self->{ssl_options} = '';
|
||||||
|
foreach (@{$self->{option_results}->{ssl_opt}}) {
|
||||||
|
if ($_ ne '') {
|
||||||
|
$self->{ssl_options} .= $append . $_;
|
||||||
|
$append = ', ';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub run {
|
sub run {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
my $timing0 = [gettimeofday];
|
my $timing0 = [gettimeofday];
|
||||||
|
|
||||||
apps::protocols::imap::lib::imap::connect($self, connection_exit => 'critical');
|
apps::protocols::imap::lib::imap::connect($self, connection_exit => 'critical');
|
||||||
apps::protocols::imap::lib::imap::quit();
|
apps::protocols::imap::lib::imap::quit();
|
||||||
|
|
||||||
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
|
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
|
||||||
|
|
||||||
my $exit = $self->{perfdata}->threshold_check(value => $timeelapsed,
|
my $exit = $self->{perfdata}->threshold_check(
|
||||||
threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
|
value => $timeelapsed,
|
||||||
$self->{output}->output_add(severity => $exit,
|
threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]
|
||||||
short_msg => sprintf("Response time %.3f ", $timeelapsed));
|
);
|
||||||
$self->{output}->perfdata_add(label => "time",
|
$self->{output}->output_add(
|
||||||
value => sprintf('%.3f', $timeelapsed),
|
severity => $exit,
|
||||||
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
|
short_msg => sprintf("Response time %.3f ", $timeelapsed)
|
||||||
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'));
|
);
|
||||||
|
$self->{output}->perfdata_add(
|
||||||
|
label => 'time',
|
||||||
|
value => sprintf('%.3f', $timeelapsed),
|
||||||
|
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
|
||||||
|
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical')
|
||||||
|
);
|
||||||
|
|
||||||
$self->{output}->display();
|
$self->{output}->display();
|
||||||
$self->{output}->exit();
|
$self->{output}->exit();
|
||||||
|
@ -109,7 +124,10 @@ Port used
|
||||||
=item B<--ssl>
|
=item B<--ssl>
|
||||||
|
|
||||||
Use SSL connection.
|
Use SSL connection.
|
||||||
(no attempt is made to check the certificate validity by default).
|
|
||||||
|
=item B<--ssl-opt>
|
||||||
|
|
||||||
|
Set SSL options: --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE" --ssl-opt="SSL_version => 'TLSv1'"
|
||||||
|
|
||||||
=item B<--username>
|
=item B<--username>
|
||||||
|
|
||||||
|
|
|
@ -31,20 +31,21 @@ sub new {
|
||||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||||
bless $self, $class;
|
bless $self, $class;
|
||||||
|
|
||||||
$options{options}->add_options(arguments =>
|
$options{options}->add_options(arguments => {
|
||||||
{
|
'hostname:s' => { name => 'hostname' },
|
||||||
"hostname:s" => { name => 'hostname' },
|
'port:s' => { name => 'port', },
|
||||||
"port:s" => { name => 'port', },
|
'ssl' => { name => 'use_ssl' },
|
||||||
"ssl" => { name => 'use_ssl' },
|
'ssl-opt:s@' => { name => 'ssl_opt' },
|
||||||
"username:s" => { name => 'username' },
|
'username:s' => { name => 'username' },
|
||||||
"password:s" => { name => 'password' },
|
'password:s' => { name => 'password' },
|
||||||
"warning:s" => { name => 'warning' },
|
'warning:s' => { name => 'warning' },
|
||||||
"critical:s" => { name => 'critical' },
|
'critical:s' => { name => 'critical' },
|
||||||
"timeout:s" => { name => 'timeout', default => '30' },
|
'timeout:s' => { name => 'timeout', default => '30' },
|
||||||
"search:s" => { name => 'search' },
|
'search:s' => { name => 'search' },
|
||||||
"delete" => { name => 'delete' },
|
'delete' => { name => 'delete' },
|
||||||
"folder:s" => { name => 'folder', default => 'INBOX' },
|
'folder:s' => { name => 'folder', default => 'INBOX' }
|
||||||
});
|
});
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,32 +63,47 @@ sub check_options {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!defined($self->{option_results}->{hostname})) {
|
if (!defined($self->{option_results}->{hostname})) {
|
||||||
$self->{output}->add_option_msg(short_msg => "Please set the --hostname option");
|
$self->{output}->add_option_msg(short_msg => 'Please set the --hostname option');
|
||||||
$self->{output}->option_exit();
|
$self->{output}->option_exit();
|
||||||
}
|
}
|
||||||
if (!defined($self->{option_results}->{search})) {
|
if (!defined($self->{option_results}->{search})) {
|
||||||
$self->{output}->add_option_msg(short_msg => "Please set the --search option");
|
$self->{output}->add_option_msg(short_msg => 'Please set the --search option');
|
||||||
$self->{output}->option_exit();
|
$self->{output}->option_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my $append = '';
|
||||||
|
$self->{ssl_options} = '';
|
||||||
|
foreach (@{$self->{option_results}->{ssl_opt}}) {
|
||||||
|
if ($_ ne '') {
|
||||||
|
$self->{ssl_options} .= $append . $_;
|
||||||
|
$append = ', ';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub run {
|
sub run {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
apps::protocols::imap::lib::imap::connect($self);
|
apps::protocols::imap::lib::imap::connect($self);
|
||||||
my ($num) = apps::protocols::imap::lib::imap::search($self);
|
my ($num) = apps::protocols::imap::lib::imap::search($self);
|
||||||
apps::protocols::imap::lib::imap::quit();
|
apps::protocols::imap::lib::imap::quit();
|
||||||
|
|
||||||
my $exit = $self->{perfdata}->threshold_check(value => $num,
|
|
||||||
threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
|
|
||||||
$self->{output}->output_add(severity => $exit,
|
|
||||||
short_msg => sprintf("%d message found(s)", $num));
|
|
||||||
$self->{output}->perfdata_add(label => "numbers",
|
|
||||||
value => $num,
|
|
||||||
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 => $num,
|
||||||
|
threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]
|
||||||
|
);
|
||||||
|
$self->{output}->output_add(
|
||||||
|
severity => $exit,
|
||||||
|
short_msg => sprintf('%d message(s) found', $num)
|
||||||
|
);
|
||||||
|
$self->{output}->perfdata_add(
|
||||||
|
label => 'numbers',
|
||||||
|
value => $num,
|
||||||
|
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
|
||||||
|
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
|
||||||
|
min => 0
|
||||||
|
);
|
||||||
|
|
||||||
$self->{output}->display();
|
$self->{output}->display();
|
||||||
$self->{output}->exit();
|
$self->{output}->exit();
|
||||||
}
|
}
|
||||||
|
@ -113,7 +129,10 @@ Port used
|
||||||
=item B<--ssl>
|
=item B<--ssl>
|
||||||
|
|
||||||
Use SSL connection.
|
Use SSL connection.
|
||||||
(no attempt is made to check the certificate validity by default).
|
|
||||||
|
=item B<--ssl-opt>
|
||||||
|
|
||||||
|
Set SSL options: --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE" --ssl-opt="SSL_version => 'TLSv1'"
|
||||||
|
|
||||||
=item B<--username>
|
=item B<--username>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue