Nagios的功能以及它所使用的場景在此不必多說.網上很多這方面資料.本文主要介紹Nagios-SNMP-Plugins 來實現對企業中的服務器的硬件的監控,如:磁盤,CPU ,內存,網卡等在出現異常或恢復時通知管理員並使用PNP插件結合rrdtool實現繪圖
一.實驗環境
1.監控機:CentOS5.4_x32(192.168.1.253)
2.兩臺被監控機器:Windows (192.168.1.206),Linux(192.168.1.124)
3.所需軟件:
Nagios3.3.1
Nagios-plugins-1.4.16
Nagios-snmp-plugins.1.1.1
Rrdtool
Pnp4nagios-0.6.18
sendEmail
二.部署過程
1.確保系統開發組件已安裝
2.使用yum安裝LAMP環境
yum -y install php php-cli php-common php-pdo php-gd php-mysql php-devel php-mbstrin
yum -y install httpd httpd-devel system-config-httpd
yum -y install mysql-devel mysql-server mysql
yum -y install gd gd-devel
注:以上組件件中有PHP擴展,關於gd gd-devel必需安裝不然map會出現異常.ndo2db會將的nagios信息轉入MySQL數據庫
3.啓動Apache和MySQL
chkconfig --add httpd || chkconfig httpd on//添加apache服務開機啓動
chkconfig --add mysqld || chkconfig mysqld on //添加MySQL服務 開機啓動
service httpd restart 啓動Apache
service mysqld restart 啓動MySQL
4.建立php測試文件,並驗證
vim /var/www/index.php
<?
phpinfo();
?>
三.依次安裝所需軟件
1.安裝Nagios
groupadd nagcmd
useradd -G nagcmd nagios
passwd nagios //手動設置密碼
usermod -a -G nagcmd apache
cd nagios-3.3.1
./configure --with-command-group=nagcmd --enable-event-broker //--enable-enevnt-broker參數用於實現NDO的功能
make all
make install
make install-init
make install-commandmode
make install-config
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin //指定Nagios的web接口登錄密碼
make install-webconf
2.安裝Nagios-plugins
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
chkconfig --add nagios || chkconfig nagios on //配置Nagios開機啓動
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg //檢測無異常即可以啓動服務
service nagios restart //重啓apache服務
service httpd restart //啓動nagios服務
3.安裝Nagios-snmp-plugins
Crypt-DES-2.07.tar.gz
Digest-HMAC-1.03.tar.gz
Digest-MD5-2.53.tar.gz
Digest-SHA1-2.13.tar.gz
Net-SNMP-v6.0.1.tar.gz
分別解壓進入各目錄執行以下操作
perl Makefile
make test
make install
tar -zxvf nagios-snmp-plugins.tar.gz
cd nagios_plugins
./install //依次回車即可,完成後會在/usr/local/nagios/libexec目錄中生成相應的健康腳本
重啓apache 和nagios
瀏覽器中輸入:http://192.168.1.253/nagios
用戶名:nagiosadmin
密碼:上面設置的密碼
三.配置監控
1,配置/usr/local/.nagios/etc/object/command.cfg文件,在文件結尾處添加以下內容.
###############################SNMP
define command {
command_name check_tcpport
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$ -w $ARG2$ -c $ARG3$
}
define command {
command_name check_snmp_disk
command_line $USER1$/check_snmp_storage.pl -H $HOSTADDRESS$ $USER7$ -p $ARG1$ -m $ARG2$ -w $ARG3$ -c $ARG4$
}
define command {
command_name check_snmp_mem
command_line $USER1$/check_snmp_storage.pl -H $HOSTADDRESS$ $USER7$ -p $ARG1$ -m $ARG2$ -w $ARG3$ -c $ARG4$ -f
}
define command {
command_name check_snmp_cpuload
command_line $USER1$/check_snmp_load.pl -H $HOSTADDRESS$ $USER7$ -p $ARG1$ -w $ARG2$ -c $ARG3$ -f
}
(-f 不可缺少否則無法出圖)
配置監控之前先檢測被檢控主機狀態,被控機上開啓SNMP服務並配置好團體名及所接收來自哪些區域的請求.
windows
linux
測試成功就可以進行配置了.
2.定義監控主機
在nagios.cfg中開啓被監控主機的目錄
以下爲192.168.1.206這臺機器的監控配置
define host{
use windows-server
host_name web-192.168.1.206
alias My Windows Server
address 192.168.1.206
hostgroups windows-server
}
##################################### //這裏是分組,注,分組中需要指定一次
define hostgroup{
hostgroup_name windows-server
alias windows-server
}
define hostgroup{
hostgroup_name linux-server
alias linux-server
}
#####################################
define service{
use generic-service
host_name web-192.168.1.206
service_description PING
check_command check_ping!100.0,20%!500.0,60%!5
}
###########Memory
define service{
use generic-service
host_name web-192.168.1.206
service_description Memor
check_command check_snmp_mem!161!"Physical Memory"!10!20
}
檢測語法是否有錯
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
無報錯即可重啓nagios 服務
service nagios restart
刷新監控頁面
三.配置監控對像使用量繪圖功能
1.安裝cgilib
tar -zxvf cgilib-0.6.tar.gz
cd cgilib-0.6
make
cp aux.h /usr/include/
cp cgi.h /usr/include/
cp libcgi.a /usr/lib/
2.安裝rrdtool
yum -y install ruby
yum -y install pango pango-devel
rpm -ivh rrdtool *
3.安裝PNP
cd pnp4nagios-0.6.18
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-rrdtool=/usr/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata
make all
make install
make install-config
4.配置apache
vi /etc/httpd/conf/httpd.conf
末尾添加如下信息
Alias /pnp4nagios "/usr/local/pnp4nagios/share"
<Directory "/usr/local/pnp4nagios/share">
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
<IfModule mod_rewrite.c>
RewriteEngine On
Options FollowSymLinks
RewriteBase /pnp4nagios/
RewriteRule ^(application|modules|system) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
</Directory>
5.配置template.cfg
define host {
name host-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
register 0
}
define service {
name srv-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
register 0
}
6.配置commands.cfg
先註釋原有的再添加
define command {
command_name process-service-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl
}
define command {
command_name process-host-perfdata
command_line /usr/bin/perl /usr/local/pnp4nagios/libexec/process_perfdata.pl -d HOSTPERFDATA
}
7.配置nagios.cfg
process_performance_data=1
enable_environment_macros=1
host_perfdata_command=process-host-perfdata
service_perfdata_command=process-service-perfdata
8.更新PHP至 5.2(CentOS 6.2可yum安裝)
yum install php-gd
rpm --import http://repo.webtatic.com/yum/RPM-GPG-KEY-webtatic-andy
wget -P /etc/yum.repos.d/ http://repo.webtatic.com/yum/webtatic.repo
yum --enablerepo=webtatic --exclude=php*5.3* list php
yum --enablerepo=webtatic --exclude=php*5.3* update -y php
驗證:
rpm -qa |grep php
service httpd restart
測試PNP .刪除或移動/usr/local/pnp4nagios/share/install.php配
9.爲主機或服務配置繪圖
重新啓動服務.並查看監控頁面.需要等一會才能出圖
service httpd restart
service nagios restart
三.配置監控LINUX主機也一樣,調用的模版文件改成linux-server.分組使用linux-server,前面定義過分組這裏不用定義
cd /usr/local/nagios/etc/object/linux
vim 192.168.1.124.cfg
define host{
use linux-server,host-pnp
host_name web-192.168.1.124
alias My Linux Server
address 192.168.1.124
hostgroups linux-server
}
#####################################
define service{
use generic-service,srv-pnp
host_name web-192.168.1.124
service_description PING
check_command check_ping!100.0,20%!500.0,60%!5
}
###########Memory
define service{
use generic-service,srv-pnp
host_name web-192.168.1.124
service_description Mem
check_command check_snmp_mem!161!"Real Memory"!90!99
}
###########disk/
define service{
use generic-service,srv-pnp
host_name web-192.168.1.124
service_description disk-/
check_command check_snmp_stro!161!/!80!90
}
#####CPU
define service{
use generic-service,srv-pnp
host_name web-192.168.1.124
service_description cpuload
check_command check_snmp_cpuload!161!80!90
}
###########Traffic
define service{
use generic-service,srv-pnp
host_name web-192.168.1.124
service_description eth0
check_command check_snmp_traffic!2!200,100!300,200
}
#####Process
define service{
use generic-service,srv-pnp
host_name web-192.168.1.124
service_description httpd
check_command check_snmp_proc!httpd!3!20!100,200
}
define service{
use generic-service,srv-pnp
host_name web-192.168.1.124
service_description port80
check_command check_tcpport!80!0.0023!0.0067
}
五:配置告警功能
1.定義宏變量
2.sendEmail的用法
下載 wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar -zxvf sendEmail
cd sendEmail
cp -p sendEmail /usr/local/bin
檢測發信
/usr/local/bin/sendEmail –f [email protected] –t [email protected] –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda –m happy
解釋:-f 表示發送者的郵箱
-t 表示接收者的郵箱
-s 表示SMTP服務器的域名或者ip
-u 表示郵件的主題-xu
表示SMTP驗證的用戶名-xp
表示SMTP驗證的密碼(注意,這個密碼貌似有限制,例如我用d!5neyland就不能被正確識別)
-m 表示郵件的內容
注:以上內容來源於網絡
添加告警配置
# 'notify-service-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTA
TE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nnDate/Time: $LONGDATETIME$n" | /usr/local/bin/sendEmail -s $USER8$ -xu $USER9$ -xp $US
ER10$ -t $CONTACTEMAIL$ -f $USER9$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" -m "***
** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$
nnDate/Time: $LONGDATETIME$n"
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SERVICEDESC$nHost: $H
OSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$nnAdditional Info:nn$SERVICEOUTPUT$" | /usr/local/b
in/sendEmail -s $USER8$ -xu $USER9$ -xp $USER10$ -t $CONTACTEMAIL$ -f $USER9$ -l /var/log/sendEmail -u "** $NOTIFICATIONTYPE$ Servic
e Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -m "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SE
RVICEDESC$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$nnAdditional Info:nn$SERVICEOUTP
UT$"
}
定義聯繫人,可以定義多個用逗號隔開
關掉系統自帶的sendmai
service sendmail stop
chkconfig sendmail off
重啓服務.觀測頁面上的notification選項和上面定義的收信郵箱
本次實驗先做到這裏,稍後附上ndo2db+nagvis的配置..