- 论坛徽章:
- 0
|
nagios通过smtp发邮件
一、安装Mail-Sender
下载地址:http://search.cpan.org/CPAN/authors/id/J/JE/JENDA/Mail-Sender-0.8.13.tar.gz
安装略
二、建立/usr/local/nagios/bin/mail.pl
#!/usr/bin/perl
use Getopt::Std;
use Mail::Sender;
#f:mail from s:subject t:rcpt to
Getopt::Std::getopts('f:s:t:', \%options);
my $from = $options{f};
my $subject = $options{s};
my $to = $options{t};
my $date_command = "/bin/date";
my $date = `$date_command`; chop($date);
while (defined($line = )) {
$content .= $line;
}
open(LOG,">>/var/log/notify.log");
my $sender = new Mail::Sender({from => "$from",
smtp => '192.168.1.1'}); #注意修改smtp服务器IP
if (!(ref $sender) =~ /Sender/i) {
print LOG "$date: $Mail::Sender::Error\n";
}
if (ref ($sender->MailMsg({to => "$to",
subject => "$subject",
headers => "Content-Type: text/plain; charset=utf-8",
msg => "$content"}))) {
print LOG "$date: Sent Msg $content to $to OK\n";
} else {
print LOG "$date: $Mail::Sender::Error\n";
}
close(LOG);
exit(0);
三、定义命令
如:
define command{
command_name notify-by-email
command_line /usr/bin/printf "%b" "$LONGDATETIME$ Info: $SERVICEOUTPUT$" | /usr/local/nagios/bin/mail.pl -f "nagios@cnmsprod.local" -s "$TIME$ $SERVICEDESC$ on $HOSTNAME$ is $SERVICESTATE$!" -t $CONTACTEMAIL$
}
注意参数:
-f:发件人邮件地址
-s:邮件主题
-t:收件人邮件地址
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13444/showart_2096063.html |
|