cacti简介:
cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool存储和更新数据,当用户需要查看数据的时候就用rrdtool生成图表呈现给用户。snmp抓到的数据不是存储在mysql中,而是存在rrdtool生成的rrd文件中,rrd文件在cacti根目录的rra文件夹下,rrdtool对数据的更新和存储就是对rrd文件的处理,rrd文件是大小固定的档案文件(Round Robin Archive),它能够存储的数据的笔数在创建时就已经定义。
cacti架构:
1)Snmp用来收集数据;
2)Rrdtool用来存储数据和生成图表;
3)Mysql用来配合php程序存储一些变量数据并对变量数据进行调用,如主机名、主机ip、模板信息和rra文件;
cacti工作流程:
1)snmp协议定时收集远程服务器上的数据;
2)rrdtool将snmp收集的数据存储到rrd数据库;
3)当用户想要查询某台服务器上的流量信息或其他状态信息;
4)在mysql数据库中查找到该服务器对应的rra文件名;
5)然后通过rrdtool命令进行绘图;
6)最后配合php页面显示将图形返回给用户;
一.配置net-snmp
1.首先安装php、net-snmp和依赖包
[root@server1 ~]# yum install php php-mysql php-snmp net-snmp net-snmp-utils net-snmp-libs net-snmp-devel libart_lgpl-devel libpng-devel freetype-devel cairo-devel pango-devel gcc -y
2.查看php安装包
[root@server1 ~]# rpm -qa | grep php
php-mysql-5.3.3-26.el6.x86_64
php-common-5.3.3-26.el6.x86_64
php-cli-5.3.3-26.el6.x86_64
php-5.3.3-26.el6.x86_64
php-snmp-5.3.3-26.el6.x86_64
php-pdo-5.3.3-26.el6.x86_64
php-xml-5.3.3-26.el6.x86_64
3.查看net-snmp安装包
[root@server1 ~]# rpm -qa | grep net
net-snmp-devel-5.5-49.el6.x86_64
net-snmp-5.5-49.el6.x86_64
net-snmp-libs-5.5-49.el6.x86_64
net-tools-1.60-110.el6_2.x86_64
net-snmp-utils-5.5-49.el6.x86_64
4.修改net-snmp的配置文件
[root@server1 ~]# vim /etc/snmp/snmpd.conf
com2sec local localhost public //监控本机,snmp默认弱指令为public
com2sec mynetwork 172.25.0.0/24 public
group MyRWGroup v1 local
group MyRWGroup v2c local
group MyRWGroup usm local
group MyROGroup v1 mynetwork
group MyROGroup v2c mynetwork
group MyROGroup usm mynetwork
#view systemview included .1.3.6.1.2.1.1
#view systemview included .1.3.6.1.2.1.25.1.1
view all included .1 80
#access notConfigGroup "" any noauth exact systemvie
access MyROGroup "" any noauth exact all none none
access MyRWGroup "" any noauth exact all all none
disk / 10000
5.测试net-snmp
[root@server1 ~]# /etc/init.d/snmpd restart
[root@server1 ~]# snmpwalk -v 1 -c public localhost IP-MIB::ipAdEntIfIndex
IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 1
IP-MIB::ipAdEntIfIndex.172.25.16.1 = INTEGER: 2
[root@server1 ~]# netstat -antlp | grep snmp
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 17842/snmpd
二.安装rrdtool
[root@server1 ~]# tar zxf rrdtool-1.4.4.tar.gz
[root@server1 ~]# cd rrdtool-1.4.4
[root@server1 rrdtool-1.4.4]# ./configure --prefix=/usr/local/rrdtool
[root@server1 rrdtool-1.4.4]# make && make install
[root@server1 rrdtool-1.4.4]# ln -s /usr/local/rrdtool/bin/rrdtool /usr/local/bin
三.安装Mysql
[root@server1 ~]# yum install httpd mysql mysql-server mysql-devel
[root@server1 ~]# mysql_secure_installation
创建cacti数据库并增加数据库用户cacti拥有对数据库的权限
[root@server1 ~]# mysql -uroot -pwestos
mysql> create database cacti;
mysql> grant all on cacti.* to cacti@localhost identified by 'cacti';
mysql> flush privileges;
四.安装cacti
[root@server1 ~]# tar zxf cacti-0.8.8c.tar.gz
[root@server1 ~]# mv cacti-0.8.8c /var/www/html/cacti
1.创建系统用户cacti,可以对cacti目录下的文件可写
[root@server1 cacti]# useradd -u 1000 -d /var/www/html/cacti/ -M cacti
[root@server1 cacti]# ls -ld log rra
drwxr-xr-x. 2 cacti users 4096 Nov 12 2014 log
drwxr-xr-x. 2 cacti users 4096 Nov 12 2014 rra
2.将cacti目录下自带的数据库文件导入cacti数据库中
[root@server1 cacti]# mysql -ucacti -pcacti cacti < cacti.sql
3.配置cacti连接数据库
[root@server1 include]# vim config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
$database_ssl = false;
4.创建计划任务,每5分钟采集数据
[root@server1 ~]# crontab -u cacti -e
[root@server1 ~]# crontab -u cacti -l
*/5 * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1
5.修改时区,保证时间一致
[root@server1 ~]# vim /etc/php.ini
date.timezone = Asia/Shanghai
6.可以查看日志信息
[root@server1 log]# tail -f cacti.log #可以看出5分钟执行一次,采集数据的方式为cmd.php(它是php写的一个调度程序,spine是用C写的,性能远远优于cmd.php)
08/18/2015 11:25:16 PM - SYSTEM STATS: Time:12.7700 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:5
08/18/2015 11:30:14 PM - SYSTEM STATS: Time:11.4033 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:5
08/18/2015 11:35:16 PM - SYSTEM STATS: Time:13.6553 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:5
08/18/2015 11:40:15 PM - SYSTEM STATS: Time:11.8906 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:5
知识点:
在snmp监控中snmpwalk命令是测试系统各种信息最有效的方式,命令格式:
snmpwalk -v 1/2(代表snmp版本) -c public(snmp的读取密码,在snmp的配置文件中已设置) ip OID(对象标示符)
snmpwalk -v 2c -c public localhost .1.3.6.1.2.1.25.2.2 获取系统总内存
snmpwalk -v 2c -c public localhost ifDescr 获取网卡信息
#具体OID参见http://www.cnblogs.com/aspx-net/p/3554044.html
五、spine轮询器
#随着生产环境机器的不断增加,系统自带的cmd.php轮询可能没法在设定的时间完成所有机器的采集,所以将采集模式修改为更快的spine轮询
1.安装spine组件
[root@server1 ~]# tar zxf cacti-spine-0.8.7g.tar.gz
[root@server1 ~]# cd cacti-spine-0.8.7g
[root@server1 cacti-spine-0.8.7g]# ./configure --prefix=/usr/local/spine/
[root@server1 cacti-spine-0.8.7g]# make && make install
2.修改配置文件
[root@server1 etc]# cp spine.conf.dist spine.conf
[root@server1 etc]# vim spine.conf
DB_Host localhost
DB_Database cacti
DB_User cacti
DB_Pass cacti
DB_Port 3306
DB_PreG 0
3.修改界面设置
#在setting中找到poller表项,修改Poller type为spine,在paths表项里面找到spine poller file paths添加spine的路径为 /usr/local/spine/bin/spine
4.查看日志,发现轮询器为spine
[root@server1 log]# tail -f cacti.log
08/24/2015 06:45:06 PM - POLLER: Poller[0] ERROR: The path: is invalid. Can not continue
08/24/2015 06:50:06 PM - POLLER: Poller[0] ERROR: The path: is invalid. Can not continue
08/24/2015 06:55:03 PM - POLLER: Poller[0] ERROR: The path: is invalid. Can not continue
08/24/2015 07:05:02 PM - POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting.
08/24/2015 07:05:02 PM - SYSTEM STATS: Time:298.3964 Method:spine Processes:1 Threads:1 Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:0
08/24/2015 07:10:03 PM - POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting.
08/24/2015 07:10:03 PM - SYSTEM STATS: Time:298.7560 Method:spine Processes:1 Threads:1 Hosts:2 HostsPerProcess:2 DataSources:5 RRDsProcessed:0
08/24/2015 07:15:03 PM - POLLER: Poller[0] Maximum runtime of 298 seconds exceeded. Exiting.