1、原理php
整合cacti和nagios是利用了cacti的一個插件nagios for cacti;它的原理是將nagios的數據經過ndo2db導入到mysql數據庫(cacti)的庫中;而後cacti讀取數據庫信息將nagios展示出來;html
1、安裝cacti擴展模塊mysql
Cacti擴展模塊須要下載安裝cacti-plugin,cacti-0.8.8a及之後版本已經集成不須要安裝(可是我使用的0.8.8b還須要安裝很鬱悶,若是使用的是老版本cacti,擴展模塊安裝以下:ios
Cd /root/cactinagiossql
Wget http://www.cacti.net/downloads/pia/cacti-plugins-0.8.7h-PA-v3.0.tar.gz數據庫
Mv cacti-plugin-0.8.7h-PA-v3.0.tar.gz cacti-plugin-arch.tar.gzjson
Tar -xf cacti-plugin-arch.tar.gz dom
Cp cacti-plugin-arch/* /var/www/html/cacti/socket
Cd /var/www/html/cacti/tcp
Mysql -u cacti –p ‘cacti’ cacti
Patch -p1 –N 而後登錄cacti;點擊consoleàuser Management àadmin àPlugin Mangement打開將其勾選上;而後點擊保存便可;
安裝Ndoutils
cd /root/cactinagios
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=/var/www/html/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
二、準備配置文件
cd db
./installdb -u cactier -p 123456 -h localhost -d cactidb
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 /var/www/html/nagios/etc/ndomod.cfg
[root@nagios ndoutils-2.0.0]# cp config/ndo2db.cfg-sample /var/www/html/nagios/etc/ndo2db.cfg
[root@nagios ndoutils-2.0.0]# cd /var/www/html/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 /var/www/html/nagios/bin
[root@nagios bin]# mv ndo2db-4x ndo2db
[root@nagios bin]# mv ndomod-4x.o ndomod.o
[root@nagios bin]# chown nagios:nagios *
三、修改配置文件
[root@nagios bin]# vi /usr/local/nagios/etc/nagios.cfg
#注意,broker_module和config_file放在一行 broker_module=/var/www/html/nagios/bin/ndomod.o config_file=/var/www/html/nagios/etc/ndomod.cfgevent_broker_options=-1
process_performance_data=1
[root@nagios bin]# vi /var/www/html/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 /var/www/html/nagios/etc/ndomod.cfg
output_type=tcpsocket
output=127.0.0.1
四、啓動守護進程
[root@nagios bin]# /var/www/html/nagios/bin/ndo2db -c /var/www/html/etc/ndo2db.cfg
[root@nagios bin]# cd /root/cactinagios/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
4、安裝NPC插件
介紹:全稱Nagios Plugin for Cacti,將nagios的數據經過ndo2db導入到mysql數據庫(前面設置的npc_開頭的表),而後cacti讀取數據庫信息將nagios的結果經過NPC展現出來。
一、安裝npc
[root@nagios ndoutils-2.0.0]# cd /root/cactinagios
[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’;
二、頁面設置npc
(1)user management–>admin–>勾上Plugin Management
(2)Plugin Management–>點擊install圖標、點擊enable圖標
(3)settings–>NPC–>以下所示:
Remote Commands:
/var/www/html/nagios/var/rw/nagios.cmd
http://192.168.10.16/nagios
三、安裝json:支持npc
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。易於人閱讀和編寫。同時也易於機器解析和生成。
注意:先執行第4步的php -m查看是否加載了json,有則跳過三、4步我這裏有;
查看是否有json被加載:
[root@nagios json-1.2.1]# php -m
修改數據庫:
[root@nagios json-1.2.1]# mysql -ucacti -pcacti mysql>use cacti; 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 六、重啓服務 [root@nagios ~]# service mysqld restart [root@nagios ~]# service httpd restart [root@nagios ~]# service ndo2db restart [root@nagios ~]# service nagios restart 七、觀察日誌 [root@nagios ~]# tail /var/log/html/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