debian下LAMP+nginx代理+awstats+cacti+nagios(四)

 版本:nagios-plugins-1.4.15.tar.gz

              nagios-3.3.1.tar.gz

              nrpe-2.13.tar.gz

nagios 3.3 是變化很大的 他需要apache和PHP 結合 加CGI 原來只用CGI 。

因爲主頁原來是/usr/local/nagios/share/index.html 

現在是 index.php  但是感覺頁面沒原來好看了。

本章 是結合前面的LAMP 環境nginx代理沒了使用正常的80端口。

一 nagios3.3的配置開始

1apache安裝及配置

 vim /usr/local/apache-2.2.21/conf/httpd.conf

#setting for nagios
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin">
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd    
Require valid-user
</Directory>
                            
Alias /nagios /usr/local/nagios/share
<Directory "/usr/local/nagios/share">
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd 
Require valid-user
</Directory>

 

(2)安裝Nagios
添加nagios用戶和組
# groupadd nagios
# useradd -g nagios nagios
 

#groupadd nagcmd

#usermod -G nagcmd nagios
# id nagios
uid=1000(nagios) gid=1000(nagios) 組=1000(nagios),1001(nagcmd)

#tar zxvf nagios-3.3.1.tar.gz 

#cd nagios/

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

#make all

#make install

這裏會報錯。。。

/usr/bin/install: omitting directory `includes/rss/extlib’
/usr/bin/install: omitting directory `includes/rss/htdocs’
/usr/bin/install: omitting directory `includes/rss/scripts’
make[1]: *** [install] Error 1
make[1]: Leaving directory `/tmp/nagios-3.3.1/nagios/html’
make: *** [install] Error 2

解決方法:

sed -i 's:for file in includes/rss/*;:for file in includes/rss/*.*;:g' ./html/Makefile
sed -i 's:for file in includes/rss/extlib/*;:for file in includes/rss/extlib/*.*;:g' ./html/Makefile

或者手動 vim html/Makefile

#make install 

#  make install-init
#  make install-config

 #  make install-commandmode

 

# /usr/local/apache-2.2.21/bin/htpasswd -c /usr/local/nagios/etc/htpasswd houzc
New password:
Re-type new password:
Adding password for user houzc

 二  Nagios默認有如下幾個配置文件修改:

# ll /usr/local/nagios/etc/objects
commands.cfg     設定默認的指令來執行某個監控,也可以自己設定
contacts.cfg          設定聯繫人,出問題時的聯繫人與聯繫組
localhost.cfg          設定對本服務器的監控,配置其他服務時可參考此文件
timeperiods.cfg      設定週一至週五7X24小時不間斷,或自定義其他時間段
hosts.cfg                設定被監控的主機(自己創建)
services.cfg            設定被監控的服務(自己創建)
1)nagios.cfg主配置文件
# cd /usr/local/nagios/etc/
# vi nagios.cfg
//添加cfg配置文件
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
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
 
# check_external_commands=0 
check_external_commands=1
// 將其值修改爲1,作用是允許在web界面下執行重啓nagios、停止主機/服務檢查等操作
 
# command_check_interval = 10s
2)修改配置文件cgi.cfg
# cd /usr/local/nagios/etc/
# vi cgi.cfg
use_authentication=1
 
authorized_for_system_information=nagiosadmin,houzc
authorized_for_configuration_information=nagiosadmin,houzc
authorized_for_system_commands=houzc//多個用戶之間用逗號隔開
authorized_for_all_services=nagiosadmin,houzc
authorized_for_all_hosts=nagiosadmin,houzc
authorized_for_all_service_commands=nagiosadmin,houzc
authorized_for_all_host_commands=nagiosadmin,houzc
// houzc的用戶名即是從執行/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd houzc 而來的
 
3)查看修改其他配置文件
定義監控時間段,查看timeperiods.cfg
# ll /usr/local/nagios/etc/objects/ timeperiods.cfg
定義聯繫人,查看contacts.cfg
# vi /usr/local/nagios/etc/objects/contacts.cfg
 
 
define contact{
contact_name houzc
use generic-contact
alias nagiosadmin
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
}
 
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin,houzc #定義聯繫組的成員
 
注:以下是各參數的解釋
service_notification_period         24x7        
服務出狀況通知的時間段,參照timeperiods.cfg中定義的
host_notification_period          24x7        
主機出狀況通知的時間段,參照timeperiods.cfg中定義的
service_notification_options         w,u,c,r     
當服務出現w—報警(warning),u—未知(unkown),c—嚴重(critical),或者r—從異常情況恢復正常,在這四種情況下通知聯繫人.
host_notification_options         d,u,r                  
d—當機(down),u—返回不可達(unreachable),r—從異常情況恢復正常
service_notification_commands     notify-service-by-email     
服務出問題通知採用的命令,這個命令是在commands.cfg中定義的,作用是給聯繫人發郵件.
host_notification_commands        notify-host-by-email
主機出問題通知採用的命令,這個命令是在commands.cfg中定義的,作用是給聯繫人發郵件.
 
 
定義被監控主機,查看hosts.cfg
# vim /usr/local/nagios/etc/objects/hosts.cfg
 
 
 
define host{
        host_name                     192.168.3.107 
        address                          192.168.3.107
        check_command                 check-host-alive
        max_check_attempts              5
        check_period                    24x7
        contact_groups                  admins
        notification_interval              10
        notification_period               24x7
        notification_options              d,u,r
}

 

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

 
#service definition
define service{
        host_name                   192.168.3.107       #被監控的主機,hosts.cfg中定義的
        service_description       check-host-alive   #被監控服務的描述
        check_command          check-host-alive #所用的命令,是commands.cfg中定義的
        max_check_attempts        5
        normal_check_interval      3
        retry_check_interval         2
        check_period                  24x7
        notification_interval          10
        notification_period           24x7
        notification_options          w,u,c,r
        contact_groups              admins            # 聯繫人組,是contactgroups.cfg中定義的
}

 

 

三 .安裝Nagios-plugins插件

 
# tar -zxvf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15/
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make
# make install
 
注:安裝成功後,會在/usr/local/nagios/目錄下生成libexec文件夾
# /usr/local/nagios/libexec/check_mrtg -h
libexec目錄下的所有程序都是可以獨立執行的,使用方法可以通過”命令名 –h”來查看

 

運行nagios之前先進行測試:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
 
…………..前面省略
Checking host dependencies...
        Checked 0 host dependencies.
Checking commands...
        Checked 24 commands.
Checking time periods...
        Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...
Total Warnings: 0
Total Errors:   0
Things look okay - No serious problems were detected during the pre-flight check

service nagios start

訪問http://192.168.3.107/nagios

 

四、監控遠程服務器的服務

主:192.168.3.107

被:192.168.3.101

nagios 是一個主動與被動的監控,什麼是主動和被動?

主動 :就是被監控端不用授權或者被監控是提供對外的服務。如:ping HTTP

下面圖是本地監控

如果想監控URL 不同參數 還要自己修改他的插件的參數。

看這篇:http://hzcsky.blog.51cto.com/1560073/493599

 

被動 :對於像磁盤容量,cpu負載這樣的“本地信息”,nagios只能監測自己所在的主機,而對其他的機器則顯得有點無能爲力,畢竟沒得到被控主機的適當權限是不可能得到這些信息的。爲了解決這個問題,nagios有一個附加組件—NRPE。用它就可以完成對linux類型主機“遠端本地信息”的監控。

 被動監控需要通過SLL 加入 隧道然後 NRPE ->libexec的監控插件採集數據。

 

1.被監控機上的配置
(1)安裝nagios-plugins插件
添加用戶和組
# groupadd nagios
# useradd -g nagios nagios
 
安裝nagios-plugins
#tar  -zxvf nagios-plugins-1.4.15.tar.gz
#cd nagios-plugins-1.4.15
#./configure
#make && make install
# chown nagios.nagios /usr/local/nagios
# chown -R nagios.nagios /usr/local/nagios/libexec

(2安裝nrpe

apt-get install libssl-dev libssl0.9.8
 centos爲:
openssl-
openssl-devel-
tar -zxvf nrpe-2.13.tar.gz 
  cd nrpe-2.13
 ./configure
 make all
make install-plugin  
注:其實只用在監測服務器上安裝,被監測機不用安裝,只是爲了測試
make  install-daemon
 make install-daemon-config
ls /usr/local/nagios 會生成 bin etc libexec share
 vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.3.107 監控服務器的IP
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d #啓動
[root@bogon nrpe-2.13]#  /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.13
這樣說明安裝成功
(3)nrpe監控命令
cd /usr/local/nagios/etc/
vim nrpe.cfg
 

 
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_dev]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

注:藍色標註的是命令名,即check_users就是等號後面/usr/local/nagios/libexec/check_users -w 5 -c 10的簡稱。這五個命令,分別是監控登錄用戶數、cpu負載、/ 分區使用情況、殭屍進程和總的進程數。

具體可以-h 查看相應參數。

 

 [root@bogon libexec]# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs
PROCS OK: 85 processes
本地測試下。
2、監控機上的配置
被監控端的NRPE 沒問題 後 主監控端也要安裝
回到107上
(1)安裝check_nrpe
apt-get install libssl-dev libssl0.9.8
# tar -zxvf nrpe-2.12.tar.gz -C /usr/src/
# cd /usr/src/nrpe-2.12
# ./configure
# make all
# make install-plugin
注:將check_nrpe 安裝到/usr/local/nagios/libexec目錄下 (必須)
這裏爲了測試裝:
make install-daemon
make install-daemon-config
/usr/local/nagios/libexec/check_nrpe -H 192.168.3.101
NRPE v2.13
也沒問題

2添加check_nrpe定義

vim /usr/local/nagios/etc/objects/commands.cfg
 
添加如下內容:
# 'check_nrpe ' command definition
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

 $USER1  是/usr/local/nagios/etc/resource.cfg  聲明的

$USER1$=/usr/local/nagios/libexec

(3)修改配置文件
在監測服務器上修改services.cfg文件
# vim /usr/local/nagios/etc/objects/services.cfg
 
define service{
        host_name               192.168.3.101 #被監控的主機名,這裏注意必須是linux且運行着nrpe,而且必須是hosts.cfg中定義的
        service_description       check-load       # 監控項目的名稱
        check_command           check_nrpe!check_load
#監控命令是check_nrpe,是在commands.cfg中定義的,帶的參數是check_load,是在nrpe.cfg中定義的
        max_check_attempts      5
        normal_check_interval    3
        retry_check_interval      2
        check_period            24x7
        notification_interval      10
        notification_period       24x7
        notification_options      w,u,c,r
        contact_groups          admins
        }
這裏只添加一個測試。
 

host 配置文件添加和107 修改沒區別只是IP地址變了。

 define host{
   host_name                     192.168.3.101
      address                        192.168.3.101
      check_command                 check-host-alive
      max_check_attempts              5
      check_period                    24x7
      contact_groups                admins
      notification_interval              10
      notification_period               24x7
      notification_options              d,u,r
}

測試nagios配置# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

重啓nagios服務
# service nagios restart

五、利用郵件手機報警。

 nagios發警告郵件是採用本機的smtp服務,可以查看commands.cfg中關於發郵件的命令進行定義。使用本機的mail命令,就需要開啓本機的smtp服務。

SendEmail是一個通過命令來發smtp郵件的程序,下面介紹它的安裝和使用。
1.安裝郵件服務器。

 我是用的公司的郵件服務器建立了個用戶而已,不然自己用postfix 搭建個SMTP端只爲nagios用更好!

 

# tar –zxvf sendEmail-v1.55.tar.gz -c /usr/src

# cp sendEmail-v1.55/sendEmail /usr/local/bin

# chmod +x /usr/local/bin/sendEmail
使用sendEmail命令發郵件
# /usr/local/bin/sendEmail -f [email protected] -t [email protected] -s mail.wsn.com.cn -u "hello" -xu [email protected] -xp 123456 -m aaa
注:以下是命令參數的介紹
-f       表示發送者的郵箱
-t       表示接收者的郵箱
-s      表示SMTP服務器的域名或者ip
-u      表示郵件的主題
-xu    表示SMTP驗證的用戶名
-xp    表示SMTP驗證的密碼
-m     表示郵件的內容,

如果你不帶-m參數的話,就會提示你自行輸入,輸入完成後使用CTRL-D來結束

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

 

#文件前面有最後先註釋 直接複製修改自己的郵件用戶和認證就可以! 

# '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\n
Date/Time: $LONGDATETIME$\n" | /usr/local/bin/sendEmail -f 
[email protected] -t $CONTACTEMAIL$ -s 192.168.25.23 -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -xu nagios@
363.com -xp hou123..
        }

# '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: $SERVICE
STATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /usr/local/bin/sendEmail -f
[email protected] -t $CONTACTEMAIL$ -s 192.168.25.23 -u "** $NOTIFICATIONTYPE$ Service
 Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu
[email protected]
-xp hou123..
        }

 

重啓nagios服務,即可實現發送郵件的功能。
默認設置5次輪詢,5次錯誤以上在會報警的。
下面是一個效果圖,郵件服務器沒壞的話 延遲基本沒有的!

 

如有問題請加羣:71922203!!!!

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