使用 cacti 监控网络设备

不管在何种发型版本上部署,基本都需要如下的包:
httpd
php
php-mysql
php-snmp
php-ldap(when using LDAP authentication)
php-xml
mysql
mysql-server
net-snmp(depending on the distro, net-snmp-utils may be required)
crond (cron, cronie or the like)
wget
patch

0.8.7h 算是比较稳定的版本,最新的 0.8.8a 在一些 plugins 会有兼容性的问题。
简单记录下安装过程。

# lsb_release  -a
LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description:    CentOS release 5.6 (Final)
Release:        5.6
Codename:       Final

# cd /etc/yum.repos.d/
# wget http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
# rpm -ivh rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
# yum makecache

# yum install mysql-server [mysql perl-DBD-MySQL  perl-DBI]
# yum install php-mysql [php-common php-pdo]
# yum install net-snmp-utils [lm_sensors net-snmp net-snmp-libs]
# yum install rrdtool [chkfontpath libFS libXfont libdbi libfontenc lua perl-rrdtool ruby ruby-libs ttmkfdir xorg-x11-font-utils xorg-x11-fonts-Type1 xorg-x11-xfs]
# yum install php php-cli php-snmp gmp
# yum install httpd [apr  apr-util  postgresql-libs]

# /etc/init.d/mysqld  start
Initializing MySQL database:  Installing MySQL system tables…
OK
Filling help tables…
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h yun-phy5.whalecloud.com password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
Starting MySQL:                                            [  OK  ]

# mysqladmin -u root password 'your passwd'
# mysql -u root -p

> create database cacti;
> GRANT ALL ON cacti.* TO 'cactiuser'@'192.16.1.%' IDENTIFIED BY 'cactiuser passwd';
> FLUSH privileges;

# cd /var/www
# wget http://www.cacti.net/downloads/cacti-0.8.7h.tar.gz
# tar zxvf cacti-0.8.7h.tar.gz
# ln -s cacti-0.8.7h cacti
# cd cacti
# mysql cacti -u cactiuser -p < /var/www/cacti/cacti.sql

# modify /var/www/cacti/include/config.php
$database_password = "cactiuser passwd";

# cat /etc/httpd/conf.d/cacti.conf
Alias /cacti /var/www/cacti/
<Directory /var/www/cacti/>
    DirectoryIndex index.php
    Options -Indexes
    AllowOverride all
    AddType application/x-httpd-php .php
    php_flag magic_quotes_gpc on
    php_flag track_vars on
</Directory>

# /etc/init.d/httpd start

# chown -R apache:apache /var/www/html/cacti
# useradd cacti
# chown -R cacti rra/ log/

# vi /etc/cron.d/cacti
*/5 * * * *    cacti   /usr/bin/php /var/www/cacti/poller.php > /dev/null 2>&1

# cat /etc/php.ini

memory_limit = 256M

date.timezone = Asia/Shanghai

# yum install  gcc make autoconf automake net-snmp-devel mysql-devel openssl-devel
# cd /usr/local/src/
# wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.8a.tar.gz
# tar zxvf cacti-spine-0.8.8a.tar.gz
# cd cacti-spine-0.8.8a
# mkdir /usr/local/spine/
# ./configure  --prefix=/usr/local/spine
# make
# make install

如果使用 ubuntu 的话,需要安装下面这些依赖:
# apt-get install libmysqlclient-dev libsnmp-dev

# cp /usr/local/spine/etc/spine.conf.dist  /etc/spine.conf
# cat /etc/spine.conf

DB_Host         192.168.1.1
DB_Database     cacti
DB_User         cactiuser
DB_Pass         cactiuser passwd
DB_Port         3306
DB_PreG         0

打开后台,执行下面两个步骤以使 spine 生效:
Console -> Settings -> Paths -> Spine Poller File Path > /usr/local/spine/sbin/spine
Console -> Settings -> Poller -> Poller Type -> spine

安装 PIA:
# /var/www/cacti
# wget http://www.cacti.net/downloads/pia/cacti-plugin-0.8.7h-PA-v3.0.tar.gz
# tar zxvf cacti-plugin-0.8.7h-PA-v3.0.tar.gz
# patch -p1 -N --dry-run < cacti-plugin-arch/cacti-plugin-0.8.7h-PA-v3.0.diff
# patch -p1 -N < cacti-plugin-arch/cacti-plugin-0.8.7h-PA-v3.0.diff
# mysql cacti -u cactiuser -h 10.18.10.17 -p < /cacti-plugin-arch/pa.sql

# cat /var/www/cacti/include/config.php
<?php

$cacti_session_name = "Cacti";
$url_path = "/cacti/";
?>

几个常用的 plugin:
http://docs.cacti.net/plugins
1. settings: 所有插件的设置界面
2. aggregate: 这个基本都需要用到,尤其在统计网络带宽的时候
3. autom8: 自动化
4. nectar: 发送报告
5. realtime: 实时出图,debug 很有用
6. monitor: 监控
7. thold: 阈值提示
8. weathermap: 一个量化设备之前流量的插件,之前中科大的 mirrors 使用的应该就是这个插件

个人认为 cacti 最好用的一点就是用来监控全网的流量,配合 SNMP,加上 "Graph Trees" 归类,以及 weathermap、thold、netcar、realtime 等插件,就能比较好的量化出当前全网的流量状况。

在自动化方面,cacti 提供有 cli,比如我们需要将所有 SW 的 CPU 放到 SW_METRIC/CPU 这棵子树里面。
首先列出我们的 host:
# php add_tree.php --list-hosts
Known Hosts: (id, hostname, template, description)
1       127.0.0.1       8       Localhost
2       192.168.0.248     5       27_1685_C18

64      192.168.0.8      5       30_1017_C18

判断出这些 host 下面 CPU 这个 graph 的 id:
# php add_tree.php --list-graphs --host-id=64 | grep Mem | awk '{print $1}'
1111

将其加入到 CPU 这棵子树下:
# php add_tree.php --type=node --node-type=graph --tree-id=7 --parent-node=62 --graph-id=1111
其中 62 是 CPU 的 id,7 是 SW_METRIC 的 ID

最后 auto 一下,简化成一句话:
for i in {1..64};do GRAPHID=$(php add_tree.php –list-graphs –host-id=$i | grep CPU | awk '{print $1}');php add_tree.php –type=node –node-type=graph –tree-id=7 –parent-node=62 –graph-id=$GRAPHID;done

关于 cacti 的使用,可以参阅《Cacti 0.8 Beginner's Guide》这本书,写的很详细,基本的功能使用都有涉及,google 一下就能找到 PDF。

cacti 虽然功能相对目前纷繁复杂的监控系统比较单一,但是这种『一次只做一件事』的品质确实值得我们学习,有些时候,什么都想做的最终结果可能是什么都做不好。