cacti简介
    Cacti是基于PHP/MySQL的开源网络监控及绘图工具,它通过SNMP获取数据并使用RRDTool予以存储,然后在需要时利用RRDTool绘图引擎绘图并通过php展示。
   Cacti的操作流程很简单:获取数据-->存储数据-->展示数据。Cacti通过poller获取数据。poller是一个通过调度的方式每隔一段时间(通常为5分钟)执行的程序,它通常通过SNMP来获取远程主机上的数据。


cacti的安装及配置
本文档中的平台是RHEL5.8用到的程序为:
cacti-0.8.8a (安装路径:/web/vhosts/cacti)
httpd-2.4.2 (安装路径:/usr/local/apache)
php-5.4.4 (安装路径:/usr/local/php)
MySQL-5.5.25a (安装路径:/usr/local/mysql)
rrdtool-1.4.7.tar.gz(安装路径:/usr/local/rrdtool)

一、 编译安装lamp平台

(这里不再给出详细步骤,若没有lamp平台以下是一个脚本,执行即可;若已经安装了则跳过此步骤,从“二” 开始即可)
准备工作:
    1、若想支持mcrypt扩展,下载libmcrypt-2.5.7-5.el5.i386.rpm  libmcrypt-devel-2.5.7- 5.el5.i386.rpm 并安装 rpm -ivh libmcrypt-*  
    2、提供了一个httpd服务的脚本,如下,放到/root目录下,命名为httpd.sh
注意:要求php在configure时指定了--enable-sockets选项,否则,cacti将无法运行。

 
  
  1. #!/bin/bash  
  2. #  
  3. # httpd        Startup script for the Apache HTTP Server  
  4. #  
  5. # chkconfig: - 85 15  
  6. # description: Apache is a World Wide Web server.  It is used to serve \  
  7. #          HTML files and CGI.  
  8. # processname: httpd  
  9. # config: /etc/httpd/conf/httpd.conf  
  10. # config: /etc/sysconfig/httpd  
  11. # pidfile: /var/run/httpd.pid  
  12.  
  13. # Source function library.  
  14. . /etc/rc.d/init.d/functions  
  15.  
  16. if [ -f /etc/sysconfig/httpd ]; then  
  17.         . /etc/sysconfig/httpd  
  18. fi  
  19.  
  20. # Start httpd in the C locale by default.  
  21. HTTPD_LANG=${HTTPD_LANG-"C"}  
  22.  
  23. # This will prevent initlog from swallowing up a pass-phrase prompt if  
  24. # mod_ssl needs a pass-phrase from the user.  
  25. INITLOG_ARGS="" 
  26.  
  27. # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server  
  28. # with the thread-based "worker" MPM; BE WARNED that some modules may not  
  29. # work correctly with a thread-based MPM; notably PHP will refuse to start.  
  30.  
  31. # Path to the apachectl script, server binary, and short-form for messages.  
  32. apachectl=/usr/local/apache/bin/apachectl  
  33. httpd=${HTTPD-/usr/local/apache/bin/httpd}  
  34. prog=httpd 
  35. pidfile=${PIDFILE-/var/run/httpd.pid}  
  36. lockfile=${LOCKFILE-/var/lock/subsys/httpd}  
  37. RETVAL=0 
  38.  
  39. start() {  
  40.         echo -n $"Starting $prog: "  
  41.         LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS  
  42.         RETVAL=$?  
  43.         echo  
  44.         [ $RETVAL = 0 ] && touch ${lockfile}  
  45.         return $RETVAL  
  46. }  
  47.  
  48. stop() {  
  49.     echo -n $"Stopping $prog: "  
  50.     killproc -p ${pidfile} -d 10 $httpd  
  51.     RETVAL=$?  
  52.     echo  
  53.     [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}  
  54. }  
  55. reload() {  
  56.     echo -n $"Reloading $prog: "  
  57.     if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then  
  58.         RETVAL=$?  
  59.         echo $"not reloading due to configuration syntax error"  
  60.         failure $"not reloading $httpd due to configuration syntax error"  
  61.     else  
  62.         killproc -p ${pidfile} $httpd -HUP  
  63.         RETVAL=$?  
  64.     fi  
  65.     echo  
  66. }  
  67.  
  68. # See how we were called.  
  69. case "$1" in  
  70.   start)  
  71.     start  
  72.     ;;  
  73.   stop)  
  74.     stop  
  75.     ;;  
  76.   status)  
  77.         status -p ${pidfile} $httpd  
  78.     RETVAL=$?  
  79.     ;;  
  80.   restart)  
  81.     stop  
  82.     start  
  83.     ;;  
  84.   condrestart)  
  85.     if [ -f ${pidfile} ] ; then  
  86.         stop  
  87.         start  
  88.     fi  
  89.     ;;  
  90.   reload)  
  91.         reload  
  92.     ;;  
  93.   graceful|help|configtest|fullstatus)  
  94.     $apachectl $@  
  95.     RETVAL=$?  
  96.     ;;  
  97.   *)  
  98.     echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"  
  99.     exit 1  
  100. esac  
  101.  
  102. exit $RETVAL 

接下来就可以执行脚本了

 
  
  1. #!/bin/bash  
  2. #  
  3. <<mark 
  4. prepare the source packages  
  5. apr-1.4.6.tar.bz2    
  6. apr-util-1.4.1.tar.bz2  
  7. httpd-2.4.2.tar.bz2   
  8. cmake-2.8.8.tar.gz  
  9. mysql-5.5.25a.tar.gz  
  10. libmcrypt-2.5.7-5.el5.i386.rpm  
  11. libmcrypt-devel-2.5.7-5.el5.i386.rpm  
  12. php-5.4.4  
  13. mark  
  14. ################compileing httpd######################  
  15. #yum -y groupinstall "Development Libraries" "Development Tools" "X Software Development"  
  16. #setenforce 0  
  17. #mkdir /source  
  18. SOURCE=/source  
  19. SysDir=/etc/rc.d/init.d  
  20. PathDir=/etc/profile  
  21.  
  22. cd $SOURCE  
  23. wget http://apache.etoak.com//apr/apr-1.4.6.tar.bz2  
  24. wget http://apache.etoak.com//apr/apr-util-1.4.1.tar.bz2  
  25. wget http://apache.etoak.com//httpd/httpd-2.4.2.tar.bz2  
  26. wget http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz  
  27. wget http://anduin.linuxfromscratch.org/sources/BLFS/svn/m/mysql-5.5.25a.tar.gz  
  28. wget http://downloads.php.net/stas/php-5.4.4.tar.bz2   
  29.  
  30. tar xf apr-1.4.6.tar.bz2    
  31. cd apr-1.4.6  
  32. ./buildconf  
  33. ./configure --prefix=/usr/local/apr  
  34. make  
  35. make install  
  36.  
  37. cd $SOURCE  
  38. tar xf apr-util-1.4.1.tar.bz2  
  39. cd apr-util-1.4.1  
  40. ./buildconf --with-apr=$SOURCE/apr-1.4.6  
  41. ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr  
  42. make  
  43. make install  
  44.  
  45. cd $SOURCE  
  46. yum -y install pcre-devel  
  47. tar xf httpd-2.4.2.tar.bz2   
  48. cd httpd-2.4.2  
  49. ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-suexec --enable-ssl --enable-rewrite --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-modules-shared=most --enable-mpms-shared=all   
  50. make  
  51. make install  
  52.  
  53. cd $SOURCE  
  54. echo "PidFile  \"/var/run/httpd.pid\" " >> /etc/httpd/httpd.conf  
  55. cp /root/httpd.sh   $SysDir/httpd  
  56. chmod +x $SysDir/httpd  
  57. chkconfig --add httpd  
  58. service httpd start  
  59. echo "PATH=\$PATH:/usr/local/apache/bin" >> $PathDir  
  60. export PATH=$PATH:/usr/local/apache/bin  
  61. echo "MANPATH  /usr/local/apache/man " >> /etc/man.config  
  62. ln -sv /usr/local/apache/include /usr/include/httpd  
  63. service httpd start  
  64.  
  65. #########################compileing mysql###################################  
  66. cd $SOURCE  
  67. tar xf cmake-2.8.8.tar.gz  
  68. cd cmake-2.8.8  
  69. ./bootstrap  
  70. make  
  71. make install  
  72.  
  73. cd $SOURCE  
  74. groupadd -r mysql  
  75. useradd -g mysql -r -d /data/mysql mysql  
  76. tar xf mysql-5.5.25a.tar.gz  
  77. cd mysql-5.5.25a  
  78. cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1  -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLE_PROFILING=1    
  79. make  
  80. make install  
  81.    
  82. cd /usr/local/mysql  
  83. chown -R :mysql .  
  84. mkdir -pv /data/mysql  
  85. chown -R mysql:mysql /data/  
  86. cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf  
  87. cp /usr/local/mysql/support-files/mysql.server $SysDir/mysqld  
  88. chmod +x $SysDir/mysqld  
  89. chkconfig --add mysqld  
  90. scripts/mysql_install_db --user=mysql --datadir=/data/mysql  
  91. sed -i '/\[mysqld\]/a \datadir = /data/mysql' /etc/my.cnf  
  92. sed -i 's/thread_concurrency = 8/thread_concurrency = 2/g' /etc/my.cnf  
  93. echo "PATH=\$PATH:/usr/local/mysql/bin" >> $PathDir  
  94. export PATH=$PATH:/usr/local/mysql/bin  
  95. echo "MATHPATH /usr/local/mysql/man" >> /etc/man.config  
  96. ln -sv /usr/local/mysql/include /usr/include/mysql  
  97. echo " /usr/local/mysql/lib " >> /etc/ld.so.conf.d/mysql.conf  
  98. ldconfig  
  99. service  mysqld start  
  100. cd  
  101. ##########################compiling php#########################  
  102. cd $SOURCE  
  103. tar xf  php-5.4.4.tar.bz2  
  104. cd  php-5.4.4  
  105. ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-sockets  
  106. cd $SOURCE  
  107. make  
  108. make install  
  109. cd $SOURCE  
  110. cp php-5.4.4/php.ini-production /etc/php.ini  
  111. echo "AddType application/x-httpd-php  .php " >> /etc/httpd/httpd.conf  
  112. echo "AddType application/x-httpd-php-source  .phps" >> /etc/httpd/httpd.conf  
  113. sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' /etc/httpd/httpd.conf  
  114. service httpd restart  
  115. cd  
  116. echo "The End!"

 OK,此时lamp平台架构好了

二、 安装配置cacti

1、cacti虚拟主机
在LAMP平台上,专门为cacti准备了一个虚拟主机cacti.magedu.com,其配置如下:

 
  
  1. #vim /etc/httpd/httpd.conf
  2. 启用虚拟主机
  3. # vim /etc/httpd/extra/httpd-vhosts.conf  //把原有的虚拟主机注释掉
  4. 添加内容如下:  
  5. <VirtualHost *:80> 
  6.     ServerName cacti.forman.com  
  7.     DocumentRoot /web/vhosts/cacti  
  8.     <Directory "/web/vhosts/cacti"> 
  9.         Options Indexes  
  10.         AllowOverride none  
  11.         Require all granted  
  12.     </Directory> 
  13.     ErrorLog "logs/cacti-error_log"  
  14.     CustomLog "logs/cacti-access_log" common  
  15. </VirtualHost> 
  16. #mkdir /web/vhosts/cacti -pv


2、net-snmp

2.1 安装
由于本机即是监控端(NMS),又是被监控端,因此我们这里直接通过yum安装net-snmp和net-snmp-utils

 
  
  1. # yum -y install net-snmp net-snmp-utils  


2.2启动服务

 
  
  1. # service snmpd start  
  2. # chkconfig snmpd on  
  3.  
  4. 使用如下命令进行测试:  
  5. # snmpwalk -v 2c -c public localhost system  
  6.  
  7. 如果有正常监控信息输出即为OK。 


3、安装rrdtool

 
  
  1. #pwd
  2. /source
  3. #wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz
  4. # tar xf rrdtool-1.4.7.tar.gz  
  5. # cd rrdtool-1.4.7  
  6. # ./configure --prefix=/usr/local/rrdtool  
  7. # make  
  8. # make install  
  9. #echo "MANPATH  /usr/local/rrdtool/share/man" >> /etc/man.config  
  10. #echo "PATH=\$PATH:/usr/local/rrdtool/bin " >> /etc/profile  
  11. #export PATH=$PATH:/usr/local/rrdtool/bin  
  12. #ln -sv /usr/local/rrdtool/include /usr/include/rrdtool  
  13. #echo "/usr/local/rrdtool/lib" >> /etc/ld.so.conf.d/rrdtool.conf  
  14. #ldconfig
  15. #cd 

4、安装cacti
4.1 安装

 
  
  1. #pwd
  2. /source
  3. #wget http://www.cacti.net/downloads/cacti-0.8.8a.tar.gz
  4. # tar xf cacti-0.8.8a.tar.gz -C /web/vhosts  
  5. # cd cacti-0.8.8a
  6. # mv ./* /web/vhosts/cacti


4.2 初始配置:
为cacti创建数据库,并初始化所需要表:

 
  
  1. # mysqladmin -uroot -p create cacti //此时的密码为mysql的密码
  2. # cd /web/vhosts/cacti  
  3. # mysql -uroot -p cacti < cacti.sql 


创建mysql用户,使用cacti能访问其cacti数据库

 
  
  1. # mysql -uroot -p -e "GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY 'cactiuser'";
  2. //创建了用户cactiuser 密码也为cactiuser  
  3. # mysqladmin -uroot -p flush-privileges  
  4. # mysql -ucactiuser -p          //验证一下是否正确  
  5. cactiuser  

接着创建系统用户cactiuser,以之运行cacti的数据获取和日志记录等工作

 
  
  1. # useradd cactiuser  
  2. # chown -R cactiuser:cactiuser /web/vhosts/cacti/{log,rra} 


以cactiuser用户的身份,每5钟获取一次数据:

 
  
  1. # echo '*/5 * * * *  /usr/local/php/bin/php /web/vhosts/cacti/poller.php &> /dev/null' > /var/spool/cron/cactiuser  


php的时区不一致,把php配置文件/etc/php.ini中的时区改为如下格式:
 

 
  
  1. #vim /etc/php.ini
  2. date.timezone = Asia/Shanghai   
  3. # hwclock -s    //同步系统时间
  4. # service httpd restart  
  5.  

       
编辑cacti的配置文件include/config.php,将实际访问cacti的URL路径,启用并修改如下行:
//$url_path = "/cacti/";
我们这里将之修改为:
$url_path = "/";

在外面的物理机的hosts文件中添加:

X .X.X.X cacti.forman.com        //实现域名的访问

而后在浏览器中打开http://cacti.forman.com/install,按提示一步步配置即可完成安装。
密码第一次输入的时候,会让修改密码,用户默认为admin 密码为admin
提示图片如下:

 

 

 

 

 

 

 

 第一次登陆的时候,会提示修改密码,如下图

 

 重新登陆即可
device--->Localhost ---->version2 --->save即可 点击graphs可看到生成的图像,如下所示:

 


到这里,cacti的基本工作做完了,后面的工作就是根据自己的需要进行配置了。本文不足之处期待指正