在CentOS 5.2上安装Nagios 3.06+Centreon 2.0.1
一、什么是Nagios
Nagios是一款用于系统和网络监控的应用程序。它可以在你设定的条件下对主机和服务进行监控,在状态变差和变好的时候给出告警信息。
Nagios更进一步的特征包括:
- 监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);
- 监控主机资源(处理器负荷、磁盘利用率等);
- 简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
- 并行服务检查机制;
- 具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;
- 当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);
- 具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位;
- 自动的日志回滚;
- 可以支持并实现对主机的冗余监控;
- 可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等
Centreon是一款开源的软件,主要用于与nagios搭配,通过页面管理nagios,通过第三方组件实现对网络,操作系统,应用程序的监控。
三、安装前的准备
A、CentOS的安装是采用Server-GUI模式,安装好CentOS后需要添加一下rpmforge的yum源,通过如下命令实现:
# rpm -Uvh http://packages.sw.be/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.i386.rpm
B、接着执行如下命令对系统进行更新:
# yum update
C、系统更新完成电脑重启后,安装下列软件包:
安装旧版软件包
# yum groupinstall "Legacy Software Development" "Legacy Software Support"
安装 Apache2
# yum install httpd httpd-manual httpd-devel
安装 GD modules
# yum install gd gd-devel perl-GD
安装 MySQL
# yum install mysql-server mysql-devel
安装 PHP
# yum install php php-mysql php-gd php-pear
安装 DBI modules
# yum install perl-DBI
安装 SNMP
# yum install perl-Digest-SHA1 perl-Digest-HMAC net-snmp-utils perl-Socket6 perl-IO-Socket-INET6 net-snmp-devel php-snmp dmidecode net-snmp-perl perl-Crypt-DES
安装 RRDtool
访问http://www.express.org/~wrl/rrdtool/,然后安装如下软件包
# rpm -ivh rrdtool- 1.2.30 -1.el5.wrl.i386.rpm
# rpm -ivh rrdtool-devel- 1.2.30 -1.el5.wrl.i386.rpm
# rpm -ivh rrdtool-doc- 1.2.30 -1.el5.wrl.i386.rpm
# rpm -ivh rrdtool-perl- 1.2.30 -1.el5.wrl.i386.rpm
# rpm -ivh rrdtool-php- 1.2.30 -1.el5.wrl.i386.rpm
# rpm -ivh rrdtool-Python- 1.2.30 -1.el5.wrl.i386.rpm
# rpm -ivh rrdtool-tcl- 1.2.30 -1.el5.wrl.i386.rpm
安装其他软件包
# yum install fping perl-Config-IniFiles graphviz gcc-c++ glib2-devel
D、设置Apache、MySQL和SNMP在系统启动时自启动
# chkconfig --level 345 httpd on
# chkconfig --level 345 mysqld on
# chkconfig --level 345 snmpd on
E、配置snmp
修改/etc/snmp/snmpd.conf文件,修改以下几行为红字所示,其中的x.x.x.x换成运行nagios的服务器的IP地址:
com2sec notConfigUser x.x.x.x public
access notConfigGroup "" any noauth exact all none none
view all included .1 80
F、配置php-PEAR
# pear channel-update pear.php.net
当出现一下几行,即表示成功:
Retrieving channel.xml from remote server
Update of Channel "pear.php.net" succeeded
四、安装nagios软件包
A、安装 Nagios
# yum install nagios nagios-devel
此处所安装的nagios是3.0.6版本
B、安装Nagios-plugins
# yum install nagios-plugins
C、配置Nagios登录用户
# htpasswd -c /etc/nagios/htpasswd.users nagiosadmin
New password: nagiosadmin
Re-type new password: nagiosadmin
Adding password for user nagiosadmin
D、登录web界面,检查Nagios是否成功运行
# service httpd start
# service nagios start
访问http://x.x.x.x//nagios(x.x.x.x为服务器ip地址)
出现以下画面,表示nagios正常运行
五、安装 Ndoutils
A、下载Ndoutils
# wget http://nchc.dl.sourceforge.net/sourceforge/nagios/ndoutils-1.4b7.tar.gz
B、安装Ndoutils
# tar -zxvf ndoutils-1.4b7.tar.gz
# cd ndoutils-1.4b7
# ./configure --prefix=/usr/lib/nagios --enable-mysql --disable-pgsql --with-ndo2db-user=nagios--with-ndo2db-group=nagios
# make clean
# make
# cp config/ndomod.cfg /etc/nagios
# cp config/ndo2db.cfg /etc/nagios
# mkdir /usr/lib/nagios/bin
# cp src/ndomod-3x.o /usr/lib/nagios/bin/ndomod.o
# cp src/ndo2db-3x /usr/lib/nagios/bin/ndo2db
# cp src/log2ndo /usr/lib/nagios/bin/log2ndo
# cp src/sockdebug /usr/lib/nagios/bin/sockdebug
# cp src/file2sock /usr/lib/nagios/bin/file2sock
# vi /etc/nagios/nagios.cfg (更改如下所示)
broker_module=/usr/lib/nagios/bin/ndomod.o config_file=/etc/nagios/ndomod.cfg
event_broker_options=-1
# vi /etc/nagios/ndomod.cfg (更改如下所示)
output_type=tcpsocket
#output_type=unixsocket
output=127.0.0.1
#output=/usr/local/nagios/var/ndo.sock
buffer_file=/var/nagios/ndomod.tmp
# vi /etc/nagios/ndo2db.cfg (更改如下所示)
#socket_type=unix
socket_type=tcp
#socket_name=/usr/local/nagios/var/ndo.sock
db_servertype=mysql
db_name=ndo
db_user=ndouser
db_pass=ndopassword
debug_level=-1
debug_verbosity=2
debug_file=/var/log/nagios/ndo2db-debug.log
# touch /var/log/nagios/ndo2db-debug.log
# chown nagios.nagios /var/log/nagios/ndo2db-debug.log
C、设置NDO DB
# wget http://download.centreon.com/centreon/centreon-2.0.1.tar.gz
# tar -zxvf centreon-2.0.1.tar.gz
# cd centreon-2.0.1
# service mysqld start
# mysql -u root -p (密码为空,可以通过mysqladmin -u root -p 'password'来设置一个密码)
mysql> CREATE DATABASE `ndo` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql>exit;
# mysql -u root -p ndo < /$centreon-2.0.1-path$/www/install/createNDODB.sql
# mysql -u root -p
mysql> GRANT SELECT , INSERT , UPDATE , DELETE ON `ndo` . * TO 'ndouser'@'localhost' IDENTIFIED BY 'ndopassword';
mysql>flush privileges;
mysql>exit;
D、设置NDO2DB服务
# vi /etc/init.d/ndo2db
#!/bin/sh
#
#
# chkconfig: 345 99 01
# description: Nagios to mysql
#
# Author : Ga?tan Lucas
# Realase : 07/02/08
# Version : 0.1 b
# File : ndo2db
# Description: Starts and stops the Ndo2db daemon
# used to provide network services status in a database.
#
status_ndo ()
{
if ps -p $NdoPID > /dev/null 2>&1; then
return 0
else
return 1
fi
return 1
}
printstatus_ndo()
{
if status_ndo $1 $2; then
echo "ndo (pid $NdoPID) is running..."
else
echo "ndo is not running"
fi
}
killproc_ndo ()
{
echo "kill $2 $NdoPID"
kill $2 $NdoPID
}
pid_ndo ()
{
if test ! -f $NdoRunFile; then
echo "No lock file found in $NdoRunFile"
echo -n " checking runing process..."
NdoPID=`ps h -C ndo2db -o pid`
if [ -z "$NdoPID" ]; then
echo " No ndo2db process found"
exit 1
else
echo " found process pid: $NdoPID"
echo -n " reinit $NdoRunFile ..."
touch $NdoRunFile
chown $NdoUser:$NdoGroup $NdoRunFile
echo "$NdoPID" > $NdoRunFile
echo " done"
fi
fi
NdoPID=`head $NdoRunFile`
}
# Source function library
# Solaris doesn't have an rc.d directory, so do a test first
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
fi
prefix=/usr/lib/nagios
exec_prefix=${prefix}
NdoBin=/usr/lib/nagios/bin/ndo2db
NdoCfgFile=/etc/nagios/ndo2db.cfg
NdoRunFile=/var/nagios/ndo2db.run
NdoLockDir=/var/lock/subsys
NdoLockFile=ndo2db.lock
NdoUser=nagios
NdoGroup=nagios
# Check that ndo exists.
if [ ! -f $NdoBin ]; then
echo "Executable file $NdoBin not found. Exiting."
exit 1
fi
# Check that ndo.cfg exists.
if [ ! -f $NdoCfgFile ]; then
echo "Configuration file $NdoCfgFile not found. Exiting."
exit 1
fi
# See how we were called.
case "$1" in
start)
echo -n "Starting ndo:"
touch $NdoRunFile
chown $NdoUser:$NdoGroup $NdoRunFile
daemon $NdoBin -c $NdoCfgFile
if [ -d $NdoLockDir ]; then
touch $NdoLockDir/$NdoLockFile;
fi
ps h -C ndo2db -o pid > $NdoRunFile
if [ $? -eq 0 ]; then
echo " done."
exit 0
else
echo " failed."
$0 stop
exit 1
fi
;;
stop)
echo -n "Stopping ndo: "
pid_ndo
killproc_ndo
# now we have to wait for ndo to exit and remove its
# own NdoRunFile, otherwise a following "start" could
# happen, and then the exiting ndo will remove the
# new NdoRunFile, allowing multiple ndo daemons
# to (sooner or later) run
#echo -n 'Waiting for ndo to exit .'
for i in 1 2 3 4 5 6 7 8 9 10 ; do
if status_ndo > /dev/null; then
echo -n '.'
sleep 1
else
break
fi
done
if status_ndo > /dev/null; then
echo
echo 'Warning - ndo did not exit in a timely manner'
else
echo 'done.'
fi
rm -f $NdoRunFile $NdoLockDir/$NdoLockFile
;;
status)
pid_ndo
printstatus_ndo ndo
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: ndo {start|stop|restart|status}"
exit 1
;;
esac
# End of this script
接着:
# chmod 755 /etc/init.d/ndo2db
# chkconfig --level 345 ndo2db on
# mv /etc/rc3.d/S99ndo2db /etc/rc3.d/S97ndo2db
# mv /etc/rc5.d/S99ndo2db /etc/rc5.d/S97ndo2db
# service nagios stop ## 必须先停止Nagios服务
# service ndo2db start
验证ndo2db TCP端口是否打开:
# netstat -tl (如下图红框所示)
# service nagios start
# tail -f /var/log/nagios/nagios.log
必须有如红框所示信息,表示ndo2db安装启动成功。