利用飞信实现nagios的报警,无费用、实现简单。本文介绍使用“飞信”实现nagios的实时报警提示
1.注册一个移动的飞信号,假设电话为13912345678, 密码为123456
2.在IE中测试
作为内容能否发到13912345678手机上,能则以下方案可用。
3.编辑 fetion_send.pl
#! /usr/bin/perl
# usage: fetion_send -f phonenumber -p password -t phonenumber
use strict;
use warnings;
use Getopt::Std;
use LWP::Simple;
my %options;
getopts('f:p:t:',\%options);
my $fetion_url="?";
my $from = "username=".$options{f};
my $password= "password=".$options{p};
my $to = "sendto=".$options{t};
my $date_command = "/bin/date";
my $date = `$date_command`; chop($date);
my $line;
my $content;
while (defined($line=)) {
while (defined($line=)) {
$content .= $line;
}
open(LOG,">>/var/log/nagios3/fetion_notify.log");
chomp($content);
my $url= $fetion_url.$from.'&'.$password.'&'.$to.'&'.'message='.$content;
#print ("\n $url \n");
my $return =get($url);
#print ("\n $return \n");
print LOG (" Sent SMS |$content| to $to \n");
print LOG (" fetion_send():url=|$url| \n");
print LOG (" Sent return= $return\n");
print LOG (" ----------------------------------------\n");
close(LOG);
3. 使 fetion_send.pl能运行, 放到/usr/lib/nagios/plugins/fetion_send.pl
chmod +x fetion_send.pl
4.测试
#echo "测试“|/usr/lib/nagios/plugins/fetion_send.pl -f 13912345678 -p 123456 -t 13912345678
测试能否收到信息
5.编辑配置文件 notify.cfg
# 'notify-host-by-fetion' command definition
define command{
command_name notify-host-by-fetion
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | $USER1$/fetion_send.pl -f 13912345678 -p 123456 -t $_CONTACTMOBILE_PHONE_NUM$
}
# 'notify-service-by-fetion' command definition
define command{
command_name notify-service-by-fetion-tt
command_line /usr/bin/printf "%b" "类型: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | $USER1$/fetion_send.pl -f 13912345678 -p 123456 -t $_CONTACTMOBILE_PHONE_NUM$
}
6. 在contact.cfg 中增加自定义变量
_mobile_phone_num 13912345678
7.在contact的相关定义项中
将在原有的Email报警项中
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
增加飞信的报警项,也可以只用飞信报警。
service_notification_commands notify-service-by-email,notify-service-by-fetion-tt
host_notification_commands notify-host-by-email,notify-service-by-fetion-tt
注意还要开启报警通知,也就是,设
host_notifications_enabled 1
service_notifications_enabled 1
8.主机和服务状态发生变化将会通过“飞信”报警到你手机上。
阅读(733) | 评论(1) | 转发(0) |