nagios 短信报警通知

默北 Nagios1 17,8419字数 2673阅读8分54秒阅读模式

nagios 报警通知可以用多种方式实现,如通过飞信,邮件。具体参见《nagios mail告警通知》,《Nagios使用飞信短信报警》。

下面来说说使用短信网关来发送短信告警信息,这个是最为高效的方式,不过需要付费的哈。文章源自运维生存时间-https://www.ttlsa.com/nagios/nagios-sms-notification/

1. 创建一个联系人

define contact{
        contact_name                    m_ttlsa
        alias                           ttlsa.com
        service_notification_period     nonworkhours
        host_notification_period        nonworkhours
        service_notification_options    w,u,c
        host_notification_options       d,u
        service_notification_commands   notify-service-by-sms
        host_notification_commands      notify-host-by-sms
        pager                           186xxxxxxxxx
        }

2. 创建一个组

define contactgroup{
        contactgroup_name       ops
        alias                   ops
        members                 m_ttlsa
        }

3. 创建个短信通知周期

# 'nonworkhours' timeperiod definition
define timeperiod{
        timeperiod_name nonworkhours
        alias           Non-Work Hours
        sunday          00:00-24:00                     ; Every Sunday of every week
        monday          00:00-08:30,17:30-24:00         ; Every Monday of every week
        tuesday         00:00-08:30,17:30-24:00         ; Every Tuesday of every week
        wednesday       00:00-08:30,17:30-24:00         ; Every Wednesday of every week
        thursday        00:00-08:30,17:30-24:00         ; Every Thursday of every week
        friday          00:00-08:30,17:30-24:00         ; Every Friday of every week
        saturday        00:00-24:00                     ; Every Saturday of every week
        }

4. 修改发送邮件命令

# 'notify-host-by-sms' command definition
define command {
        command_name    notify-host-by-sms
        command_line    $USER1$/sendsms.pl $CONTACTPAGER$ "Service: $SERVICEDESC$ Address: $HOSTADDRESS$  State: $SERVICESTATE$ Time: $SHORTDATETIME$ Info:$SERVICEOUTPUT$"
        }
# 'notify-service-by-sms' command definition
define command {
        command_name    notify-service-by-sms
        command_line    $USER1$/sendsms.pl $CONTACTPAGER$ "Service: $SERVICEDESC$ Address: $HOSTADDRESS$ State: $SERVICESTATE$ Time: $SHORTDATETIME$ Info:$SERVICEOUTPUT$"
        }

5. 发送短信

#!/usr/bin/env perl

use strict;
use warnings;
use POSIX "strftime";
use WWW::Curl::Easy;
use URI::Escape;

my $smslog = "/usr/local/nagios/var/sendsms.log";
my ($mobile, $content) = @ARGV;
my $message = uri_escape($content);

my $url = 'http://test.ttlsa.com:8080/sendMsg?mobile='.$mobile.'&content='.$message.'&handlerId=XXXXXX';
my $curl = WWW::Curl::Easy->new;

$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_TIMEOUT, 1);
$curl->setopt(CURLOPT_URL, $url);
open my $response_body,">/dev/null";
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

my $retcode = $curl->perform;

if ($retcode == 0) {
        #print("time:". current_time() . " send sucess\n");
        print { open my $fh, ">>", $smslog; $fh or die } "time:". current_time() . " send sucess\n";
} else {
        #print("time:". current_time() . " Error: code " .$retcode. " ".$curl->strerror($retcode)."\n");
        print { open my $fh, ">>", $smslog; $fh or die } "time:". current_time() . " Error: code " .$retcode. " ".$curl->strerror($retcode)."\n";
}

sub current_time {    
        return strftime("%Y-%m-%d %H:%M:%S", localtime());
}

6. 指定监控项目

define service {
    use                 generic-service
    hostgroup_name          Mongo Servers
    service_description     Mongo Mapped Memory Usage
    check_command           check_mongodb!10.0.0.160!12345!'ttlsa'!'www.ttlsa.com'!memory_mapped!20!28
    contact_groups          ops
}
文章源自运维生存时间-https://www.ttlsa.com/nagios/nagios-sms-notification/文章源自运维生存时间-https://www.ttlsa.com/nagios/nagios-sms-notification/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 13/06/2014 01:00:32
  • 转载请务必保留本文链接:https://www.ttlsa.com/nagios/nagios-sms-notification/
评论  1  访客  0

    来自外部的引用

评论已关闭!