首發於http://www.linux-ch.com/post-11.htmlphp
花了幾天的時間,完成了近500臺服務器,多個節點的nagios監控佈署.經過nagios可清晰地看到整個監控網絡的拓撲,主機狀態,服務狀態等狀況.但在這裏我不會貼出真實網絡的任何信息,由於這會產生不少不良影響.但會給出我在實際生產過程當中使用的量產工具(也就是爲了避免多打鍵盤而弄的shell).
系統環境: centos5.5 x86_64
web: apache+php (yum 安裝)
nagios: nagios 3.2.1
nagios-plugins: 1.4.15
沒有涉及遠程主機監控,因此沒弄nrpe模塊.
由於nagios的頁面是要利用cgi生成的,得先讓apache支持cgi才行,只要在httpd.conf後面添加下面的內容,不過你自定了nagios的安裝路徑,那就得以實際爲準了
先讓apache支持cgi
將下面這行前的#號去掉
#AddHandler cgi-script .cgi
添加目錄
scriptalias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin">
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
alias /nagios /usr/local/nagios/share
<Directory "/usr/local/nagios/share">
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>
下面就準備安裝nagios了
先爲nagios添加一個運行賬號,並將apache的運行賬號添加到nagios組裏面,要否則沒法使用頁面管理主機
useradd -r -d /usr/local/nagios -s /sbin/nologin nagios
usermod -G nagios apache
解包編譯安裝
# tar -xvf nagios-3.2.1.tar.gz
# cd nagios-3.2.1
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
# chkconfig --add nagios
到此,nagios已經安裝到/usr/local/nagios/,但沒有plugin,它什麼也幹不了,接下來還得編譯plugin
# tar -xvf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
# ./confingure
# make
# make install
plugins已經所有放到了/usr/local/nagios/libexec,安裝過程已經結束了
默認是能夠直接啓動nagios,它將監控本機,但這並非我要的結果,因此要修改下配置文件
首先修改nagios.cfg,在/usr/local/nagios/etc下面,只對修改的做一個說明
注析掉下面一行,由於它是對本的監控,不必.改爲
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
#主機組配置文件
cfg_file=/usr/local/nagios/etc/objects/hostgroup.cfg
若是要使用一個新的配置文件,在些文件中指定就好了.但它還提供了另外一個功能:cfg_dir.指定這個參數後,程序會在目錄下搜索全部以cfg結尾的文件.如何使用就看你的具體狀況,我在實際中就是新那兩個文件夾,一個是用來存放host信息,別一個是存放server信息,其它的是在nagios.cfg中指定.
主機配置文件夾
cfg_dir=/usr/local/nagios/etc/cfg
修改cgi.cfg,這個應該是賬號受權了
authorized_for_system_information=motu
authorized_for_configuration_information=motu
authorized_for_system_commands=motu
authorized_for_all_services=motu
authorized_for_all_hosts=motu
authorized_for_all_service_commands=motu
authorized_for_all_host_commands=motu
修改etc/objects/contacts.cfg
define contact{
contact_name motu
use generic-contact
alias Nagios Admin
email flyskyst@163.com
}
define contactgroup{
contactgroup_name sagroup
alias Nagios Administrators
members motu
}
生成受權文件
htpasswd -c /usr/local/nagios/etc/htpasswd motu
下面就要批量生成監控主機的信息了.爲此須要一些shell,還有一些配置文件的模板.還有一個很是重要的就是ip列表,並且是要作好分類的,具體是看你的實際狀況,這個是前期一個準備工做,很重要也很乏味.我這裏的是分兩類:服務器的位置和節點.分好類後就能夠利用shell批量生成配置文檔了,這又須要模板,以下:
host.temp
-----------------------------
define host{
host_name
alias
address
check_command check-host-alive
max_check_attempts 5
check_period 24x7
contact_groups sagroup
notification_interval 10
notification_period 24x7
notification_options d,u,r
}
services.temp
define service{
host_name
service_description check_tcp 80
check_command check_tcp!80
max_check_attempts 5
check_interval 5
retry_interval 3
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups sagroup
}
#!/bin/bash
echo "input hostgroup name:"
read hostgroup
for address in $(cat ip.txt)
do
sed -e /host_name/{s/$/$address/} -e /alias/{s/$/$address/} -e /address/{s/$/$address/} host.temp > /usr/local/nagios/etc/hosts/$address.cfg
sed -e /host_name/{s/$/$address/} services.temp > /usr/local/nagios/etc/servers/$address.cfg
done
members=$(sed ':a N;$!b a;s/\n/\,/g' ip.txt)
echo "define hostgroup{" >> /usr/local/nagios/etc/objects/host.cfg
echo " hostgroup_name $hostgroup" >> /usr/local/nagios/etc/objects/hostgroup.cfg
echo " alias $hostgroup" >> /usr/local/nagios/etc/objects/hostgroup.cfg
echo " members $members" >> /usr/local/nagios/etc/objects/hostgroup.cfg
echo " }" >> /usr/local/nagios/etc/objects/hostgroup.cfg
exit 0
把上面三個文件放在同一目錄下,而後將ip列表存入同一目錄下的ip.txt文件裏就能夠了html
接下來將要定義整個網絡拓撲中的父子關係,報警設置等,將在下次更新linux