1 关于Cacti
Cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户,官方主页请戳
Cacti的几个关键点:
1.Snmp关系着数据的收集,rrdtool关系着数据存储和图表的生成。
2.Mysql配合PHP程序存储一些变量数据并对变量数据进行调用,如:主机名、主机ip、snmp团体名、端口号、模板信息等变量。
3.snmp抓到数据不是存储在mysql中,而是存在rrdtool生成的rrd文件中(在cacti根目录的rra文件夹下)。rrdtool对数据的更新和存储就是对rrd文件的处理,rrd文件是大小固定的档案文件(Round Robin Archive),它能够存储的数据笔数在创建时就已经定义。
Cacti工作原理图如下,
2 安装配置Cacti
Cacti需要安装snmp服务(如net-snmp)、LAMP/LNMP环境以及rrdtool,其中LAMP/LNMP环境的安装配置不再赘述,以下为rrdtool和snmp的安装配置:
2.1 安装rrdtool
rrdtool的安装可以采用yum安装或者源码安装,此处采用前一种方式,需要安装:rrdtool,rrdtool-devel.rrdtool-perl,rrdtool-php
#yum install rrdtool,rrdtool-devel.rrdtool-perl,rrdtool-php
#rpm -qa | grep rrdtool
rrdtool-python-1.3.8-6.el6.i686
rrdtool-devel-1.3.8-6.el6.i686rrdtool-doc-1.3.8-6.el6.i686rrdtool-perl-1.3.8-6.el6.i686rrdtool-php-1.3.8-6.el6.i686rrdtool-tcl-1.3.8-6.el6.i686rrdtool-ruby-1.3.8-6.el6.i686rrdtool-1.3.8-6.el6.i6862.2 安装net-snmp
SNMP:简单网络管理协议。通过UDP端口161和162来实现。
利用SNMP,一个管理工作站可以远程管理所有支持这种协议的网络设备,包括监视网络状态、修改网络设备配置、接收网络事件警告等。
SNMP采用了Client/Server模型的特殊形式:代理/管理站模型。对网络的管理与维护是通过管理工作站与SNMP代理间的交互工作完成的。每个SNMP从代理负责回答SNMP管理工作站(主代理)关于MIB定义信息的各种查询。
net-snmp是SNMP的开源实现,支持snmpv1、snmpv2、snmpv3,支持基于IPv4和IPv6的SNMP应用程序。
1. 安装net-snmp: 依序安装net-snmp-libs,net-snmp,net-snmp-utils
2. 启动并加入开机自启:
service snmpd start
chkconfig snmpd on
3. 配置snmp:snmpd.conf
# sec.name source community
com2sec notConfigUser 127.0.0.1 public #default为允许哪台机器通过snmp得到你的信息,此处修改为127.0.0.1,可添加相应的机器ip,public为密钥access notConfigGroup "" any noauth exact all none none #允许群组对allview只有只读权限
view all included .1 80 #启用全部功能
配置完重启:service snmpd restart
4. 防火墙开启UDP 161端口:vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m udp -p udp --dport 161 -j ACCEPT
5. snmp服务验证:
5.1 使用snmpwalk命令,查看本机localhost的主机名:
snmpwalk -v 2c -c public localhost sysName.0
5.2 使用snmptranslate命令,检查snmp工具是否可以使用:
#snmptranslate -To | head
.1.1.3
.1.3.6.1.3.6.1.1.3.6.1.1.1.3.6.1.2.1.3.6.1.2.1.1.3.6.1.2.1.1.1.3.6.1.2.1.1.1.1.3.6.1.2.1.1.2.1.3.6.1.2.1.1.3.3.1.3.6.1.3.6.1.1.3.6.1.1.1.3.6.1.2.1.3.6.1.2.1.1.3.6.1.2.1.1.1.3.6.1.2.1.1.1.1.3.6.1.2.1.1.2.1.3.6.1.2.1.1.36. 测试远程主机是否可以获取本机数据:snmpwalk -v 2c -c public 192.168.56.1
ps:远程主机需要安装并配置好snmp服务。
2.3 安装Cacti
wget
tar zxvf cacti-0.8.8b.tar.gz
mv cacti-0.8.8b.tar.gz /var/www/html/cacti
2.4 创建cacti数据库
mysql> create database cacti;
mysql> grant all on cacti.* to 'cacti'@'localhost' identified by '131415';
mysql> flush privileges;
2.5 导入cacti表内容到创建的数据库中
cd /var/www/html/cacti
mysql -ucacti -p131415 cacti < /var/www/html/cacti/cacti.sql
2.6 配置cacti:修改config.php,global.php,修改的内容一致
vim /var/www/html/cacti/include/config.php
vim /var/www/html/cacti/include/global.php
/* make sure these values refect your actual database/host/user/password */
$database_type = "mysql";$database_default = "cacti";$database_hostname = "localhost";$database_username = "cacti";$database_password = "131415";$database_port = "3306";$database_ssl = false;2.7 添加cacti用户并授予其目录权限
useradd -r -M cacti
chown –R cacti /var/www/html/cacti/rra/
chown –R cacti /var/www/html/cacti/log/
这一步很关键,关系到是否有图
2.8 创建计划任务来画图
#su cacti
#crontab -e
*/5 * * * * cacti php /var/www/html/cacti/poller.php > /dev/null 2>&1
php /var/www/html/cacti/poller.php > /dev/null 2>&1
exit
2.9 启动cacti
登录,输入相应账户及密码来查看web界面
3 Cacti spine配置安装
spine是一个基于C语言的,非常快速的轮询引擎。它是默认的cmd.php的可选替代,主要是为了加快SNMP轮询。
3.1 spine安装
添加rpmforge的软件仓库后可以使用yum安装,或者使用源码安装,此处采用后一种方式:
1. 安装所需依赖包:
#yum install -y net-snmp-devel openssl-devel mysql-devel
#wget
#tar zxvf cacti-spine-0.8.8b.tar.gz
#cd cacti-spine-0.8.8b
#./configure
#make && make install
3.2 配置
cp /usr/local/spine/etc/spine.conf.dist /usr/local/spine/etc/spine.conf
vim /usr/local/spine/etc/spine.conf:
DB_Host localhost
DB_Database cactiDB_User cactiDB_Pass 131415DB_Port 3306DB_PreG 03.3 开启spine轮询
启动spine:
#/usr/local/spine/bin/spine --conf=/usr/local/spine/etc/spine.conf
SPINE: Using spine config file [/usr/local/spine/etc/spine.conf]SPINE: Version 0.8.8b startingSPINE: Time: 2.1262 s, Threads: 1, Hosts: 5在Cacti Web界面中设置使能:
a)以admin用户登录到Cacti。
b)从目录菜单中选择设置”Settings”,选择”Paths”选项卡。c)在”Spine Poller File Path”中输入Spine可执行程序的完整路径,我的是/usr/local/spine/bin/spine,保存设置。d)选择”Poller”选项卡。e)在”Poller Type”下拉框设置中,选择”spine”。f)保存设置
查看Cacti日志信息:
04/08/2014 09:30:03 PM - SYSTEM STATS: Time:2.1398 Method:spine Processes:1 Threads:1 Hosts:5 HostsPerProcess:5 DataSources:11 RRDsProcessed:7
4 与Nagios整合
Nagios+Cacti的监控体系是运维人员进行监控的有力武器,结合了Cacti的强大画图和自定义画图能力,以及Nagios的可控报警。
4.1 安装ndoutils
ndoutils是将Nagios的配置及监控信息存储到数据库里,NPC通过调用ndo所存储的数据来展现Nagios的信息。
1. 编译安装
wget
tar zxvf ndoutils-2.0.0.tar.gz ;cd ndoutils-2.0.0
./configure --prefix=/usr/local/nagios/ --enable-mysql --disable-pgsql --with-mysql-inc=/usr/include/mysql/ --with-mysql-lib=/usr/lib/mysql/
make
ps:由于ndoutils-2.0.0版本与Nagios-4.0.*及以上版本有兼容问题,所以楼主还是建议使用Nagios-3.*版本。
2. 复制配置文件
#cp -v src/{ndomod-3x.o,ndo2db-3x,file2sock,log2ndo} /usr/local/nagios/bin/
#cd db
#./installdb -ucacti -p131415 -hlocalhost -d cacti
DBD::mysql::db do failed: Table 'cacti.nagios_dbversion' doesn't exist at ./installdb line 51.
** Creating tables for version 2.0.1 Using mysql.sql for installation...** Updating table nagios_dbversionDone!#cd ..
#cp -v config/{ndo2db.cfg-sample,ndomod.cfg-sample} /usr/local/nagios/etc
#mv /usr/local/nagios/etc/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
#mv /usr/local/nagios/etc/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
#chmod 644 /usr/local/nagios/etc/ndo*
#chown nagios:nagios /usr/local/nagios/etc/*
#chown nagios:nagios /usr/local/nagios/bin/*
3. 配置Nagios
vim /usr/local/nagios/etc/nagios.cfg
#add flowing entry
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
event_broker_options=-14. 修改ndo2db.cfg
vim /usr/local/nagios/etc/ndo2db.cfg:
#socket_type=unix
socket_type=tcpdb_servertype=mysql
db_host=localhost
db_port=3306
# DATABASE NAME# This option specifies the name of the database that should be used.db_name=cactidb_user=cacti
db_pass=1314155. 修改ndomod.cfg
vim /usr/local/nagios/etc/ndomod.cfg:
#output_type=file
output_type=tcpsocket#output_type=unixsocket#output=/usr/local/nagios/var/ndo.dat
output=127.0.0.1#output=/usr/local/nagios/var/ndo.sock6. 制作ndo2db系统启动项
cd ndoutils-2.0.0
cp ./daemon-init /etc/init.d/ndo2db
vim /etc/init.d/ndo2db:将Ndo2dbBin的值修改为:
Ndo2dbBin=/usr/local/nagios/bin/ndo2db-3x
chmod +x /etc/init.d/ndo2db
7. 启动ndo2db
#service ndo2db start
Starting ndo2db: done.
#service nagios restart
Stopping nagios: [ OK ]
Starting nagios: [ OK ]注解:由于前文所说兼容问题,重启Nagios会报错:
Error:Could not load module '/usr/local/nagios/bin/ndomod-3x.o' ->/usr/local/nagios/bin/ndomod-3x.o: undefined symbol:servicedependency_list
解决:最好的办法是使用Nagios的3.x版本。
4.2 安装NPC
NPC:(Nagios Plugin for Cacti)是一个Cacti插件,安装后可以在Cacti界面里使用Nagios的功能。
1. 安装Cacti扩展模块
wget
tar zxvf 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 -ucacti -p131415 cacti < pa.sql
patch -p1 -N < cacti-plugin-0.8.7h-PA-v3.0.diff
vim include/config.php:
$url_path = "/cacti/";
登录Cacti Web界面,启用cacti plugin扩展
2. 安装NPC
tar zxvf npc-2.0.4.tar.gz
mv npc /var/www/html/cacti/plugins/
vim /var/www/html/cacti/include/config.php:
#add flowing entry
$plugin = 'npc';
3. 安装NPC支持:json
wget
tar zxvf json-1.2.1.tgz
cd json-1.2.1
phpize #确保事前有安装php-devel包
./configure
make && make install
查看是否有导入信息:php -i | grep php.ini
报以下错误:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/modules/json.so' - /usr/lib/php/modules/json.so: undefined symbol: ZVAL_DELREF in Unknown on line 0
原因是重写了json,需要重新安装php-common: yum reinstall php-common
php -m | grep json
PHP Warning: Module 'json' already loaded in Unknown on line 0json4. 在php.ini中添加对json的支持
extension = json.so
5. 修改配置文件让npc读取到新的数据
vim /usr/local/nagios/etc/ndo2db.cfg:
db_prefix=npc_
修改mysql表结构:
mysql -ucacti -p
Enter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 9872Server version: 5.1.73 Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use cacti;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.33 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.19 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.19 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table npc_notifications add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.17 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.20 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.18 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table npc_statehistory add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.21 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output;Query OK, 0 rows affected, 1 warning (0.22 sec)Records: 0 Duplicates: 0 Warnings: 06. 重启ndo2db、nagios
/etc/init.d/ndo2db restart
/etc/init.d/nagios restart
7. 设置NPC功能
在Web界面中,Settings->NPC,勾选Remote Commands
Nagios Command File Path:/usr/local/nagios/var/rw/nagios.cmd
Nagios URL:
——Rango Chen