--> nagios默认使用mail发送邮件通知,系统如果关闭sendmail的话,邮件就不会被发出去,因此我写了一个用Perl脚本,通过Email::Send模块来发送邮件通知。
我将这个脚本命名为notify_via_smtp,并将其保存在Nagios安装目录的libexec目录下,脚本内容如下:
#!/usr/bin/perl -w
use strict;
use Email::Send;
use Getopt::Std;
my %opts;
getopt('ts',\%opts);
my $subject=$opts{s};
my $receiver=$opts{t};
my $sender='nagios@supersun.biz';
my $string=join '',<>;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime;
$year+=1900;
my @month=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @week=qw(Sun Mon Tue Wed Thu Fri Sat);
my $time=$week[$wday].",".$mday.' '.$month[$mon].' '.$year.' '.$hour.':'.$min.':'.$sec.' +0800';
my $msg=<< "__MESSAGE__";
To: $receiver
From: $sender
Subject: $subject
Date: $time
$string
__MESSAGE__
my $sendit= Email::Send->new({mailer => 'SMTP'});
$sendit->mailer_args([Host => 'smtp.supersun.biz']);
$sendit->send($msg);
注意加粗字体部分变更为你自定义的配置。
然后定义command,即编缉Nagios安装目录下的etc/command.cfg文件或者添到自定义的命令文件中,内容如下:
define command{
command_name host-notify-by-smtp
command_line /usr/bin/printf "%b" "***** Nagios 2.10 *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | $USER1$/notify_via_smtp -s "Host $HOSTSTATE$ alert for $HOSTNAME$!" -t $CONTACTEMAIL$
}
define command{
command_name notify-by-smtp
command_line /usr/bin/printf "%b" "***** Nagios 2.10 *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | $USER1$/notify_via_smtp -s "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -t $CONTACTEMAIL$
}
然后定义联系人:
define contact{
contact_name supersun
alias supersun
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-by-smtp
host_notification_commands host-notify-by-smtp
email supersun@supersun.biz
}

发表评论