Nagios集成

本文介绍如何对Nagios监控源进行集成对接。

简介

Nagios是企业IT基础设施监控产品,运维事件中心支持简单、快速的集成Nagios,实现报警和事件的统一管理;查看以下集成步骤完成告警记录集成。

如何集成Nagios监控?

步骤1:找到Nagios监控源

进入运维事件中心>集成中心>集成配置菜单找到阿里云云监控监控源,进入Nagios的新增集成页面查看集成步骤完成集成接入。

步骤2:配置nagios报警媒介

1)找到配置文件安装目录

将配置文件nagios-alert.py放至 nagios/libexec/目录下,并将api_url = "URL"中的URL替换成以下URL地址:

https://public-alert.aliyuncs.com/event/nagios/{集成密钥} 

将Python脚本导入requests库,运行命令pip install requests;

Nagios-alert.py 配置文件如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import json
import sys
import os

headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "URL"

def msg(text):
    json_text= {
     "msgtype": "text",
        "text": {
            "content": text
        }
    }
    print requests.post(api_url,json.dumps(json_text),headers=headers).content

if __name__ == '__main__':
    text = sys.argv[1]
    msg(text)
2)配置nagios推送方法
# 'notify-host-by-alert' command definition
define command{
    command_name    notify-host-by-alert
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/nagios/libexec/nagios-alert.py '[$NOTIFICATIONTYPE$] Host Alert: $HOSTNAME$ is $HOSTSTATE$ 【ECS-nagios监控】'
    }

# 'notify-service-by-alert' command definition
define command{
    command_name    notify-service-by-alert
    command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/local/nagios/libexec/nagios-alert.py '[$NOTIFICATIONTYPE$] Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ 【ECS-nagios监控】'
    }
3)配置Nagios联系方式
修改contacts.cfg配置文件 在文件中加入以下两行 (vi /usr/local/nagios/etc/contacts.cfg)
    service_notification_commands   notify-service-by-alert        
    host_notification_commands      notify-host-by-alert            
contacts.cfg 配置文件如下:
define contact {
    contact_name                     nagiosadmin
    alias                            Nagios Admin
    host_notifications_enabled       1
    service_notifications_enabled    1
    host_notification_period         nagiosadmin_notification_times
    service_notification_period      nagiosadmin_notification_times
    host_notification_options        d,u,r,f,s
    service_notification_options     w,u,c,r,f,s
    email                            nagios@localhost
    use                              xi_contact_generic
    service_notification_commands   notify-service-by-alert        
    host_notification_commands      notify-host-by-alert            
}
4)重启nagios
systemctl restart nagios

步骤3:确认集成

完成以上配置后点击集成密钥下的确认集成按钮完成集成配置。

步骤4:验证集成是否成功

在Nagios监控侧配置告警、推送规则并触发告警后,前往集成配置页面查看到Nagios监控源状态为“已接收”报警则说明已集成成功。