溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

nagios的安裝及配置方法

發布時間:2021-08-12 09:33:46 來源:億速云 閱讀:144 作者:chen 欄目:移動開發

這篇文章主要介紹“nagios的安裝及配置方法”,在日常操作中,相信很多人在nagios的安裝及配置方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”nagios的安裝及配置方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

一、服務端安裝(主要監控磁盤或系統負載,可以自定義腳本,不能查看歷史數據)

1、配置好擴展源并安裝

yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe

2、設置登錄nagios后臺的用戶和密碼:

htpasswd -c /etc/nagios/passwd nagiosadmin

3、編輯配置文件

vim /etc/nagios/nagios.cfg

nagios -v /etc/nagios/nagios.cfg #檢測配置文件

4、啟動服務

啟動服務:service httpd start; service nagios start

5、瀏覽器訪問: http://10.10.13.239/nagios

nagios管理員賬號:nagiosadmin密碼:abc.123

二、客戶端安裝

1、配置好擴展源后安裝

yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe 

2、編輯配置文件

vim /etc/nagios/nrpe.cfg  找到“allowed_hosts=127.0.0.1” 改為 “allowed_hosts=127.0.0.1,10.10.13.239” 后面的ip為服務端ip; 找到” dont_blame_nrpe=0” 改為  “dont_blame_nrpe=1” 
啟動客戶端 /etc/init.d/nrpe start

3、服務端添加配置文件

cd /etc/nagios/conf.d/

vim 10.10.13.247.cfg

define host{

        use                     linux-server

        host_name                 10.10.13.247

        alias                       13.247

        address                  10.10.13.247

        }

define service{

        use                     generic-service

        host_name                 10.10.13.247

        service_description          check_ping

        check_command              check_ping!100.0,20%!200.0,50%

        max_check_attempts 5

        normal_check_interval 1

}

define service{

        use                     generic-service

        host_name                 10.10.13.247

        service_description          check_ssh

        check_command              check_ssh

        max_check_attempts           5    #當nagios檢測到問題時,一共嘗試檢測5次都有問題才會告警,如果該數值為1,那么檢測到問題立即告警

        normal_check_interval         1    #重新檢測的時間間隔,單位是分鐘,默認是3分鐘

        notification_interval         60    #在服務出現異常后,故障一直沒有解決,nagios再次對使用者發出通知的時間。單位是分鐘。如果你認為,>所有的事件只需要一次通知就夠了,可以把這里的選項設為0。

}

define service{

        use                     generic-service

        host_name                 10.10.13.247

        service_description          check_http

        check_command              check_http

        max_check_attempts           5

        normal_check_interval         1

}

#配置完檢測是否有錯

nagios -v /etc/nagios/nagios.cfg

4、客戶端上重啟一下nrpe服務: service nrpe restart
服務端也重啟一下nagios服務: service nagios restart

5、 刷新地址http://10.10.13.239/nagios看host和services已經添加了相關項目

三、監控客戶端的本地資源(需要借助于nrpe服務)

1、服務端vim /etc/nagios/objects/commands.cfg
增加:define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
2、繼續編輯 vim /etc/nagios/conf.d/10.10.13.247.cfg 
增加如下內容:define service{
        use     generic-service
        host_name       10.10.13.247
        service_description     check_load
        check_command           check_nrpe!check_load #可以在客戶端/etc/nagios/nrpe.cfg里面找到check_load腳本路徑
        max_check_attempts 5
        normal_check_interval 1
}


define service{
        use     generic-service
        host_name       10.10.13.247
        service_description     check_disk_sda1
        check_command           check_nrpe!check_hda1#可以在客戶端/etc/nagios/nrpe.cfg里面找到check_hda1腳本路徑
        max_check_attempts 5
        normal_check_interval 1
}

3、修改客戶端的nrpe配置文件

vim /etc/nagios/nrpe.cfg 

修改command[check_hda1]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1

改為command[check_hda1]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda1

4、重啟服務端的nagios和nrpe服務重啟客戶端的nrpe服務

[root@server ~]# /etc/init.d/nagios restart

[root@server ~]# /etc/init.d/nrpe restart

[root@client ~]# /etc/init.d/nrpe restart

5、刷新地址http://10.10.13.239/nagios看services已經添加了一負載和硬盤使用量的監控

四、添加郵件告警功能

1、編輯配置文件

vim /etc/nagios/objects/contacts.cfg

添加

define contact{                                          #定義成員123
        contact_name               123
        use                             generic-contact
        alias                           river
        email             313841662@qq.com
        }


define contact{
        contact_name               456               #定義成員456
        use                             generic-contact
        alias                            aaa
        email             313841662@qq.com
        }


define contactgroup{                                 #定義組,組成員為123,456
        contactgroup_name           common
        alias                                  common
        members                          123,456
        }

2、然后在要需要告警的服務里面加上contactgroup

vim /etc/nagios/conf.d/10.10.13.247.cfg

define service{
        use     generic-service
        host_name       10.10.13.247
        service_description     check_load
        check_command           check_nrpe!check_load
        max_check_attempts 5
        normal_check_interval 1
        contact_groups        common
        notifications_enabled  1                 ;是否開啟提醒功能。1為開啟,0為禁用。一般,這個選項會在主配置文件(nagios.cfg)中定義,效果相同。
        notification_period   24x7              ;發送提醒的時間段。非常重要的主機(服務)我定義為7×24,一般的主機(服務)就定義為上班時間。如果不在定義的時間段內,無論什么問題發生,都不會發送提醒。(時間格式為24x7不是24*7,如果寫成后者就會報錯)        
        notification_options w,u,c,r            ;這個是service的狀態。w為waning, u為unknown, c為critical, r為recover(恢復了),f為flapping,n為不發送提醒。類似的還有一個  host對應的狀態:d,u,r   d = 狀態為DOWN, u = 狀態為UNREACHABLE , r = 狀態恢復為OK,需要加入到host的定義配置里。(這一選項后面不能跟冒號跟冒號就會報錯,必須為空格)
}

#具體告警格式可以百度nagios 郵件告警

五、利用sendmail發送告警郵件

1.軟件檢測及安裝

#rpm -q sendmail

若不存在則安裝

#yum -y install sendmail

2.停止sendmail服務

這里不需要sendmail作為郵件服務端來運行,所以關閉sendmail服務。

#service sendmail stop

3.配置sendmail服務

配置發送郵件的郵箱認證信息

vi /etc/mail.rc 

添加如下內容:

set from=chenconghe@benco.com.cn

set smtp=mail.benco.com.cn

set smtp-auth-user=chenconghe

set smtp-auth-password=20160427150456

set smtp-auth=login

4.測試發送郵件功能

格式:echo "郵件內容" | mail -s "郵件標題" 目標郵件地址

echo "hello word" | mail -s "mail title" 313841662@qq.com

5.配置nagios郵件發送命令

修改nagios命令配置文件

#vim /etc/nagios/objects/commands.cfg

#notify-host-by-email命令的定義 

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" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

        }

#notify-service-by-email命令的定義 

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" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$

       }

6、接著修改nagios中定義的主機和服務的報警機制,實現nagios利用郵件自動報警。 

vim /etc/nagios/conf.d/10.10.13.238.cfg

define service{

        use     generic-service

        host_name       10.10.13.247

        service_description     notify-service-by-email

        check_command           notify-service-by-email

        max_check_attempts 5

        normal_check_interval 1

        notification_period 24x7

        contact_groups common

        notifications_enabled  1

        notification_options w,u,c,r

}

define service{

        use     generic-service

        host_name       10.10.13.247

        service_description     notify-host-by-email

        check_command           notify-host-by-email

        max_check_attempts 5

        normal_check_interval 1

        contact_groups common

        notifications_enabled  1

        notification_period 24x7

        notification_options w,u,c,r

}

#當重啟服務時出現以下錯誤:

Warning: Duplicate definition found for command 'notify-host-by-email' (config file '/etc/nagios/objects/commands.cfg', starting on line 244)

Error: Could not add object property in file '/etc/nagios/objects/commands.cfg' on line 245.

有可能是因為復制粘貼的時候復制了兩次,注意檢查配置文件

7、測試

將10.10.13.247關掉,QQ郵箱收到一封報警信,測試成功。

#當重啟客戶端的時候有可能在Nagios的監控頁面上有關需要nrpe輔助的項目會顯示出錯,需要重新檢查客戶端nrpe配置文件allowed_hosts和dont_blame_nrpe是否需要修改。突然斷電等可能導致配置信息丟失需要重新檢查


測試自動發送郵件參考此文章:http://4709096.blog.51cto.com/4699096/1703980

參考: 
調用短信接口   http://www.aminglinux.com/bbs/thread-7380-1-1.html
整合微信  http://www.aminglinux.com/bbs/thread-7917-1-1.html

到此,關于“nagios的安裝及配置方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女