1.前言
Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具。为方便以后了解学习,现将cacti安装步骤及图形设置整理成文档。
2 cacti安装配置
2.1 需要安装软件包
yum install httpd php php-mysql php-snmp php-ldap php-xml mysql mysql-server net-snmp net-snmp-utils libtool gcc mysql-devel zlib libpng freetype libjeg fontconfig gd libxm2 net-snmp-devel mysql-devel openssl-devel libtool php-mbstring php-gd libdbi-dbd-mysql mysql-connector-odbc
2.2 安装rrdtool
wget http://www.express.org/~wrl/rrdtool/rrdtool-1.2.30-1.el5.wrl.i386.rpm
wget http://www.express.org/~wrl/rrdtool/rrdtool-perl-1.2.30-1.el5.wrl.i386.rpm
rpm -ivh rrdtool-1.2.30-1.el5.wrl.i386.rpm rrdtool-perl-1.2.30-1.el5.wrl.i386.rpm
2.3 安装cacti
wget http://www.cacti.net/downloads/cacti-0.8.7e.tar.gz
tar zxvf cacti-0.8.7e.tar.gz
mv cacti-0.8.7e /var/www/html/cacti
l 修改配置文件
vi /var/www/html/cacti/include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
$config['url_path'] = '/cacti/';
l 增加cacti用户
useradd cacti
passwd cacti
su - cacti
crontab -e
*/5 * * * * php /var/www/html/cacti/poller.php > /dev/null
su - root
chow n apache.cacti -R /var/www/html/cacti
chmod 770 -R /var/www/html/cacti
chmod 2770 /var/www/html/cacti
l 配置数据库
/etc/init.d/mysqld start
mysql -u root -p
mysql>create database cacti;
mysql>grant all privileges on cacti.* to cacti@'localhost' identified by 'cacti';
mysql>flush privileges;
mysql>exit
mysql -ucacti -p cacti < /var/www/html/cacti/cacti.sql
l 完成cacti安装
/etc/init.d/httpd start
输入http://ip/cacti按照提示安装。
2.4 安装snmp
被监控机安装
vi /etc/snmp/snmp.conf
sec.name source community
com2sec notConfigUser default public
将"comunity"字段改为你要设置的密码.比如"public".
将“default”改为你想哪台机器可以看到你的snmp信息,如10.10.10.10。
服务端改为127.0.0.1,默认允许所有
# Finally, grant the group read-only access to the systemview view.
# group context sec.model sec.level prefix read write notif
access notConfigGroup "" any noauth exact all none none
这里改成all
# incl/excl subtree mask
view all included .1 80
将该行前面的"#"去掉.
2.5 添加图形
l 增加主机
l 填写信息
l 增加图形
3 安装cacti spine
l spine是用C语言写的用来替代cmd.php产生图片的,默认的POLLER是cmd.php,而spine是使用C写的程序,效率会比cmd.php高几倍!
wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.7e.tar.gz(版本和cacti要一致)
tar -xf cacti-spine-0.8.7e.tar.gz
cd cacti-spine-0.8.7e
./configure
make
make install
l 安装好以后,我们需要再配置一下:
vim /usr/local/spine/etc/spine.conf
DB_Host localhost
DB_Database cacti
DB_User cacti
DB_Pass cacti
DB_Port 3306
最后,在cacti的web配置界面选择使用cacti,进入界面后点击settings,选择poller选项卡,把poller type选择spine即可。
4 监控硬盘IO
l 下载地址:
http://forums.cacti.net/download/file.php?id=5233&sid=a3d87578a1591299ad842588faec0b95
l 进入cacti页面导入模板
导入这两个文件
cacti_data_query_snmp_disk_statistics.xml
cacti_graph_template_disk_io_bytessec.xml
l 安装snmpdiskio-0.9.4.tar.gz
先上传到被监控机
tar zxvf snmpdiskio-0.9.4.tar.gz
cd snmpdiskio-0.9.4
install -m 0755 -o root -g root -p -v snmpdiskio /usr/local/bin/
cp partition.xml cacti目录/resource/snmp_queries/
vi partition.xml
将<index_order>hdDescr:hdName:hdIndex</index_order>
改为<index_order>hdDescr:hdIndex</index_order>
l 将snmpdiskio-0.9.4升级到0.9.6
注:为什么要升级?因为kernel 2.4 和 kernel 2.6 中记录磁盘信息的位置不同。在kernel 2.6中,是通过 /proc/diskstats 来记录;在kernel 2.4 中是通过 /proc/partitions 来记录,并且2.6的文件中的文件结构与2.4 中的结构不一样。因此在使用yum等安装的老版本时,需要升级支持kernel 2.6。
cd /usr/local/bin
vi snmpdiskio #将下面内容替换
#!/bin/bash
# $Id: snmpdiskio,v 1.5 2008/01/31 21:19:50 dverlaeckt Exp $
# snmpdiskio v0.9.6 (c) 2008 Dieter Verlaeckt <dieter.verlaeckt@gmail.com>
# snmpdiskio v0.9.5 (c) 2007 Pablo Destefanis <pdestefanis@gmail.com>
# snmpdiskio v0.9.4 (c) 2006 Mikael Fridh <mikael@meanstreak.se>
# Fields in /proc/partitions (kernel 2.4)
# major minor #blocks name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq
# Fields in /proc/diskstats (kernel 2.6) for disks (i.e. hda)
# major minor name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq
# Fields in /proc/diskstats (kernel 2.6) for partitions (i.e. hda1)
# major minor name rio rsect wio wsect
# InBlocks = sectors written to disk
# OutBlocks = sectors read from disk
# Set default procfile for kernel 2.4
PROCFILE="/proc/partitions"
MODE="linux24"
# Probably kernel 2.6:
if [ -f /proc/diskstats ]; then
PROCFILE=/proc/diskstats
MODE="linux26"
fi
function hdNum()
{
awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++ } END { print num } ' $PROCFILE
}
function hdIndex()
{
awk ' BEGIN { num=0 } $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { num++; print num } ' $PROCFILE
}
function hdDescr()
{
if [ "$MODE" = "linux26" ]; then
awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $3 }' $PROCFILE
else
awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ { printf "%s\n", $4 }' $PROCFILE
fi
}
function hdInBlocks()
{
if [ "$MODE" = "linux26" ]; then
awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f\n", $7 * 512 }
$1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f\n", $10 * 512 } ' $PROCFILE
else
awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f\n", $11 * 512 } ' $PROCFILE
fi
}
function hdOutBlocks()
{
if [ "$MODE" = "linux26" ]; then
awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 7 { printf "%.0f\n", $5 * 512 }
$1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 14 { printf "%.0f\n", $6 * 512 } ' $PROCFILE
else
awk ' $1 ~ /[0-9]+/ && $2 ~ /[0-9]+/ && NF == 15 { printf "%.0f\n", $7 * 512 } ' $PROCFILE
fi
}
function usage()
{
cat <<-EOUSAGE
Usage: $0 <hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks>
EOUSAGE
}
if [ 1 -ne $# ]; then
usage
exit 1
fi
case $1 in
hdNum|hdIndex|hdDescr|hdInBlocks|hdOutBlocks)
$1
;;
'hdNum')
hdNum
;;
'hdIndex')
hdIndex
;;
'hdDescr')
hdDescr
;;
'hdInBlocks')
hdInBlocks
;;
'hdOutBlocks')
hdOutBlocks
;;
*)
usage
exit 1
;;
esac
exit 0
l 修改被监控机的snmpd.conf,在文件的结尾添加如下:
exec .1.3.6.1.4.1.2021.54 hdNum /bin/sh /usr/local/bin/snmpdiskio hdNum
exec .1.3.6.1.4.1.2021.55 hdIndex /bin/sh /usr/local/bin/snmpdiskio hdIndex
exec .1.3.6.1.4.1.2021.56 hdDescr /bin/sh /usr/local/bin/snmpdiskio hdDescr
exec .1.3.6.1.4.1.2021.57 hdInBlocks /bin/sh /usr/local/bin/snmpdiskio hdInBlocks
exec .1.3.6.1.4.1.2021.58 hdOutBlocks /bin/sh /usr/local/bin/snmpdiskio hdOutBlocks
l 增加图形
5 自定义OID通过脚本进行画图
5.1 原理介绍
l 在被监控机编写一个脚本,想监控什么就写什么吧,我这里只写了一个输出随机数的简单脚本
#!/bin/bash
echo $RANDOM
l 让snmpd可以获到脚本的输出
在snmpd.conf里(被监控机)添加我们自己写的脚本:
exec .1.3.6.1.4.1.2021.1 test /root/test.sh
这行配置是什么意思呢,我们切开来说明:
exec 执行函数
.1.3.6.1.4.1.2021.1 这是一个自定义的snmp OID,可以这样理解吧.1.3.6.1.4.1.2021这一段是固定的,已经被一些组织注册的,后面的可以自己使用,前提是要确认这个OID没有被其它程序占用,可以测试一下,例如:
/usr/local/net-snmp/bin/snmpwalk -c public -v 2c localhost .1.3.6.1.4.1.2021.2
UCD-SNMP-MIB::prIndex.1 = INTEGER: 1
UCD-SNMP-MIB::prIndex.2 = INTEGER: 2
UCD-SNMP-MIB::prIndex.3 = INTEGER: 3
UCD-SNMP-MIB::prNames.1 = STRING: mountd
UCD-SNMP-MIB::prNames.2 = STRING: ntalkd
UCD-SNMP-MIB::prNames.3 = STRING: sendmail
UCD-SNMP-MIB::prMin.1 = INTEGER: 0
UCD-SNMP-MIB::prMin.2 = INTEGER: 0
UCD-SNMP-MIB::prMin.3 = INTEGER: 1
UCD-SNMP-MIB::prMax.1 = INTEGER: 0
UCD-SNMP-MIB::prMax.2 = INTEGER: 4
UCD-SNMP-MIB::prMax.3 = INTEGER: 10
UCD-SNMP-MIB::prCount.1 = INTEGER: 0
UCD-SNMP-MIB::prCount.2 = INTEGER: 0
UCD-SNMP-MIB::prCount.3 = INTEGER: 0
UCD-SNMP-MIB::prErrorFlag.1 = INTEGER: 1
UCD-SNMP-MIB::prErrorFlag.2 = INTEGER: 0
UCD-SNMP-MIB::prErrorFlag.3 = INTEGER: 1
UCD-SNMP-MIB::prErrMessage.1 = STRING: No mountd process running.
UCD-SNMP-MIB::prErrMessage.2 = STRING:
UCD-SNMP-MIB::prErrMessage.3 = STRING: Too few sendmail running (# = 0)
UCD-SNMP-MIB::prErrFix.1 = INTEGER: 0
UCD-SNMP-MIB::prErrFix.2 = INTEGER: 0
UCD-SNMP-MIB::prErrFix.3 = INTEGER: 0
UCD-SNMP-MIB::prErrFixCmd.1 = STRING:
UCD-SNMP-MIB::prErrFixCmd.2 = STRING:
UCD-SNMP-MIB::prErrFixCmd.3 = STRING:
这样说明.1.3.6.1.4.1.2021.2 已经有程序在用了,再看看下一个:
/usr/local/net-snmp/bin/snmpwalk -c public -v 2c localhost .1.3.6.1.4.1.2021.3
UCD-SNMP-MIB::ucdavis.3 = No Such Object available on this agent at this OID
如果看见这样的信息,那么恭喜,这个OID可以使用
接着看:
test 是一个名称而已
/root/test.sh 脚本的绝对路径
重启snmpd
用snmpwalk命令测试获取被监控机上刚才定义的OID数据:
/usr/local/net-snmp/bin/snmpwalk -c public -v 2c localhost .1.3.6.1.4.1.2021.1
UCD-SNMP-MIB::ucdavis.1.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdavis.1.2.1 = STRING: "test"
UCD-SNMP-MIB::ucdavis.1.3.1 = STRING: "/root/test.sh"
UCD-SNMP-MIB::ucdavis.1.100.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.1.101.1 = STRING: "27230"
UCD-SNMP-MIB::ucdavis.1.102.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.1.103.1 = ""
l 命令后面那一串OID就是刚才在被监控机上snmpd.conf中配置的OID,很明显UCD-SNMP-MIB::ucdavis.1.101.1 = STRING: "27230" 这一段是我们需要的数据,然后再进一步精确获取出来,看见我标的蓝色字体的数字吧,这是它的准确OID,把这段数字加在原本的OID后面,再来获取一次看看:
/usr/local/net-snmp/bin/snmpwalk -c public -v 2c localhost .1.3.6.1.4.1.2021.1.101.1
UCD-SNMP-MIB::ucdavis.1.101.1 = STRING: "12995"
这样我们就可以准确获取到我们想要的数据了,记住这串OID,我们要从cacti里生成数据源和制图了
l 添加图形
5.2 实际工作脚本范例
5.2.1 监控MobileStkServer程序CPU占有率
#!/bin/bash
while [ "1" = "1" ];
do
sleep 10s
echo $mob
while [ "1" = "1" ];
do
mob=`top -n 1 | grep MobileStkServer | awk -F" " '{ print $9 }'`
if [ ! -z $mob ];
then
break
fi
done
done
5.2.2 监控MobileStkServer程序内存占有率
#!/bin/bash
while [ "1" = "1" ];
do
sleep 10s
echo $mob
while [ "1" = "1" ];
do
mob=`top -n 1 | grep MobileStkServer | awk -F" " '{ print $9 }'`
if [ ! -z $mob ];
then
break
fi
done
done
5.2.3 监控MobileStkServer程序线程数
#!/bin/bash
while [ "1" = "1" ];
do
sleep 40s
mob=`netstat -an | grep 12345 | grep -v 0.0.0.0 | wc -l`
echo $mob
done