Cacti+Nagios完全攻略(二)nagios配置安裝
Nagios簡(jiǎn)介
Nagios是一個(gè)用來監(jiān)視系統(tǒng)和網(wǎng)絡(luò)的開源應(yīng)用軟件,它通常運(yùn)行于一個(gè)主
服務(wù)器上,這個(gè)服務(wù)器運(yùn)行 Liunx 或 Unix 操作系統(tǒng)。Nagios 利用其眾多的插件實(shí)現(xiàn)對(duì)本機(jī)和遠(yuǎn)端服務(wù)的監(jiān)控,當(dāng)被監(jiān)控對(duì)象出現(xiàn)異常,Nagios 就會(huì)及時(shí)給管理人員告警。它是一個(gè)基于TCP/IP協(xié)議的軟件包,包含有nagios主程序和它的各個(gè)插件,配置非常靈活,可以監(jiān)視的項(xiàng)目很多,也可以通過自定義 shell 腳本進(jìn)行監(jiān)控服務(wù),非常適合各類企業(yè)的網(wǎng)絡(luò)應(yīng)用。
Nagios功能非常強(qiáng)大,它可以監(jiān)控服務(wù)和主機(jī),但是他自身并不包括這部分功能的代碼,所有的監(jiān)控、檢測(cè)功能都是有插件來完成的。再說報(bào)警功能,如果監(jiān)控系統(tǒng)發(fā)現(xiàn)問題不能報(bào)警那就沒有意義了,所以報(bào)警也是nagios很重要的功能之一。但是,同樣的,nagios自身也沒有報(bào)警部分的代碼,甚至沒有插件,而是交給用戶或者其他相關(guān)開源項(xiàng)目組去完成。nagios安裝,是指基本平臺(tái),也就是nagios軟件包的安裝。它是監(jiān)控體系的框架,也是所有監(jiān)控的基礎(chǔ)。
打開nagios官方的文檔,會(huì)發(fā)現(xiàn)nagios基本上沒有什么依賴包,只要求系統(tǒng)是linux或者其他nagios支持的系統(tǒng)。不過如果你沒有安裝apache(http服務(wù)),那么你就沒有那么直觀的界面來查看監(jiān)控信息了,所以apache姑且算是一個(gè)前提條件。
Nagios 系統(tǒng)的特點(diǎn)主要有下面幾點(diǎn):
- 監(jiān)控主機(jī)資源和網(wǎng)絡(luò)服務(wù)
-
- 允許用戶通過設(shè)計(jì)實(shí)現(xiàn)簡(jiǎn)單的插件來監(jiān)控自己特定的服務(wù)
-
- 當(dāng)被監(jiān)控對(duì)象出現(xiàn)問題時(shí),會(huì)及時(shí)通知管理人員
-
- 事先定義事件處理程序,當(dāng)對(duì)象出現(xiàn)問題時(shí)自動(dòng)調(diào)用對(duì)應(yīng)的處理程序
-
- 通過 web 頁(yè)面來監(jiān)視對(duì)象狀態(tài),警告提示和日志文件。
需要安裝軟件列表:
php-devel-5.1.6-39.el5_8.rpm
php-json-ext-1.2.1.tar.bz2
開啟系統(tǒng)sendmail服務(wù)
在nagios監(jiān)控服務(wù)器上開啟sendmail服務(wù)的主要作用是讓nagios在檢測(cè)到故障時(shí)可以發(fā)送報(bào)警郵件,目前幾乎所有的linux發(fā)行版本都默認(rèn)自帶了sendmail服務(wù),所以,在安裝系統(tǒng)時(shí)只需開啟sendmail服務(wù)即可,并且不需要在sendmail上做任何配置。
Nagios安裝
1、 創(chuàng)建nagios用戶和組
[root@cacti-qmail nagios-3.2.0]# useradd nagios
[root@cacti-qmail nagios-3.2.0]#mkdir /usr/local/nagios
[root@cacti-qmail nagios-3.2.0]#chown nagios.nagios /usr/local/nagios
2、安裝nagios
[root@cacti-qmail soft]# wget http://ncu.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.2.1/nagios-3.2.0.tar.gz
[root@cacti-qmail soft]# tar -zxvf nagios-3.2.0.tar.gz
[root@cacti-qmail soft]#cd nagios-3.2.0
[root@cacti-qmail nagios-3.2.0]# ./configure --prefix=/usr/local/nagios
[root@cacti-qmail nagios-3.2.0]#make all
輸入make all 得到下面具體信息
==============================================================
*** Compile finished ***
If the main program and CGIs compiled without any errors, you
can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
make install
- This installs the main program, CGIs, and HTML files
使用make install來安裝主程序,CGI和HTML文件
make install-init
- This installs the init script in /etc/rc.d/init.d
使用make install-init在/etc/rc.d/init.d安裝啟動(dòng)腳本
make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file
使用make install-commandmode來配置目錄權(quán)限
make install-config
- This installs *SAMPLE* config files in /usr/local/nagios/etc
You'll have to modify these sample files before you can
use Nagios. Read the HTML documentation for more info
on doing this. Pay particular attention to the docs on
object configuration files, as they determine what/how
things get monitored!
使用make install-commandmode來安裝示例配置文件,安裝的路徑是/usr/local/nagios/etc
make install-webconf
- This installs the Apache config file for the Nagios
web interface
使用make install-webconf來安裝apache的配置文件
=================================================================
make install出現(xiàn)以下錯(cuò)誤是因?yàn)闆]有創(chuàng)建nagios用戶及組并且賦權(quán):
=================================================================
cd ./base && make install
make[1]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'
make install-basic
make[2]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin
/usr/bin/install: invalid user `nagios' 非法用戶nagios
make[2]: *** [install-basic] Error 1
make[2]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'
make[1]: *** [install] Error 2
make[1]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'
make: *** [install] Error 2
=====================================================================
[root@cacti-qmail nagios-3.2.0]#make all
[root@cacti-qmail nagios-3.2.0]#make install && make install-init && make install-commandmode && make install-config && make install-webconf
驗(yàn)證程序是否被正確安裝
切換目錄到安裝路徑(這里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var這五個(gè)目錄,如果存在則可以表明程序被正確的安裝到系統(tǒng)了。后表是五個(gè)目錄功能的簡(jiǎn)要說明:
bin Nagios執(zhí)行程序所在目錄,nagios文件即為主程序
etc Nagios配置文件位置,初始安裝完后,只有幾個(gè)*.cfg-sample文件
sbin Nagios Cgi文件所在目錄,也就是執(zhí)行外部命令所需文件所在的目錄
Share Nagios網(wǎng)頁(yè)文件所在的目錄
Var Nagios日志文件、spid 等文件所在的目錄
修改nagios頁(yè)面文件
[root@cacti-qmail nagios-3.2.1]# more /etc/httpd/conf/nagios.conf
# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
# Last Modified: 11-26-2005
#
# This file contains examples of entries that need
# to be incorporated into your Apache web server
# configuration file. Customize the paths, etc. as
# needed to fit your system.
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
# SSLRequireSSL
Options None
AllowOverride None
Order allow,deny
Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
基本上這個(gè)文件可以直接使用,所以直接復(fù)制過去
[root@cacti-qmail nagios-3.2.1]# cp /etc/httpd/conf/nagios.conf /var/www/html/
生成nagios頁(yè)面訪問賬號(hào)
根據(jù)這個(gè)配置文件中指定的AuthUserFile項(xiàng),它指定的是通過Apache訪問Nagios的合法用戶的帳戶名單庫(kù)文件,需要使用htpasswd命令對(duì)這個(gè)名單庫(kù)進(jìn)行用戶的添加。而默認(rèn)安裝環(huán)境下在/usr/local/nagios/etc/目錄下是沒有 htpasswd.uses這個(gè)文件的,所以需要手動(dòng)添加。請(qǐng)?zhí)貏e注意,用root身份建立的htpasswd.user文件的權(quán)限問題,應(yīng)該在建立該文件完畢后立即更改為nagios用戶nagios組的所有權(quán)
[root@cacti-qmail ~]# touch /usr/local/nagios/etc/htpasswd.users
建立Nagios的網(wǎng)頁(yè)訪問用戶帳戶文件(這里必須要與Nagios網(wǎng)頁(yè)配置文件當(dāng)中AuthUserFile指定的路徑文件名一致)
[root@cacti-qmail ~]# /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users killer
New password:
Re-type new password:
Adding password for user killer
[root@cacti-qmail nagios-3.2.0]# cat /usr/local/nagios/etc/htpasswd.user
killer:HbfEis6w0gKqI
修改nagios主配置文件
[root@cacti-qmail nagios-3.2.0]# vi /usr/local/nagios/etc/nagios.cfg
log_file=/usr/local/nagios/var/nagios.log( 默認(rèn)無nagios.log文件,需要手動(dòng)創(chuàng)建)
command_check_interval=15s
#command_check_interval=-1
設(shè)定命令檢查的時(shí)間間隔,將原來值-1的項(xiàng)注釋掉,啟用原來備用值為15s(15秒)。這項(xiàng)的時(shí)間間隔值請(qǐng)按實(shí)際需求情況設(shè)定。
[root@cacti-qmail nagios-3.2.0]# touch /usr/local/nagios/var/nagios.log
[root@cacti-qmail nagios-3.2.0]# chown nagios.nagios /usr/local/nagios/var/nagios.log
Nagios的CGI配置文件
[root@cacti-qmail nagios-3.2.0]# vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1 (默認(rèn)是啟動(dòng)身份驗(yàn)證)
設(shè)定啟用身份驗(yàn)證
authorized_for_system_information=killer
設(shè)定系統(tǒng)信息的授權(quán)用戶
authorized_for_configuration_information=killer
設(shè)定配置信息的授權(quán)用戶
authorized_for_system_commands=killer
設(shè)定系統(tǒng)命令的授權(quán)用戶
authorized_for_all_services=killer
設(shè)定全部監(jiān)測(cè)服務(wù)的授權(quán)用戶。
authorized_for_all_hosts=killer
設(shè)定全部被監(jiān)測(cè)主機(jī)的授權(quán)用戶
authorized_for_all_service_commands=killer
設(shè)定全部監(jiān)測(cè)服務(wù)命令的授權(quán)用
authorized_for_all_host_commands=killer
設(shè)定全部主機(jī)命令的授權(quán)用戶
apache關(guān)于nagios 的配置
將apache的運(yùn)行用戶加到nagios組里面,從httpd.conf中過濾出當(dāng)前的apache運(yùn)行用戶
grep "User" /etc/httpd/conf/httpd.conf
usermod -G nagios apache
或者 直接把User和Group 都改為nagios
User nagios
Group nagios
vim /etc/httpd/conf/httpd.conf
然后找到
DirectoryIndex index.html index.html.var
修改為
DirectoryIndex index.html index.php
接著增加如下內(nèi)容:
AddType application/x-httpd-php .php
測(cè)試nagios是否成功
設(shè)置nagios為開機(jī)啟動(dòng)
[root@cacti-qmail ~]#chkconfig nagios on
[root@cacti-qmail ~]# service nagios start
測(cè)試nagios是否成功啟動(dòng)
在IE輸入 http://192.168.100.1/nagios/ 跳出以下窗口