您好,登錄后才能下訂單哦!
一、環(huán)境配置
1、系統(tǒng):CentOS release 5.6 IP:192.168.4.200 主機名:JW01
二、軟件下載
Nagios安裝至少也應該配置下Apache的Httpd(已裝)服務器,否則沒有界面,只能靠查看日志去找錯誤,那就是完全沒有意外了。
Nagios下載地址:http://www.nagios.org/download/
nagios-3.4.4.tar.gz nagios-plugins-1.4.16.tar.gz
必須安裝GCC?。?! 其實,我們的apache可以直接用yum來安裝,不必指定路徑,就可以直接配置apache了。
三、安裝前的準備工作
1、解決安裝Nagios的依賴關系:
Nagios基本組件的運行依賴于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、所需要安裝組件:
服務端所用的安裝包為 nagios nagios-plugins ndoutils(非必須)
linux客戶端 nrpe
windows客戶端 NSClient++
3、進行selinux設置,或者關閉selinux
[root@jw01 ~]# vim /etc/selinux/config |
[root@jw01 ~]# groupadd nagcmd |
四、編譯安裝nagios
1、安裝nagios
[root@jw01 software]# tar -zxvf nagios-3.4.4.tar.gz #解壓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:19 var |
存在 etc、bin、 sbin、 share、 var這五個目錄,如果存在則可以表明程序被正確的安裝到系統(tǒng)了。后表是五個目錄功能的簡要說明:
bin |
|
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 |
說明一下的是在配置過程指定的安裝路徑是/usr/local/nagios,而不是/usr/local/nagios-plus,安裝完成后,將在目錄/usr/local/nagios生成目錄libexec(里面有很多文件),這正是nagios所需要的。
六、安裝Apache (前段時間APACHE已經(jīng)安裝了)
Web服務不是nagios所必須的,但是如果nagios沒有web,查看監(jiān)控對象的對象將是非常費事和沒有趣味的事情(只有通過查看nagios的日志來判斷狀態(tài))。我不愿干特無聊的事,所以就花少許時間把web安裝一下。
在unix/linux世界,apache是web服務器的首選對象,其下載網(wǎng)站為www.apache.org 。建議下載源碼。因為我們不需要很復雜的web功能,因此簡單的執(zhí)行一下幾個步驟就可以正確的把apache安裝到系統(tǒng):
1、安裝Apache;
[root@jw01 software]# tar -zxvf httpd-2.2.0.tar.gz |
2、Apache 配置
第一步是修改apache的配置文件httpd.conf,這里的文件路徑是/usr/local/apache/conf/httpd.conf 。把apache的運行用戶[1]和運行組改成nagios,往下把下面的行追加到文件httpd.conf的末尾:
我自己是寫在代理的目錄下,效果都是一樣的。
[root@jw01 ~]# /usr/local/apache/conf/httpd.conf [root@jw01 conf.d]# pwd |
上述文本塊的作用是對nagios的目錄進行用戶驗證,只有合法的授權(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 & 把apache啟動,然后從另外的機器的瀏覽器輸入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]# |
1、修改nagios的主配置文件nagios.cfg
[root@jw01 etc]# vim nagios.cfg |
2、修改cgi.cfg 配置文件
[root@jw01 etc]# vim cgi.cfg |
3、修改commands.cfg 配置文件,這個文件的主要功能是用來發(fā)送報警短信和報警郵件。
[root@jw01 objects]# vim commands.cfg |
修改配置文件或者新增以下內(nèi)容:
#host-notify-by-sms //發(fā)送短信報警 #service notify by sms //發(fā)送短信報警
# ‘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{ 在文件最后面修改: define contactgroup{ |
多個成員之間用逗號做分界符,如果有更多的聯(lián)系組,就依相同的格式在文件中追加余下的組。
5、新增hosts.cfg 配置文件,下面是我定義的兩個主機的基本樣式;
define host{ define host{ define host{ |
更多的主機依此格式逐個追加進來。
6、新增services.cfg 配置文件,沒有這個文件,什么監(jiān)控也沒用。
define service{ #################### jw01 ###################### define service{ define service{ define service{
define service{ define service{ define service{ define service{ define service{ define service{ define service{ |
書寫時要注意的是,check_tcp與要監(jiān)控的服務端口之間要用”!”做分隔符。如果服務太多,以應該考慮用腳本來生成。
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 # Definitions for monitoring the local (Linux) host
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 //服務配置文件路徑
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg //主機配置文件路徑
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg //本機配置文件路徑
8、檢查文件配置的正確性
[root@jw01 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg Total Warnings: 0 Things look okay - No serious problems were detected during the pre-flight check |
這樣的情況,大功告成;但我卻沒有這么幸運,修改了好多個地方才成功。
問題總結(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后臺進程,執(zhí)行以下命令
[root@jw01 objects]# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
9、將nagios 作為守護進程
[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/
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。