cacti是什么?

1. cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户。因此,snmprrdtoolcacti的关键。Snmp关系着数据的收集,rrdtool关系着数据存储和图表的生成。


2. Mysql配合PHP程序存储一些变量数据并对变量数据进行调用,如:主机名、主机ipsnmp团体名、端口号、模板信息等变量。


3. snmp抓到数据不是存储在mysql中,而是存在rrdtool生成的rrd文件中(在cacti根目录的rra文件夹下)。rrdtool对数据的更新和存储就是对rrd文件的处理,rrd文件是大小固定的档案文件(Round Robin Archive),它能够存储的数据笔数在创建时就已经定义。


cacti的工作流程可以用下图表示:

200319849.jpg


cacti的安装:

配置本地yum

[root@cacti ~]# vim /etc/yum.repos.d/rhel-debuginfo.repo

[rhel-Server]

name=Red Hat Enterprise Linux Server

baseurl=file:///mnt/cdrom/Server

enabled=1

gpgcheck=1

gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release



[root@cacti ~]# mkdir /mnt/cdrom

[root@cacti ~]# mount /dev/cdrom/mnt/cdrom/

mount: block device /dev/cdrom iswrite-protected, mounting read-only


[root@cacti ~]# yum install httpd php php-mysql php-snmp mysql-serverperl-DBD-MySQL php-pdo net-snmp net-snmp-libs net-snmp-utils net-snmp-develruby –y


[root@cacti ~]# service httpd start

[root@cacti ~]# chkconfig httpd on


[root@cacti ~]# service mysqld start

[root@cacti ~]# chkconfig mysqld on


[root@cacti ~]# mysqladmin -u root -ppassword '123'


[root@cacti ~]# mysql -u root –p

mysql> CREATE DATABASE cacti;

mysql> grant all privileges on cacti.* to cacti@localhostidentified by 'cacti';

mysql> grant all privileges on cacti.* to cacti@127.0.0.1identified by 'cacti';

mysql> grant all privileges on cacti.* tocacti@192.168.2.0 identified by 'cacti';

mysql> flush privileges;

mysql> quit


上传需要用的软件

200436989.jpg

软件资料下载地址http://down.51cto.com/data/994246

cacti-0.8.7g.tar.gz cacti的主程序

cacti-plugin-0.8.7g-PA-v2.8.tar.gz 插件管理器

feition.tar.bz2 用于飞信报警的程序

monitor-latest.tgz 实现只管监测的插件

sendMsg.zip 发送MSN消息的应用

settings-latest.tgz 增强功能的插件,例如发送电子邮件

thold-latest.tgz 实现达到预值告警的插件



[root@cacti software]# yum localinstallrrdtool* --nogpgcheck –y


安装cacti的主程序

[root@cacti software]# tar -zxvf cacti-0.8.7g.tar.gz -C /var/www/html/

[root@cacti software]# cd /var/www/html/

[root@cacti html]# mv cacti-0.8.7g/ cacti


[root@cacti html]# service snmpd start

[root@cacti html]# chkconfig snmpd on

[root@cacti html]# vim /etc/snmp/snmpd.conf

添加一行view systemview included .1.3.6.1.2.1.25.1.6 /使能监控tcp流量



编辑计划任务

[root@cacti html]# crontab -e

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


导入数据库

[root@cacti html]# mysql -u root -p cacti</var/www/html/cacti/cacti.sql

输入mysql管理员口令


编辑cacti连接数据库的文件

[root@cacti html]# vim cacti/include/config.php

29 $database_username = "cacti";

30 $database_password = "cacti";


新建一个用户cacti,口令也为cacti

[root@cacti html]# useradd cacti

[root@cacti html]# passwd cacti


[root@cacti html]# cd cacti/

[root@cacti cacti]# chown -R cacti rra log


此时就可以通过页面登录cacti了,用户和口令都为admin

202113388.jpg

202119859.jpg

202129496.jpg

202132863.jpg

202137388.jpg

202143661.jpg



实现对apache的监控

[root@cacti software]# cd ApacheStats_0.8.2/

[root@cacti ApacheStats_0.8.2]#ll

total 116

-rw-r--r-- 1 root root 102879 Oct 27 18:51 cacti_host_template_webserver_-_apache.xml

-rw-r--r-- 1 root root 4108 Oct 27 18:51 ss_apache_stats.php

[root@cacti ApacheStats_0.8.2]#cp ss_apache_stats.php /var/www/html/cacti/scripts/

[root@cacti ApacheStats_0.8.2]#chown 1000:users /var/www/html/cacti/scripts/ss_apache_stats.php


cacti_host_template_webserver_-_apache.xml 为下面要导入的模版文件



通过控制台导入模版

203021662.jpg

203040521.jpg

203409619.jpg

203424241.jpg

203705255.jpg

修改apache的配置文件

[root@cacti ApacheStats_0.8.2]#cd

[root@cacti ~]# vim/etc/httpd/conf/httpd.conf


217 ExtendedStatus On /打开这一行


903 #<Location /server-status>

904 # SetHandler server-status

905 # Order deny,allow

906 # Deny from all

907 # Allow from .example.com

908 #</Location>

909 <Location /server-status>

910 SetHandler server-status

911 Order deny,allow

912 Deny from all

913 Allow from all

914 </Location>

apache重启

[root@cacti ~]# service httpd restart

203719676.jpg


在安装插件之前要先安装插件管理器

[root@cacti cacti]# cd /root/software/

[root@cacti software]# tar -zxvf cacti-plugin-0.8.7g-PA-v2.8.tar.gz

[root@cacti software]# cp -R cacti-plugin-arch/* /var/www/html/cacti/

[root@cacti software]# cd /var/www/html/cacti/

[root@cacti cacti]# mysql -u root -pcacti<pa.sql

[root@cacti cacti]# patch -p1 -N<cacti-plugin-0.8.7g-PA-v2.8.diff

[root@cacti cacti]# vim include/config.php

42 $url_path = "/cacti/"; /修改这一行的路径



安装插件

[root@cacti cacti]# cd /root/software/

[root@cacti software]# tar -zxvf monitor-latest.tgz

[root@cacti software]# tar -zxvf settings-latest.tgz

[root@cacti software]# tar -zxvf thold-latest.tgz

[root@cacti software]# mv monitor-0.9/ /var/www/html/cacti/plugins/monitor

[root@cacti software]# mv settings-0.6/ /var/www/html/cacti/plugins/settings

[root@cacti software]# mv thold-0.41/ /var/www/html/cacti/plugins/thold


在控制台启用插件

203329517.jpg

203343804.jpg

203354534.jpg


配置支持发送msn消息

[root@cacti software]# unzip sendMsg.zip

[root@cacti software]# mv sendMsg/ var/www/html/


[root@cacti software]# vim /var/www/html/cacti/plugins/thold/thold_functions.php

添加下面这两行

560 exec("echo $subject>>/var/www/html/cacti/plugins/thold/alert.log");

561 exec("sh /var/www/html/cacti/plugins/thold/sendmessage.sh");


编写个sendmessage.sh脚本,在/var/www/html/cacti/plugins/thold/这个目录下(实现自动发送)

[root@cacti software]# vim /var/www/html/cacti/plugins/thold/sendmessage.sh

脚本内容


#!/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)

msnaddr=(接收信息的帐号)

sendMsg()

{

num=0

while [ $num -lt 1 ];

do

wget ?post-data"sender=发送信息的帐号&password=密码&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/null2>&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



配置支持飞信报警

[root@cacti software]# tar -jxvf feition.tar.bz2

[root@cacti software]# mkdir -pv /var/www/html/sendsms/linuxso

[root@cacti software]# cp linuxso_20101113/* /lib/

[root@cacti software]# mv linuxso_20101113/* /var/www/html/sendsms/linuxso/

[root@cacti software]# vim /var/www/html/cacti/plugins/thold/thold_functions.php

添加这两行

562 exec("echo $subject>>/var/www/html/cacti/plugins/thold/alert.log");

563 exec("/var/www/html/cacti/plugins/thold/sendsms.sh");


[root@cacti software]# vim /var/www/html/cacti/plugins/thold/sendsms.sh

#!/bin/sh

#send sms by fetion

#Write by pingsun2010@hotmail.com

if [ ! -e"/var/www/html/cacti/plugins/thold/alert.log" ];then

echo "Usage:alert.log does notexist"

exit

fi

if [ -n "`cat/var/www/html/cacti/plugins/thold/alert.log`" ]; then

admin="接收信息的帐号?

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=发送信息的账户 --pwd=密码 --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


[root@cacti thold]# chmod a+x send*

给新建的脚本添加可执行权限


vim /var/www/html/cacti/plugins/thold/alert.log

随便写点儿内容,当飞信程序执行的时候会自动删除里面的内容

可以新建alert.log以及sms.txt,其中在alert.log中添加一个内容。然后手工运行脚本进行测试,需要保证本机能正常接入互联网。


目前cacti的环境已经搭建完成,cacti的使用还需要好好学习……