| 1 |
#!/usr/bin/perl |
| •2 |
use strict; |
| 3 |
use POSIX qw(strftime); |
| 4 |
my $VERSION = '0.7.3'; |
| 5 |
my $COPYRIGHT = 'Copyright (C) 2005-2011 Jonathan Buhacoff <[email protected]>'; |
| 6 |
my $LICENSE = 'http://www.gnu.org/licenses/gpl.txt'; |
| 7 |
my %status = ( 'OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3 ); |
| 8 |
|
| 9 |
# look for required modules |
| 10 |
exit $status{UNKNOWN} unless load_modules(qw/Getopt::Long Net::SMTP/); |
| 11 |
|
| 12 |
BEGIN { |
| 13 |
if( grep { /^--hires$/ } @ARGV ) { |
| 14 |
eval "use Time::HiRes qw(time);"; |
| 15 |
warn "Time::HiRes not installed\n" if $@; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
Getopt::Long::Configure("bundling"); |
| 20 |
my $verbose = 0; |
| 21 |
my $help = ""; |
| 22 |
my $help_usage = ""; |
| 23 |
my $show_version = ""; |
| 24 |
my $smtp_server = ""; |
| 25 |
my $default_smtp_port = "25"; |
| 26 |
my $default_smtp_ssl_port = "465"; |
| 27 |
my $default_smtp_tls_port = "587"; |
| 28 |
my $smtp_port = ""; |
| 29 |
my @mailto = (); |
| 30 |
my $mailfrom = ""; |
| 31 |
my @header = (); |
| 32 |
my $body = ""; |
| 33 |
my $stdin = ""; |
| 34 |
my $template = ""; |
| 35 |
my $expect_response = "250"; |
| 36 |
my $warntime = 15; |
| 37 |
my $criticaltime = 30; |
| 38 |
my $timeout = 60; |
| 39 |
my $tls = 0; |
| 40 |
my $ssl = 0; |
| 41 |
my $auth_method = undef; |
| 42 |
my $username = ""; |
| 43 |
my $password = ""; |
| 44 |
my $time_hires = ""; |
| 45 |
my $mx_lookup = 0; |
| 46 |
my $ok; |
| 47 |
$ok = Getopt::Long::GetOptions( |
| 48 |
"V|version"=>\$show_version, |
| •49 |
"v|verbose+"=>\$verbose,"h|help"=>\$help,"usage"=>\$help_usage, |
| 50 |
"w|warning=i"=>\$warntime,"c|critical=i"=>\$criticaltime,"t|timeout=i"=>\$timeout, |
| 51 |
# smtp settings |
| 52 |
"H|hostname=s"=>\$smtp_server,"p|port=i"=>\$smtp_port, |
| 53 |
"mailto=s"=>\@mailto, "mailfrom=s",\$mailfrom, |
| 54 |
"header=s"=>\@header, "body=s"=>\$body, "stdin"=>\$stdin, |
| 55 |
"template!"=>\$template, |
| 56 |
# SSL/TLS/auth options |
| 57 |
"tls!"=>\$tls, "ssl!"=>\$ssl, "auth=s"=>\$auth_method, |
| 58 |
"U|username=s"=>\$username,"P|password=s"=>\$password, |
| 59 |
# Server response |
| 60 |
"E|expect-response=s"=>\$expect_response, |
| 61 |
# Time |
| 62 |
"hires"=>\$time_hires, |
| 63 |
); |
| 64 |
|
| 65 |
if( $show_version ) { |
| 66 |
print "$VERSION\n"; |
| 67 |
if( $verbose ) { |
| •68 |
print "Default warning threshold: $warntime seconds\n"; |
| 69 |
print "Default critical threshold: $criticaltime seconds\n"; |
| 70 |
print "Default timeout: $timeout seconds\n"; |
| 71 |
} |
| 72 |
exit $status{UNKNOWN}; |
| 73 |
} |
| 74 |
|
| 75 |
if( $help ) { |
| 76 |
exec "perldoc", $0 or print "Try `perldoc $0`\n"; |
| 77 |
exit $status{UNKNOWN}; |
| 78 |
} |
| 79 |
|
| 80 |
if( $smtp_server eq "" && scalar(@mailto) == 1 ) { |
| 81 |
# no SMTP server specified but one mailto address given means we can look up the MX record |
| 82 |
$mx_lookup = 1; |
| 83 |
} |
| 84 |
|
| 85 |
my @required_module = (); |
| 86 |
push @required_module, 'Net::SMTP::SSL' if $ssl; |
| 87 |
push @required_module, ('MIME::Base64','Authen::SASL') if $ssl && $username; |
| 88 |
push @required_module, 'Net::SMTP::TLS' if $tls; |
| 89 |
push @required_module, 'Net::SMTP_auth' if $auth_method and not $tls; # whereas if auth_method and tls we use TLS_auth, which is included in this script! |
| 90 |
push @required_module, 'Text::Template' if $template; |
| 91 |
push @required_module, 'Net::DNS' if $mx_lookup; |
| 92 |
push @required_module, 'Email::Address' if $mx_lookup; |
| 93 |
exit $status{UNKNOWN} unless load_modules(@required_module); |
| 94 |
|
| 95 |
|
| 96 |
# split up @mailto if commas were used instead of multiple options |
| 97 |
@mailto = split(/,/,join(',',@mailto)); |
| 98 |
|
| 99 |
if( $help_usage || |
| 100 |
( |
| 101 |
($smtp_server eq "" && !$mx_lookup) || scalar(@mailto)==0 || $mailfrom eq "" |
| 102 |
) |
| 103 |
) { |
| 104 |
print "Usage: $0 [-H host [-p port]] --mailto recipient\@your.net [--mailto recipient2\@your.net ...] --mailfrom sender\@your.net --body 'some text' [-w <seconds>] [-c <seconds>]\n"; |
| 105 |
exit $status{UNKNOWN}; |
| 106 |
} |
| 107 |
|
| 108 |
# initialize |
| 109 |
my $report = new PluginReport; |
| 110 |
my $time_start = time; |
| •111 |
my $actual_response = undef; |
| 112 |
my @warning = (); |
| 113 |
my @critical = (); |
| 114 |
|
| 115 |
my $smtp_debug = 0; |
| 116 |
$smtp_debug = 1 if $verbose >= 3; |
| 117 |
|
| 118 |
# default date and message id headers |
| 119 |
push @header, default_date_header() unless find_header("Date",@header); |
| 120 |
push @header, default_messageid_header() unless find_header("Message-ID",@header); |
| 121 |
|
| 122 |
# look up MX server if necessary |
| 123 |
if( $mx_lookup ) { |
| 124 |
my $addr = Email::Address->new( undef, $mailto[0] ); |
| 125 |
my $mx_domain = $addr->host; |
| 126 |
print "MX lookup " . $mx_domain . "\n" if $verbose > 1; |
| 127 |
my $res = Net::DNS::Resolver->new; |
| 128 |
my @mx = Net::DNS::mx($res, $mx_domain); |
| 129 |
if( @mx ) { |
| 130 |
# use the first server |
| 131 |
foreach my $rr (@mx) { |
| 132 |
print "pref : " . $rr->preference . " exchange: " . $rr->exchange . "\n" if $verbose > 2; |
| 133 |
} |
| 134 |
$smtp_server = $mx[0]->exchange; |
| 135 |
print "smtp server: $smtp_server\n" if $verbose; |
| 136 |
} |
| 137 |
else { |
| 138 |
print "SMTP SEND CRITICAL - Cannot find MX records for $mx_domain\n"; |
| 139 |
exit $status{CRITICAL}; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
# connect to SMTP server |
| 144 |
# create the smtp handle using Net::SMTP, Net::SMTP::SSL, Net::SMTP::TLS, or an authentication variant |
| 145 |
my $smtp; |
| 146 |
eval { |
| 147 |
if( $tls and $auth_method ) { |
| 148 |
$smtp_port = $default_smtp_tls_port unless $smtp_port; |
| 149 |
$smtp = TLS_auth->new($smtp_server, Timeout=>$timeout, Port=>$smtp_port, User=>$username, Password=>$password, Auth_Method=>$auth_method); |
| 150 |
if( $smtp ) { |
| 151 |
my $message = oneline($smtp->message()); |
| 152 |
die "cannot connect with TLS/$auth_method: $message" if $smtp->code() =~ m/53\d/; |
| 153 |
} |
| 154 |
} |
| 155 |
elsif( $tls ) { |
| •156 |
$smtp_port = $default_smtp_tls_port unless $smtp_port; |
| 157 |
$smtp = Net::SMTP::TLS->new($smtp_server, Timeout=>$timeout, Port=>$smtp_port, User=>$username, Password=>$password); |
| 158 |
if( $smtp ) { |
| 159 |
my $message = oneline($smtp->message()); |
| •160 |
die "cannot connect with TLS: $message" if $smtp->code() =~ m/53\d/; |
| 161 |
} |
| 162 |
} |
| 163 |
elsif( $ssl ) { |
| •164 |
$smtp_port = $default_smtp_ssl_port unless $smtp_port; |
| 165 |
$smtp = Net::SMTP::SSL->new($smtp_server, Port => $smtp_port, Timeout=>$timeout,Debug=>$smtp_debug); |
| 166 |
if( $smtp && $username ) { |
| 167 |
$smtp->auth($username, $password); |
| 168 |
my $message = oneline($smtp->message()); |
| 169 |
die "cannot connect with SSL/password: $message" if $smtp->code() =~ m/53\d/; |
| 170 |
} |
| •171 |
} |
| 172 |
elsif( $auth_method ) { |
| 173 |
$smtp_port = $default_smtp_port unless $smtp_port; |
| 174 |
$smtp = Net::SMTP_auth->new($smtp_server, Port=>$smtp_port, Timeout=>$timeout,Debug=>$smtp_debug); |
| 175 |
if( $smtp ) { |
| 176 |
$smtp->auth($auth_method, $username, $password); |
| 177 |
my $message = oneline($smtp->message()); |
| •178 |
die "cannot connect with SSL/$auth_method: $message" if $smtp->code() =~ m/53\d/; |
| 179 |
} |
| 180 |
} |
| 181 |
else { |
| 182 |
$smtp_port = $default_smtp_port unless $smtp_port; |
| 183 |
$smtp = Net::SMTP->new($smtp_server, Port=>$smtp_port, Timeout=>$timeout,Debug=>$smtp_debug); |
| 184 |
if( $smtp && $username ) { |
| 185 |
$smtp->auth($username, $password); |
| 186 |
my $message = oneline($smtp->message()); |
| 187 |
die "cannot connect with password: $message" if $smtp->code() =~ m/53\d/; |
| 188 |
} |
| 189 |
} |
| 190 |
}; |
| 191 |
if( $@ ) { |
| 192 |
$@ =~ s/\n/ /g; # the error message can be multiline but we want our output to be just one line |
| 193 |
print "SMTP SEND CRITICAL - $@\n"; |
| 194 |
exit $status{CRITICAL}; |
| 195 |
} |
| 196 |
unless( $smtp ) { |
| 197 |
print "SMTP SEND CRITICAL - Could not connect to $smtp_server port $smtp_port\n"; |
| 198 |
exit $status{CRITICAL}; |
| 199 |
} |
| 200 |
my $time_connected = time; |
| 201 |
|
| 202 |
# add the monitored server's banner to the report |
| 203 |
if( $tls ) { |
| 204 |
$report->{banner} = ""; |
| 205 |
} |
| 206 |
elsif( $ssl ) { |
| 207 |
$report->{banner} = $smtp->banner || ""; |
| 208 |
chomp $report->{banner}; |
| 209 |
} |
| 210 |
else { |
| 211 |
$report->{banner} = $smtp->banner || ""; |
| 212 |
chomp $report->{banner}; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
# send email |
| 217 |
if( $stdin ) { |
| 218 |
$body = ""; |
| 219 |
while(<STDIN>) { |
| 220 |
$body .= $_; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
# if user wants to use template substitutions, this is the place to process body and headers |
| 225 |
if( $template ) { |
| 226 |
foreach my $item (@header,$body) { |
| 227 |
my $t = Text::Template->new(TYPE=>'STRING',SOURCE=>$item,PACKAGE=>'SmtpMessageTemplate'); |
| 228 |
$item = $t->fill_in(PREPEND=>q{package SmtpMessageTemplate;}); |
| 229 |
# print "item: $item\n"; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
$smtp->mail($mailfrom); |
| 235 |
foreach( @mailto ) { |
| 236 |
# the two SMTP modules have different error reporting mechanisms: |
| 237 |
if( $tls ) { |
| 238 |
# Net::SMTP::TLS croaks when the recipient is rejected |
| 239 |
eval { |
| 240 |
$smtp->to($_); |
| 241 |
}; |
| 242 |
if( $@ ) { |
| 243 |
print "SMTP SEND CRITICAL - Could not send to $_\n"; |
| 244 |
print "Reason: $@\n" if $verbose; |
| 245 |
exit $status{CRITICAL}; |
| 246 |
} |
| 247 |
} |
| 248 |
else { |
| 249 |
# Net::SMTP returns false when the recipient is rejected |
| 250 |
my $to_returned = $smtp->to($_); |
| 251 |
if( !$to_returned ) { |
| 252 |
print "SMTP SEND CRITICAL - Could not send to $_\n"; |
| 253 |
print "Reason: Recipient rejected or authentication failed\n" if $verbose; |
| 254 |
exit $status{CRITICAL}; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
# Net::SMTP::TLS doesn't implement code() so we need to wrap calls in eval to get our error messages |
| 260 |
|
| 261 |
# start data transfer (expect response 354) |
| 262 |
$smtp->data(); |
| 263 |
|
| 264 |
# send data |
| 265 |
$smtp->datasend("To: ".join(", ",@mailto)."\n"); |
| 266 |
$smtp->datasend("From: $mailfrom\n"); |
| 267 |
foreach( @header ) { |
| 268 |
$smtp->datasend("$_\n"); |
| 269 |
} |
| 270 |
$smtp->datasend("\n"); |
| 271 |
$smtp->datasend($body); |
| 272 |
$smtp->datasend("\n"); |
| 273 |
|
| 274 |
eval { |
| 275 |
# end data transfer (expect response 250) |
| 276 |
$smtp->dataend(); |
| 277 |
}; |
| 278 |
if( $@ ) { |
| 279 |
$actual_response = $tls ? get_tls_error($@) : $smtp->code(); |
| 280 |
} |
| 281 |
else { |
| 282 |
$actual_response = $tls ? "250" : $smtp->code(); # no error means we got 250 |
| 283 |
} |
| 284 |
|
| 285 |
eval { |
| 286 |
# disconnect from SMTP server (expect response 221) |
| 287 |
$smtp->quit(); |
| 288 |
}; |
| 289 |
if( $@ ) { |
| 290 |
push @warning, "Error while disconnecting from $smtp_server"; |
| 291 |
} |
| 292 |
|
| 293 |
# calculate elapsed time and issue warnings |
| 294 |
my $time_end = time; |
| 295 |
my $elapsedtime = $time_end - $time_start; |
| 296 |
$report->{seconds} = $elapsedtime; |
| 297 |
|
| 298 |
push @warning, "connection time more than $warntime" if( $time_connected - $time_start > $warntime ); |
| 299 |
push @critical, "connection time more than $criticaltime" if( $time_connected - $time_start > $criticaltime ); |
| 300 |
push @critical, "response was $actual_response but expected $expect_response" if ( $actual_response ne $expect_response ); |
| 301 |
|
| 302 |
# print report and exit with known status |
| 303 |
my $perf_data = "elapsed=".$report->{seconds}."s;$warntime;$criticaltime"; # TODO: need a component for safely generating valid perf data format. for notes on the format, see http://www.perfparse.de/tiki-view_faq.php?faqId=6 and http://nagiosplug.sourceforge.net/developer-guidelines.html#AEN185 |
| 304 |
my $short_report = $report->text(qw/seconds/) . " | $perf_data"; |
| 305 |
my $long_report = join("", map { "$_: $report->{$_}\n" } qw/banner/ ); |
| 306 |
if( scalar @critical ) { |
| 307 |
my $crit_alerts = join(", ", @critical); |
| 308 |
print "SMTP SEND CRITICAL - $crit_alerts; $short_report\n"; |
| 309 |
print $long_report if $verbose; |
| 310 |
exit $status{CRITICAL}; |
| 311 |
} |
| 312 |
if( scalar @warning ) { |
| 313 |
my $warn_alerts = join(", ", @warning); |
| 314 |
print "SMTP SEND WARNING - $warn_alerts; $short_report\n"; |
| 315 |
print $long_report if $verbose; |
| 316 |
exit $status{WARNING}; |
| 317 |
} |
| 318 |
print "SMTP SEND OK - $short_report\n"; |
| 319 |
print $long_report if $verbose; |
| 320 |
exit $status{OK}; |
| 321 |
|
| 322 |
|
| 323 |
# utility to load required modules. exits if unable to load one or more of the modules. |
| 324 |
sub load_modules { |
| 325 |
my @missing_modules = (); |
| 326 |
foreach( @_ ) { |
| 327 |
eval "require $_"; |
| 328 |
push @missing_modules, $_ if $@; |
| 329 |
} |
| 330 |
if( @missing_modules ) { |
| 331 |
print "Missing perl modules: @missing_modules\n"; |
| 332 |
return 0; |
| 333 |
} |
| 334 |
return 1; |
| 335 |
} |
| 336 |
|
| 337 |
# utility to extract error codes out of Net::SMTP::TLS croak messages |
| 338 |
sub get_tls_error { |
| 339 |
my ($errormsg) = @_; |
| 340 |
$errormsg =~ m/: (\d+) (.+)/; |
| 341 |
my $code = $1; |
| 342 |
return $code; |
| 343 |
} |
| 344 |
|
| 345 |
# looks for a specific header in a list of headers; returns true if found |
| 346 |
sub find_header { |
| 347 |
my ($name, @list) = @_; |
| 348 |
return scalar grep { m/^$name: /i } @list; |
| 349 |
} |
| 350 |
|
| 351 |
# RFC 2822 date header |
| 352 |
sub default_date_header { |
| 353 |
return strftime "Date: %a, %e %b %Y %H:%M:%S %z (%Z)", gmtime; |
| 354 |
} |
| 355 |
|
| 356 |
# RFC 2822 message id header |
| 357 |
sub default_messageid_header { |
| 358 |
my $random = randomstring(16,qw/0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z/); |
| 359 |
my $hostname = `hostname`; |
| 360 |
chomp $hostname; |
| 361 |
return "Message-ID: <".time.".".$random.".checksmtpsend@".$hostname.">"; |
| 362 |
} |
| 363 |
|
| 364 |
# returns a random string of specified length using characters from specified set |
| 365 |
sub randomstring { |
| 366 |
my ($length,@set) = @_; |
| 367 |
my $size = scalar @set; |
| 368 |
my $string = ""; |
| 369 |
while($length--) { |
| 370 |
$string .= $set[int(rand($size))]; |
| 371 |
} |
| 372 |
return $string; |
| 373 |
} |
| 374 |
|
| 375 |
# replaces all newlines in the input string with spaces |
| 376 |
sub oneline { |
| 377 |
my ($input) = @_; |
| 378 |
$input =~ s/[\r\n]+/ /g; |
| 379 |
return $input; |
| 380 |
} |
| 381 |
|
| 382 |
# NAME |
| 383 |
# PluginReport |
| 384 |
# SYNOPSIS |
| 385 |
# $report = new PluginReport; |
| 386 |
# $report->{label1} = "value1"; |
| 387 |
# $report->{label2} = "value2"; |
| 388 |
# print $report->text(qw/label1 label2/); |
| 389 |
package PluginReport; |
| 390 |
|
| 391 |
sub new { |
| 392 |
my ($proto,%p) = @_; |
| 393 |
my $class = ref($proto) || $proto; |
| 394 |
my $self = bless {}, $class; |
| 395 |
$self->{$_} = $p{$_} foreach keys %p; |
| 396 |
return $self; |
| 397 |
} |
| 398 |
|
| 399 |
sub text { |
| 400 |
my ($self,@labels) = @_; |
| 401 |
my @report = map { "$self->{$_} $_" } grep { defined $self->{$_} } @labels; |
| 402 |
my $text = join(", ", @report); |
| 403 |
return $text; |
| 404 |
} |
| 405 |
|
| 406 |
package SmtpMessageTemplate; |
| 407 |
|
| 408 |
sub trim { |
| 409 |
my ($text) = @_; |
| 410 |
$text =~ s/^\s*//; |
| 411 |
$text =~ s/\s*$//; |
| 412 |
return $text; |
| 413 |
} |
| 414 |
|
| 415 |
# NAME |
| 416 |
# TLS_auth |
| 417 |
# SYNOPSYS |
| 418 |
# |
| 419 |
# Based on contribution by Brad Guillory |
| 420 |
package TLS_auth; |
| 421 |
#use Net::SMTP::TLS; |
| 422 |
our @ISA = qw(Net::SMTP::TLS); |
| 423 |
use Carp; |
| 424 |
sub new { |
| 425 |
my ($proto,$server,%p) = @_; |
| 426 |
my $class = ref($proto) || $proto; |
| 427 |
#my $self = bless {}, $class; |
| 428 |
no strict 'refs'; |
| 429 |
no warnings 'once'; |
| 430 |
*Net::SMTP::TLS::login = *TLS_auth::login; # override parent's login with ours so when it's called in the constructor, our overriden version will be used |
| 431 |
my $self = Net::SMTP::TLS->new($server,%p); |
| 432 |
return $self; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
sub login { |
| 437 |
my ($self) = @_; |
| 438 |
my $type = $self->{features}->{AUTH}; |
| 439 |
if(not $type){ |
| 440 |
die "Server did not return AUTH in capabilities\n"; # croak |
| 441 |
} |
| 442 |
# print "Feature: $type\nAuth Method: $self->{Auth_Method}\n"; |
| 443 |
if($type =~ /CRAM\-MD5/ and $self->{Auth_Method} =~ /CRAM\-MD5/i){ |
| 444 |
$self->auth_MD5(); |
| 445 |
}elsif($type =~ /LOGIN/ and $self->{Auth_Method} =~ /LOGIN/i){ |
| 446 |
$self->auth_LOGIN(); |
| 447 |
}elsif($type =~ /PLAIN/ and $self->{Auth_Method} =~ /PLAIN/i){ |
| 448 |
#print "Calling auth_PLAIN\n"; |
| 449 |
$self->auth_PLAIN(); |
| 450 |
}else{ |
| 451 |
die "Unsupported Authentication mechanism: $self->{Auth_Method}\n"; # croak |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
package main; |
| 457 |
1; |
| 458 |
|
| 459 |
__END__ |
| 460 |
|
| 461 |
=pod |
| 462 |
|
| 463 |
=head1 NAME |
| 464 |
|
| 465 |
check_smtp_send - connects to an SMTP server and sends a message |
| 466 |
|
| 467 |
=head1 SYNOPSIS |
| 468 |
|
| 469 |
check_smtp_send -vV |
| 470 |
check_smtp_send -? |
| 471 |
check_smtp_send --help |
| 472 |
|
| 473 |
=head1 OPTIONS |
| 474 |
|
| 475 |
=over |
| 476 |
|
| 477 |
=item --warning <seconds> |
| 478 |
|
| 479 |
Warn if it takes longer than <seconds> to connect to the SMTP server. Default is 15 seconds. |
| 480 |
Also known as: -w <seconds> |
| 481 |
|
| 482 |
=item --critical <seconds> |
| 483 |
|
| 484 |
Return a critical status if it takes longer than <seconds> to connect to the SMTP server. Default is 30 seconds. |
| 485 |
Also known as: -c <seconds> |
| 486 |
|
| 487 |
=item --timeout <seconds> |
| 488 |
|
| 489 |
Abort with critical status if it takes longer than <seconds> to connect to the SMTP server. Default is 60 seconds. |
| 490 |
The difference between timeout and critical is that, with the default settings, if it takes 45 seconds to |
| 491 |
connect to the server then the connection will succeed but the plugin will return CRITICAL because it took longer |
| 492 |
than 30 seconds. |
| 493 |
Also known as: -t <seconds> |
| 494 |
|
| 495 |
=item --hostname <server> |
| 496 |
|
| 497 |
Address or name of the SMTP server. Examples: mail.server.com, localhost, 192.168.1.100 |
| 498 |
|
| 499 |
If not provided, and if there is only one --mailto address, the script will automatically look up the MX record |
| 500 |
for the --mailto address and use that as the hostname. You can use this to check that your MX records are correct. |
| 501 |
When omitting the --hostname option, it doesn't really make sense to specify --port, --username, or --password |
| 502 |
but you can still do so and they will have their normal effect. To look up the MX records you need to have the |
| 503 |
module Net::DNS and Email::Address installed. |
| 504 |
|
| 505 |
Also known as: -H <server> |
| 506 |
|
| 507 |
=item --port <number> |
| 508 |
|
| 509 |
Service port on the SMTP server. Default is 25 for regular SMTP, 465 for SSL, and 587 for TLS. |
| 510 |
Also known as: -p <number> |
| 511 |
|
| 512 |
=item --tls |
| 513 |
|
| 514 |
=item --notls |
| 515 |
|
| 516 |
Enable TLS/AUTH protocol. Requires Net::SMTP::TLS, availble on CPAN. |
| 517 |
|
| 518 |
When using this option, the default port is 587. |
| 519 |
You can specify a port from the command line using the --port option. |
| 520 |
|
| 521 |
Use the notls option to turn off the tls option. |
| 522 |
|
| 523 |
Also, you may need to fix your copy of Net::SMTP::TLS. Here is the diff against version 0.12: |
| 524 |
|
| 525 |
254c254 |
| 526 |
< $me->_command(sprintf("AUTH PLAIN %S", |
| 527 |
--- |
| 528 |
> $me->_command(sprintf("AUTH PLAIN %s", |
| 529 |
|
| 530 |
|
| 531 |
=item --ssl |
| 532 |
|
| 533 |
=item --nossl |
| 534 |
|
| 535 |
Enable SSL protocol. Requires Net::SMTP::SSL and Authen::SASL, availble on CPAN. |
| 536 |
|
| 537 |
When using this option, the default port is 465. You can override with the --port option. |
| 538 |
|
| 539 |
Use the nossl option to turn off the ssl option. |
| 540 |
|
| 541 |
=item --auth <method> |
| 542 |
|
| 543 |
Enable authentication with Net::SMTP_auth (sold separately). |
| 544 |
For example, try using --auth PLAIN or --auth CRAM-MD5. |
| 545 |
|
| 546 |
=item --username <username> |
| 547 |
|
| 548 |
=item --password <password> |
| 549 |
|
| 550 |
Username and password to use when connecting to SMTP server. |
| 551 |
Also known as: -U <username> -P <password> |
| 552 |
|
| 553 |
=item --body <message> |
| 554 |
|
| 555 |
Use this option to specify the body of the email message. If you need newlines in your message, |
| 556 |
you might need to use the --stdin option instead. |
| 557 |
|
| 558 |
=item --header <header> |
| 559 |
|
| 560 |
Use this option to set an arbitrary header in the message. You can use it multiple times. |
| 561 |
|
| 562 |
=item --stdin |
| 563 |
|
| 564 |
Grab the body of the email message from stdin. |
| 565 |
|
| 566 |
=item --mailto [email protected] |
| 567 |
|
| 568 |
You can send a message to multiple recipients by repeating this option or by separating |
| 569 |
the email addresses with commas (no whitespace allowed): |
| 570 |
|
| 571 |
$ check_smtp_send -H mail.server.net --mailto [email protected],[email protected] --mailfrom [email protected] |
| 572 |
|
| 573 |
SMTP SEND OK - 1 seconds |
| 574 |
|
| 575 |
=item --mailfrom [email protected] |
| 576 |
|
| 577 |
Use this option to set the "from" address in the email. |
| 578 |
|
| 579 |
=item --template |
| 580 |
|
| 581 |
=item --notemplate |
| 582 |
|
| 583 |
Enable (or disable) processing of message body and headers. Requires Text::Template. |
| 584 |
|
| 585 |
Use this option to apply special processing to your message body and headers that allows you to use the |
| 586 |
results of arbitrary computations in the text. For example, you can use this feature to send a message |
| 587 |
containing the hostname of the machine that sent the message without customizing the plugin configuration |
| 588 |
on each machine. |
| 589 |
|
| 590 |
When you enable the --template option, the message body and headers are parsed by |
| 591 |
Text::Template. Even a message body provided using the --stdin option will be parsed. |
| 592 |
See the Text::Template manual for more information, but in general any expression |
| 593 |
written in Perl will work. |
| 594 |
|
| 595 |
There is one convenience function provided to you, trim, which will remove leading and trailing whitespace |
| 596 |
from its parameter. Here's an example: |
| 597 |
|
| 598 |
check_smtp_send -H mail.server.net --mailto [email protected] --mailfrom [email protected] |
| 599 |
--template --body 'hello, this message is from {use Sys::Hostname; hostname}' |
| 600 |
--header 'Subject: test message from {trim(`whoami`)}' |
| 601 |
|
| 602 |
|
| 603 |
=item --expect-response <code> |
| 604 |
|
| 605 |
Use this option to specify which SMTP response code should be expected from the server |
| 606 |
after the SMTP dialog is complete. The default is 250 (message accepted). |
| 607 |
|
| 608 |
Also known as: -E <code> |
| 609 |
|
| 610 |
=item --hires |
| 611 |
|
| 612 |
Use the Time::HiRes module to measure time, if available. |
| 613 |
|
| 614 |
=item --verbose |
| 615 |
|
| 616 |
Display additional information. Useful for troubleshooting. |
| 617 |
|
| 618 |
One --verbose will show extra information for OK, WARNING, and CRITICAL status. |
| 619 |
|
| 620 |
Use one --verbose together with --version to see the default warning and critical timeout values. |
| 621 |
|
| 622 |
Three --verbose (or -vvv) will show debug information, unless you're using --tls because Net::SMTP::TLS |
| 623 |
does not have a Debug feature. |
| 624 |
|
| 625 |
Also known as: -v |
| 626 |
|
| 627 |
=item --version |
| 628 |
|
| 629 |
Display plugin version and exit. |
| 630 |
Also known as: -V |
| 631 |
|
| 632 |
=item --help |
| 633 |
|
| 634 |
Display this documentation and exit. Does not work in the ePN version. |
| 635 |
Also known as: -h |
| 636 |
|
| 637 |
=item --usage |
| 638 |
|
| 639 |
Display a short usage instruction and exit. |
| 640 |
|
| 641 |
=back |
| 642 |
|
| 643 |
=head1 EXAMPLES |
| 644 |
|
| 645 |
=head2 Send a message with custom headers |
| 646 |
|
| 647 |
$ check_smtp_send -H mail.server.net --mailto [email protected] --mailfrom [email protected] |
| 648 |
--body 'Homeruns 5' --header 'Subject: Hello, world!' --header 'X-Your-Header: Yes' |
| 649 |
|
| 650 |
SMTP SEND OK - 1 seconds |
| 651 |
|
| 652 |
=head1 EXIT CODES |
| 653 |
|
| 654 |
Complies with the Nagios plug-in specification: |
| 655 |
0 OK The plugin was able to check the service and it appeared to be functioning properly |
| 656 |
1 Warning The plugin was able to check the service, but it appeared to be above some "warning" threshold or did not appear to be working properly |
| 657 |
2 Critical The plugin detected that either the service was not running or it was above some "critical" threshold |
| 658 |
3 Unknown Invalid command line arguments were supplied to the plugin or the plugin was unable to check the status of the given hosts/service |
| 659 |
|
| 660 |
=head1 NAGIOS PLUGIN NOTES |
| 661 |
|
| 662 |
Nagios plugin reference: http://nagiosplug.sourceforge.net/developer-guidelines.html |
| 663 |
|
| 664 |
This plugin does NOT use Nagios DEFAULT_SOCKET_TIMEOUT (provided by utils.pm as $TIMEOUT) because |
| 665 |
the path to utils.pm must be specified completely in this program and forces users to edit the source |
| 666 |
code if their install location is different (if they realize this is the problem). You can view |
| 667 |
the default timeout for this module by using the --verbose and --version options together. The |
| 668 |
short form is -vV. |
| 669 |
|
| 670 |
Other than that, it attempts to follow published guidelines for Nagios plugins. |
| 671 |
|
| 672 |
=head1 CHANGES |
| 673 |
|
| 674 |
Wed Oct 29 14:05:00 PST 2005 |
| 675 |
+ version 0.1 |
| 676 |
|
| 677 |
Wed Nov 9 15:01:48 PST 2005 |
| 678 |
+ now using an inline PluginReport package to generate the report |
| 679 |
+ added stdin option |
| 680 |
+ copyright notice and GNU GPL |
| 681 |
+ version 0.2 |
| 682 |
|
| 683 |
Thu Apr 20 16:00:00 PST 2006 (by Geoff Crompton <[email protected]>) |
| 684 |
+ added bailing if the $smtp->to() call fails |
| 685 |
+ added support for mailto recipients separated by commas |
| 686 |
+ version 0.2.1 |
| 687 |
|
| 688 |
Tue Apr 24 21:17:53 PDT 2007 |
| 689 |
+ moved POD text to separate file in order to accomodate the new embedded-perl Nagios feature |
| 690 |
+ version 0.2.3 |
| 691 |
|
| 692 |
Fri Apr 27 20:26:42 PDT 2007 |
| 693 |
+ documentation now mentions every command-line option accepted by the plugin, including abbreviations |
| 694 |
+ version 0.3 |
| 695 |
|
| 696 |
Sun Oct 21 10:34:14 PDT 2007 |
| 697 |
+ added support for TLS and authentication via the Net::SMTP::TLS module. see --tls option. |
| 698 |
+ version 0.4 |
| 699 |
|
| 700 |
Sun Oct 21 13:54:26 PDT 2007 |
| 701 |
+ added support for SSL via the Net::SMTP::SSL module. see --ssl option. |
| 702 |
+ port is no longer a required option. defaults to 25 for regular smtp, 465 for ssl, and 587 for tls. |
| 703 |
+ added port info to the "could not connect" error message |
| 704 |
+ version 0.4.1 |
| 705 |
|
| 706 |
Tue Dec 4 07:42:32 PST 2007 |
| 707 |
+ added --usage option because the official nagios plugins have both --help and --usage |
| 708 |
+ added --timeout option to match the official nagios plugins |
| 709 |
+ fixed some minor pod formatting issues for perldoc |
| 710 |
+ version 0.4.2 |
| 711 |
|
| 712 |
Mon Feb 11 19:09:37 PST 2008 |
| 713 |
+ fixed a bug for embedded perl version, variable "%status" will not stay shared in load_modules |
| 714 |
+ version 0.4.3 |
| 715 |
|
| 716 |
Mon May 26 09:12:14 PDT 2008 |
| 717 |
+ fixed warning and critical messages to use "more than" or "less than" instead of the angle brackets, to make them more web friendly |
| 718 |
+ version 0.4.4 |
| 719 |
|
| 720 |
Wed Jul 2 07:12:35 PDT 2008 |
| 721 |
+ added --expect-response option submitted by Christian Kauhaus <[email protected]> |
| 722 |
+ added support for authentication via Net::SMTP_auth. see --auth option. |
| 723 |
+ version 0.4.5 |
| 724 |
|
| 725 |
Sun Oct 5 15:18:23 PDT 2008 |
| 726 |
+ added error handling for smtp server disconnects ungracefully during QUIT (gmail.com does) |
| 727 |
+ version 0.4.6 |
| 728 |
|
| 729 |
Thu Oct 1 12:09:35 PDT 2009 |
| 730 |
+ added --template option to allow arbitrary substitutions for body and headers, and provided one convenience function for trimming strings |
| 731 |
+ added performance data for use with PNP4Nagios! |
| 732 |
+ version 0.5.0 |
| 733 |
|
| 734 |
Thu Oct 8 11:17:04 PDT 2009 |
| 735 |
+ added more detailed error messages when using --verbose |
| 736 |
+ version 0.5.1 |
| 737 |
|
| 738 |
Tue Feb 9 12:14:49 PST 2010 |
| 739 |
+ added support for combining --auth with --tls using a subclass of Net::SMTP::TLS submitted by Brad Guillory; please note that to use the "PLAIN" authentication type you need to patch your Net::SMTP:TLS because it has a bug in sub auth_PLAIN (sprintf %S instead of %s) |
| 740 |
+ version 0.5.2 |
| 741 |
|
| 742 |
Mon Jan 3 10:39:42 PST 2011 |
| 743 |
+ added default Date and Message-ID headers; Date header uses POSIX strftime and Message-ID header uses hostname command to get localhost name |
| 744 |
+ version 0.7.0 |
| 745 |
|
| 746 |
Fri May 6 08:35:09 AST 2011 |
| 747 |
+ added --hires option to enable use of Time::Hires if available |
| 748 |
+ version 0.7.1 |
| 749 |
|
| 750 |
Wed Jul 6 19:18:26 AST 2011 |
| 751 |
+ the --hostname is now optional; if not provided the plugin will lookup the MX record for the --mailto address (requires Net::DNS) |
| 752 |
+ version 0.7.2 |
| 753 |
|
| 754 |
Tue Dec 13 09:24:04 PST 2011 |
| 755 |
+ separated authentication errors from connection errors |
| 756 |
+ version 0.7.3 |
| 757 |
|
| 758 |
=head1 AUTHOR |
| 759 |
|
| 760 |
Jonathan Buhacoff <[email protected]> |
| 761 |
|
| 762 |
=head1 COPYRIGHT AND LICENSE |
| 763 |
|
| 764 |
Copyright (C) 2005-2011 Jonathan Buhacoff |
| 765 |
|
| 766 |
This program is free software; you can redistribute it and/or modify |
| 767 |
it under the terms of the GNU General Public License as published by |
| 768 |
the Free Software Foundation; either version 3 of the License, or |
| 769 |
(at your option) any later version. |
| 770 |
|
| 771 |
This program is distributed in the hope that it will be useful, |
| 772 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 773 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 774 |
GNU General Public License for more details. |
| 775 |
|
| 776 |
You should have received a copy of the GNU General Public License |
| 777 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 778 |
|
| 779 |
http://www.gnu.org/licenses/gpl.txt |
| 780 |
|
| 781 |
=cut |
| 782 |
|