nagios安裝與配置

 

nagios安裝與配置
1.安裝Nagios
useradd nagios
mkdir /usr/local/nagios
chown nagios.nagios /usr/local/nagios
或者
Useradd –d /usr/local/nagios nagios

tar zxvf nagios-2.9.tar.gz
cd nagios-2.9
./configure --prefix=/usr/local/nagios    //配置nagios
make all                             //編譯nagios
make install                       //安裝主要的程序,CGI及HTML文件
make install-init                  //在/etc/rc.d/init.d安裝啓動腳本
make install-commandmode   //給外部命令訪問nagios配置文件的權限
make install-config              //將配置文件的例子複製到nagios的安裝目錄
驗證程序是否被正確安裝
看是否存在etc,bin,sbin,share,var五個目錄
bin     Nagios執行程序所在目錄,nagios文件即爲主程序
etc     Nagios配置文件位置
sbin    Nagios cgi文件所在目錄,執行外部命令所需文件所在的目錄
share Nagios網頁文件所在的目錄
var     Nagios日誌文件,spid等文件所在的目錄

2.安裝插件
ar zxvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure --prefix=/usr/local/nagios/
make
make install
ls /usr/local/nagios/libexec/         //會顯示安裝的插件文件

3.修改apache配置
grep ^User /opt/lamp/etc/httpd.conf
User nobody
usermod -G nagios nobody

vi /opt/lamp/etc/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>
/opt/lamp/bin/apachectl -t           //檢查配置文件是否正確
/opt/lamp/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin
cat /usr/local/nagios/etc/htpasswd            //查看認證文件內容
/opt/lamp/lampp start       //啓動apache

4.配置Nagios
Nagios自定義了一套規則用於配置文件,在nagios裏面定義了一些基本對象,如:
聯繫人       contact               出了問題像誰報告?一般當然是系統管理員了
監控時間段   timeperiod          7X24小時不間斷還是週一至週五,或是自定義的其他時間段
被監控主機   host                  所需要監控的服務器,當然可以是監控機自己
監控命令     command nagios  發出的哪個指令來執行某個監控,這也是自己定義的
被監控的服務 service              例如主機是否存活,80是否開,磁盤使用情況或自定義的服務等
另:多個被監控主機可以定義爲一個主機組
    多個聯繫人可以被定義爲一個聯繫人組
    多個服務也可以被定義爲一個服務組
將配置文件改名:
cd /usr/local/nagios/etc/
mv cgi.cfg-sample cgi.cfg
mv commands.cfg-sample commands.cfg
mv localhost.cfg-sample localhost.cfg
mv nagios.cfg-sample nagios.cfg
mv resource.cfg-sample resource.cfg

修改Nagios的配置文件:
------------------------------------------------------------------------
vi nagios.cfg修改nagios的主配置文件                                     //
#cfg_file=/usr/local/nagios//etc/localhost.cfg    //註釋此行 37
//將下面幾行的註釋去掉
cfg_file=/usr/local/nagios/etc/contactgroups.cfg  //聯繫組配置文件路徑
cfg_file=/usr/local/nagios/etc/contacts.cfg          //聯繫人配置文件路徑
cfg_file=/usr/local/nagios/etc/hostgroups.cfg      //主機組配置文件路徑
cfg_file=/usr/local/nagios/etc/hosts.cfg              //主機配置文件路徑
cfg_file=/usr/local/nagios/etc/services.cfg          //服務配置文件路徑
cfg_file=/usr/local/nagios/etc/timeperiods.cfg    //監視時段配置文件路徑
check_external_commands=0     //將 0 改成 1,允許在web界面下執行重啓Nagios
command_check_interval=15s    //改成 10s, 命令檢查時間間隔
------------------------------------------------------------------------
vi cgi.cfg修改cgi腳本控制文件                         //
use_authentication=1               //確保值爲 1
default_user_name=nagiosadmin            //
修改爲認證用戶
//後面修改內容如下:
authorized_for_system_information=nagiosadmin,nagiosadmin
authorized_for_configuration_information=nagiosadmin,nagiosadmin
authorized_for_system_commands=nagiosadmin         //多個用戶之間用逗號隔開
authorized_for_all_services=nagiosadmin,nagiosadmin
authorized_for_all_hosts=nagiosadmin,nagiosadmin
authorized_for_all_service_commands=nagiosadmin,nagiosadmin
authorized_for_all_host_commands=nagiosadmin,nagiosadmin
-------------------------------------------------------------------------
vi misccommands.cfg                //主要功能是用於發送報警短信和報警郵件
#host-notify-by-sms              //發送短信報警
define command {
       command_name      host-notify-by-sms
       command_line      /usr/local/bin/sms_send "Host $HOSTSTATE$ alert for $HOSTNAME$! on '$DATETIME$' " $CONTACTPAGER$
       }
#service notify by sms        //發送短信報警
define command {
       command_name     service-notify-by-sms
       command_line     /usr/local/bin/sms_send "'$HOSTADDRESS$' $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTPAGER$
       }

-----------------------------------------------------------------------------
vi timeperiods.cfg定義監控時間段,名稱是24*7,監控時間是全天24小時           //
define timeperiod {
        timeperiod_name         24x7     //時間段的名稱,這個地方不要有空格
        alias                   24 Hours A Day,7Days 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
        }
-----------------------------------------------------------------------------
vi contacts.cfg定義聯繫人     //
define contact {
        contact_name         sa                     //不要有空格
        alias                system administrator
        service_notification_period    24x7      //服務出了狀況通知的時間段,由timeperiods.cfg中定義的.
        host_notification_period       24x7      //主機出了狀況通知的時間段,由timeperiods.cfg中定義的.
        service_notification_options   w,u,c,r   //當服務出現故障
        host_notification_options       d,u,r     //當主機出現故障
        service_notification_commands  service-notify-by-sms,service-notify-by-email  //命令讀配置miscommands.cfg
        host_notification_commands     host-notify-by-email,host-notify-by-sms      //命令讀配置miscommands.cfg
        email                          [email protected]
        pager                          132********
        }
 
define contact {
        contact_name         nagiosadmin
        alias                system administrator
        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-by-email   
                       //notify-by-mail
是在commands.cfg中定義的,給聯繫人發郵件
        host_notification_commands     host-notify-by-email 
                       //主機出現故障時,給聯繫人發郵件
        email                         
[email protected]    //聯繫人郵件地址
        pager                          132********   
                      //
聯繫人的手機,如果支持短信通知的話,會很實用
        }
//如果不需要手機報警的話,則改成如下:
service_notification_commands   notify-by-email
host_notification_commands      host-notify-by-email
//上面的文件定義了2個聯繫人,如果有更多聯繫人的話,照這個格式在後面追加即可。
//服務通知選項(service_notification_options)
//與主機通知選項(host_notification_options)的幾個選項在這裏說明一下:
//w-warning 報警 , u-unknown 未知, c-critical 嚴重 , r-recovery 從異常情況恢復正常 ; d-down 關機了,
//u-unreachable,注意一下,主機報警和服務報警有些差異。


 
-----------------------------------------------------------------------------
vi contactgroups.cfg將多個聯繫人定義一個聯繫人組       //
define contactgroup{
        contactgroup_name       sagroup
        alias                   System Administrators
        members                 sa,nagiosadmin
        }
-----------------------------------------------------------------------------
vi hosts.cfg定義被監控主機              //
#define monitor  host
define host {
       host_name                  nagios-server
       alias                      nagios server
       address                    192.168.4.226
       contact_groups             sagroup             
                             //多個聯繫組用逗號分隔,數據來源於contactgroups.cfg
       check_command              check-host-alive
                            //這個命令來自commands.cfg,用來監控主機是否存活
       max_check_attempts         5          //檢查失敗後重試的次數
       notification_interval      10             //提醒的時間,每隔10秒提醒一次
       notification_period        24x7                
                            //提醒的週期,24*7,來自之前timeperiods.cfg中定義的
       notification_options       d,u,r               
                            //指定什麼情況下提醒,來自contacts.cfg中定義的
       }
 
define host {
       host_name                  mail12.supertalent.com
       alias                          nagios test client
       address                     192.168.4.41
       contact_groups             sagroup
       check_command           check-host-alive
       max_check_attempts    5
       notification_interval      10
       notification_period        24x7
       notification_options       d,u,r
       }
------------------------------------------------------------------------------
vi hostgroups.cfg將多個主機定義一個主機組          //
define hostgroup{
        hostgroup_name          sa-servers   //主機組名稱
        alias                         sa Servers   //別名
        members                    nagios-server
                     //
組的成員主機,多個主機以逗號相隔,必須是上面hosts.cfg中定義的
        }
------------------------------------------------------------------------------
vi services.cfg定義監控的服務           //
#service definition

define service{
        host_name               nagios-server   
                           //要監控的主機, 必須是hosts.cfg 中定義的
        service_description     check-host-alive    
                          //定義的是監控這個主機是不是存活
                                       //給監控項目起個名字,任意起都可以,如check ftp
        check_command           check-host-alive    
                           //所用的命令,是commands.cfg中定義的
                                      //所用的命令,必須是commands.cfg中定義的
        check_period            24x7                  
                           //
監控的時間段,是timeperiods.cfg中定義的
        max_check_attempts      5
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup    //聯繫人組, contactgroups.cfg中定義的
        notification_interval   10
        notification_period     24x7        //通知的時間段, ,是timeperiods.cfg中定義的
        notification_options    w,u,c,r                                    
        }

define service {
        host_name               mail12.supertalent.com
        service_description     check_tcp 80

define service {
        host_name               mail12.supertalent.com
        service_description     check_http
      check_command         check_http      //
檢查tcp 80端口服務是否正常
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval       2
        contact_groups              sagroup
        notification_interval         10
        notification_period           24x7
        notification_options          w,u,c,r
        }
//書寫時要注意的是,check_tcp與要監控的服務端口之間要用”!”做分隔符。如果服務太多,以考慮用腳本來生成。
-----------------------------------------------------------------------------
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg   //檢查所有配置文件的正確性
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg   //作爲守護進程後臺啓動Nagios
echo "/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg" >> /etc/rc.local    //開機自動運行
-----------------------------------------------------------------------------
使用命令和插件監控更多信息
cd /usr/local/nagios/libexec        //插件默認的安裝路徑
./check_disk -w 10% -c 5% /     
//檢查根分區的使用情況,若剩餘10%以下,爲警告狀態(warning)
 //若剩餘 5%以下,爲嚴重狀態(critical)

先監控一臺主機
在hosts.cfg中定義主機名 --- 在services.cfg中定義監控內容
------------------------------------------------------------------------------
使用NRPE監控Linux上的"本地信息"
對系統爲Linux的主機進行如下監控: CPU負載,磁盤容量,登陸用戶數,總進程數,殭屍進程數,swap分區使用情況
在被監控主機上.
useradd nagios
passwd nagios

tar zxvf nagios-plugins-1.4.9.tar.gz
cd nagios-plugins-1.4.9
./configure --prefix=/usr/local/nagios
make
make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec/

tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure                      //NRPE port: 5666
make all
make install-plugin             
make install-daemon              //安裝daemon
make install-daemon-config       //安裝配置文件

ls /usr/local/nagios/
bin/     etc/     libexec/ share/     //現在nagios目錄會有4個目錄了

將NRPE daemon作爲xinetd下的一個服務運行
service xinetd restart
chkconfig --level 3 xinetd on
make install-xinetd          //安裝xinetd腳本

vi /etc/xinetd.d/nrpe
only_from  = 127.0.0.1 192.168.4.226   //在後面增加監控主機的地址,以空格間隔

vi /etc/services
nrpe            5666/tcp                        # nrpe    //增加這一行
vi /etc/sysconfig/iptables   //增加一條5666的端口
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT
vi /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1, 192.168.4.226     //在後面增加監控主機的地址,以”,”間隔
chkconfig nrpe on
service xinetd restart
netstat -at | grep nrpe     //查看NRPE是否已經啓動
netstat -an | grep 5666     //查看5666端口是否被監聽

/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1     //測試NRPE是否正常工作
NRPE v2.8.1           //正常的結果會顯示當前NRPE的版本號

查看NRPE的監控命令
cd /usr/local/nagios/etc
vi 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_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/hda1
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_nrpe的-c參數可以接的內容,=後面是實際執行的插件程序
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_users        //檢測登陸用戶數
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_load         //CPU負載
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_zombie_procs //殭屍進程
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_total_procs  //總進程數
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_disk  //磁盤使用量

其中:
//check_load -w 15,10,5 -c 30,25,20
//在unix裏面負載的均值通常表示是1分鐘,5分鐘,15分鐘內平均有多少進程處於等待狀態
//當1分鐘多於15個進程等待,5分鐘多於10個,15分鐘多於5個則爲warning狀態
//當1分鐘多於30個進程等待,5分鐘多於25個,15分鐘多於20個則爲critical狀態


記住監測磁盤時,先用
fdisk -l   //查看磁盤的分區,如果sda, 則應該改成下面的形式,把默認的hda1改成sda1
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14         268     2048287+  82  Linux swap / Solaris
/dev/sda3             269       19457   154135642+  83  Linux


nrpe.cfg中加入:
command[check_sda1例如查看根分區的使用情況,執行
/usr/local/nagios/libexec/check_disk -w 10% -c 5% /
命令的含義是檢查分區/的使用情況,若剩餘10%以下,爲警告狀態(warning),5%以下爲嚴重狀態(critical),
執行後我們會看到下面這條信息
DISK WARNING - free space: / 135169 MB (97% inode=99%);| /=3113MB;145789;145799;0;145809
說明當前是warning的狀態,空閒空間還有97%.
]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
command[check_sda3]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda3


/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_sda1
DISK OK - free space: /boot 77 MB (82% inode=99%);| /boot=16MB;78;88;0;98
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_sda3
DISK OK - free space: / 135169 MB (97% inode=99%);| /=3113MB;145789;145799;0;145809


 
-------------------------------------------------------------------------------------
在運行Nagios的監控主機上
因爲之前已經將Nagios運行起來了,現在要做的是:
安裝check_nrpe插件
在commands.cfg中創建check_nrpe中的命令,只有在commands.cfg中定義過的命令才能在services.cfg中使用
創建對被監控主機的監控項目

tar zxvf nrpe-2.8.1.tar.gz
cd nrpe-2.8.1
./configure
make all
make install-plugin    //只運行這一步即可,只需要check_nrpe插件

/usr/local/nagios/libexec/check_nrpe -H 192.168.4.30
NRPE v2.8.1       
       //測試一下監控機使用check_nrpe與被監控機運行的nrpedaemon之間的通信
          //看到已經返回了正確的NRPE的版本信息,說明一切正常
        //如返回"Could not complete SSL handshake",則要檢查被監控機/etc/xinetd.d/nrpe中only_from這項是否允許監控機訪問

在commands.cfg中增加對check_nrpe的定義
vi /usr/local/nagios/etc/commands.cfg
#################################################################
# 2008.12.4 by nagiosadmin
#################################################################
# 'check_nrpe' command definition
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

//command_name check_nrpe 定義命令名稱爲check_nrpe, 在services.cfg中要使用這個名稱
//command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$  定義實際運行的插件程序


接下來可以在services.cfg中定義對主機CPU負載的監控
vi services.cfg
define service {
        host_name               mail1
        service_description     check-load
        check_command           check_nrpe!check_load
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }

define service {
        host_name               mail1
        service_description     check-users
        check_command           check_nrpe!check_users
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }

define service {
        host_name               mail1
        service_description     check-zombie-process
        check_command           check_nrpe!check_zombie_procs
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }
define service {
        host_name               mail1
        service_description     /home space
        check_command           check_nrpe! check_sda3      // 這裏的check_sda3跟被監控主機上nrpe.cfg文件裏對應
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }

----------------------------------------------------------------------
在被監控機上增加check_swap命令的定義
vi /usr/local/nagios/etc/nrpe.cfg
增加下面這一行
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

//如果在被監控機上是以daemon運行的nrpe,則需要手動重啓
//如果在被監控機上是以xinetd運行的,則不需要
----------------------------------------------------------------------

在監控機上增加這個監控項目:
vi /etc/services.cfg
define service {
        host_name               mail1
        service_description     check-swap
        check_command           check_nrpe!check_swap
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        contact_groups          sagroup
        }


------------------------------------------------------------------------------
所有配置都已經修改好,現在重啓Nagios,釘掉Nagios進程,再重啓,過一會就可以看到畫面了
killall nagios
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
或使用
/etc/init.d/nagiso restart/start/stop/status
如查報錯,則可能是腳本路徑設置錯誤,
vi /etc/init.d/nagios
將prefix=/usr/local/nagiosaa改爲安裝的目錄/etc/init.d/nagios
 
 
 
 
文件:
大小:
40KB
下載:
 
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章