一、環(huán)境配置
站在用戶的角度思考問題,與客戶深入溝通,找到橫縣網(wǎng)站設(shè)計與橫縣網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋橫縣地區(qū)。
1、系統(tǒng):CentOS release 5.6 IP:192.168.4.200 主機(jī)名:JW01
二、軟件下載
Nagios安裝至少也應(yīng)該配置下Apache的Httpd(已裝)服務(wù)器,否則沒有界面,只能靠查看日志去找錯誤,那就是完全沒有意外了。
Nagios下載地址:http://www.nagios.org/download/
nagios-3.4.4.tar.gz nagios-plugins-1.4.16.tar.gz
必須安裝GCC?。?! 其實,我們的apache可以直接用yum來安裝,不必指定路徑,就可以直接配置apache了。
三、安裝前的準(zhǔn)備工作
1、解決安裝Nagios的依賴關(guān)系:
Nagios基本組件的運(yùn)行依賴于httpd、gcc和gd??梢酝ㄟ^以下命令來檢查nagios所依賴的rpm包是否已經(jīng)完全安裝:
[root@jw01 ~] # yum -y install httpd gcc glibc glibc-common *gd* php php-MySQL mysql mysql-server |
2、所需要安裝組件:
服務(wù)端所用的安裝包為 nagios nagios-plugins ndoutils(非必須)
linux客戶端 nrpe
windows客戶端 NSClient++
3、進(jìn)行selinux設(shè)置,或者關(guān)閉selinux
[root@jw01 ~]# vim /etc/selinux/config SELINUX=disabled |
4、添加nagios運(yùn)行所需要的用戶和組:
[root@jw01 ~]# groupadd nagcmd [root@jw01 ~]# useradd -m nagios [root@jw01 ~]# usermod -a -G nagcmd nagios |
四、編譯安裝nagios
1、安裝nagios
[root@jw01 software]# tar -zxvf nagios-3.4.4.tar.gz #解壓nagios [root@jw01 software]# cd nagios/ [root@jw01 nagios]# ./configure -prefix=/usr/local/nagios #配置nagios的安裝路徑 [root@jw01 nagios]# make all #編譯 [root@jw01 nagios]# make install #主要安裝程序、CGI以及HTML文件 make install-config - This installs sample config files in /usr/local/nagios/etc make[1]: Leaving directory `/opt/software/nagios' [root@jw01 nagios]# make install-commandnode #給外部命令訪問nagios配置文件權(quán)限 /usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/var/rw chmod g+s /usr/local/nagios/var/rw *** External command directory configured *** [root@jw01 nagios]# /usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/var/rw [root@jw01 nagios]# chmod g+s /usr/local/nagios/var/rw [root@jw01 nagios]# mekde install-config #把配置文件的例子復(fù)制到nagios的安裝目錄 [root@jw01 nagios]# make install-init #把nagios做成一個運(yùn)行腳本,使nagios隨系統(tǒng)開機(jī)啟動 /usr/bin/install -c -m 755 -d -o root -g root /etc/rc.d/init.d /usr/bin/install -c -m 755 -o root -g root daemon-init /etc/rc.d/init.d/nagios *** Init script installed *** [root@jw01 nagios]# usr/bin/install -c -m 755 -d -o root -g root /etc/rc.d/init.d [root@jw01 nagios]# /usr/bin/install -c -m 755 -o root -g root daemon-init /etc/rc.d/init.d/nagios |
2、驗證程序是否安裝正確
[root@jw01 nagios]# cd /usr/local/nagios/ [root@jw01 nagios]# ll total 28 drwxrwxr-x 2 nagios nagios 4096 Feb 7 17:00 bin drwxrwxr-x 3 nagios nagios 4096 Feb 7 15:40 etc drwxr-xr-x 2 root root 4096 Feb 7 14:52 include drwxrwxr-x 2 nagios nagios 4096 Feb 7 14:52 libexec drwxrwxr-x 2 nagios nagios 4096 Feb 7 17:00 sbin drwxrwxr-x 11 nagios nagios 4096 Feb 7 17:00 share drwxrwxr-x 5 nagios nagios 4096 Feb 7 14:19var |
存在 etc、bin、 sbin、 share、 var這五個目錄,如果存在則可以表明程序被正確的安裝到系統(tǒng)了。后表是五個目錄功能的簡要說明:
bin |
Nagios執(zhí)行程序所在目錄,這個目錄只有一個文件nagios |
etc | Nagios配置文件位置,初始安裝完后,只有幾個*.cfg-sample文件 |
sbin | Nagios Cgi文件所在目錄,也就是執(zhí)行外部命令所需文件所在的目錄 |
share | Nagios網(wǎng)頁文件所在的目錄 |
var | Nagios日志文件、spid 等文件所在的目錄 |
五、安裝nagios插件
[root@jw01 software]# tar -zxvf nagios-plugins-1.4.16.tar.gz [root@jw01 software]# cd nagios-plugins-1.4.16 [root@jw01 software]# ./configure -prefix=/usr/local/nagios #這里的路徑是安裝nagios的路徑 [root@jw01 software]# make [root@jw01 software]#make install |
說明一下的是在配置過程指定的安裝路徑是/usr/local/nagios,而不是/usr/local/nagios-plus,安裝完成后,將在目錄/usr/local/nagios生成目錄libexec(里面有很多文件),這正是nagios所需要的。
六、安裝Apache (前段時間APACHE已經(jīng)安裝了)
Web服務(wù)不是nagios所必須的,但是如果nagios沒有web,查看監(jiān)控對象的對象將是非常費事和沒有趣味的事情(只有通過查看nagios的日志來判斷狀態(tài))。我不愿干特?zé)o聊的事,所以就花少許時間把web安裝一下。
在unix/linux世界,apache是web服務(wù)器的首選對象,其下載網(wǎng)站為www.apache.org 。建議下載源碼。因為我們不需要很復(fù)雜的web功能,因此簡單的執(zhí)行一下幾個步驟就可以正確的把a(bǔ)pache安裝到系統(tǒng):
1、安裝Apache;
[root@jw01 software]# tar -zxvf httpd-2.2.0.tar.gz [root@jw01 software]# cd httpd-2.2.0 [root@jw01 software]# ./configure –prefix=/usr/local/apache #指定Apache的安裝路徑 [root@jw01 software]# make [root@jw01 software]# make install [root@jw01 software]# ./usr/local/apache/bin/apachectl –t #檢查一下apache是否正確安裝 [root@jw01 ~]# /usr/sbin/apachectl -t Syntax OK [root@jw01 ~]# #自己的的路徑 |
2、Apache 配置
第一步是修改apache的配置文件httpd.conf,這里的文件路徑是/usr/local/apache/conf/httpd.conf 。把a(bǔ)pache的運(yùn)行用戶[1]和運(yùn)行組改成nagios,往下把下面的行追加到文件httpd.conf的末尾:
我自己是寫在代理的目錄下,效果都是一樣的。
[root@jw01 ~]# /usr/local/apache/conf/httpd.conf #setting for nagios ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin AuthType Basic Options ExecCGI AllowOverride None Order allow,deny Allow from all AuthName "Nagios Access" AuthUserFile /usr/local/nagios/etc/htpasswd Require valid-user Alias /nagios /usr/local/nagios/share AuthType Basic Options None AllowOverride None Order allow,deny Allow from all AuthName "nagios Access" AuthUserFile /usr/local/nagios/etc/htpasswd Require valid-user [root@jw01 conf.d]# pwd /etc/httpd/conf.d [root@jw01 conf.d]# ll total 136 -rw-r--r-- 1 root root 648 Feb 7 17:30 nagios.conf #自己在代理目錄下新增了nagios.conf文件 |
上述文本塊的作用是對nagios的目錄進(jìn)行用戶驗證,只有合法的授權(quán)用戶才可以訪問nagios的頁面文件。第二步是生成用戶驗證文件:只要執(zhí)行命令
[root@jw01~]# /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd kuangl |
就會生成web的合法訪問用戶kuangl;命令交互執(zhí)行,需要輸入2次密碼,然后就在文件/usr/local/nagios/etc/htpasswd寫入一行-第一個字段是剛生成的用戶名,第二個是加密后的密碼,如果還要添加更多的用戶,執(zhí)行命令 htpasswd 就不需要選項 “-c”,否則就會覆蓋所有已經(jīng)生成的行。
配置完成后,執(zhí)行/usr/local/apache/bin/apachctl –t 檢查apache配置文件是否有語法錯誤,無誤后用/usr/local/apache/bin/apachctl start & 把a(bǔ)pache啟動,然后從另外的機(jī)器的瀏覽器輸入nagios 的訪問地址(如:http://192.168.4.200/nagios),如果正常,將出現(xiàn)下圖的登錄驗證窗口等待用戶輸入:
七、nagios配置
剛安裝完成的nagios,其配置文件的目錄是/usr/local/nagios/etc,下圖是其etc目錄的文件:
[root@jw01 conf.d]# cd /usr/local/nagios/etc/ [root@jw01 etc]# ll total 68 -rw-rw-r-- 1 nagios nagios 11717 Feb 7 17:57 cgi.cfg -rw-r--r-- 1 root root 21 Feb 20 13:47 htpasswd -rw-rw-r-- 1 nagios nagios 44710 Feb 7 14:43 nagios.cfg drwxrwxr-x 2 nagios nagios 4096 Feb 20 15:23 objects -rw-rw---- 1 nagios nagios 1340 Feb 7 14:43 resource.cfg [root@jw01 etc]# |
從nagios2.6版開始,不需要更改文件名也不用修改配置文件localhost.cfg就可以直接運(yùn)行../bin/nagios –v nagios.cfg驗證程序是否能正常運(yùn)行(nagios2.5及以前版本的最小運(yùn)行的配置文件是minimal.cfg,但需要修改這個文件多處才能驗證成功)。
1、修改nagios的主配置文件nagios.cfg
[root@jw01 etc]# vim nagios.cfg 在文件中的143行改check_external_commands=0為check_external_commands=1 這行的作用是允許執(zhí)行在web界面下重啟nagios、停止主機(jī)/服務(wù)檢查等操作。 在文件中的161行改command_check_interva=15s的值從默認(rèn)的15 command_check_interval=10s |
2、修改cgi.cfg 配置文件
[root@jw01 etc]# vim cgi.cfg 在文件的78行確保use_authentication=1。曾看過不少的文章,都是建議把use_authentication的值設(shè)置成”0”來取消驗證,這是一個十分糟糕的想法。接下來修改default_user_name=sery ,再后面的修改在下表列出: authorized_for_system_information=nagiosadmin,kuangl authorized_for_configuration_information=nagiosadmin,kuangl authorized_for_system_commands=nagiosadmin,kuangl //多個用戶之間用逗號隔開 authorized_for_all_services=nagiosadmin,kuangl authorized_for_all_hosts=nagiosadmin,kuangl authorized_for_all_service_commands=nagiosadmin,kuangl authorized_for_all_host_commands=nagiosadmin,kuangl |
|
那么上述用戶名打那里來的呢?是執(zhí)行命令
/usr/local/apache/bin/htpasswd–c /usr/local/nagios/etc/htpasswd kuangl所生成的,這個要注意,不能隨便加沒有存在的驗證用戶,為了安全起見,不要添加過多的驗證用戶。
3、修改commands.cfg 配置文件,這個文件的主要功能是用來發(fā)送報警短信和報警郵件。
[root@jw01 objects]# vim commands.cfg |
修改配置文件或者新增以下內(nèi)容:
#host-notify-by-sms //發(fā)送短信報警 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 //發(fā)送短信報警 define command { command_name service-notify-by-sms command_line /usr/local/bin/sms_send "'$HOSTADDRESS$'$HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" $CONTACTPAGER$ } # ‘notify-host-by-email’ command definition define command{ command_name host-notify-by-email command_line /usr/bin/printf “%b” “***** Nagios *****\n\nNotification Typ$NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState:$HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n” | /bin/mai -s “** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **” $CONTTEMAIL$ } # ’service_notification_commands’ command definition define command{ command_name service-notify-by-email command_line /usr/bin/printf “%b” “***** Nagios *****\n\nNotification Typ$NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$” | /bin/mail -s “** $NOTIFICATIONTYPE$ Service ert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **” $CONTACTEMAIL$ } |
4、修改contacts.cfg 配置文件;
define contact{ contact_name kuangl ; Short name of user use generic-contact ; Inherit default values from generic-contact template (defined above) alias Nagios Admin ; Full name of user 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 kuangl@lorient-meadia.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ****** pager 138XXXXXX79 } 在文件最后面修改: define contactgroup{ contactgroup_name sysadmin #定義的屬組 alias Nagios Administrators members kuangl #定義的用戶 } |
多個成員之間用逗號做分界符,如果有更多的聯(lián)系組,就依相同的格式在文件中追加余下的組。
5、新增hosts.cfg 配置文件,下面是我定義的兩個主機(jī)的基本樣式;
define host{ name generichost ; Referenced in other host definitions, used for template recursion/resolution notifications_enabled 1 ; Host notifications are enabled event_handler_enabled 1 ; Host event handler is enabled flap_detection_enabled 1 ; Flap detection is enabled process_perf_data 1 ; Process performance data retain_status_information 1 ; Retain status information across program restarts retain_nonstatus_information 1 ; Retain non-status information across program restarts register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE! check_command check-host-alive max_check_attempts 3 contact_groups sysadmin notification_interval 60 notification_period 24x7 notification_options d,u,r } define host{ name generichost1 ; Referenced in other host definitions, used for template recursion/resolution notifications_enabled 1 ; Host notifications are enabled event_handler_enabled 1 ; Host event handler is enabled flap_detection_enabled 1 ; Flap detection is enabled process_perf_data 1 ; Process performance data retain_status_information 1 ; Retain status information across program restarts retain_nonstatus_information 1 ; Retain non-status information across program restarts register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE! check_command check_tcp!80 max_check_attempts 3 contact_groups sysadmin notification_interval 60 notification_period 24x7 notification_options d,u,r } define host{ use generichost host_name jw01 alias jw01 address 192.168.4.200 } define host{ use generichost host_name jw02 alias jw02 address 192.168.4.244 } |
更多的主機(jī)依此格式逐個追加進(jìn)來。
6、新增services.cfg 配置文件,沒有這個文件,什么監(jiān)控也沒用。
define service{ name genericservice 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 register 0 is_volatile 0 max_check_attempts 2 check_interval 3 retry_interval 2 event_handler_enabled 1 check_period 24x7 contact_groups sysadmin notification_interval 60 notification_period 24x7 notification_options w,u,c,r #notes_url http://webops/nocweb/home/template/bpv%2CBp***View.vm?instancename=BDC_NMS } #################### jw01 ###################### define service{ use genericservice host_name jw01 service_description PING check_command check-host-alive } define service{ use genericservice host_name jw01 service_description tcp_22 check_command check_tcp!22 } define service{ use genericservice host_name jw01 service_description tcp_10000 check_command check_tcp!10000 } define service{ use genericservice host_name jw01 service_description tcp_80 check_command check_tcp!80 } define service{ use genericservice host_name jw01 service_description tcp_8081 check_command check_tcp!8081 } define service{ use genericservice host_name jw01 service_description tcp_971 check_command check_tcp!971 }
define service{ use genericservice host_name jw01 service_description tcp_111 check_command check_tcp!111 } define service{ use genericservice host_name jw01 service_description HTTP check_command check_http } define service{ use genericservice host_name jw01 service_description SSH check_command check_ssh } define service{ use genericservice host_name jw01 service_description Nrpe_users check_command check_nrpe!check_users } define service{ use genericservice host_name jw01 service_description Nrpe_load check_command check_nrpe!check_load } define service{ use genericservice host_name jw01 service_description Nrpe_sda1 check_command check_nrpe!check_/boot } define service{ use genericservice host_name jw01 service_description Nrpe_zomie_procs check_command check_nrpe!check_zombie_procs } |
書寫時要注意的是,check_tcp與要監(jiān)控的服務(wù)端口之間要用”!”做分隔符。如果服務(wù)太多,以應(yīng)該考慮用腳本來生成。
7、修改nagios配置文件
[root@jw01 objects]# vim /usr/local/nagios/etc/nagios.cfg |
在配置文件里面增加services.cfg、hosts.cfg等文件保存退出!
cfg_file=/usr/local/nagios/etc/objects/commands.cfg cfg_file=/usr/local/nagios/etc/objects/contacts.cfg //聯(lián)系人配置文件路徑 cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg //監(jiān)視時段配置文件路徑 cfg_file=/usr/local/nagios/etc/objects/templates.cfg cfg_file=/usr/local/nagios/etc/objects/services.cfg //服務(wù)配置文件路徑 cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //主機(jī)配置文件路徑 # Definitions for monitoring the local (Linux) host cfg_file=/usr/local/nagios/etc/objects/localhost.cfg //本機(jī)配置文件路徑 |
8、檢查文件配置的正確性
[root@jw01 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg Total Warnings: 0 Total Errors: 0 Things look okay - No serious problems were detected during the pre-flight check |
這樣的情況,大功告成;但我卻沒有這么幸運(yùn),修改了好多個地方才成功。
問題總結(jié):
報錯1:Error: Contact group 'admins' specified in service 'Current Load' for host 'localhost' is not defined anywhere!
解決:將templates.cfg配置中的admins組更改為contactgroups.cfg中定義的sysadmin #自己定義的屬組
報錯2:Error: Service notification command 'service-notify-by-email' specified for contact 'kuangl' is not defined anywhere!
Error: Host notification command 'host-notify-by-email' specified for contact 'kuangl' is not defined anywhere!
解決:在這兩條報錯證明在commands.cfg里沒有定義這兩條,在commands.cfg里添加以下內(nèi)容:
# ‘notify-host-by-email’ command definition define command{ command_name host-notify-by-email command_line /usr/bin/printf “%b” “***** Nagios *****\n\nNotification Typ$NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState:$HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n” | /bin/mai -s “** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **” $CONTTEMAIL$ } # ’service_notification_commands’ command definition define command{ command_name service-notify-by-email command_line /usr/bin/printf “%b” “***** Nagios *****\n\nNotification Typ$NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$” | /bin/mail -s “** $NOTIFICATIONTYPE$ Service ert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **” $CONTACTEMAIL$ } |
報錯3:如果提示“You don't have permission to access /nagios/ on this server” 那絕對是你沒有配置好php
解決:
[root@jw01 etc]# yum -y install php |
報錯4:如果提示“Whoops! Error: Could not read object configuration data! ”,這是因為沒有啟動nagios后臺進(jìn)程,執(zhí)行以下命令
[root@jw01 objects]# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg |
9、將nagios 作為守護(hù)進(jìn)程
[root@jw01 objects]# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg [root@jw01 objects]#ps -ef |grep nagios nagios 8640 1 0 15:25 ? 00:00:00 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg root 11397 5291 0 17:25 pts/1 00:00:00 grep nagios |
10、在IE輸入:https://192.168.4.200/nagios/
新聞標(biāo)題:Nagios遠(yuǎn)程監(jiān)控軟件的安裝與配置詳解
URL地址:
http://weahome.cn/article/psijsi.html