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