利用Nagios調用Python程序控制微信公衆平臺發佈報警信息

轉載請註明原文連接:http://my.oschina.net/caiyuanbo/blog/383916php

到Centos官網下載Centos 7 64位版系統並安裝。html

http://www.centos.org/python

 

安裝完進入系統更新應用linux

yum updateios

 

安裝php及http服務器web

yum install httpd phpapache

 

安裝nagios所需的庫json

yum install gcc glibc glibc-common make gd gd-devel net-snmpvim

 

安裝命令行下載軟件windows

yum install wget

 

安裝vim文本編輯軟件(我的喜愛)

yum install vim

 

新建用戶:nagios

useradd nagios

 

新建用戶組:nagcmd

groupadd nagcmd 

 

將用戶加入用戶組

usermod -G nagcmd nagios

usermod -G nagcmd apache

 

在/root目錄下建立nagios文件夾

mkdir ~/nagios 

 

進入/root/nagios文件夾

cd ~/nagios

 

下載nagios程序及nagios增長插件

wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-4.0.8.tar.gz

wget http://www.nagios-plugins.org/download/nagios-plugins-2.0.3.tar.gz

 

解壓nagios增長插件程序包及增長插件

tar zxvf nagios-4.0.8.tar.gz 

tar zxvf nagios-plugins-2.0.3.tar.gz

 

進入/root/nagios/nagios-4.0.8文件夾

cd nagios-4.0.8

 

編譯安裝nagios-4.0.8

 ./configure --with-command-group=nagcmd

make all 

make install

make install-init

make install-commandmode

make install-config

 

編輯nagios的contacts.cfg文件(存放設置接收報警的用戶的,此布最後修改)

vim /usr/local/nagios/etc/objects/contacts.cfg

################################  華麗分割線O(∩_∩)O~ ########################

define contact{

        contact_name                    caiyuanbo ; Short name of user

use generic-contact ; Inherit default values from generic-contact template (defined above)

        alias                           caiyuanbo ; Full name of user  後續要對應微信公衆帳號中通信錄的用戶名

        email                           15000000000@139.com ;這裏設置郵箱

pager 15000000000                 ;這裏是我後來添加的字段,若是要在commands.cfg配置文件中獲取這個字段信息就使用 $CONTACTPAGER$ 表示(既是contact中的pager字段)。

        }

define contact{

        contact_name                    luojinping

use generic-contact

        alias                           luojinping

        email                           13400000000@139.com

pager 13400000000

        }

 

define contactgroup{

        contactgroup_name       admins

        alias                   Nagios Administrators

        members                 caiyuanbo

        members                 luojinping

        }

################################  華麗分割線O(∩_∩)O~ ###################

 

make install-webconf

 

設置nagios登陸用戶nagiosadmin的密碼

htpasswd -s -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

 

注:關於nagios監控-多用戶管理,如下文章不錯

http://blog.csdn.net/liqfyiyi/article/details/8778671

 

進入/root/nagios/nagios-plugins-2.0.3文件夾

cd ~/nagios/nagios-plugins-2.0.3

安裝nagios增長插件

make

make install

 

使用下面的命令覈對nagios配置文件是否有錯誤,最好每次修改nagios配置文件都測試下。

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

 

chkconfig --add nagios

chkconfig --level 35 nagios on

systemctl start nagios.service

 

注:關於nagios在Centos7.0上的安裝方法我參照的是下面這個連接的文章(洋文的)

http://www.tuicool.com/articles/vYFv2i7

 

啓用http服務

 systemctl start httpd.service

 

設置httpd開機啓動

chkconfig httpd on

systemctl enable httpd

 

修改Centos 7的防火牆,使其http服務可被訪問。

 

查看防火牆所在網絡區域,默認爲public

firewall-cmd --get-default-zone

 

查看public所容許訪問的服務

firewall-cmd --zone=public --list-services

 

添加運行訪問http服務

firewall-cmd --permanent --zone=public --add-service=http

 

刪除不須要的服務(可選)

firewall-cmd --permanent --zone=public --remove-service=dhcpv6-client

 

從新加載防火牆

firewall-cmd --reload

 

查看public所容許訪問的服務,驗證是否修改爲功

firewall-cmd --zone=public --list-services

 

配置sendEmail發送nagios郵件報警

 

到官網下載sendEmail安裝包

http://caspian.dotconf.net/menu/Software/SendEmail/

 

解壓sendEmail程序包(這裏下載後放在我統一/root目錄下)

cd /root

tar zxvf sendEmail-v1.56.tar.gz

 

進入解壓出來後的目錄

cd sendEmail-v1.56

 

複製sendEmail程序至/usr/local/bin

cp sendEmail /usr/local/bin

 

查看/usr/local/bin/sendEmail的權限,是否普通用戶可讀可執行

ll /usr/local/bin/sendEmail

 

這裏可嘗試用sendEmail發送郵件

/usr/local/bin/sendEmail -f fasongzhe@126.com -t jieshouzhe@qq.com -s smtp.126.com -u "這裏設置主題" -xu fasongzhe -xp fasongzhedemima

解釋:

-f 表示發送者的郵箱

-t 表示接收者的郵箱

-s 表示SMTP服務器的域名或者ip

-u 表示郵件的主題

-xu 表示SMTP驗證的用戶名

-xp 表示SMTP驗證的密碼(注意,這個密碼貌似有限制,例如我用d!5neyland就不能被正確識別)

-m 表示郵件的內容

若是你不帶-m參數的話,就會提示你自行輸入

Reading message body from STDIN because the ‘-m’ option was not used.

If you are manually typing in a message:

- First line must be received within 60 seconds.

- End manual input with a CTRL-D on its own line

輸入完成後使用CTRL-D來結束

 

既然nagios要使用sendEmail來發警告郵件,那麼就要修改commands.cfg中關於發郵件的命令的定義,咱們如今來修改notify-by-email這個命令,以下

################################  華麗分割線O(∩_∩)O~ ####################

# 'notify-host-by-email' command definition

define command{

command_name notify-host-by-email

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/bin/sendEmail -f  fasongzhe@126.com -t $CONTACTEMAIL$ -s smtp.126.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTNAME$ $HOSTADDRESS$ is $HOSTSTATE$ **" -xu fasongzhe -xp fasongzhedemima

}

 

# 'notify-service-by-email' command definition

define command{

command_name notify-service-by-email

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/bin/sendEmail -f fasongzhe@126.com -t $CONTACTEMAIL$ -s smtp.126.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$ $HOSTADDRESS$/$SERVICEDESC$  is $SERVICESTATE$ **" -xu fasongzhe -xp fasongzhedemima

}

################################  華麗分割線O(∩_∩)O~ ########################

 

修改templates.cfg文件,修改報警方式爲郵件報警

vim /usr/local/nagios/etc/objects/templates.cfg

################################  華麗分割線O(∩_∩)O~ #############################

define contact{

        name                            generic-contact     ; The name of this contact template

        service_notification_period     24x7 ; service notifications can be sent anytime

        host_notification_period        24x7 ; host notifications can be sent anytime

        service_notification_options    w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events

        host_notification_options       d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events

        service_notification_commands   notify-service-by-email ; send service notifications via email

        host_notification_commands      notify-host-by-email ; send host notifications via email

        register                        0        ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

        }

################################  華麗分割線O(∩_∩)O~ ###########################

 

注:關於郵件報警配置我參考的是如下這個連接的文章

http://xiaosa.blog.51cto.com/665033/381310/

 

微信報警配置

 

首先註冊個微信公共平臺帳號

一、基本信息---->二、郵箱激活

https://mp.weixin.qq.com/cgi-bin/readtemplate?t=register/step1_tmpl&lang=zh_CN

---->三、選擇類型

選擇「企業號」

 

---->四、信息登記---->五、公衆號信息

 

(注:「我選的是「其餘組織」,能夠在百度查下本身公司的「組織機構代碼」)

 

登陸微信公共平臺進行配置

打開通信錄添加成員

 

打開應用中心添加應用

 

查看並記住發送報警使用的應用的ID

 

點擊設置號本身的帳號信息,點擊「權限管理」右邊的「管理」按鍵進入權限管理頁面,新建管理組(名字本身定)

根據添加管理員(內部)

根據實際狀況修改「通信錄權限」、「應用權限」、「敏感接口權限」的配置。

在該頁面最下方有個開發者憑據,這個是客戶端與服務器創建鏈接須要用到的。

 

簡單的使用nagios發微信報警主要用到微信API如下兩個格動做:

一、創建鏈接:獲取AccessToken;

二、發送消息。

可在微信企業號接口調試工具的頁面測試下。

一、發送請求地址,獲取AccessToken

二、發送請求地址,發送消息

其中的body部分能夠這樣定義:

{

   "touser": "這裏設置公衆平臺通信錄中的用戶名,可用@all表明全部用戶",

   "msgtype": "text",

   "agentid": "0",

   "text": {

       "content": "發送的內容,稍後會用python發送請求地址,暫不能發送中文"

   },

   "safe":"0"

}

詳細接口信息可參考微信官方接口文檔介紹

http://qydev.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5

 

安裝Python-3.4

 

科普下:

有些人在linux下使用python發送微信鏈接時會報urllib2.URLError: <urlopen error unknown url type: https>這個錯誤,是由於python沒有SSL模塊,需從新編譯安裝python。步驟以下

 

安裝openssl與openssl-devel包

yum install openssl 

yum install openssl-devel

 

到官網下載Python3的最新版

https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz

 

解壓Python安裝包(我默認放在/root下)

cd /root

tar xzvf Python-3.4.3.tgz

 

進入python源代碼文件夾,進入Modules文件夾,修改Setup.dist,使其支持發送https請求。

cd /root/Python-3.4.3/Modules

vim Setup.dist

# Socket module helper for SSL support; you must comment out the other

# socket line above, and possibly edit the SSL variable:

#SSL=/usr/local/ssl

#_ssl _ssl.c \

#        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \

#        -L$(SSL)/lib -lssl -lcrypto

修改成

# Socket module helper for SSL support; you must comment out the other

# socket line above, and possibly edit the SSL variable:

SSL=/usr/local/ssl

_ssl _ssl.c \

        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \

        -L$(SSL)/lib -lssl -lcrypto

 

編譯安裝,設置安裝目錄爲/usr/local/python-3.4,以避免與系統已安裝的python2衝突。

./configure --prefix=/usr/local/python-3.4

make

make install

 

#############################這部分有興趣能夠弄,可忽略###################

爲方便後續測試可在PATH中添加python-3.4的運行路徑,之後在終端可直接輸入命令python3.4執行文件。

vim /etc/profile

在文檔最後,添加:

export PATH="/usr/local/python-3.4/bin:$PATH"

保存,退出,而後運行

source /etc/profile

可驗證是否修改無誤。

################################  華麗分割線O(∩_∩)O~ #########################

 

安裝完成後開始編輯python腳本調用微信公衆平臺。

我將編輯好的腳本放在/usr/local/nagios/python目錄下

mkdir /usr/local/nagios/python

cd /usr/local/nagios/python

編輯主機報警調用腳本

vim notify-host-by-weixin.py

代碼以下:

################################  華麗分割線O(∩_∩)O~ #########################

import urllib.request

import json

import sys

#以上是導入模塊

 

#建立獲取AccessToken的方法

def gettoken(corp_id,corp_secret):

    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret

    try:

        token_file = urllib.request.urlopen(gettoken_url)

    except urllib.error.HTTPError as e:

        print(e.code)

        print(e.read().decode("utf8"))

    token_data = token_file.read().decode('utf-8')

    token_json = json.loads(token_data)

    token_json.keys()

    token = token_json['access_token']

    return token

 

#這裏是發送消息的方法

def senddata(access_token,notify_str):

    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token

#我傳入的參數是一段字符串每一個信息用separator連起來,只要再用字符串的split("separator")方法分開信息就能夠了。

    notifydata = notify_str.split("separator")

    user = notifydata[0]

    cationtype = notifydata[1]

    name = notifydata[2]

    state = notifydata[3]

    address = notifydata[4]

    output = notifydata[5]

    datatime = notifydata[6]

    content = '[擦汗][擦汗] Nagios警報 [擦汗][擦汗] \n\n類型: ' + cationtype + '\n主機名: ' + name + '\n狀態: ' + state + '\nIP地址: ' + address + '\n[豬頭]日誌: ' + output + '\n\n[瓢蟲]時間: ' + datatime + '\n'

    send_values = {

        "touser":user,

        "msgtype":"text",

        "agentid":"0",

        "text":{

            "content":content

            },

        "safe":"0"

        }

    send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8')

#設置爲非ascii解析,使其支持中文

    send_request = urllib.request.Request(send_url, send_data)

    response = urllib.request.urlopen(send_request)

#這個是返回微信公共平臺的信息,調試時比較有用

    msg = response.read()

    return msg

 

default_encoding = 'utf-8'

if sys.getdefaultencoding() != default_encoding:

    reload(sys)

    sys.setdefaultencoding(default_encoding)

#我編輯的腳本是要獲取nagios傳入的一段參數的(字符串),下面這條代碼是獲取執行腳本後獲取的第一個參數(經測試nagios只能傳入一個參數進python,因此把全部包括用戶名跟報警主機報警信息放進一個字符串裏)

notifystr = str(sys.argv[1])

corpid = '這裏輸入你的微信公衆平臺corpid'

corpsecret = '這裏輸入你的微信公衆平臺corpsecret'

accesstoken = gettoken(corpid,corpsecret)

msg = senddata(accesstoken,notifystr)

print(msg)

################################  華麗分割線O(∩_∩)O~ ############################

 

編輯服務報警調用腳本

vim notify-service-by-weixin.py

代碼以下:

################################  華麗分割線O(∩_∩)O~ #########################

import urllib.request

import json

import sys

def gettoken(corp_id,corp_secret):

    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret

    try:

        token_file = urllib.request.urlopen(gettoken_url)

    except urllib.error.HTTPError as e:

        print(e.code)

        print(e.read().decode("utf8"))

    token_data = token_file.read().decode('utf-8')

    token_json = json.loads(token_data)

    token_json.keys()

    token = token_json['access_token']

    return token

 

def senddata(access_token,notify_str):

    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token

    notifydata = notify_str.split("separator")

    user = notifydata[0]

    cationtype = notifydata[1]

    desc = notifydata[2]

    alias = notifydata[3]

    address = notifydata[4]

    state = notifydata[5]

    datatime = notifydata[6]

    output = notifydata[7]

    content ='[擦汗][擦汗] Nagios警報 [擦汗][擦汗] \n\n類型: ' + cationtype + '\n\n服務名: ' + desc + '\n主機名: ' + alias + '\nIP地址: ' + address + '\n狀態: ' + state + '\n\n[瓢蟲]時間: ' + datatime + '\n\n[豬頭]日誌:\n\n' + output + '\n'

    send_values = {

        "touser":user,

        "msgtype":"text",

        "agentid":"0",

        "text":{

            "content":content

            },

        "safe":"0"

        }

    send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8')

    send_request = urllib.request.Request(send_url, send_data)

    response = urllib.request.urlopen(send_request)

    msg = response.read()

    return msg

 

default_encoding = 'utf-8'

if sys.getdefaultencoding() != default_encoding:

    reload(sys)

    sys.setdefaultencoding(default_encoding)

notifystr = str(sys.argv[1])

corpid = '這裏輸入你的微信公衆平臺corpid'

corpsecret = '這裏輸入你的微信公衆平臺corpsecret'

accesstoken = gettoken(corpid,corpsecret)

msg = senddata(accesstoken,notifystr)

print(msg)

 

################################  華麗分割線O(∩_∩)O~ #############################

 

編輯好python程序後可使用如下命令測試,主機報警示例(將下面命令中文改成英文):

/usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/notify-host-by-weixin.py "微信通信錄中的用戶名separator時間標題separator主機名separator主機狀態separator主機地址separator主機輸出信息separator時間"

成功後會有 b'{"errcode":0,"errmsg":"ok"}' 的提示。

 

接下來開始對接nagios報警

 

而後定義發送微信的命令,修改commands.cfg文件,定義主機報警命令notify-host-by-weixin,及服務報警命令notify-service-by-weixin。

vim /usr/local/nagios/etc/objects/commands.cfg

在文檔最後面添加一下信息便可。

##############################################################################

# weixin #

##############################################################################

 

 define command{

 command_name notify-host-by-weixin

 command_line /usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/notify-host-by-weixin.py "$CONTACTALIAS$separator$NOTIFICATIONTYPE$separator$HOSTNAME$separator$HOSTSTATE$separator$HOSTADDRESS$separator$HOSTOUTPUT$separator$LONGDATETIME$"

 }

 

 define command{

 command_name notify-service-by-weixin

 command_line /usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/notify-service-by-weixin.py "$CONTACTALIAS$separator$NOTIFICATIONTYPE$separator$SERVICEDESC$separator$HOSTALIAS$separator$HOSTADDRESS$separator$SERVICESTATE$separator$LONGDATETIME$separator$SERVICEOUTPUT$"

 }

 

注:這部分我參考了這個網頁中的部分漢化資料,仍是比較有用的,介紹配置文件很詳細,能夠去看看。

http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html

 

修改templates.cfg文件,添加微信報警(這裏我刪除了郵件報警,由於不必了,要添加也能夠,在郵件報警後面添加個逗號和微信報警的命令就好了)

vim /usr/local/nagios/etc/objects/templates.cfg

define contact{

        name                            generic-contact     ; The name of this contact template

        service_notification_period     24x7 ; service notifications can be sent anytime

        host_notification_period        24x7 ; host notifications can be sent anytime

        service_notification_options    w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events

        host_notification_options       d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events

        service_notification_commands   notify-service-by-weixin

        host_notification_commands      notify-host-by-weixin

        register                        0        ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

        }

修改templates.cfg文件添加監控設備類別的模板

# Define a template for switches that we can reuse

define host{

name generic-switch ; The name of this host template

use generic-host ; Inherit default values from the generic-host template

check_period 24x7 ; By default, switches are monitored round the clock

check_interval 1 ; Switches are checked every 5 minutes  檢測設備的時間間隔

retry_interval 1 ; Schedule host check retries at 1 minute intervals 確認是否故障再次檢測的時間間隔

max_check_attempts 1 ; Check each switch 10 times (max)  

check_command check-host-alive ; Default command to check if routers are "alive"

notification_period 24x7 ; Send notifications at any time

notification_interval 30 ; Resend notifications every 30 minutes 設置爲0表示只報一次警,這裏設置爲30分鐘報一次

notification_options d,r ; Only send notifications for specific host states

contact_groups admins ; Notifications get sent to the admins by default

register 0 ; DONT REGISTER THIS - ITS JUST A TEMPLATE

}

#下面是在添加匯聚交換機模板,繼承了generic-switch,這個是本身定義的,也可使用默認的例如generic-switch

# Define Convergence_Layer_Switches

define host{

name Convergence_Layer_Switches

#新建模板的名字

use generic-switch

#繼承默認的generic-switch模板

statusmap_image Convergence_Layer_Switches.png

#這裏是定義map中繪製的拓撲中顯示的圖標,能夠本身定義。而後存放在/usr/local/nagios/share/images/logos目錄

}

# Define Access_Layer_Switches

define host{

name Access_Layer_Switches

use generic-switch

statusmap_image Access_Layer_Switches.png

}

 

注:關於MAP繪圖我參考的是這個連接

http://www.361way.com/nagios-map/2457.html

 

修改nagios.cfg修改主機配置文件的讀取目錄,使用額外的文件保存主機信息(方便管理)。

vim /usr/local/nagios/etc/nagios.cfg

# You can specify individual object config files as shown below:

cfg_file=/usr/local/nagios/etc/objects/commands.cfg

cfg_file=/usr/local/nagios/etc/objects/contacts.cfg

cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg

cfg_file=/usr/local/nagios/etc/objects/templates.cfg

 

# Definitions for monitoring the local (Linux) host

#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg

#在這裏我將默認的本機檢測關掉取消了。

# Definitions for monitoring a Windows machine

#cfg_file=/usr/local/nagios/etc/objects/windows.cfg

 

# Definitions for monitoring a router/switch

#cfg_file=/usr/local/nagios/etc/objects/switch.cfg

 

# Definitions for monitoring a network printer

#cfg_file=/usr/local/nagios/etc/objects/printer.cfg

 

 

# You can also tell Nagios to process all config files (with a .cfg

# extension) in a particular directory by using the cfg_dir

# directive as shown below:

 

#cfg_dir=/usr/local/nagios/etc/servers

#cfg_dir=/usr/local/nagios/etc/printers

#cfg_dir=/usr/local/nagios/etc/switches

#cfg_dir=/usr/local/nagios/etc/routers

cfg_dir=/usr/local/nagios/etc/campus/south

#添加了一個讀取配置文件的目錄,想放哪本身決定,之後將主機的信息放在/usr/local/nagios/etc/campus/south這個目錄即可讀取

 

建立文件夾/usr/local/nagios/etc/campus/south

mkdir /usr/local/nagios/etc/campus

mkdir /usr/local/nagios/etc/campus/south

建立主機配置文件(名字隨便取)

cd /usr/local/nagios/etc/campus/south

vim switches.cfg

 

#定義主機,我下面監控的是匯聚交換機

define host{

use Convergence_Layer_Switches

#使用的templates.cfg中的Convergence_Layer_Switches(這個是我本身定義的)

host_name S5-103_1F-RG-3760-68

#主機名

alias 192.168.1.202

#別名

address 192.168.1.222

#IP地址

hostgroups S-103

#隸屬於哪一個主機組

}

#定義主機,我下面監控的是鏈接匯聚交換機的接入層交換機

define host{

use Access_Layer_Switches

host_name S_103_2F_RG_S2952A

alias 192.168.1.3

address 192.168.1.3

parents S5-103_1F-RG-3760-68

#上一級節點名字,填寫清楚,在nagios的map頁面的繪圖纔會清晰顯示結構

hostgroups S-103

}

define host{

use Access_Layer_Switches

host_name S_103_3F_RG_S2952A

alias 192.168.1.4

address 192.168.1.4

parents S5-103_1F-RG-3760-68

hostgroups S-103

}

#建立主機組

define hostgroup{

hostgroup_name S-103

#主機組名

alias S-103-PeiXunGongYu

#主機組別名

}

#定義監控方式這裏是ping

define service{

use generic-service

hostgroup_name S-103

service_description PING

check_command check_ping!200.0,20%!600.0,60%

normal_check_interval 5

retry_check_interval 1

}

 

覈對nagios配置文件是否有錯誤

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

 

沒有報錯就重啓nagios服務

systemctl start nagios.service

 

效果圖:

轉載請註明原文連接:http://my.oschina.net/caiyuanbo/blog/383916

教程比較簡略,可能有些紕漏,若是有問題能夠跟我一塊兒研究。

QQ:384152164

E-mail:caiyb@vip.qq.com

相關文章
相關標籤/搜索