一、原理
整合cacti和nagios是利用了cacti的一个插件nagios for cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios的结果展示出来。
二、cacti扩展模块
cacti扩展模块需要下载安装cacti-plugin,cacti-0.8.8a及以后版本已集成此扩展模块不需要再单独安装,如果使用的是老版本cacti,扩展模块安装如下:
cd /root/download
wget http://www.cacti.net/downloads/pia/cacti-plugin-0.8.7h-PA-v3.0.tar.gz
tar xvf cacti-plugin-0.8.7h-PA-v3.0.tar.gz
cp -R cacti-plugin-arch/* /var/www/html/cacti/
cd /var/www/html/cacti/
mysql -u cactier -p 123456 cactidb < pa.sql
patch -p1 -N < cacti-plugin-0.8.7h-PA-v3.0.diff
vim include/config.php
$url_path = "/cacti/";
从web进入cacti,启用cacti plugin扩展
三、安装Ndoutils插件
1、安装Ndoutils
cd /root/download
wget http://jaist.dl.sourceforge.net/project/nagios/ndoutils-2.x/ndoutils-2.0.0/ndoutils-2.0.0.tar.gz
tar zxvf ndoutils-2.0.0.tar.gz
cd ndoutils-2.0.0
./configure --prefix=/usr/local/nagios/ --with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib64/mysql --enable-mysql --disable-pgsql --with-ndo2db-user=nagios --with-ndo2db-group=nagios
make
2、准备配置文件
cd db
./installdb -u cactier -p 123456 -h localhost -d cactidb
cd ..
cp src/{ndomod-4x.o,ndo2db-4x,log2ndo,file2sock} /usr/local/nagios/bin
nagios是4.x版本的就使用ndomod-4x.o和ndo2db-4x,如果是3.x版本就复制对应的3x文件
cp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
cd /usr/local/nagios/etc/
chown nagios:nagios ndo2db.cfg ndomod.cfg
chmod 664 ndo2db.cfg ndomod.cfg
cd /usr/local/nagios/bin
mv ndo2db-4x ndo2db
mv ndomod-4x.o ndomod.o
chown nagios:nagios *
3、修改配置文件
vi /usr/local/nagios/etc/nagios.cfg
#注意,broker_module和config_file放在一行
broker_module=/usr/local/nagios/bin/ndomod.o config_file=/usr/local/nagios/etc/ndomod.cfg event_broker_options=-1
process_performance_data=1
vi /usr/local/nagios/etc/ndo2db.cfg
socket_type=tcp
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=cactidb
db_prefix=npc_
db_user=cactier
db_pass=123456
vi /usr/local/nagios/etc/ndomod.cfg
output_type=tcpsocket
output=127.0.0.1
4、启动守护进程
/usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
cd /root/download/ndoutils-2.0.0
cp ./daemon-init /etc/init.d/ndo2db
chmod +x /etc/init.d/ndo2db
service ndo2db status
chkconfig --add ndo2db
chkconfig ndo2db on
四、安装NPC插件
介绍:全称Nagios Plugin for Cacti,将nagios的数据通过ndo2db导入到mysql数据库(前面设置的npc_开头的表),然后cacti读取数据库信息将nagios的结果通过NPC展示出来。
1、安装npc
cd /root/download
wget http://down.drv5.cn/www.drv5.cn/npc-2.0.4.tar.gz
tar zxvf npc-2.0.4.tar.gz
mv npc /var/www/html/cacti/plugins/
vi /var/www/html/cacti/include/config.php
$plugins[] = ‘npc‘;
2、页面设置npc
user management-->admin-->勾上Plugin Management
Plugin Management-->点击install图标、点击enable图标
settings-->NPC-->设置如下
3、安装json:支持npc
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。
注意:先执行第4步的php -m查看是否加载了json,有则跳过3、4步,
cd /root/download
wget http://pkgs.fedoraproject.org/repo/pkgs/php-pecl-json/json-1.2.1.tgz/d8904d2f004ceec85eeacf524cd25539/json-1.2.1.tgz
tar zxvf json-1.2.1.tgz
cd json-1.2.1
vi JSON_parser.c
#define ZVAL_DELREF Z_DELREF_P
phpize
./configure
make && make install
4、配置php以支持json
vi /etc/php.d/json.ini
extension=json.so
service httpd restart
查看是否有json被加载
php -m
查看是否有导入信息
php -i | grep php.ini
5、修改数据库(缺少个别字段)
tail /var/log/messages
ndo2db: mysql_error: ‘Unknown column ‘long_output‘ in ‘field list‘‘
mysql -ucactier -p123456
use cactidb;
ALTER TABLE npc_eventhandlers ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_hostchecks ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_hoststatus ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_notifications ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_servicechecks ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_servicestatus ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_statehistory ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_systemcommands ADD long_output TEXT NOT NULL DEFAULT ‘‘ AFTER output;
ALTER TABLE npc_services ADD importance smallint(6) NOT NULL DEFAULT ‘0‘;
ALTER TABLE npc_hosts ADD importance smallint(6) NOT NULL DEFAULT ‘0‘;
ALTER TABLE npc_contacts ADD minimum_importance smallint(6) NOT NULL DEFAULT ‘0‘;
quit
6、重启服务
service mysqld restart
service httpd restart
service ndo2db restart
service nagios restart
7、观察日志
tail /usr/local/nagios/var/nagios.log
tail /var/www/html/cacti/log/cacti.log
tail /var/log/mysqld.log
tail /var/log/messages
五、遇到的问题
1、NPC页面上显示Nagios为Off
错误:
tail /usr/local/nagios/var/nagios.log
ndomod: Could not open data sink! I‘ll keep trying, but some output may get lost...
解决:
cd /usr/local/nagios/etc/
chown nagios:nagios ndo2db.cfg ndomod.cfg
chmod 664 ndo2db.cfg ndomod.cfg
service ndo2db restart
service nagios restart
2、ndo2db服务启动失败
错误:
Could not bind socket: Address already in use
解决:
ps aux|grep ndo2db 看到多个ndo2db的进程
kill-9 进程id 杀掉进程,再重新运行
sudo /usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg
3、消息发送失败
错误:
tail /var/log/message
ndo2db: Warning: queue send error, retrying... ndo2db: Message sent to queue.
解决:
vi /etc/sysctl.conf
kernel.msgmax = 131072000
kernel.msgmnb = 131072000
kernel.msgmni = 65536000
Cacti+Nagios监控系统(四):Cacti整合Nagios