Nagios安裝
Nagios服務端安裝
5.1 基礎支援套件:gcc glibc glibc-common gd gd-devel xinetd openssl-devel
# rpm -q gcc glibc glibc-common gd gd-devel xinetd openssl-devel
如果系統中沒有這些套件,使用yum 安裝
# yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel
5.2 建立nagios使用者和使用者組
# useradd -s /sbin/nologin nagios # mkdir /usr/local/nagios # chown -R nagios.nagios /usr/local/nagios
檢視nagios 目錄的許可權
# ll -d /usr/local/nagios/
5.3 編譯安裝Nagios
wget http://downloads.sourceforge.net/project/nagios/nagios-4.x/nagios-4.2.0/nagios-4.2.0.tar.gztar zxvf nagios-4.2.0.tar.gz
cd nagios-4.2.0
./configure --prefix=/usr/local/nagios
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
# chkconfig --add nagios
# chkconfig --level 35 nagios on
# chkconfig --list nagios
5.4 驗證程式是否被正確安裝
切換目錄到安裝路徑(這裡是/usr/local/nagios),看是否存在etc、bin、sbin、share、var 這五個目錄,如果存在則可以表明程式被正確的安裝到系統了。Nagios 各個目錄用途說明如下:
bin | Nagios 可執行程式所在目錄 |
etc | Nagios 配置檔案所在目錄 |
sbin | Nagios CGI 檔案所在目錄,也就是執行外部命令所需檔案所在的目錄 |
share | Nagios網頁檔案所在的目錄 |
libexec | Nagios 外部外掛所在目錄 |
var | Nagios 日誌檔案、lock 等檔案所在的目錄 |
var/archives | Nagios 日誌自動歸檔目錄 |
var/rw | 用來存放外部命令檔案的目錄 |
5.5 安裝Nagios 外掛
wget http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gztar zxvf nagios-plugins-2.1.2.tar.gz
cd nagios-plugins-2.1.2
./configure --prefix=/usr/local/nagios
make && make install
5.6 安裝與配置Apache和Php
Apache 和Php 不是安裝nagios 所必須的,但是nagios提供了web監控介面,通過web監控介面可以清晰的看到被監控主機、資源的執行狀態,因此,安裝一個web服務是很必要的。
需要注意的是,nagios在nagios3.1.x版本以後,配置web監控介面時需要php的支援。這裡我們下載的nagios版本為nagios-3.4.3,因此在編譯安裝完成apache後,還需要編譯php模組,這裡選取的php版本為php5.4.10。
a. 安裝Apache
wget http://archive.apache.org/dist/httpd/httpd-2.2.23.tar.gztar zxvf httpd-2.2.23.tar.gz
cd httpd-2.2.23
./configure --prefix=/usr/local/apache2 --enable-rewrite
make && make install
若出現錯誤:
則在編譯時入加 --with-included-apr 即可解決。
b. 安裝Php
wget http://cn2.php.net/distributions/php-5.4.26.tar.gzyum install libxml2 –y
yum install libxml2-devel –y
tar -xvf php-5.4.26.tar.gz
cd php-5.4.26
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-gd --with-zlib --enable-sockets
有報錯資訊:
Configuring extensions
checking size of long... (cached) 8
checking size of int... (cached) 4
checking for int32_t... yes
checking for uint32_t... yes
checking for sys/types.h... (cached) yes
checking for inttypes.h... (cached) yes
checking for stdint.h... (cached) yes
checking for string.h... (cached) yes
checking for stdlib.h... (cached) yes
checking for strtoll... yes
checking for atoll... yes
checking for strftime... (cached) yes
checking which regex library to use... php
checking whether to enable LIBXML support... yes
checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.
需要重新安裝lib包
yum install libxml2 –y
yum install libxml2-devel –y
如果有問題:
yum install libpng-devel
yum
install libpng
之後繼續make,成功了
# make && make install
c. 配置apache
找到apache 的配置檔案/usr/local/apache2/conf/httpd.conf
找到:
User daemon
Group daemon
修改為
User nagios
Group nagios
然後找到
<IfModule dir_module> DirectoryIndex index.html </IfModule>
修改為
<IfModule dir_module> DirectoryIndex index.html index.php </IfModule>
接著增加如下內容:
AddType application/x-httpd-php .php
最後修改新增 ServerName 127.0.0.1:80
為了安全起見,一般情況下要讓nagios 的web 監控頁面必須經過授權才能訪問,這需要增加驗證配置,即在httpd.conf 檔案最後新增如下資訊:
#setting for nagiosScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/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
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory "/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
</Directory>
d. 建立apache目錄驗證檔案
在上面的配置中,指定了目錄驗證檔案htpasswd,下面要建立這個檔案:
# /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd david
這樣就在/usr/local/nagios/etc 目錄下建立了一個htpasswd 驗證檔案,當通過http://192.168.1.108/nagios/ 訪問時就需要輸入使用者名稱和密碼了。
e. 檢視認證檔案的內容
# cat /usr/local/nagios/etc/htpasswd
f. 啟動apache 服務
# /usr/local/apache2/bin/apachectl start
到這裡nagios 的安裝也就基本完成了,你可以通過web來訪問了。
配置Nagios
Nagios 主要用於監控一臺或者多臺本地主機及遠端的各種資訊,包括本機資源及對外的服務等。預設的Nagios 配置沒有任何監控內容,僅是一些模板檔案。若要讓Nagios 提供服務,就必須修改配置檔案,增加要監控的主機和服務,下面將詳細介紹。
6.1 預設配置檔案介紹
Nagios 安裝完畢後,預設的配置檔案在/usr/local/nagios/etc目錄下。
每個檔案或目錄含義如下表所示:
檔名或目錄名 | 用途 |
cgi.cfg | 控制CGI訪問的配置檔案 |
nagios.cfg | Nagios 主配置檔案 |
resource.cfg | 變數定義檔案,又稱為資原始檔,在些檔案中定義變數,以便由其他配置檔案引用,如$USER1$ |
objects | objects 是一個目錄,在此目錄下有很多配置檔案模板,用於定義Nagios 物件 |
objects/commands.cfg | 命令定義配置檔案,其中定義的命令可以被其他配置檔案引用 |
objects/contacts.cfg | 定義聯絡人和聯絡人組的配置檔案 |
objects/localhost.cfg | 定義監控本地主機的配置檔案 |
objects/printer.cfg | 定義監控印表機的一個配置檔案模板,預設沒有啟用此檔案 |
objects/switch.cfg | 定義監控路由器的一個配置檔案模板,預設沒有啟用此檔案 |
objects/templates.cfg | 定義主機和服務的一個模板配置檔案,可以在其他配置檔案中引用 |
objects/timeperiods.cfg | 定義Nagios 監控時間段的配置檔案 |
objects/windows.cfg | 監控Windows 主機的一個配置檔案模板,預設沒有啟用此檔案 |
6.2 配置檔案之間的關係
在nagios的配置過程中涉及到的幾個定義有:主機、主機組,服務、服務組,聯絡人、聯絡人組,監控時間,監控命令等,從這些定義可以看出,nagios各個配置檔案之間是互為關聯,彼此引用的。
成功配置出一臺nagios監控系統,必須要弄清楚每個配置檔案之間依賴與被依賴的關係,最重要的有四點:
第一:定義監控哪些主機、主機組、服務和服務組;
第二:定義這個監控要用什麼命令實現;
第三:定義監控的時間段;
第四:定義主機或服務出現問題時要通知的聯絡人和聯絡人組。
6.3 配置Nagios
為了能更清楚的說明問題,同時也為了維護方便,建議將nagios各個定義物件建立獨立的配置檔案:
- 建立hosts.cfg檔案來定義主機和主機組
- 建立services.cfg檔案來定義服務
- 用預設的contacts.cfg檔案來定義聯絡人和聯絡人組
- 用預設的commands.cfg檔案來定義命令
- 用預設的timeperiods.cfg來定義監控時間段
- 用預設的templates.cfg檔案作為資源引用檔案
define command{
command_name check_nt
command_line $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s 123456 -v $ARG1$ $ARG2$
}
cgi.cfg檔案
此檔案用來控制相關cgi指令碼,如果想在nagios的web監控介面執行cgi指令碼,例如重啟nagios程序、關閉nagios通知、停止nagios主機檢測等,這時就需要配置cgi.cfg檔案了。
由於nagios的web監控介面驗證使用者為david,所以只需在cgi.cfg檔案中新增此使用者的執行許可權就可以了,需要修改的配置資訊如下:
default_user_name=david
authorized_for_system_information=nagiosadmin,david
authorized_for_configuration_information=nagiosadmin,david
authorized_for_system_commands=david
authorized_for_all_services=nagiosadmin,david
authorized_for_all_hosts=nagiosadmin,david
authorized_for_all_service_commands=nagiosadmin,david
authorized_for_all_host_commands=nagiosadmin,david
nagios.cfg檔案
nagios.cfg預設的路徑為/usr/local/nagios/etc/nagios.cfg,是nagios的核心配置檔案,所有的物件配置檔案都必須在這個檔案中進行定義才能發揮其作用,這裡只需將物件配置檔案在Nagios.cfg檔案中進行引用即可。
6.4 驗證Nagios 配置檔案的正確性
Nagios 在驗證配置檔案方面做的非常到位,只需通過一個命令即可完成:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfgNagios提供的這個驗證功能非常有用,在錯誤資訊中通常會打印出錯誤的配置檔案以及檔案中的哪一行,這使得nagios的配置變得非常容易,報警資訊通常是可以忽略的,因為一般那些只是建議性的。
看到上面這些資訊就說明沒問題了,然後啟動Nagios 服務。
七、Nagios的啟動與停止7.1 啟動Nagios
a. 通過初始化指令碼啟動nagios
# /etc/init.d/nagios start
or
# service nagios start
b. 手工方式啟動nagios
通過nagios命令的“-d”引數來啟動nagios守護程序:# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
7.2 重啟Nagios
當修改了配置檔案讓其生效時,需要重啟/過載Nagios服務。
a. 通過初始化指令碼來重啟nagios
# /etc/init.d/nagios reload
or
# /etc/init.d/nagios restart
or
# service nagios restart
b. 通過web監控頁重啟nagios
可以通過web監控頁的 "Process Info" -> "Restart the Nagios process"來重啟nagios
c. 手工方式平滑重啟
# kill -HUP <nagios_pid>
7.3 停止Nagios
a. 通過初始化指令碼關閉nagios服務
# /etc/init.d/nagios stop
or
# service nagios stop
b. 通過web監控頁停止nagios
可以通過web監控頁的 "Process Info" -> "Shutdown the Nagios process"來停止nagios
c. 手工方式停止Nagios
# kill <nagios_pid>
八、檢視初步配置情況
8.2 點選左面的Current Status -> Hosts 可以看到所定義的三臺主機已經全部UP了。
8.3 點選Current Status -> Services 檢視服務監控情況。
看到Nagios-Linux和Nagios-Server的服務狀態已經OK了,但是Nagios-Windows的服務狀態為CRITICAL,Status Information 提示Connection refused。因為Nagios-Windows上還未安裝外掛,內部服務還無法檢視,所以出現這種情況。將在下面具體講解。