一、cacti基础部分
cacti是一套基于PHP(实现cacti软件)、mysql(存储数据)、snmp(获取数据)、及rrdtool(储存和更新数据及生成图表)开发的网络流量监测图形分析工具。
snmpget获取数据 ->
snmp version1 version2 version3,v1最广泛 是cacti的核心,做数据采集
public ,很重要,用来做认证,可以采集数据
rrdtool,绘图仪器,在cacti中已内嵌,直接调用
数据的大小是一致的,一旦超过最大值,则会覆盖,环型数据库,有内置函数,总共有四种(min、max、avg)
采集: snmp -> poller -> cmd.php -> snmpd -> 被监控主机端
存储: rrdtool -> ra -> *.rd
展示:php -> mysql -> mysql里取主机名、cacti团体名称等变量
二、安装并启动cacti
做监控测试前要先关闭防火墙,设置selinux=disabled;
做监控的主机有:
172.25.45.1 server1.example.com
172.25.45.2 server2.example.com
需要安装的软件有:
Installed:
mysql.x86_64 0:5.1.71-1.el6
mysql-server.x86_64 0:5.1.71-1.el6
php.x86_64 0:5.3.3-26.el6
php-mysql.x86_64 0:5.3.3-26.el6
php-xml.x86_64 0:5.3.3-26.el6
rrdtool.x86_64 0:1.3.8-6.el6
net-snmp-utils.x86_64 1:5.5-49.el6
php-snmp-5.3.3-26.el6.x86_64.rpm 该软件需要网上下载,yum源中本身不存在
[root@server1 ~]# yum install -y httpd mysql php php-mysql php-snmp php-xml mysql-server rrdtool
lftp 172.25.254.45:/pub/software/cacti> get cacti-0.8.8f.tar.gz 下载cacti的安装包
[root@server1 ~]# tar zxf cacti-0.8.8f.tar.gz -C /var/www/html/ 对cacti的安装包解压并指定解压到apache的默认发布目录
[root@server1 ~]# ln -s /var/www/html/cacti-0.8.8f/ /var/www/html/cacti 做软连接,便于使用
[root@server1 ~]# php -m | grep snmp 查看到php中包含snmp服务
snmp
[root@server1 ~]# ll /usr/share/zoneinfo/Asia/Shanghai
-rw-r--r--. 2 root root 405 Oct 3 2013 /usr/share/zoneinfo/Asia/Shanghai
[root@server1 ~]# vim /etc/php.ini 设置时区
@@@@@@
946 date.timezone = Asia/Shanghai
@@@@@@
[root@server1 ~]# useradd -u 1000 -d /var/www/html/cacti -M cacti 添加用户cacti
[root@server1 ~]# vim /var/www/html/cacti/include/config.php
@@@@@@
26 $database_type = "mysql";
27 $database_default = "cacti";
28 $database_hostname = "localhost";
29 $database_username = "cacti";
30 $database_password = "lvah";
31 $database_port = "3306";
32 $database_ssl = false;
39 $url_path = "/cacti/";
42 $cacti_session_name = "Cacti";
@@@@@@
[root@server1 ~]# /etc/init.d/httpd start
[root@server1 ~]# /etc/init.d/mysqld start
[root@server1 ~]# mysql_secure_installation
Set root password? [Y/n] y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
[root@server1 ~]# mysql -plvah #创建数据库并授权
mysql> create database cacti;
mysql> grant all on cacti.* to cacti@localhost identified by 'lvah';
mysql> flush privileges;
mysql> Bye
[root@server1 ~]# cd /var/www/html/cacti
[root@server1 cacti]# mysql -ucacti -plvah cacti < cacti.sql
[root@server1 cacti]# su - cacti
[cacti@server1 ~]$ crontab -e
@@@@@@
* * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1
@@@@@@
[cacti@server1 ~]$ logout
[root@server1 cacti]# tail -f /var/www/html/cacti/log/cacti.log
然后安装cacti前端界面,在浏览器上输入172.25.254.4/cacti/install,依次按下一步完成安装,初次登录时用户与密码都是admin,会强制要求设置密码
三、cacti监控页面中添加其他主机
[root@server1 ~]# vim /etc/snmp/snmpd.conf
@@@@@@
41 #com2sec notConfigUser default public
42 com2sec local localhost public
43 com2sec mynetwork 172.25.45.0/24 public
49 #group notConfigGroup v1 notConfigUser
50 #group notConfigGroup v2c notConfigUser
51 group MyRWGroup v1 local
52 group MyRWGroup v2c local
53 group MyRWGroup usm local
54 group MyROGroup v1 mynetwork
55 group MyROGroup v2c mynetwork
56 group MyROGroup usm mynetwork
62 view systemview included .1.3.6.1.2.1
63 view systemview included .1.3.6.1.2.1.25.1.1
64 view all included .1 80
70 #access notConfigGroup "" any noauth exact systemview none n one
71 access MyROGroup "" any noauth exact all none none
72 access MyRWGroup "" any noauth exact all all none
171 syslocation RHEL6.5
172 syscontact Root <root@localhost>
332 disk / 10000
@@@@@@
[root@server1 ~]# /etc/init.d/snmpd start
[root@server1 ~]# snmpwalk -v 1 -c public 172.25.45.2 IP-MIB::ipAdEntIfIndex
IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 1
IP-MIB::ipAdEntIfIndex.172.25.45.2 = INTEGER: 2
( -c SNMP读密码 -v 1或2(代表SNMP版本) OID(对象标示符)).snmpwalk是一个SNMP小程序,它使用SNMP的GETNEXT请求查询指定OID(SNMP协议中的对象标识)入口的所有OID树信息,并显示给用户。
(1) -v: 指定snmp的版本, 1或者2;
(2) -c: 指定连接设备SNMP读密码;
(3) IP: 指定要walk的设备的IP地址;
(4) Oid:代表要获取设备的指标oid;
[root@server2 ~]# yum install -y net-snmp-utils.x86_64
[root@server2 ~]# yum install -y php-snmp-5.3.3-26.el6.x86_64.rpm ##该软件需要网上下载,yum源中本身不存在
[root@server2 ~]# vim /etc/snmp/snmpd.conf ##该文件与server1端的一致,可用scp复制过来
[root@server2 ~]# /etc/init.d/snmpd start ##启动snmpd服务
[root@server2 ~]# snmpwalk -v 1 -c public localhost IP-MIB::ipAdEntIfIndex
IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 1
IP-MIB::ipAdEntIfIndex.172.25.45.2 = INTEGER: 2
接下来的操作是在浏览器上登录cacti的前端界面,添加被监控主机
先在devices里添加要监控的主机,然后再New Graphs中添加新的图形,最后在graph trees里添加主机
四、安装spine功能(安装spine替换cmd.php)实现多线程监控
Cacti默认轮询器cmd.php执行效率有点低,spine为C语言编写的组件,性能要好许多
172.25.254.45:/pub/software/cacti> get cacti-spine-0.8.8f.tar.gz
[root@server1 ~]# tar zxf cacti-spine-0.8.8f.tar.gz
##安装cacti-spine编译所需要的依赖包
[root@server1 ~]# yum install net-snmp-devel mysql-devel openssl-devel -y
[root@server1 ~]#yum install dos2unix autoconf automake binutils libtool gcc cpp glibc-headers kernel-headers glibc-devel -y
[root@server1 ~]# cd cacti-spine-0.8.8f
[root@server1 cacti-spine-0.8.8f]# sh bootstrap
[root@server1 cacti-spine-0.8.8f]# ./configure
[root@server1 cacti-spine-0.8.8f]# make && make install
[root@server1 ~]# cd /usr/local/spine/etc/
[root@server1 etc]# cp spine.conf.dist /etc/spine.conf
@@@@@@
DB_Host localhost
DB_Database cacti #数据库的名称
DB_User cacti #具有登录到数据库权限的用户
DB_Pass lvah #该用户的密码
DB_Port 3306
DB_PreG 0
@@@@@@
[root@server1 etc]# su - cacti -c /usr/local/spine/bin/spine ##检测到spine添加成功
SPINE: Using spine config file [/etc/spine.conf]
SPINE: Version 0.8.8f starting
SPINE: Time: 0.1073 s, Threads: 1, Hosts: 3
在setting中进行设置,修改paths下Spine Poller File Path 的路径写成 /usr/local/spine/bin/spine和poller 下的Poller Type改成spine
五、cacti上添加monitor插件
cacti上安装monitor插件(monitor分类显示主机信息的图)monitor插件,提供更简略、直观的设备状态图示
lftp 172.25.45.250:/pub/software/cacti> get monitor-v1.3-1.tgz ##下载monitor插件包
##/var/www/html/cacti/plugins/是放置cacti插件的位置
[root@server1 ~]# tar zxf monitor-v1.3-1.tgz -C /var/www/html/cacti/plugins ##解压到指定目录
[root@server1 ~]# cd /var/www/html/cacti/plugins
接下来直接在Plugin Management,上下载安装monitor,安装时会将数据导入到数据库中,然后Settings中Misc中进行相应的设置。就完成了该插件的配置(其中View有5种类型)。