开源网络监控软件 Cacti 的简单应用
常见应用:
服务器资源: CPU 、内存、磁盘、进程、连接数等
服务器类型: WEB Mail FTP 、数据库、中间件
网络接口:流量、转发速度、丢包率
设备运行状态:风扇、电源、温度
机房运行环境:电流、电压、温湿度
<?xml:namespace prefix = o />

 

快速安装 cacti
测试平台: rhel 5.4           cacti 版本: 0.8.7g
运行环境: LAMP 环境 

 

1. 安装依赖包:
yum -y install httpd php php-mysql php-snmp mysql mysql-server perl-DBD-MySQL php-pdo php-pear net-snmp net-snmp-libs net-snmp-utils net-snmp-devel ruby ruby-devel
2. 设置开机启动 snmpd,httpd,mysqld 服务
3. 安装 rrdtools
到这里下载最新 RRDTools rpm 包: http://www.express.org/~wrl/rrdtool/
yum -y localinstall --nogpgcheck rrdtool-*.rpm
4. 配置 snmp
#  vi /etc/snmp/snmp.conf

 

com2secnotConfigUser default public
改为: com2secnotConfigUser 127.0.0.1 public    ( 这是监控本地,被监控主机设置 )
access  notConfigGroup “”   any   noauth    exact  systemview  none none
改为: access  notConfigGroup “” any noauth   exact   all none  none
#view all    included  .1         80
将前面的 # 注释去掉。

 

重新启动 snmp 服务
5. 安装 cacti 数据以及模板
配置 mysql 数据库
# mysql
Mysql > CREATE DATABASE cacti;
Mysql > grant all privileges on cacti.* to cacti@localhost identified by cacti ;
Mysql > grant all privileges on cacti.* to cacti@127.0.0.1 identified by cacti ;
Mysql > grant all privileges on cacti.* to cacti@192.168.0.0 identified by cacti ;
Mysql >flush privileges;
Mysql >quit
# tar xzvf  cacti-0.8.7g.tar.gz
# mv cacti-0.8.7g/ /var/www/html/cacti
# mysql -ucacti -pcacti cacti < /var/www/html/cacti/cacti.sql
vim /var/www/html/cacti/include/config.php
修改成:
$database_type= mysql ;
$database_default = cacti ;
$database_hostname = localhost ;
$database_username = cacti ;
$database_password = cacti ;

 

useradd cacti
passwd cacti
cd   /var/www/html/cacti/
chown -R cacti rra/ log/

 

# crontab -e
*/5 * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1

 

打开浏览器,访问 http://YOURIP/cacti 并 开始安装。

 

注意:
1 vim /etc/httpd/conf/httpd.conf
DirectoryIndex   index.php  index.html  index.html.var
2 selinux=permissive
Service iptables stop

 

第一次登陆 User Name Password 都为 admin ,并且需要强行修改。
第一次绘图比较慢。需要一定的时间,请耐心等待。

 

 

四、cacti 常用插件安装
要安装别的插件前 , 先要安装 cacti 的一个 patch -- Plugin Architecture, 才能支持插件 PA 2.8 = cacti 0.8.7g
# tar xvf cacti-plugin-0.8.7g-PA-v2.8.tar.gz
# cp -R cacti-plugin-arch/* /var/www/html/cacti/
cd /var/www/html/cacti/
mysql -ucacti -pcacti cacti <pa.sql
patch -p1 -N <cacti-plugin-0.8.7g-PA-v2.8.diff
vim  include/config.php
修改为 $url_path = /cacti/ ;

 

回到 web 管理界面,找到 console—Utilities—User Management—admin

注意保存。
安装常用插件 Monitor,Settings,thold
# tar zxvf monitor-latest.tgz
# tar zxvf settings-latest.tgz
# tar zxvf thold-latest.tgz
# mv monitor-0.9  /var/www/html/cacti/plugins/monitor
# mv settings-0.6  /var/www/html/cacti/plugins/settings
# mv thold-0.41   /var/www/html/cacti/plugins/thold
回到 web 管理界面,找到 console—Configuration—Plugin Management ,你将看到

 

登陆安装启用对应的插件即可。
注意事项:
1. 官方文档说要修改 /var/www/html/cacti/include/global.php  里面的内容。我试验后发现,修改后反而不能找到插件。不修改即可找到插件。
2. 在安装 thold 时,可能会出现 You are missing a dependency for thold, please install the 'Settings' plugin. 这样的警告。只需要先安装 settings 插件就 OK  

 

五、Cacti MSN,mail, 飞信报警设置
1.E-mail 报警设置。
2.MSN 报警设置。
3. 飞信报警设置。

 

1 E-mail 报警设置
回到 web 管理界面,找到 console—Configuration—Settings—Alerting/Thold
并保存。
找到 console—Configuration—Settings—Mail/DNS
并保存。
注意:此邮箱为为测试邮箱,点击右边 ,此邮箱将受到 test mail
并且显示如下对话框。 Root@localhost 为任意地址,服务器不予与验证。
如果 SMTP 换为 Sendmail ,将可能出现错误信息。所有,最好选择 SMTP 。记得保存。

 

 

 

2.MSN 报警设置
1 )修改 cacti 插件 thold 目录下的 thold_functions.php
vim /var/www/html/cacti/plugins/thold/thold_functions.php
thold_mail 找到文件中的第一个这一句
if (trim($alert_emails) != "")
thold_mail($alert_emails, '', $subject, $msg, $file_array);
然后下面添加
exec("echo $subject >>/var/www/html/cacti/plugins/thold/alert.log");
exec("sh /var/www/html/cacti/plugins/thold/sendmessage.sh");
保存退出
3. 然后编写个 sendmessage.sh 脚本,在 /var/www/html/cacti/plugins/thold/ 这个目录下
脚本内容
======================================================================
#!/bin/sh
echo `echo -n "date:"&&date +%Y-%m-%d-%H:%M` >> /var/www/html/sendMsg/msn.txt.1
cat /var/www/html/cacti/plugins/thold/alert.log >>/var/www/html/sendMsg/msn.txt.1
now=`date +%Y-%m-%d-%H:%M`
SA=(test)
sendMsg()
{
num=0
while [ $num -lt 1 ];
do
wget —post-data "sender=pingsun2012@hotmail.com&password=123456&recipient=${1}&message=${2}" http://127.0.0.1/sendMsg/index.php -O /var/www/html/sendMsg/index.php.1 >/dev/null 2>&1
if [ -f /var/www/html/sendMsg/index.php.1 ]; then
if cat /var/www/html/sendMsg/index.php.1 |grep -i successfully >/dev/null 2>&1;then
num=1
elif cat /var/www/html/sendMsg/index.php.1 |grep -i "The user appears to be offline" >/dev/null 2>&1;then
num=1
echo "The user is offline."
exit 0
elif cat /var/www/html/sendMsg/index.php.1 |grep -i "Something went wrong trying to connect to the server" >/dev/null 2>&1;then
num=1
echo "MSN server is online."
exit 0
else
num=0
fi
rm -f /var/www/html/sendMsg/index.php.1
else
num=0
fi
done
}
mv /var/www/html/sendMsg/msn.txt /var/www/html/sendMsg/bak/msn$now.txt -f 1>/dev/null 2>&1
mv /var/www/html/sendMsg/msn.txt.1 /var/www/html/sendMsg/msn.txt -f 1>/dev/null 2>&1
rm /var/www/html/cacti/plugins/thold/alert.log -f 1>/dev/null 2>&1
for i in 0
do
if cat /var/www/html/sendMsg/msn.txt ; then
messages=`cat /var/www/html/sendMsg/msn.txt `
sendMsg "${msnaddr[$i]}" "$messages"
else
continue
fi
done
===========================================================
然后保存退出。 ( 发送和接受者必须互为好友 )
4. 手动运行 sendmessage.sh 这个脚本,如果没有问题,应该可以收到消息。
4. 飞信报警功能实现
1. 下载最新版飞信机器人。
根据情况下载,因为会经常更新。 下载两个一个是 fetion 程序,一个是 Linux 下的库包 linuxso-20101113.tar.gz
2. 飞信机器人放置位置
cd /var/www/html
mkdir sendsms
cp -a linuxso_20101113 /var/www/html/sendsms/linuxso
cp fetion /var/www/html/sendsms/linuxso/fetion
chmod 777 -R  /var/www/html/sendsms/linuxso
thold_mail 找到文件中的第一个这一句
if (trim($alert_emails) != "")
thold_mail($alert_emails, '', $subject, $msg, $file_array);
然后下面添加
exec("echo $subject >>/var/www/html/cacti/plugins/thold/alert.log");
exec("/var/www/html/cacti/plugins/thold/sendsms.sh");
编写 sendsms.sh 脚本,内容如下:
===========================================================================
#!/bin/sh
#send sms by fetion
if [ ! -e "/var/www/html/cacti/plugins/thold/alert.log" ];then
echo "Usage:alert.log does not exist"
exit
fi
if [ -n "`cat /var/www/html/cacti/plugins/thold/alert.log`" ]; then
admin="150382XXXX"
echo "sms $admin "`cat /var/www/html/cacti/plugins/thold/alert.log` >>/var/www/html/cacti/plugins/thold/sms.txt
echo "exit" >> /var/www/html/cacti/plugins/thold/sms.txt
else
echo "Usage:no alert"
exit
fi
if [ -n "`cat /var/www/html/cacti/plugins/thold/sms.txt`" ] ; then
LD_LIBRARY_PATH=/var/www/html/sendsms/linuxso /var/www/html/sendsms/linuxso/fetion --mobile=158038XXX90 --pwd=123xxx --to=$admin --file-utf8=/var/www/html/cacti/plugins/thold/sms.txt
rm -f /var/www/html/cacti/plugins/thold/sms.txt 1>/dev/null 2>&1
rm -f /var/www/html/cacti/plugins/thold/alert.log 1>/dev/null 2>&1
else
echo "Usgae:no message to send"
exit
fi
=====================================================================
保存,退出。测试一下。
可以新建 alert.log 以及 sms.txt, 其中在 alert.log 中添加一个内容。
然后运行脚本
./sendsms.sh
会返回以下结果,表示成功。 同时对方收到短信。
[root@station1 thold]# ./sendsms.sh
图形验证码已经生成,文件名为 :15803826990.jpg 请识别后输入图形验证码 :
Nm8p     # 一般第一次会让你输入验证码。这个图片,在你的 fetion 的目录下。
您输入的识别码是 :nm8p
SIP-C/4.0 280 Send SMS OK
T: sip:759885962@fetion.com.cn;p=9283
I: 3
Q: 1 M
L: 115
D: Mon, 22 Nov 2010 15:28:45 GMT
XI: 516723D0200B5C60F8747A8E9B4CFF55
[root@station1 thold]#
5. 本次实验,难点是飞信这个地方,网上没有最新的解决方案。这里经过好久测试,最终测试出可行的办法。如果有问题,欢迎大家反馈。

 

 

六、常见故障排除
安装完毕在浏览器上无法看到数据的 png 图片。看 apache log
如果出现:
 ========================
[Thu Feb 09 15:12:24 2006] [error] [client 127.0.0.1] File does not exist: /var/www/html/favicon.ico
ERROR: opening '/var/www/html/cacti/rra/localhost_mem_buffers_3.rrd': Permission denied
    PS: 解决办法:关闭 selinux ,即可解决问题。
PS :以上无法获取数据图大多和poller.php cmd.php 权限有关。
cacti 有图没有数据时,而且状态为nan 的错误
PS :这个很可能是snmp 的问题, 执行以下命令,没有得到如图的结果。就说明snmp 不支持64 MIB 库。重新编译安装snmp
# snmpwalk -c public -v 2c 127.0.0.1 IF-MIB::ifHCInOctets
IF-MIB::ifHCInOctets.1 = Counter64: 7437357
IF-MIB::ifHCInOctets.2 = Counter64: 353773IF-MIB::ifHCInOctets.3 = Counter64: 0
PS :被监控主机无法获得snmp 信息,还有可能是对方主机snmp 版本和当前主机的snmp 版本不一致导致的。
PS rrdtools 版本要一致,特别是在升级cacti 时候。版本不一致,可能rra 数据格式不同。就无法处理。

 

排错思路
1、 查看 log 下的日志文件。一般那里会有提示
2、测试 SNMP 是不是工作正常 snmpwalk -v 2c -c public hostIP   if 正常的话会出现一些数据。不正常会出现一些错误,也会有对应的错误提示。
3、 自动运行 poller.php 没有。有没有加入 cacti 的的用户。。有没有给 cacti 用户写入 rra/ log/ 的权限。。
4、 crontab u cactiuser e cactiuser 加上自动运行 poller.php 的任务:
*/5     *       *       *       *      
root    /usr/local/bin/php /usr/local/share/cacti/poller.php /dev/null 2>&1
       5 分钟刷新一次数据。你也可以根据需要还设置。
5、 cacti 目录里的 cmd.php poller.php 文件加下运行的权限。

 

本文摘自 http://www.pcwind.net/cactimsn.html ,严重感谢“疯狂的小蜗牛”同学辛勤劳动成果。