ubuntu14.04&debian8.9下安裝Naigos

ubuntu14.04&debian8.9下安裝Naigos 

Nagios是一個監視系統運行狀態和網絡信息的監視系統。Nagios能監視所指定的本地或遠程主機以及服務,同時提供異常通知功能等。    

安裝環境:ubuntu14.04,全是最新的nagios和nagios插件

wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.4.tar.gz#_ga=2.198978473.708726039.1506225859-901224012.1506225859

wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz#_ga=2.224003997.708726039.1506225859-901224012.1506225859

wget https://nchc.dl.sourceforge.net/project/pnp4nagios/PNP-0.6/pnp4nagios-0.6.26.tar.gz

wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.1/nrpe-3.2.1.tar.gz

1.編譯安裝nagios

Adding the Nagios User and Group:

useradd nagios

groupadd nagcmd

usermod -a -G nagcmd nagios

usermod -a -G nagios,nagcmd www-data

 

sudo apt-get install wget build-essential apache2 php5 php5-gd libgd-dev unzip

tar -zxvf nagios-4.3.4.tar.gz

cd nagios-4.3.4.tar.gz

./configure --with-command-group=nagcmd --with-mail=/usr/bin/sendmail --with-httpd-conf=/etc/apache2/

make all

make install

make install-init

make install-config

make install-commandmode

make install-webconf

cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/

chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

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

修改apache2配置:

sudo a2enmod rewrite cgi

cp /etc/apache2/nagios.conf /etc/apache2/conf-enabled/

vi /etc/apache2/apache2.conf

新增如下內容:

ServerName localhost

重啓apache2:

serviceapache2 restart

修改nagios啓動腳本:

sudo vi /etc/init.d/nagios     (加入如下內容:)

DESC="Nagios"

NAME=nagios

DAEMON=/usr/local/nagios/bin/$NAME

DAEMON_ARGS="-d /usr/local/nagios/etc/nagios.cfg"

PIDFILE=/usr/local/nagios/var/$NAME.lock

Add a default user for Web Interface Access(設置登錄賬戶及密碼):

htpasswd –c /usr/local/nagios/etc/htpasswd.users nagiosadmin

2.Nagios Plugin 安裝:

tar –zxvf nagios-plugins-2.2.1.tar.gz

cd nagios-plugins-2.2.1

./configure --with-nagios-user=nagios --with-nagios-group=nagios

make

make install

設置nagios開機啓動:

sudo update-rc.d nagios defaults

3.編譯安裝nrpe

NRPE是nagios的一個擴展,它被用於被監控的服務器上,向nagios監控平臺提供該服務器的一些本地的情況。例如,cpu負載、內存使用、硬盤使用等等。NRPE可以稱爲nagios的for linux 客戶端。

apt-get install openssl libssl-dev

tar-zxvf nrpe-3.2.1.tar.gz

cd nrpe-3.2.1

./configure --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu-enable-command-args

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config

make install-config

make install-inetd

make install-init

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

/usr/local/nagios/libexec/check_nrpe ‐H localhost

修改commands.cfg(新增):

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

define command{

command_name   check_nrpe

command_line           /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

    }

這就說明check_nrpe連接nrpe daemon是正常的,其他被監控的主機都得這樣check_nrpe

vi /etc/rc.local添加開機啓動:

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

監控網絡流量check_iftraffic

安裝snmp:

sudo apt-get install snmpd snmp snmp-mibs-downloader

1.將原有“agentAddress udp:127.0.0.1:161”改爲:

agentAddress 127.0.0.1,172.16.126.137爲本機IP,即監控服務器要監控的主機IP*/

 

2.加入一行如下:

accessMyROSystem "" any noauth exact all none none

3.將原有“rocommunity public default -V systemonly” 的"-V systemonly" 參數去掉,變成:

rocommunity public default

4.將“#trap2sink localhost public”和“#informsink localhost public”前面的“#”去掉,改爲:

trap2sink localhost public

informsink localhost public

5.重啓SNMP服務:

/etc/init.d/snmpd restart

6.檢驗snmp獲取數據:

snmpwalk -v 2c -c public 192.168.1.9

添加Nrpe.cfg

command[check_iftraffic]=/usr/local/nagios/libexec/check_iftraffic -i eth0 -w5 -c 10 -b 10 -u m

服務定義

define service{

use                          generic-service

host_name                    localhost

service_description          iftraffic

check_command               check_nrpe!check_iftraffic

}

提示unable to read output的, 被控機的/tmp下有個traffic_ifeth*,把他的屬主改成nagios就可以了

4.Pnp4nagios安裝

pnp4nagios是基於RRD輪循(環狀)數據庫中所提供的綜合信息,以可視化圖形的方式呈現給用戶的一款nagios插件

apt-get install rrdtool librrds-perl

tar zxvf pnp4nagios-0.6.26.tar.gz

cd pnp4nagios-0.6.26

./configure --prefix=/usr/local/pnp4nagios

make all

makeinstall

makeinstall-webconf

makeinstall-config

makeinstall-init

配置pnp4nagios

cd /usr/local/pnp4nagios/etc/

mv misccommands.cfg-sample misccommands.cfg

mv nagios.cfg-sample nagios.cfg

mv rra.cfg-sample rra.cfg

cd pages/

mv web_traffic.cfg-sample web_traffic.cfg

 

配置pnp4nagios web頁面:

cp /etc/httpd/conf.d/pnp4nagios.conf /etc/apache2/conf-enabled/

cd /usr/local/pnp4nagios/share/

mv install.php install.php.bak

可以開啓process_perfdata debug模式修改/usr/local/pnp4nagios/etc/process_perfdata.cfg

LOG_LEVEL = 2

會在/usr/local/pnp4nagios/var/perfdata.log顯示錯誤日誌,方便查找原因

貌似Synchronous Mode在nagios4.x有bug,日誌顯示一直找不到nagios環境,但是看官方文檔更改enable_environment_macros=1應該影響開啓nagios環境。

Bulk Mode with NPCD模式配置:

修改nagios.cfg

vi /usr/local/nagios/etc/nagios.cfg

修改0—>1:

process_performance_data=1

enable_environment_macros=1

新增如下內容:

# service performance data

 

#

 

service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata

 

service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$

 

service_perfdata_file_mode=a

 

service_perfdata_file_processing_interval=15

 

service_perfdata_file_processing_command=process-service-perfdata-file

 

#

 

# host performance data #

 

#

 

host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata

 

host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$

 

host_perfdata_file_mode=a

 

host_perfdata_file_processing_interval=15

 

host_perfdata_file_processing_command=process-host-perfdata-file

 

修改commands.cfg

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

註釋掉已有的'process-host-perfdata'和'process-service-perfdata',新增如下內容:

# Bulk with NPCD mode

#

define command {

command_name    process-service-perfdata-file

       command_line    /bin/mv /usr/local/pnp4nagios/var/service-perfdata /usr/local/pnp4nagios/var/spool/service-perfdata.$TIMET$

}

 

define command {

command_name    process-host-perfdata-file

       command_line    /bin/mv /usr/local/pnp4nagios/var/host-perfdata /usr/local/pnp4nagios/var/spool/host-perfdata.$TIMET$

}

修改templates.cfg添加模板:

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

define host {

name      hosts-pnp

register  0

        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_ 

}

define service {

name      services-pnp

register  0

        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$

}

修改hosts.cfg和services.cfg,增加hosts-pnp和services-pnp配置:

define host{

use                     linux-server,hosts-pnp            ; Name of host template to use

                                                        ; This host definition will inherit all variables that are defined

                                                        ; in (or inherited by) the linux-server host template definition.

host_name               nagios-server

alias                   nagios

address                 192.168.20.132

        }

# Define a service to "ping" the local machine

define service{

use                             local-service,services-pnp         ; Name of service template to use

host_name                       nagios-server

service_description             PING

        check_command                   check_ping!100.0,20%!500.0,60%

        }

# Define a service to check the disk space of the root partition

# on the local machine.  Warning if < 20% free, critical if

#< 10% free space on partition.

define service{

use                             local-service,services-pnp         ; Name of service template to use

host_name                       nagios-server

service_description             Root Partition

check_command                   check_local_disk!20%!10%!/

}

重啓nagios,然後就出圖了。

http://images.cnitblog.com/i/633430/201407/011601298095600.jpg

http://images.cnitblog.com/i/633430/201407/011602200278508.jpg

http://images.cnitblog.com/i/633430/201407/011604284807618.jpg

一般使用的是默認模板,可以根據不同需求自定義模板temple

 

Nagios網頁界面之Visual Shell

Nagios 提供的網頁界面是自帶的CGI程序,目前只提供英文語言,如果動手修改其排版,或者漢化核心CGI程序(C語言),則每一次版本更新都需要進行更改,這將是非常繁鎖的工作。爲了解決這個問題,我們可以通過讀取Nagios監控系統的狀態信息,即var/status.dat文件,根據其中的字段值來重新設計顯示界面。

Nagios Visual shell (Vshell)是一個使用PHP編寫的輕量級開源Nagios網頁前端,輸出的XHTML+CSS代碼可以通過W3C標準驗證。個人認爲,Vshell的界面配色很一般,看一會兒就是會覺得很辛苦,以後的版本還需要加強美工。

apt-get install php-apc

V-Shell takes advantage of the Alternative PHP Cache system (APC), which is of particular importance if you are managing a large installation.

You may need to increase your PHP 'memory_limit' setting in your system's php.ini file if you're using APC.  For large systems try a setting as high as 256-512MB.

 

wget http://assets.nagios.com/downloads/exchange/nagiosvshell/vshell.tar.gz

tar zxf vshell.tar.gz

cd vshell

修改install.php

define('TARGETDIR',"/usr/local/vshell");

define('APACHECONF',"/etc/httpd/conf.d");       (centos)

define('APACHECONF',"/etc/apache2/conf.d");   (ubuntu)

chmod +x install.php

./install.php

 

http://images.cnitblog.com/i/633430/201407/011605295747149.jpg

 

Nginx端口映射:

        location /nagios {

         #   root   html;

         #   index  index.html index.htm;

            proxy_pass http://172.16.126.137:8081;

            proxy_redirect off;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

 

告警配置

  1. 郵件告警
    1. 設置聯繫人

vi contacts.cfg

define contact{

        contact_name                    nagiosadmin             ; Short name of user

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

        alias                           Nagios Admin            ; Full name of user

 

        email                           [email protected]    ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

        address4                        CuiSongBiao

        }

 

    1. 設置command

vi commands.cfg

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

define command{

        command_name    notify-host-by-email

        command_line    /usr/local/nagios/script/sendmail.py -t $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **"  -m  "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"

        }

 

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

define command{

        command_name    notify-service-by-email

        command_line    /usr/local/nagios/script/sendmail.py -t  $CONTACTEMAIL$ -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **"  -m  "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$"

        }

    1. 郵件告警腳本

cd /usr/local/nagios/script

vi sendmail.py

#!/usr/bin/env python

#Script is Sendmail

#Version 1.0.5

#coding=utf-8

import smtplib

import string

import sys

import getopt

 

def usage():

   print """sendmail is a send mail Plugins

   Usage:

 

   sendmail [-h|--help][-t|--to][-s|--subject][-m|--message]

 

   Options:

          --help|-h)

                 print sendmail help.

          --to|-t)

                 Sets sendmail to email.

          --subject|-s)

                  Sets the mail subject.

          --message|-m)

                  Sets the mail body

    Example:

          ./sendmail.py -t '[email protected]' -s 'hello' -m 'hello ,this is send mail test!'"""

 

   sys.exit(3)

 

 

try:

   options,args = getopt.getopt(sys.argv[1:],"ht:s:m:",["help","to=","subject=","message="])

except getopt.GetoptError:

   usage()

for name,value in options:

    if name in ("-h","--help"):

       usage()

    if name in ("-t","--to"):

# accept message user

       TO = value

       TO = TO.split(",")

    if name in ("-s","--title"):

       SUBJECT = value

    if name in ("-m","--message"):

       MESSAGE = value

       MESSAGE = MESSAGE.split('\\n')

       MESSAGE = '\n'.join(MESSAGE)

 

#SMTP HOST

HOST = "smtp.126.com"

#smtp port

PORT = "465"

#FROM mail user

USER = 'ops'

#FROM mail password

PASSWD = 'xxx123'

#FROM EMAIL

FROM = "[email protected]"

 

try:

   BODY = string.join((

      "From: %s" % FROM,

      "To: %s" % TO,

      "Subject: %s" % SUBJECT,

      "",

      MESSAGE),"\r\n")

 

   smtp = smtplib.SMTP_SSL()

   smtp.connect(HOST,PORT)

   smtp.login(USER,PASSWD)

   smtp.sendmail(FROM,TO,BODY)

   smtp.quit()

except Exception, e:

   print str(e)

   print "please look help"

   print "./sendmail.py -h"

 

  1. 微信企業號告警

2.1申請微信企業號:

CorpID = 'wwfd5cccccc6dbe60'

 

AgentId = '1000002'

 

Secret = 'FBdnNGxxxxxxxxEAk4M8UJpma3d23P0'

 

2.2設置聯繫人

vi contacts.cfg

define contact{

        contact_name                    nagiosadmin             ; Short name of user

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

        alias                           Nagios Admin            ; Full name of user

 

        email                           [email protected]    ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******

        address4                        CuiSongBiao

        }

2.3設置command

vi commands.cfg

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

define command{

        command_name    notify-host-by-weixin

        command_line    /usr/local/nagios/script/weixin.py $CONTACTADDRESS4$ "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n"

        }

 

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

define command{

        command_name    notify-service-by-weixin

        command_line    /usr/local/nagios/script/weixin.py $CONTACTADDRESS4$ "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$"

        }

 

2.4 設置模板

vi 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-email,notify-service-by-weixin        ; send service notifications via email

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

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

        }

 

2.5 微信企業號告警腳本

#!/usr/bin/env python

# _*_coding:utf-8 _*_

import urllib2

import json

import sys

 

reload(sys)

sys.setdefaultencoding('utf-8')

 

def gettoken(corpid, corpsecret):

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

    try:

        token_file = urllib2.urlopen(gettoken_url)

    except urllib2.HTTPError as e:

        e.code

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

        sys.exit()

    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, user, party, agent, subject, content):

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

    send_values = "{\"touser\":\"" + user + "\",\"toparty\":\"" + party + "\",\"totag\":\"\",\"msgtype\":\"text\",\"agentid\":\"" + agent + "\",\"text\":{\"content\":\"" + subject + content + "\"},\"safe\":\"0\"}"

    send_request = urllib2.Request(send_url, send_values)

    response = json.loads(urllib2.urlopen(send_request).read())

    print "send sucessed!!"

    str(response)

 

 

if __name__ == '__main__':

    user = str(sys.argv[1])                                    # 參數1:發送給用戶的賬號,必須關注企業號,並對企業號有發消息權限

#    user = "nagios"

    party = str('2')                                                  # 參數2:發送給組的id號,必須對企業號有權限

    agent = str('1000002')                                      # 參數3:企業號中的應用AgentId

    subject = str(sys.argv[2])                                  # 參數4:標題【消息內容的一部分】

    content = str(sys.argv[3])                                  # 參數5:文本具體內容

#    content = "Hello  this is test !!"

    corpid = 'wwfdxxxxxxdbe60'                               # CorpID是企業號的標識

    corpsecret = 'FBdnNGgfmp5xxxxxxxUJpma3d23P0'  # corpsecret是Secret是管理組憑證密鑰

    try:

        accesstoken = gettoken(corpid, corpsecret)

        senddata(accesstoken, user, party, agent, subject, content)

    except Exception, e:

        print "error"

        str(e) + "Error Please Check \"corpid\" or \"corpsecret\" Config"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章