Nagios的安装部署和与Cacti的整合
注:所需软件自行下载 gd gd-devel png png-devel jpeg jpeg-devel zlib zlib-devel 保证 安装
第一部分:Nagios的安装
1.安装基础nagios
useradd nagios
wget wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.3.1.tar.gz
tar xvf nagios-3.3.1.tar.gz
cd nagios
注:安装到apache web 目录
./configure --prefix=/data/www/wwwroot/nagios --with-gd-lib=/usr/lib --with-gd-inc=/usr/include
make all
make install && make install-init && make install-commandmode && make install-config
mv sample-config/httpd.conf /usr/local/apache2/conf/extra/nagios.conf
在apache的配置文件中 加入 Include conf/extra/nagios.conf
注:apache具体地址根据实际情况
htpasswd -c /data/www/wwwroot/nagios/etc/htpasswd.users nagiosadmin
设置管理密码
chkconfig nagios on
2.安装nagios-plugins
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
tar xvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/data/www/wwwroot/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
3.安装nagios-snmp-plugins
tar xvf nagios-plugins-snmp-0.6.0.gz
cd nagios-plugins-snmp
./configure --prefix=/data/www/wwwroot/nagios
make
make install
客户机
4.nagios-plugins 和 nrpe 安装
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
tar xvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/data/www/wwwroot/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
tar -zxvf nrpe-2.12.tar.gz && cd nrpe-2.12
./configure --prefix=/data/www/wwwroot/nagios/ --enable-ssl --with-ssl-lib
make all && make install-plugin && make install-daemon && make install-daemon-config
3)配置nrpe
#配置nrpe信息
vi /usr/local/nagios/etc/nrpe.cfg,查找并修改如下一行
allowed_hosts=192.168.1.100,127.0.0.1 #注意修改为nagios服务器的IP:192.168.1.100
#配置监控对象
说明:由监控原理可知被监控端做监控,然后将数据传给监控服务器绘总,设置监控详细参数主要是设置被监控端的nrpe.cfg文件,可以看到里面监控对象
vi /usr/local/nagios/etc/nrpe.cfg,查找并修改如下一行
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 10% -c 5%
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 500 -c 550
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_ping]=/usr/local/nagios/libexec/check_ping -H 192.168.1.1 -w 100.0,20% -c 500.0,60%
............................................#自已添加..........................................................
#command[check_http]=/usr/local/nagios/libexec/check_http -I 127.0.0.1 -w 10 -c 20
#command[check_ssh]=/usr/local/nagios/libexec/check_ssh -4 127.0.0.1
4)启动nrpe,并测试
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
echo '/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d &> /dev/null' >> /etc/rc.local
netstat -atulnp | grep 'nrpe'
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 3308/nrpe
/usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
5)nrpe常见故障
错误一:/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
CHECK_NRPE: Error - Could not complete SSL handshake
解析办法:配置allowed_hosts=192.168.1.100,127.0.0.1,然后kill进程再重启就OK了
错误二:/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
Connection refused by host
解析办法:Nrpe进程没有启动
/data/www/wwwroot/nagios/bin/nrpe -c /data/www/wwwroot/nagios/etc/nrpe.cfg -d
4.其他设置
/data/www/wwwroot/nagios/bin/nagios -v /data/www/wwwroot/nagios/etc/nagios.cfg
检查nagios的配置是否正确,在后面配置nagios过程中我们为了检测配置的是否正确需要不断执行该命令来检查配置文件。
二。cacti安装
wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz
yum install cairo-devel libxml2-devel pango-devel pango libpng-devel freetype freetype-devel libart_lgpl-devel intltool
tar zxvf rrdtool-1.4.7.tar.gz
cd rrdtool-1.4.7
./configure --prefix=/usr/local/rrdtool --disable-tcl
make && make install
ln -s /usr/local/rrdtool/bin/* /usr/local/bin/
wget http://sourceforge.net/projects/net-snmp/files/net-snmp/5.3.4/net-snmp-5.3.4.tar.gz/download
tar xvf net-snmp-5.3.4.tar.gz
cd net-snmp-5.3.4
./configure --prefix=/usr/local/net-snmp
make && make install
ln -s /usr/local/net-snmp/bin/* /usr/local/bin/
cp EXAMPLE.conf /usr/local/net-snmp/share/snmp/snmpd.conf
/usr/local/net-snmp/sbin/snmpd -c /usr/local/net-snmp/share/snmp/snmpd.conf
echo "#/usr/local/net-snmp/sbin/snmpd -c /usr/local/net-snmp/share/snmp/snmpd.conf " >>/etc/rc.local
wget http://www.cacti.net/downloads/cacti-0.8.7h.tar.gz
tar xvf cacti-0.8.7h.tar.gz
mv cacti-0.8.7h /data/www/wwwroot/cacti
Mysql>create database cacti;
Mysql>grant all on cacti.* to cacti@'localhost' identified by 'zzzzzzz';
Mysql>flush privileges;
mysql> use cacti;
mysql> source /data/www/wwwroot/cacti/cacti.sql
vim /data/www/wwwroot/cacti/include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "zzzzzz";
$database_port = "3306";
$database_ssl = false;
在浏览器中访问 cacti 根据提示安装
http://192.168.1.21/cacti
安装时注意 rrdtool、snmp 程序路径
生成图形
/usr/local/php-5.2.17/bin/php /data/www/wwwroot/cacti/poller.php
crontab -e
*/5 * * * * /usr/local/php-5.2.17/bin/php /data/www/wwwroot/cacti/poller.php > /dev/null 2>&1
默认设置,账户和密码都是admin,登录后强制提示更改密码
三. 整合
1、安装ndoutils
wget http://sourceforge.net/projects/nagios/files/ndoutils-1.x/ndoutils-1.4b9/ndoutils-1.4b9.tar.gz/download
tar xzvf ndoutils-1.4b9.tar.gz
cd ndoutils-1.4b9
yum install mysql-devel
./configure --enable-mysql --disable-pgsql --with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql
make
cp -v src/{ndomod-3x.o,ndo2db-3x,file2sock,log2ndo} /data/www/wwwroot/nagios/bin/
mysql> create database ndodb;
mysql> grant all privileges on ndodb.* to ndoutils@localhost identified by 'zzzzzzz';
mysql> flush privileges;
mysql> \q
cd db
./installdb -u ndoutils -p zzzzzzz -h localhost -d ndodb
============================注意===========================================
若出现类似错误
install_driver(mysql) failed: Can't locate DBD/mysql.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at (eval 5) line 3.
Perhaps the DBD::mysql perl module hasn't been fully installed,
or perhaps the capitalisation of 'mysql' isn't right.
Available drivers: DBM, ExampleP, File, Proxy, Sponge.
at ./installdb line 41
执行 yum install perl-DBD-MySQL
若出现
DBI connect('database=ndodb;host=localhost','ndoutils',...) failed: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) at ./installdb line 41
mkdir -p /var/lib/mysql
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock(根据实际情况而定)
=============================END_注意=========================================
cd ..
cp -v config/{ndo2db.cfg-sample,ndomod.cfg-sample} /data/www/wwwroot/nagios/etc/
cd /data/www/wwwroot/nagios/etc/
mv ndo2db.cfg-sample ndo2db.cfg
mv ndomod.cfg-sample ndomod.cfg
chmod 644 ndo*
chown -R nagios:nagios /data/www/wwwroot/nagios/
vim /data/www/wwwroot/nagios/etc/nagios.cfg
在最后添加
broker_module=/data/www/wwwroot/nagios/bin/ndomod-3x.o config_file=/data/www/wwwroot/nagios/etc/ndomod.cfg
vim /data/www/wwwroot/nagios/etc/ndo2db.cfg
============================begin============================
socket_type=tcp
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=ndodb
db_prefix=nagios_
db_user=ndoutils
db_pass=zzzzzzz
lock_file=/data/www/wwwroot/nagios/var/ndo2db.lock
debug_file=/data/www/wwwroot/nagios/var/ndo2db.debug
===============================end===========================
vim /data/www/wwwroot/nagios/etc/ndomod.cfg
=============================begin==========================
output_type=tcpsocket
output=127.0.0.1
buffer_file=/data/www/wwwroot/nagios/var/ndomod.tmp
================================end================================
启动 /data/www/wwwroot/nagios/bin/ndo2db-3x -c /data/www/wwwroot/nagios/etc/ndo2db.cfg
service nagios restart
tail /var/log/message
================出现类似内容===================================================
Mar 2 15:52:28 localhost nagios: Nagios 3.3.1 starting... (PID=20779)
Mar 2 15:52:28 localhost nagios: Local time is Fri Mar 02 15:52:28 CST 2012
Mar 2 15:52:28 localhost nagios: LOG VERSION: 2.0
Mar 2 15:52:28 localhost nagios: ndomod: NDOMOD 1.4b9 (10-27-2009) Copyright (c) 2009 Nagios Core Development Team and Community Contributors
Mar 2 15:52:28 localhost nagios: ndomod: Successfully connected to data sink. 5000 queued items to flush.
Mar 2 15:52:28 localhost nagios: ndomod: Successfully flushed 5000 queued items to data sink.
Mar 2 15:52:28 localhost nagios: Event broker module '/data/www/wwwroot/nagios/bin/ndomod-3x.o' initialized successfully.
Mar 2 15:52:29 localhost nagios: Finished daemonizing... (New PID=20781)
============================ok成功==========================================================
cp /usr/local/src/ndoutils-1.4b9/daemon-init /etc/init.d/ndo2db
chmod +x /etc/init.d/ndo2db
vim /etc/init.d/ndo2db
=======================修改下面内容===========================
servicename=ndo2db
prefix=/data/www/wwwroot/nagios
exec_prefix=/data/www/wwwroot/nagios
Ndo2dbBin=/data/www/wwwroot/nagios/bin/ndo2db-3x
Ndo2dbCfgFile=/data/www/wwwroot/nagios/etc/ndo2db.cfg
Ndo2dbVarDir=/data/www/wwwroot/nagios/var
Ndo2dbRunFile=$Ndo2dbVarDir/ndo2db.lock
Ndo2dbLockDir=/var/lock/subsys
Ndo2dbLockFile=ndo2db
==========================end======================
chkconfig --add ndo2db
chkconfig ndo2db on
service ndo2db start
service nagios start
2、安装ncp,以在Cacti中展现Nagios
cd /usr/local/src
wget http://www.cacti.net/downloads/pia/cacti-plugin-0.8.7h-PA-v3.0.tar.gz
tar xzvf cacti-plugin-0.8.7h-PA-v3.0.tar.gz
cp -R cacti-plugin-arch/* /data/www/wwwroot/cacti
cd /data/www/wwwroot/cacti
mysql -ucacti -pzzzzzzz cacti < pa.sql
patch -p1 -N < cacti-plugin-0.8.7h-PA-v3.0.diff
vim include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
$database_ssl = false;
$url_path = "/cacti/"; 注:此路径根据cacti 在apache中的设置而定
用浏览器打开cacti 点击 User Management>admin>最下面勾选Plugin Management>save
npc 包 见附件
tar xzvf npc-2.0.4.tar.gz
mv npc /data/www/wwwroot/cacti/plugins/
vim /data/www/wwwroot/cacti/include/config.php
$plugins[] = 'npc';
wget http://pkgs.fedoraproject.org/repo/pkgs/php-pecl-json/json-1.2.1.tgz/d8904d2f004ceec85eeacf524cd25539/json-1.2.1.tgz
tar xvf json-1.2.1.tgz
cd json-1.2.1
/usr/local/php-5.2.17/bin/phpize
./configure --with-php-config=/usr/local/php-5.2.17/bin/php-config --enable-shared --enable-static --with-json
make&&make install
vim /usr/local/php-5.2.17/etc/php.ini
extension_dir = "/usr/local/php-5.2.17/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "json.so"
访问cacti
Configuration -> Plugin Management -> Actions -> 箭头点击安装 -> 右击绿色箭头启用,这时页面就有NPC选项
mysql -u cacti -pzzzzzzz
use cacti;
Database changed
mysql> alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_notifications add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_statehistory add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output;
mysql> \q
在Settings->npc中 ,勾上Remote Commands
Nagios Command File Path=/data/www/wwwroot/nagios/var/rw/nagios.cmd
Nagios URL=http://localhost/nagios/
点击 npc 只出图不出数据,检查是否安装了pdo-mysql,编译安装方法如下(路径根据实际情况):
cd /usr/local/src/php-5.2.17/ext/pdo_mysql
/usr/local/php-5.2.17/bin/phpize
./configure --with-php-config=/usr/local/php-5.2.17/bin/php-config --with-pdo-mysql=/usr/local/mysql-5.1.56/
make && make install
vim /usr/local/local/php-5.2.17/etc/php.ini
===================最后加入============================
extension_dir = "/usr/local/php-5.2.17/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "pdo_mysql.so"
============================END=====================================
四、Nconf的安装与配置
[root@rhel ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 49
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database nconf; (创建nconf数据库)
Query OK, 1 row affected (0.02 sec)
mysql> grant all privileges on nconf.* to nconf@localhost identified by 'zzzzzzz'; (创建nconf用户并赋权)
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; (刷新特权表)
Query OK, 0 rows affected (0.00 sec
wget http://sourceforge.net/projects/nconf/files/nconf/1.2.6-0/nconf-1.2.6-0.tgz
tar xvf nconf-1.2.6-0.tgz
mv nconf /data/www/wwwroot/cacti
chown -R nagios:nagios /data/www/wwwroot/nconf
访问 http://ip/nconf
DBHOST localhost
DBNAME nconf
DBUSER nconf
DBPASS nconf
rm -rf INSTALL INSTALL.php UPDATE UPDATE.php
ln -s /data/www/wwwroot/nagios/bin/nagios /data/www/wwwroot/nconf/bin/nagios
chmod +x /data/www/wwwroot/nconf/bin/*
chmod +x /data/www/wwwroot/nagios/bin/nagios
chmod +x /data/www/wwwroot/nconf/ADD-ONS/deploy_local.sh
vim /data/www/wwwroot/nconf/ADD-ONS/deploy_local.sh
OUTPUT_DIR="/data/www/wwwroot/nconf/output/"
NAGIOS_DIR="/data/www/wwwroot/nagios/etc/"
/data/www/wwwroot/nconf/ADD-ONS/deploy_local.sh
五、补充 cacti插件
montor
安装monitor
1、下载及解压monitor
[root@localhost home]# wget http://docs.cacti.net/_media/plugin:monitor-v1.2-1.tgz
mv plugin\:monitor-v1.2-1.tgz monitor.tgz
tar xvf monitor.tgz
mv monitor/ /data/www/wwwroot/cacti/plugins/
cd /data/www/wwwroot/cacti/plugins/monitor/
mysql -h localhost -u cacti -p cacti < /data/www/wwwroot/cacti/plugins/monitor/monitor.sql
Enter password:
2、启用monitor
点击plugin management 进入启用monitor即可
3、设置monitor
进入settings-misc配置参数如下并保存
Alarm Sound 设置是否启用告警声音
Refresh Interval设置一页显示监控主机数量
Icon Spacing设置一行显示主机数量
Show Icon Legend设置显示图例
Grouping 设置显示群组类型
View 设置图标类型
安装settings
1、下载及解压settings
wget http://docs.cacti.net/_media/plugin:settings-v0.7-1.tgz
mv plugin\:settings-v0.7-1.tgz seting.tgz
tar xvf seting.tgz
mv settings/ /data/www/wwwroot/cacti/plugins/
2、启用settings
点击console-Configuration-plugin management进行启用settings插件
3、配置settings插件
启用settings后settings选项中多了mail/dns选项
填写测试邮箱如:[email protected]
选择发件服务器类型为:smtp
填写发送的邮箱为:[email protected]
设置发件人:a-cacti
设置自动换行字符数量:120
设置邮件服务器地址:mail.qq.com
设置邮件服务器smtp端口:25
设置发件邮箱用户名:[email protected]
填写邮箱登陆密码:xxx
重复输入密码:xxx
设置dns地址:如广东的dns地址202.96.128.86/202.96.134.133
设置dns查询超时时间:500
点击右上角的send a test mail进行测试看告警的邮件服务器是否设置正确,收到测试邮件说明配置正确Se
安装thold
1、安装thold
wget http://cactiusers.org/downloads/thold.tar.gz
tar xvf thold-0.4.3.tar.gz
mv thold/ /data/www/wwwroot/cacti/plugins/
cd /data/www/wwwroot/cacti/plugins/thold
导入数据库文件
mysql -h localhost -u cacti -p cacti < /data/www/wwwroot/cacti/plugins/thold/thold.sql
2、启用thold
console-Plugin Management进入pia插件管理面板启用阈值告警插件thold
3、配置thold
进入settings-Thresholds选项填写告警邮件,发件人等信息