- 監視網絡服務 (SMTP, POP3, HTTP, NNTP, PING等)
- 監視主機資源 (進程, 磁盤等)
- 簡單的插件設計可以輕松擴展Nagios的監視功能
- 服務等監視的並發處理
- 錯誤通知功能 (通過email, pager, 或其他用戶自定義方法)
- 可指定自定義的事件處理控制器
- 可選的基於瀏覽器的WEB界面以方便系統管理人員查看網絡狀態,各種系統問題,以及日志等等
它也有一個 Windows 下的客戶端: http://www.oschina.net/p/nsclientpp
有一個中文的Nagios網站比較不錯,大家如果想詳細理解,可以看看:
http://nagios-cn.sourceforge.net/nagios-cn/index.html
一、軟件下載
Nagios安裝至少也應該配置下Apache的Httpd服務器,否則沒有界面,只能靠查看日志去找錯誤,那就是完全沒有意外了
Nagios下載地址:http://www.nagios.org/download/
Httpd下載地址:http://mirror.bit.edu.cn/apache/httpd/
必須安裝GCC!!! 其實,我們的apache可以直接用yum來安裝,不必指定路徑,就可以直接配置apache了。
二、安裝軟件過程
1、Nagios安裝
#tar -zxvf nagios-2.6.tar.gz
#mkdir /usr/local/nagios
#./configure –prefix=/usr/local/nagios //設置安裝的目錄,可隨自己喜歡
#make all
附圖片


與別的軟件安裝稍有不同,nagios的安裝要好幾步才能完成
#make install //安裝主要的程序、CGI及HTML文件; #make install-commandmode //給外部命令訪問nagios配置文件的權限; #make install-config //把配置文件的例子復制到nagios的安裝目錄,按照安裝向導的提示
#make install-init //作用是把nagios做成一個運行腳本 在/etc/rc.d/init.d安裝啟動腳本
正確圖解:



若make install出現如下問題

原因:由於系統找不到nagios這個用戶
解決辦法:直接找到“第三、配置用戶” 進行配置即可
驗證程序是否被正確安裝。
切換目錄到安裝路徑,看是否存在 etc、bin、 sbin、 share、 var 這五個目錄,其中也可能存在一個libexec,但里面沒有文檔,如果存在則可以表明程序被正確的安裝到系統了
#cd /usr/local/nagios
#ls
bin etc sbin share libexec var
下表是五個目錄主要功能的簡要說明:
| bin |
Nagios執行程序所在目錄,這個目錄只有一個文件nagios |
| etc |
Nagios配置文件位置,初始安裝完后,只有幾個*.cfg-sample文件 |
| sbin |
Nagios Cgi文件所在目錄,也就是執行外部命令所需文件所在的目錄 |
| Share |
Nagios網頁文件所在的目錄 |
| Var |
Nagios日志文件、spid 等文件所在的目錄 |
2、插件安裝
Nagios的插件在 www.nagios.org 上可以找到,接着我們用wget下載它,除了下載常用的插件外,我們還可以根據實際要求編寫自己的插件
安裝它是很簡單
#tar -zxvf nagios-plugins-1.4.16.tar.gz
# ./configure -prefix=/usr/local/nagios #make #make install
說明:安裝路徑是/usr/local/nagios ,完成后,在目錄/usr/local/nagios生成目錄 libexec 里面有很多文件
圖解



3、安裝web
Web服務不是nagios所必須的,但是如果nagios沒有web,查看監控對象的對象將是非常費事和沒有趣味的事情,只有通過查看nagios的日志來判斷狀態
在unix/linux世界,apache是web服務器的首選對象,其下載網站為 www.apache.org
#tar -zxvf httpd-2.2.22.tar.gz
#cd httpd-2.2.22
#mkdir /usr/local/apache/
#./configure –prefix=/usr/local/apache
#make
#make install
安裝完成后,執行命令檢查一下apache是否正確安裝
#./usr/local/apache/bin/apachectl –t (用yum安裝的,#apachectl -t)
說明:沒有提示出錯,那就是已經正確安裝
假期出現如下錯誤:
Httpd Could not reliably determine the server`s fully qualified domain name
vi /usr/local/apache/httpd/conf/httpd.conf
加入一句 ServerName localhost:80
或將#ServerName www.example.com:80——>ServerName www.example.com:80
#/usr/local/apache/bin/apachectl restart 重啟即可
假若再出現如下情況:
(98)Address already in use: make_sock: could not bind to address [::]:80 (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
方法 :
#netstat -lnp | grep 80
#ps -aux | grep http
#ps -aux | grep apache
將看到的http及apache的進程kill
三、配置
引用(yahoon 的BLOG):
|
聯系人
|
contact
|
出了問題像誰報告?一般當然是系統管理員了
|
|
監控時間段
|
timeperiod
|
7X24小時不間斷還是周一至周五,或是自定義的其他時間段
|
|
被監控主機
|
host
|
所需要監控的服務器,當然可以是監控機自己
|
|
監控命令
|
command
|
nagios發出的哪個指令來執行某個監控,這也是自己定義的
|
|
被監控的服務
|
service
|
例如主機是否存活,80端口是否開,磁盤使用情況或者自定義的服務等
|
1、添加Nagios用戶(為了安全,不讓root登錄)
#useradd nagios (或useradd –g nagios nagios) #passwd nagios #groupadd nagiosG #usermod -G nagiosG nagios #usermod -G nagiosG apache //假若沒有此用戶,在安裝完成httpd后再添加
2、添加目錄權限
#mkdir /usr/local/nagios (沒有就新建) #chown –R nagios.nagios /usr/local/nagios(或chown –R nagios:nagios /usr/local/nagios)
3、測試發送信息
(1)看sendmail是否正常,我們需要使用sendmail來發送故障報警信息,所以這個包必須能夠正常工作
Sendmail分為服務器和客戶端兩部分,有2種發送報警郵件的方式:
A、nagios所在的機器通過sendmail客戶端程序把郵件發送到專門的郵件服務器,再由郵件服務器把消息發送到用戶郵箱。
B、郵件客戶端和服務器端就用nagios所在系統sendmail。
第一種方式用起來非常規范,但更麻煩,例如需要做地址解析、修改郵件服務器的配置;另外還有一個問題-它還依賴別的系統,增加了故障點和復雜度。
第二種方法十分簡單,只需啟動sendmail服務即可,而且它不再依賴於別的系統和服務。
幾乎所有的linux/unix發行版都默認安裝sendmail,但我們必須要將 sendmail 服務運行起來。
#rpm -qa | grep sendmail //檢查系統有沒有安裝sendmail #yum install -y sendmail* //假若沒有就用yum來安裝默認配置也可以了,如有需要也可以參考網上的相關配置
#mail -s "hope" abc@mail.com < /tmp/test.ini
You have new mail in /var/spool/mail/root
另外一種方法:
安裝sendmail組件
首先要確保sendmail相關組件的完整安裝,我們可以使用如下的命令來完成sendmail的安裝:
# yum install -y sendmail* # service sendmail start #setenforce 0 #service iptables stop # echo "aa" | mail abcde@163.com
(2)手機信息功能(待測)
假若公司是有自己的短信通道,直接把發送短信的客戶端程序sms_send拷貝到目錄/usr/local/bin/下。如果沒有短信下發的網關通道,那怎么辦呢?網絡上有很多短信發送的客戶端程序,很有名的就是smsclient,把它下載下來,解包后安裝。不要忘記購買手機modem和手機卡,modem只支持SIM卡而不支持cdma。
安裝完smsclient軟件和硬件modem后,測試一下是否正常。如果沒有modem又怎么辦?辦法還是有的:讓你的手機號可以接受郵件,這需要你去營業廳開通這項功能。短信報警功能是最有用的功能,我們不可能成天盯着監視屏幕,也不可能成天接受電子郵件,但我們的手機卻可以24小時在線,只要被監控對象發生故障,馬上就可以收到故障報警短信。
4、Apache配置
修改apache的配置文件httpd.conf,把apache的運行用戶和運行組改成nagios和nagiosG
#vim /usr/local/apache/conf/httpd.conf
往下把下面的行追加到文件httpd.conf的末尾:
#setting for nagios ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin <Directory "/usr/local/nagios/sbin"> #Cgi文件所在目錄 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 <Directory "/usr/local/nagios/share"> #nagios頁面文件目錄 AuthType Basic Options None AllowOverride None Order allow,deny Allow from all AuthName "nagios Access" AuthUserFile /usr/local/nagios/etc/htpasswd #驗證文件路徑 Require valid-user
生成用戶驗證文件:
# /usr/local/apache/bin/htpasswd –c /usr/local/nagios/etc/htpasswd sery (用yum安裝的,#htpsswd -c ~~~~~~~)
#vim /usr/local/nagios/etc/htpasswd //第一個字段是剛生成的用戶名,第二個是加密后的密碼
配置完成后檢查apache配置文件是否有語法錯誤
#/usr/local/apache/bin/apachctl –t
#/usr/local/apache/bin/apachctl start &
如何提示如下錯誤:
Syntax error on line 424 of /usr/local/apache/conf/httpd.conf:
AuthType not allowed here
原因是,你沒有添加cgi文件及nagios文件相關的目錄
在瀏覽器輸入nagios 的訪問地址(如:http://localhost/nagios 或 http://ip/nagios ),如果正常,將出現登錄驗證窗口等待用戶輸入
5、nagios配置
(1)修改配置文件:
Nagios的主配置文件是nagios.cfg
#vim /usr/local/nagios/etc/nagios.cfg
注釋行
#cfg_file=/usr/local/nagios/etc/localhost.cfg
然后把下面幾行的注釋去掉及修改其他
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 ——> check_external_commands=1 //改為這行的作用是允許執行在web界面下重啟nagios、停止主機/服務檢查等操作
command_check_interval=1 ——> command_check_interval=10s
若發現/usr/local/nagios/etc並沒有文件hosts.cfg等一干文件,稍后手動創建它們
接下來修改修改配置文件/usr/local/nagios/etc/cgi.cfg,它的作用是控制相關cgi腳本 ,必須default_user_name=sery,再后面的修改
use_authentication=1
authorized_for_system_information=nagiosadmin,sery authorized_for_configuration_information=nagiosadmin,sery authorized_for_system_commands=sery #多個用戶之間用逗號隔開 authorized_for_all_services=nagiosadmin,sery authorized_for_all_hosts=nagiosadmin,sery authorized_for_all_service_commands=nagiosadmin,sery authorized_for_all_host_commands=nagiosadmin,sery
(2)修改的配置文件 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$ }
主機和服務的郵件報警通知已經在文件中,不須更改,也可以把短信和郵件報警通知這些配置塊寫到文件commands.cfg中,效果是一樣的
(3)增加新的配置文件
#vim timeperiods.cfg
其內容
define timeperiod{ timeperiod_name 24x7 alias 24 Hours A Day, 7 Days 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 }
(4)再創建的配置文件是
#vim 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-noti fy-by-sms //這個命令讀配置文件miscommands.cfg email sery@163.com pager 13333333333 //手機號,收報警短信 } //不要把這個符號寫掉了 define contact { contact_name sery 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-noti fy-by-sms email sery@sohu.com pager 13312345678 }
//如果有更多聯系人的話,照這個格式在后面追加即可
說明:服務通知選項(service_notification_options)與主機通知選項(host_notification_options)
分為:w-warning , u-unknown,c-critical,r-recovery;d-down,u-unreachable
引用解釋各主要含義:
|
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—從異常情況恢復正常,在這3種情況下通知聯系人
|
|
service_notification_commands notify-by-email
服務出問題通知采用的命令notify-by-email,這個命令是在commands.cfg中定義的,作用是給聯系人發郵件.至於commands.cfg之后將專門介紹
|
|
host_notification_commands host-notify-by-email
同上,主機出問題時采用的也是發郵件的方式通知聯系人
|
|
email yahoon@test.com
很明顯,聯系的人email地址
|
|
pager 133xxxx
聯系人的手機,如果支持短信的通知的話,這個就很有用了.
|
|
alias是聯系人別名,address是地址 意義不大.
|
(5)再創建的配置文件
#vim contactgroups.cfg
內容如下
define contactgroup { contactgroup_name sagroup //不要用空格 alias system administrator group members sa sery //本例有2個成員 }
//多個成員之間用逗號做分界符,如果有更多的聯系組,就依相同的格式在文件中追加余下的組
(6)重要配置文件
#vim hosts.cfg
定義的兩個主機的基本樣式
#define monitor host ################################################################# # Wangjing IDC servers # ################################################################# define host { host_name nagios-server alias nagios server //別名 address 61.x..x.49 contact_groups sagroup //多個聯系組用逗號分隔,數據來源於contactgroups.cfg check_command check-host-alive //監控的命令check-host-alive,這個命令來自commands.cfg,用來監控主機是否存活 max_check_attempts 5 //檢查失敗后重試的次數 notification_interval 10 //提醒的間隔,其值可調,大小什么值合適需自己測定 notification_period 24x7 //提醒的周期, 24x7,同樣來自於我們之前在timeperiods.cfg中定義的 notification_options d,u,r //指定什么情況下提醒,具體含義見之前contacts.cfg部分的介紹 } define host { host_name 24-25 alias server 24-25 address 202.X.24.25 contact_groups sagroup check_command check-host-alive //down機就發報警通知 max_check_attempts 5 notification_interval 10 notification_period 24x7 notification_options d,u,r }
//更多的主機依此格式逐個追加進來
小技巧:如果是連續的ip段,最好自己寫個腳本生成hosts.cfg文件,為了以后維護方便,盡可能在文件中使用易讀的注釋
(7)再一個重量級的配置文件是
#vim 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_period 24x7 max_check_attempts 4 normal_check_interval 3 retry_check_interval 2 contact_groups sagroup //聯系人組,來源:contactgroups.cfg notification_interval 10 notification_period 24x7 notification_options w,u,c,r check_command check-host-alive //檢查主機是否存活 } define service { host_name 24-25 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與要監控的服務端口之間要用”!”做分隔符。如果服務太多,以應該考慮用腳本來生成
(8)主機組配置文件
#vim hostgroups.cfg
(可選的項目,建立在文件hosts之上)
內容如下
define hostgroup { hostgroup_name sa-servers alias sa servers members nagios-server,24-25 //用逗號間隔多個主機 }
所有的文件配置基本完成,現在來檢查所有配置文件的正確性!!!!
#/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
問題總結:
1、最好將上面那些代碼的所有注解都去掉或換行!
2、當出現如下錯誤時:
Error: Service notification command ‘notify-by-email’ specified for contact ‘sery’ is not defined anywhere!
Error: Host notification command ‘host-notify-by-email’ specified for contact ‘sery’ is not defined anywhere!
請在commands.cfg中加入如下內容
# ‘notify-host-by-email’ command definition
define command{
command_name host-notify-by-email
command_line /usr/bin/printf “%b” “***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState:$HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n” | /bin/mail -s “** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **” $CONTACTEMAIL$
}
# ’service_notification_commands’ command definition
define command{
command_name service-notify-by-email
command_line /usr/bin/printf “%b” “***** Nagios *****\n\nNotification Type: $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 Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **” $CONTACTEMAIL$
}
這樣問題就會解決了!是因為command中沒有定義那兩個東東~~~~
附一張結果圖!

最后測試
打開本地瀏覽器輸入輸入 http://localhost/nagios,再輸驗證所需的用戶名和密碼,就可點擊頁面右邊的相關連接來查看各種狀態及管理它們了
試試在里面刪除某些嗠,我們一會就會收到報警短信和報警郵件,然后在把測試所有的服務開啟或把拔下來的網線查上去,片刻后,網頁里的紅色報警表格消失,手機短信或郵件通知故障恢復,如果你的情況也這樣,那么真正大功告成
還有更多的內容可以去Nagios的官網去了解!http://www.nagios.com/
感想:花了很多很多的時間才搞掂,無語了!!!
問題1:
如果提示“You don't have permission to access /nagios/ on this server” 那絕對是你沒有配置好php
#yum install -y php
問題2:
如果提示“Whoops! Error: Could not read object configuration data! ”,這是因為沒有啟動nagios后台進程,執行以下命令
#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
最好上傳幾張成功的圖片!哈哈~~~~


