jojo's blog--快樂憂傷都與你同在
          為夢想而來,為自由而生。 性情若水,風起水興,風息水止,故時而激蕩,時又清平……
          posts - 11,  comments - 30,  trackbacks - 0
          同事記錄的,我轉了過來。
          原文地址:http://www.ritto.cn/2008/12/129.html
          他的博客上記錄了好多安裝日記,有空的朋友可以去瞅瞅。

          我還沒有看,一會看完了了給他提點意見。^___^
          有顏色和粗體部分是我看完后加的。

          注意:這篇筆記里面的軟件盡量安裝所給的版本。



          1.安裝Nagios

          useradd nagios
          mkdir /usr/local/nagios
          chown nagios.nagios /usr/local/nagios

          tar zxvf nagios-2.9.tar.gz
          cd nagios-2.9
          ./configure --prefix=/usr/local/nagios --with-cgiurl=CGIURL --with-htmurl=HTMURL --with-nagios-user=nagios --with-nagios-group=nagios
          CGIURL 和HTMURL替換成自己的。
          如果沒有錯的話,繼續下面的步驟。
              //配置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.安裝插件
          tar 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配置
          Apache 的目的是可以在WEB上更加人性化的查看監控結果,其他的WEB服務器也可以。
          grep ^User /usr/local/apache2/conf/httpd.conf
          User daemon
          usermod -G nagios daemon
          此處替換成自己的。比如我的機器上是apache
          usermod -G nagios apache 把apache和nagios 都放到nagios 組中。
          vi /usr/local/apache2/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>

          /usr/local/apache2/bin/apachectl -t      //檢查配置文件是否正確
          httpd -t
          /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd ritto
          cat /usr/local/nagios/etc/htpasswd       //查看認證文件內容
          /usr/local/apache2/bin/apachectl start   //啟動apache
          service httpd restart


          4.配置Nagios

          Nagios自定義了一套規則用于配置文件,在nagios里面定義了一些基本對象,如:

          聯系人       contact         出了問題像誰報告?一般當然是系統管理員了
          監控時間段   timeperiod      7X24小時不間斷還是周一至周五,或是自定義的其他時間段
          被監控主機   host            所需要監控的服務器,當然可以是監控機自己
          監控命令     command nagios  發出的哪個指令來執行某個監控,這也是自己定義的
          被監控的服務 service         例如主機是否存活,80是否開,磁盤使用情況或自定義的服務等

          另:多個被監控主機可以定義為一個主機組
              多個聯系人可以被定義為一個聯系人組
              多個服務也可以被定義為一個服務組

          將配置文件改名:
          cd /usr/local/nagios/etc/
          tar zcvf bak.tar.gz *
          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, 命令檢查時間間隔
          ------------------------------------------------------------------------
          配置相關CGI腳本。
          vi cgi.cfg                         //修改cgi腳本控制文件

          use_authentication=1               //確保值為 1
          default_user_name=ritto            //修改為認證用戶
          //后面修改內容如下:
          authorized_for_system_information=nagiosadmin,test
          authorized_for_configuration_information=nagiosadmin,test
          authorized_for_system_commands=test         //多個用戶之間用逗號隔開
          authorized_for_all_services=nagiosadmin,test
          authorized_for_all_hosts=nagiosadmin,test
          authorized_for_all_service_commands=nagiosadmin,test
          authorized_for_all_host_commands=nagiosadmin,test
          -------------------------------------------------------------------------
          關于所有命令的配置都在commands.cfg里面。
          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
                  host_notification_period       24x7
                  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                          monitor@wswtek.com
                  pager                          13297949944
                  }
           
          define contact {
                  contact_name         ritto
                  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  service-notify-by-sms,service-notify-by-email
                  host_notification_commands     host-notify-by-email,host-notify-by-sms
                  email                          ritto.zhao@wswtek.com
                  pager                          13297949944
                  }

          //如果不需要手機報警的話,則改成如下:
          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                 ritto,sa
                  }
          -----------------------------------------------------------------------------
          vi hosts.cfg              //定義被監控主機

          #define monitor  host
           
          ############################################
          # Wangjing IDC servers                     #
          ############################################
          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
          ###############################################
          #  Wangjing IDC servers service for host-live #
          ###############################################
          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     //在監控的結果是wucr時通知聯系人,具體含義看前文.
                  }
          define service {
                  host_name               mail12.supertalent.com
                  service_description     check_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_command           check_tcp!80      //檢查tcp 80端口服務是否正常
                  }

          //書寫時要注意的是,check_tcp與要監控的服務端口之間要用”!”做分隔符。如果服務太多,以考慮用腳本來生成。

          -----------------------------------------------------------------------------

          確保這一部的warning 和 error 為0,則繼續下一步。
          /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    //開機自動運行

          -----------------------------------------------------------------------------

          使用命令和插件監控更多信息
          /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/
          至于NRPE的作用,下面的博客講的很詳細了。
          http://yahoon.blog.51cto.com/13184/41893
          tar zxvf nrpe-2.8.1.tar.gz
          cd nrpe-2.8.1
          ./configure                      //NRPE port: 5666
          make all
          make install-plugin           
          之前說過監控機需要安裝check_nrpe這個插件,被監控機并不需要,我們在這里安裝它是為了測試的目的
          make install-daemon              //安裝daemon
          make install-daemon-config       //安裝配置文件

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

          如果機器上沒有xinetd,就yum 安裝一個。
          將NRPE daemon作為xinetd下的一個服務運行
          yum -y install xinetd
          service xinetd start
          chkconfig --level 3 xinetd on

          make install-xinetd          //安裝xinetd腳本
          編輯這個腳本
          vi /etc/xinetd.d/nrpe
          only_from  = 127.0.0.1 192.168.4.226   //在后面增加監控主機的地址,以空格間隔

          增加nrpe服務
          vi /etc/services
          nrpe            5666/tcp                        # nrpe    //增加這一行

          重啟xinetd服務
          service xinetd restart

          netstat -at | grep nrpe     //查看NRPE是否已經啟動
          netstat -an | grep 5666     //查看5666端口是否被監聽
          如果以上都沒有任何結果,重復檢查一下上面添加的東西是否正確!

          vi /etc/sysconfig/iptables   //增加一條5666的端口
          -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT

          在被監控機器上測試一下NRPE是否正常工作。
          /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1     //測試NRPE是否正常工作
          NRPE v2.8.1           //正常的結果會顯示當前NRPE的版本號

          /usr/local/nagios/libexec/check_nrpe –h查看這個命令的用法
          可以看到用法是check_nrpe –H 被監控的主機 -c要執行的監控命令
          注意:-c后面接的監控命令必須是nrpe.cfg文件中定義的.也就是NRPE daemon只運行nrpe.cfg中所定義的命令


          查看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參數可以接的內容,=后面是實際執行的插件程序。

          比如,我現在要監控硬盤的情況,加入以下內容:
          command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda1
          command[check_hda3]=/usr/local/nagios/libexec/check_disk -w 20 -c 10 -p /dev/sda3

          所以以上nrpe.cfg配置的命令用法如下:
          [root@ytt etc]# check_nrpe -H127.0.0.1 -c check_hda1
          DISK OK - free space: /boot 77 MB (82% inode=99%);| /boot=16MB;78;88;0;98
          [root@ytt etc]# check_nrpe -H127.0.0.1 -c check_hda3
          DISK OK - free space: / 134960 MB (97% inode=99%);| /=3322MB;145789;145799;0;145809

          /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  //總進程數

          //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狀態

          -------------------------------------------------------------------------------------

          在運行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插件

          這一步,記得在被監控機器上的
          /etc/xinetd.d/nrpe里面加上監控機器的IP地址!
          /usr/local/nagios/libexec/check_nrpe -H 192.168.4.30
          NRPE v2.8.1        //測試一下監控機使用check_nrpe與被監控機運行的nrpedaemon之間的通信
                             //看到已經返回了正確的NRPE的版本信息,說明一切正常

          在commands.cfg中增加對check_nrpe的定義
          vi /usr/local/nagios/etc/commands.cfg
          #################################################################
          # 2008.12.4 by ritto
          #################################################################
          # '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負載的監控
          注意機器必須是LINUX系統,并且安裝了NRPE.
          vi services.cfg
          define service {
                  host_name               mail1
                  service_description     check-load
                  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_command           check_nrpe!check_load
                  }

          ----------------------------------------------------------------------

          在被監控機上增加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_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_command           check_nrpe!check_swap
                  }


          ------------------------------------------------------------------------------

          建議再檢查一下配置是否正確再啟動服務!
          所有配置都已經修改好,現在重啟Nagios,釘掉Nagios進程,再重啟,過一會就可以看到畫面了
          killall nagios
          /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
          或使用
          /etc/init.d/nagios restart/start/stop/status
          如查報錯,則可能是腳本路徑設置錯誤,
          vi /etc/init.d/nagios
          將prefix=/usr/local/nagios改為安裝的目錄/etc/init.d/nagios

           

          參考學習地址:
          http://sery.blog.51cto.com/10037/20520
          http://yahoon.blog.51cto.com/13184/41778
          posted on 2009-05-13 18:08 Blog of JoJo 閱讀(133) 評論(0)  編輯  收藏 所屬分類: 每日一記

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(6)

          隨筆檔案

          文章分類

          文章檔案

          新聞分類

          新聞檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 林芝县| 琼海市| 神木县| 长岛县| 南京市| 新野县| 崇文区| 英德市| 兰考县| 定安县| 大厂| 汝阳县| 习水县| 柏乡县| 托克托县| 遵义市| 九寨沟县| 大埔区| 阿瓦提县| 寿光市| 苏尼特右旗| 内丘县| 肥乡县| 镇原县| 新平| 灌阳县| 浮山县| 察隅县| 华安县| 伊宁县| 鹿邑县| 怀柔区| 海南省| 和平区| 赣州市| 台南县| 封丘县| 吉隆县| 葵青区| 西乌| 泽州县|