天天看點

50.nagios監控工具

<b>nagios監控工具</b>

Nagios是一款開源的計算機系統和網絡監視工具,能有效的監控windows,linux和unix的主機狀态,在系統和伺服器狀态異常的時候發送電子郵件或短信報警,第一時間通知網站運維成員,在狀态恢複後發出正常的電子郵件或短信通知。

Nagios四種監控狀态:

v 0(OK)表示狀态正常/綠色

v 1(WARNING)表示出現警告/黃色

v 2(CRITICAL)表示出現非常嚴重的錯誤/紅色

v 3(UNKNOWN)表示未知錯誤/深黃色

Nagios通過NRPE插件來遠端管理服務。

Nagios配置思想:

要監控那台主機,監控主機上那些服務,我要在什麼時間段内做監控,當被監控端出現故障時我要給那個聯系人發送郵件通知。

Nagios配置檔案路徑:/usr/local/nagios/etc/

Nagios的配置檔案解析:

v Nagios.cfg:主配置檔案,定義其他配置檔案存放位置

v Hostgroups.cfg:主機組配置檔案,定義主機組

v Contacts.cfg:聯系人配置檔案,定義聯系人和聯系人組

v Commands.cfg:指令配置檔案,定義使用那些指令做監控

v Timeperiods.cfg:時間段配置檔案,定義在那個時間範圍做監控

v Templates.cfg:模闆檔案,用于資源引用

v Localhost.cfg:本地主機配置檔案,用于監控本地

部署nagios監控系統:

v 關閉防火牆

#iptbles -F

#setenforce 0

v 建立nagios運作使用者群組,建立安裝目錄,授權目錄

#useradd  -s /sbin/nologin  nagios

#mkdir /usr/local/nagios

#chown -R nagios.nagios   /usr/local/nagios

v 編譯安裝nagios軟體

#tar xzvf nagios-4.0.1.tar.gz

#cd agios-4.0.1

#./configure --prefix=/usr/local/nagios

#make all

#make install

#make install-init

#make install-commandmode

#make install-config

#chkconfig --add nagios

#chkconfig nagios on

v 安裝nagios-plugins插件

#tar xzvf nagios-plugins-1.5.tar.gz

#cd nagios-plugins-1.5

#make &amp;&amp; make install

v 安裝NRPE插件

#yum -y install openssl-devel

#tar xzvf nrpe-2.15.tar.gz

#cd nrpe-2.15

#./configure

#make install-plugin

v 安裝配置apche和PHP

#yum -y install httpd php

#vim /etc/httpd/conf/httpd.conf

添加相關内容:Nagios的web頁面需要經過授權才可以通路是以!

ScriptAlias  /nagios/cgi-bin "/usr/local/nagios/sbin"

&lt;Directory "/usr/local/nagios/sbin"&gt;

    Options ExecCGI

    AllowOverride None

    Order allow,deny

    Allow from all

    Authname "Nagios Access"

    AuthType  Basic

    AuthUserFile /usr/local/nagios/etc/htpasswd.users    用于此目錄通路身份驗證的檔案

    Require  valid-user

&lt;/Directory&gt;

Alias /nagios "/usr/local/nagios/share"

&lt;Directory "/usr/local/nagios/share"&gt;

    Options None

    AuthUserFile /usr/local/nagios/etc/htpasswd.users

#service httpd restart

v 建立web頁面的使用者為nagiosadm密碼為nagiosadm

# /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadm

v 配置nagios系統

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

添加參數:

cfg_dir=/usr/local/nagios/etc/conf         指定主機檔案目錄

cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg     指定主機組檔案位置

#mkdir /usr/local/nagios/etc/conf/         建立目錄

# cp localhost.cfg   /usr/local/nagios/etc/conf/192.168.254.129.cfg   拷貝主機檔案

#vim  /usr/local/nagios/etc/conf/192.168.254.129.cfg        主機檔案

修改内容:

define host{                  被監控主機

        use                     linux-server            調用模闆參數

        host_name               web                  主機名

        alias                    web                  别名

        address                 192.168.254.129        被控主機位址

        }

define service{                被監控主機服務                       

        use                    local-service             調用模闆參數

        host_name              web                   主機名

        service_description       PING                   服務描述

        check_command         check_ping!100.0,20%!500.0,60% 使用的指令

#vim /usr/local/nagios/etc/objects/hostgroups.cfg

define hostgroup{            被監控主機組

        hostgroup_name   webs                       主機組名

        alias             webs                       主機組别名

        members         web                        成員主機(調用主機)

}

#vim /usr/local/nagios/etc/objects/templates.cfg              模闆檔案

define host{

        name                   generic-host              模闆名(主機)

        notifications_enabled           1                 通告開關

        event_handler_enabled         1                 事件處理開關

        flap_detection_enabled         1                 移動檢測開關

        process_perf_data             1                 過程性能資料開關

        retain_status_information       1                 保持狀态資訊開關

        retain_nonstatus_information    1                 保持無狀态資訊開關

        notification_period             24x7              通告周期(調用時間)

        register                        0                不注冊

define service{

        name                   generic-service          模闆名(服務)

        active_checks_enabled          1                存活檢查開關   

        passive_checks_enabled         1                被動檢查開關     

        parallelize_check               1                并行化檢查開關      

        obsess_over_service            1                癡迷服務開關    

        check_freshness               0                檢查新的服務開關  

        notifications_enabled           1                通告開關      

        event_handler_enabled         1                 事件處理開關    

        flap_detection_enabled         1                 移動監測開關    

        process_perf_data             1                 過程性能資料開關    

        retain_status_information       1                 保持狀态資訊開關     

        retain_nonstatus_information    1                 保持無狀态資訊開關     

        is_volatile                    0                 服務是否不穩定    

        check_period                 24x7               檢查周期(調用時間)

        max_check_attempts           3                  最大檢查嘗試時間(分鐘)  

        normal_check_interval          10                正常檢查間隔(分鐘)     

        retry_check_interval            2                 重試檢查間隔(分鐘)   

        contact_groups                admins            聯系人組(調用聯系人)   

        notification_options            w,u,c,r            通告告警級别  

        notification_interval            60                通告間隔(分鐘)     

        notification_period             24x7              通告周期(調用時間)    

         register                      0                 不注冊    

define contact{

        name                      generic-contact    模闆名(聯系人)

        service_notification_period     24x7            服務通告周期(調用時間)       

        host_notification_period        24x7            主機通告周期(調用時間)      

        service_notification_options    w,u,c,r,f,s         服務告警級别  

        host_notification_options       d,u,r,f,s          主機在什麼狀态下發送通知   

service_notification_commands notify-service-by-email 服務發送通知郵箱(調用指令)

  host_notification_commands     notify-host-by-email   主機發送通知郵箱(調用指令)

        register                        0                  不注冊

        name                        linux-server        模闆名(主機)

        use                          generic-host       調用了模闆中的參數

        check_period                  24x7             檢查周期(調用時間)

        check_interval                 5                檢查間隔(分鐘)

        retry_interval                  1                重試間隔(分鐘)

        max_check_attempts           10               最大嘗試時間(分鐘)

        check_command              check-host-alive     檢查指令(調用指令)

        notification_period             workhours         通告周期(調用時間)

        notification_interval            120               通告間隔(分鐘)

        notification_options            d,u,r             主機在什麼狀态下發送通知

        contact_groups                admins           聯系人組(調用聯系人)

        register                     0                 不注冊

define service{      

        name                      local-service         模闆名(服務)

        use                        generic-service      調用模闆中的參數

        max_check_attempts          4                 最大檢查嘗試時間(分鐘)   

        normal_check_interval         5                 正常檢查間隔(分鐘)

        retry_check_interval          1                  重試檢查間隔(分鐘)

#vim /usr/local/nagios/etc/objects/timeperiods.cfg        時間檔案

define timeperiod{

        timeperiod_name 24x7                        定義時間段名

        alias           24 Hours A Day, 7 Days A Week   定義時間别名

        sunday          00:00-24:00                 星期天

        monday          00:00-24:00                星期一

        tuesday         00:00-24:00                 星期二

        wednesday       00:00-24:00                星期三

        thursday        00:00-24:00                 星期四

        friday          00:00-24:00                 星期五

        saturday        00:00-24:00                 星期六

        timeperiod_name workhours                  定義時間段名

        alias           Normal Work Hours           定義時間别名

        monday          09:00-17:00               星期一

        tuesday         09:00-17:00                星期二

        wednesday      09:00-17:00                星期三

        thursday        09:00-17:00                星期四

        friday           09:00-17:00                星期五

#vim /usr/local/nagios/etc/objects/contacts.cfg                    聯系人檔案

        contact_name                    nagiosadmin           聯系人名   

        use                             generic-contact        調用模闆

        alias                            Nagios Admin           别名

        email                           [email protected]      郵箱位址

define contactgroup{

        contactgroup_name       admins                        聯系人組

        alias                   Nagios Administrators             聯系人組别名

        members                 nagiosadmin                   成員

注釋:上面的是詳細的配置檔案詳解

配置nagios檔案經驗:

對于全新的nagios隻需要根據需求

定義主機檔案:要監控那台主機,該主機上用指令監控那些服務

定義聯系人:要給那個聯系人發送通知,指定郵箱位址

定義時間段:在那個時間段發送通知

即可完成配置

check_ping!100.0,20%!500.0,60%意思是:使用check_ping指令檢測中,如果延遲延遲&gt;=100或丢包率超過20%則觸發warning警告,如果延遲&gt;=500,或丢包率超過60%,則觸發critical警告;否則不觸發警告。‘!’表示分割,‘,’表示或者

告警級别:w:warning警告  u:unknown未知錯誤  c:critical嚴重錯誤

主機狀态:d:down關機  u:unreachable不可達  r:recovery恢複

v 關閉身份驗證

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

Use-authentication = 0

v 部署被控端

# yum -y install openssl openssl-devel

#useradd nagios -s /sbin/nologin

# cd nagios-plugins-1.5

#make  all &amp;&amp; make install-plugin &amp;&amp; make install-daemon &amp;&amp; make install-daemon-config

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

添加相關内容:指定監控伺服器

allowed_hosts=127.0.0.1,192.168.254.128

commond[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

commond[check_load]=/usr/local/nagios/libexc/check_load -w 15,10,5 -c 30,25,20

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d       啟動nrpe服務

# netstat -lnupt | grep 5666                                     nrpe服務端口

v 監控端測試nrpe是否正常

# /usr/local/nagios/libexec/check_nrpe  -H 192.168.254.129

v 重新開機服務

# service nagios restart

Linux使用Sendmail發送郵件:

# yum -y install sendmail postfix mailx

# systemctl restart sendmail

發送郵件的方式:

# echo ‘内容’ | mail -s ‘主題’ 郵箱位址

&gt;主題:

&gt;内容:

按快捷方式Ctrl+D。

Linux使用外部IMAP郵箱身份發送郵件方式:

郵箱協定:

SMTP:用于郵件的發送 端口号:25

POP3:用于接收郵件 端口号:110

IMAP:網絡郵箱協定,用于郵件線上傳輸。

# yum -y install mailx

# vim /etc/mail.rc

添加相關參數:

set [email protected]

set imap=imap.163.com

set imap-auth=login

set [email protected]

set imap-auth-password=xyz110110

解決mail郵件不能發送問題

報錯資訊

解決方法

#Cat /var/log/maillog

DSN: Service unavailable

更換hostname:

1.編輯/etc/sysconfig/network,更改hostname

2.把hostname寫入/etc/hosts

 本文轉自   觸動的風   51CTO部落格,原文連結:http://blog.51cto.com/10978134/1945790

繼續閱讀