nagios邮件通知脚本

| 暂无评论 | 暂无引用通告

-->   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
        }



暂无引用通告

发送引用通告网址: http://supersun.info/mt/mt-tb.cgi/830
如果您想引用这篇日记到您的Blog,请复制上面的链接,放置到您发表文章时的相应界面中。

发表评论

最新资源

  • IMG_1437.JPG
  • line.png
  • bar.png
  • perl_calander.jpg

关于此日记

此日记由 supersun 发表于 2008年1月 7日 18:02

此Blog上的上一篇日记迪拜帆船酒店(BurjAl-ArabHotel)

此Blog上的下一篇日记ntp服务器简单配置

首页归档页可以看到最新的日记和所有日记。