在开始操作之前,先简单介绍几个概念
*SNMP :简单网络管理协议
      它主要是根据用户的需求获取远程被监控主机的一些信息而已,
*RRDTOOL : 是一个强大的绘图工具
       它主要是根据用户的需求将从SNMP协议(当然也可以是其他的方式,比如说sheel脚本等,)获取到信息,绘制成图像,
*cacti : cacti( http://www.cacti.net )
        是一个基于PHP开发的强大的检测分析工具而已,cacti主要是通过SNMP或sheel脚本等方式获取到被监控主机的数据, 然后通过rrdtool存储更新数据,当用户需要查看数据的时候,使用rrdtool生成图表通过cacai展示给用户,

****************************************************************************************

下面将在一台默认安装有rhel5.8的系统上安装配置cacti 

****************************************************************************************
上面说过cacit是一个php语言开发的网页工具,那么我们需要安装LAMP平台,还要生成图像展示给用户,需要安装rrdtool,如果要使用snmp协议监控其他主机或本机,需要安装snmp
****************************************************************************************
准备以下软件包,存放至/usr/src目录下
****************************************************************************************
apr-1.4.6.tar.bz2
apr-util-1.4.1.tar.bz2
cmake-2.8.8.tar.gz          这些包主要是LAMP环境所用到的软件包
httpd-2.4.2.tar.bz2
mysql-5.5.25a.tar.gz
php-5.4.4.tar.bz2
****************************************************************************************
rrdtool-1.4.7.tar.gz
cacti-spine-0.8.8a.tar.gz   
cacti-0.8.8a.tar.gz
****************************************************************************************
准备完成好,下面来安装配置了,需要注意的是软件包一定要放到/usr/src目录,而且要配置好yum源,

一, 构建LAMP环境,这里使用脚本自动化编译安装并配置,(当然也可以使用yum安装,)
1. 构建LAMP平台,脚本内容如下

  1. #!/bin/bash 
  2. # andy_f 
  3. # 编译安装LAMP平台 
  4. local_ip=`ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'` 
  5. # install path  
  6. http_path=/usr/local/apache 
  7. apr_path=/usr/local/apr 
  8. apr_util_path=/usr/local/apr-util 
  9. php_path=/usr/local/php 
  10. mysql_path=/usr/local/mysql 
  11.  
  12. re=0 
  13.  
  14. # install package name 
  15. apr="apr-1.4.6.tar.bz2" 
  16. apr_util="apr-util-1.4.1.tar.bz2" 
  17. http="httpd-2.4.2.tar.bz2" 
  18. php="php-5.4.4.tar.bz2" 
  19. cmake="cmake-2.8.8.tar.gz" 
  20. mysql="mysql-5.5.25a.tar.gz" 
  21. # Compiler parameters 
  22. make_apr="--prefix=$apr_path" 
  23. make_apr_util="--prefix=$apr_util_path --with-apr=$apr_path" 
  24. make_http="--prefix=$http_path --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=$apr_path --with-apr-util=$apr_util_path --enable-suexec --with-suexec" 
  25. make_mysql="-DCMAKE_INSTALL_PREFIX=$mysql_path -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1  -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0  -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci " 
  26. make_php="--prefix=$php_path --with-mysql=$mysql_path --with-config-file-path=$php_path/etc --with-openssl --enable-sockets --with-mysqli=$mysql_path/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml -with-apxs2=$http_path/bin/apxs --with-bz2" 
  27. ################################### 
  28. host1=/cacti 
  29. ################################## 
  30. check_apache() { 
  31.   [ -e $apr_path ] && echo -e "\033[31m apr installed\033[0m" && re=1 
  32.   [ -e $apr_util_path ] && echo -e "\033[31m apr-util installed\033[0m" && re=1 
  33.   [ -e $http_path ] && echo -e "\033[31m apache installed\033[0m" && re=1 
  34.  
  35. check_php() { 
  36.   [ -e $php_path ] && echo -e "\033[31m php installed\033[0m" && re=1 
  37.  
  38. check_mysql() { 
  39.   [ -e $mysql_path ] && echo -e "\033[31m mysql installed\033[0m" && re=1 
  40.  
  41. check_all() { 
  42.   count=0 
  43.   check_apache 
  44.   [ ! $re -eq 0 ] && count=1  
  45.   check_php  
  46.   [ ! $re -eq 0 ] && count=${count+1} 
  47.   check_mysql  
  48.   [ ! $re -eq 0 ] && count=${count+1} 
  49.   [ ! $count -eq 0 ] && exit 1 
  50.  
  51. init_install() { 
  52.   setenforce 0 
  53.   service iptables stop &>/dev/null 
  54.   yum -y groupinstall "Development Libraries" "Development Tools" "X Software Development"  &>/dev/null 
  55.   yum -y install pcre-devel &>/dev/null 
  56. #################################### 
  57.   if [ -e /usr/src/$apr ];then 
  58.     cd /usr/src 
  59.     tar xjf $apr &>/dev/null 
  60.     cd /usr/src/`echo $apr | awk -F.tar '{print $1}'` 
  61.     ./buildconf &>/dev/null 
  62.     ./configure $make_apr &>/dev/null 
  63.     make  &>/dev/null && make install &>/dev/null 
  64.       if [ $? -eq 0 ];then 
  65.          echo "$apr install ok" 
  66.       else 
  67.          echo "$apr install fail" 
  68.          exit 2 
  69.      fi 
  70.   else 
  71.     echo "/usr/src/$apr no file"  
  72.     exit 3 
  73.   fi 
  74. #################################### 
  75.    if [ -e /usr/src/$apr_util ];then 
  76.      cd /usr/src 
  77.      tar xjf $apr_util 
  78.      cd /usr/src/`echo $apr_util | awk -F.tar '{print $1}'` 
  79.      ./buildconf --with-apr=/usr/src/`echo $apr | awk -F.tar '{print $1}'`  &>/dev/null 
  80.      ./configure $make_apr_util &>/dev/null 
  81.      make  &>/dev/null &&  make install &>/dev/null  
  82.         if [ $? -eq 0 ];then 
  83.            echo "$apr_util install ok"  
  84.         else 
  85.            echo "$apr_util install fail"  
  86.            exit 2 
  87.         fi 
  88.    else 
  89.      echo "/usr/src/$apr_util no file"  
  90.      exit 3 
  91.   fi 
  92. #################################### 
  93.   if [ -e /usr/src/$cmake ];then 
  94.      cd /usr/src 
  95.      tar xzf  $cmake 
  96.      cd /usr/src/`echo $cmake | awk -F.tar '{print $1}'` 
  97.      ./bootstrap &>/dev/null 
  98.      make  &>/dev/null && make install &>/dev/null  
  99.         if [ $? -eq 0 ];then 
  100.             echo "$cmake install ok"   
  101.         else 
  102.             echo "$cmake install fail"  
  103.             exit 2 
  104.         fi 
  105.   else 
  106.      echo "/usr/src/$cmake no file"  
  107.      exit 3 
  108.   fi 
  109. #################################### 
  110.  
  111. install_httpd() { 
  112.   if [ -e /usr/src/$http ];then 
  113.      cd /usr/src 
  114.      tar xjf $http  
  115.      cd /usr/src/`echo $http | awk -F.tar '{print $1}'` 
  116.      ./configure $make_http &>/dev/null 
  117.      make  &>/dev/null && make install &>/dev/null  
  118.        if [ $? -eq 0 ];then 
  119.           echo "$http install ok"  
  120.        else 
  121.            echo "$http install fail" 
  122.            exit 2 
  123.        fi 
  124.    else 
  125.      echo "/usr/src/$http no file"  
  126.      exit 3 
  127.    fi 
  128.  
  129. install_mysql() { 
  130.   id mysql &>/dev/null 
  131.   [ $? -eq 0 ] || useradd -s /sbin/nologin -M mysql 
  132.   if [ -e /usr/src/$mysql ];then 
  133.      cd /usr/src 
  134.      tar xzf $mysql 
  135.      cd /usr/src/`echo $mysql | awk -F.tar '{print $1}'` 
  136.      cmake . $make_mysql &>/dev/null 
  137.      make  &>/dev/null && make install &>/dev/null  
  138.        if [ $? -eq 0 ];then 
  139.           echo "$mysql install ok"  
  140.        else 
  141.           echo "$mysql install fail" 
  142.           exit 2 
  143.        fi 
  144.   else 
  145.     echo "/usr/src/$mysql no file"  
  146.     exit 3 
  147.   fi 
  148.  
  149. install_php() { 
  150.   if [ -e /usr/src/$php ];then 
  151.      cd /usr/src 
  152.      tar xjf $php 
  153.      cd /usr/src/`echo $php | awk -F.tar '{print $1}'` 
  154.      ./configure $make_php &>/dev/null 
  155.      make  &>/dev/null && make install &>/dev/null  
  156.        if [ $? -eq 0 ];then 
  157.           echo "$php install ok"  
  158.        else 
  159.           echo "$php install fail" 
  160.           exit 2 
  161.        fi 
  162.   else 
  163.      echo "/usr/src/$php no file"  
  164.      exit 3 
  165.   fi 
  166.  
  167. config_lamp() { 
  168. ######################################################## 
  169.   echo "pidfile "/var/run/httpd.pid"" >> $http_path/conf/httpd.conf 
  170.   echo "AddType application/x-httpd-php  .php" >> $http_path/conf/httpd.conf 
  171.   echo "AddType application/x-httpd-php-source  .phps" >> $http_path/conf/httpd.conf 
  172.   echo "DirectoryIndex  index.php  index.html" >> $http_path/conf/httpd.conf 
  173.   echo "Include conf/vhost/*.conf" >> $http_path/conf/httpd.conf 
  174.   mkdir -p $http_path/conf/vhost 
  175.   ln -s $http_path/bin/* /sbin/ 
  176.   ln -s $http_path/include/ /usr/include/apache 
  177.   echo "$local_ip   `hostname`" >> /etc/hosts 
  178.   if [ -e /media/Server/httpd-2.2.3-63.el5.i386.rpm ];then 
  179.      cp /media/Server/httpd-2.2.3-63.el5.i386.rpm /var/tmp/ 
  180.      cd /var/tmp 
  181.      rpm2cpio httpd-2.2.3-63.el5.i386.rpm | cpio -id &>/dev/null 
  182.      cp /var/tmp/etc/rc.d/init.d/httpd /etc/init.d/ 
  183.      sed -i '40,55d;62d' /etc/init.d/httpd 
  184.      sed -i s@/usr/sbin/apachectl@$http_path/bin/apachectl@g /etc/init.d/httpd 
  185.      sed -i s@/usr/sbin/httpd@$http_path/bin/httpd@g /etc/init.d/httpd 
  186.      chmod a+x /etc/init.d/httpd 
  187.      chkconfig --add httpd 
  188.      chkconfig httpd on 
  189.   else 
  190. cat > /etc/init.d/httpd <<EOF 
  191. #!/bin/bash 
  192. # httpd        Startup script for the Apache HTTP Server 
  193. # chkconfig: - 85 15 
  194. # description: Apache is a World Wide Web server.  It is used to serve \ 
  195. #          HTML files and CGI. 
  196. # processname: httpd 
  197. . /etc/rc.d/init.d/functions 
  198. if [ -f /etc/sysconfig/httpd ]; then 
  199.         . /etc/sysconfig/httpd 
  200. fi 
  201. HTTPD_LANG=${HTTPD_LANG-"C"} 
  202. INITLOG_ARGS="" 
  203. apachectl=/usr/local/apache/bin/apachectl 
  204. httpd=${HTTPD-/usr/local/apache/bin/httpd} 
  205. prog=httpd 
  206. pidfile=${PIDFILE-/var/run/httpd.pid} 
  207. lockfile=${LOCKFILE-/var/lock/subsys/httpd} 
  208. RETVAL=0 
  209. STOP_TIMEOUT=${STOP_TIMEOUT-10} 
  210. start() { 
  211.         echo -n $"Starting $prog: " 
  212.         LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS 
  213.         RETVAL=$? 
  214.         echo 
  215.         [ $RETVAL = 0 ] && touch ${lockfile} 
  216.         return $RETVAL 
  217. stop() { 
  218.     echo -n $"Stopping $prog: " 
  219.     killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd 
  220.     RETVAL=$? 
  221.     echo 
  222.     [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 
  223. reload() { 
  224.     echo -n $"Reloading $prog: " 
  225.     if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then 
  226.         RETVAL=$? 
  227.         echo $"not reloading due to configuration syntax error" 
  228.         failure $"not reloading $httpd due to configuration syntax error" 
  229.     else 
  230.         killproc -p ${pidfile} $httpd -HUP 
  231.         RETVAL=$? 
  232.     fi 
  233.     echo 
  234. case "$1" in 
  235.   start) 
  236.     start 
  237.     ;; 
  238.   stop) 
  239.     stop 
  240.     ;; 
  241.   status) 
  242.         status -p ${pidfile} $httpd 
  243.     RETVAL=$? 
  244.     ;; 
  245.   restart) 
  246.     stop 
  247.     start 
  248.     ;; 
  249.   condrestart) 
  250.     if [ -f ${pidfile} ] ; then 
  251.         stop 
  252.         start 
  253.     fi 
  254.     ;; 
  255.   reload) 
  256.         reload 
  257.     ;; 
  258.   graceful|help|configtest|fullstatus) 
  259.     $apachectl $@ 
  260.     RETVAL=$? 
  261.     ;; 
  262.   *) 
  263.     echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" 
  264.     exit 1 
  265. esac 
  266. exit $RETVAL 
  267. EOF 
  268.      chmod a+x /etc/init.d/httpd 
  269.      chkconfig --add httpd 
  270.      chkconfog httpd on 
  271.   fi 
  272. ######################################################## 
  273.   ln -s $mysql_path/lib/* /lib/ 
  274.   ln -s $mysql_path/include/ /usr/include/mysql 
  275.   ln -s $mysql_path/bin/* /sbin/ 
  276.   chown -R mysql:mysql $mysql_path 
  277.   cp /usr/src/`echo $mysql | awk -F.tar '{print $1}'`/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf 
  278.   cp /usr/src/`echo $mysql | awk -F.tar '{print $1}'`/support-files/my-innodb-heavy-4G.cnf /etc/my.cnf 
  279.   cp /usr/src/`echo $mysql | awk -F.tar '{print $1}'`/support-files/mysql.server /etc/init.d/mysqld 
  280.   chmod a+x /etc/init.d/mysqld 
  281.   chkconfig --add mysqld  
  282.   chkconfig mysqld on 
  283.   $mysql_path/scripts/mysql_install_db --user=mysql --basedir=$mysql_path --datadir=$mysql_path/data/ &>/dev/null 
  284. ######################################################## 
  285.   cp /usr/src/`echo $php | awk -F.tar '{print $1}'`/php.ini-production $php_path/etc/php.ini 
  286. ######################################################## 
  287.  
  288. vhost1() { 
  289.   [ -e $host1 ] ||  mkdir -p $host1 
  290. cat > $http_path/conf/vhost/cacti.conf <<EOF 
  291. <VirtualHost *:80> 
  292.         ServerAdmin admin@andy.com 
  293.         DocumentRoot $host1 
  294.         ServerName cacti.andy.com 
  295.         ErrorLog logs/cacti.err 
  296.         CustomLog logs/cacti.access" common 
  297.    <Directory "$host1"> 
  298.         Options Indexes FollowSymLinks 
  299.         AllowOverride None 
  300.     <RequireAll> 
  301.         Require all granted 
  302.         </RequireAll> 
  303.    </Directory> 
  304. </VirtualHost> 
  305. EOF 
  306. start_service() { 
  307. service httpd restart && echo "httpd is up." || echo "httpd  error." 
  308. service mysqld restart && echo "mysqld is up." || echo "mysqld error." 
  309. check_all 
  310. init_install 
  311. install_httpd 
  312. install_mysql 
  313. install_php 
  314. config_lamp  
  315. vhost1 
  316. start_service 

执行此脚本即可, 如果不想使用脚本安装的话,可以参考
http://ant595.blog.51cto.com/5074217/920996   不过需要注意的是在编译安装php的时候需要加 --enable-sockets 参数,

二,安装SNMP
1, 使用yum方式安装,

  1. #yum -y install net-snmp net-snmp-devel net-snmp-utils  
  2.  
  3. 说明:  
  4. net-snmp 此包是被监控端需要安装的包 
  5. net-snmp-utils 此包是监控端需要安装的包 

2, 配置SNMP

  1. #vim /etc/snmp/snmpd.conf   编辑snmp主配置文件修改以下内容 
  2. com2sec notConfigUser  127.0.0.1       public 
  3. access  notConfigGroup ""      any       noauth    exact  all none none 
  4. view all    included  .1                               80 

3,启动snmpd服务

  1. #chkconfig snmpd on  
  2. #service snmpd restart 

三, 编译安装rrdtool 
1, 编译安装rrdtool

  1. #cd /usr/src 
  2. #tar xzvf rrdtool-1.4.7.tar.gz  
  3. #cd rrdtool-1.4.7 
  4. #./configure --prefix=/usr/local/rrdtool 
  5. #make  
  6. #make install 

四,安装配置cacti
1, 配置cacti

  1. #cd /usr/src 
  2. #tar xzvf cacti-0.8.8a.tar.gz 
  3. #mv cacti-0.8.8a/* /cacti/ 
  4. #chown -R root:root /cacti 
  5. #chmod -R a+w /cacti/log /cacti/rra 

 2,导入cacti所需数据库并建立cacti所使用的数据库用户

  1. #mysql -e "create database cacti;" 
  2. #mysql cacti < /cacti/cacti.sql 
  3. #mysql -e "grant all privileges on cacti.* to cacti@localhost identified by 'redhat';" 
  4. #mysql -e "flush privileges;" 

 3,修改cacti配置文件

  1. #vim /cacti/include/config.php 
  2. $database_type = "mysql"
  3. $database_default = "cacti"
  4. $database_hostname = "localhost"
  5. $database_username = "cacti"
  6. $database_password = "redhat"
  7. $database_port = "3306"
  8. $database_ssl = false
  9. $url_path = "/";    

4,安装cacti-spine

  1. #cd /usr/src 
  2. #tar xzvf cacti-spine-0.8.8a.tar.gz  
  3. #cd cacti-spine-0.8.8a 
  4. #./configure  
  5. #make 
  6. #make install 

 5,配置cacti-spine

  1. #vim /usr/local/spine/etc/spine.conf  
  2. DB_Host         localhost 
  3. DB_Database     cacti 
  4. DB_User         cacti 
  5. DB_Pass         redhat 
  6. DB_Port         3306 
  7. DB_PreG         0 

6,将spine命令加入系统搜索路径

  1. #ln -sv /usr/local/spine/bin/spine /sbin/ 

7,生成初始图像文件

  1. #vim /usr/local/php/etc/php.ini 修改以下内容
  2. date.timezone = Asia/Shanghai 如果不修改此行配置的话,执行下面的命令会报错,
  3. #/usr/local/php/bin/php /cacti/poller.php  
  4. #crontab -e 内容如下
  5. */5 * * * * /usr/local/php/bin/php /cacti/poller.php &>/dev/null

8,安装cacti, 上面说过cacti是一个php开发的web页面,那么虚拟主机肯定是要配置的,上面的脚本中已经配置过了,域名是cacti.andy.com 
8.1 在浏览器中输入cacti.andy.com 将看到cacti的安装界面, 前提是客户端要能访问cacti.andy.com的域名,见图,

构建cacti监控平台, LAMP使用脚本自动化编译安装_cacti

8.2 选择安装的方式
构建cacti监控平台, LAMP使用脚本自动化编译安装_监控_02

8.3, 设置cacti所需要依赖的一些软件及命令的位置,
构建cacti监控平台, LAMP使用脚本自动化编译安装_cacti_03

8.4,输入cacti的用户及密码,默认的用户及密码为admin
构建cacti监控平台, LAMP使用脚本自动化编译安装_监控_04

8.5,第一次登录需要重置下admin的密码.

构建cacti监控平台, LAMP使用脚本自动化编译安装_cacti_05

8.6,cacti的主界面,构建cacti监控平台, LAMP使用脚本自动化编译安装_cacti_06

8.7 点击左上角的graphs-->localhost, 可以看到默认监控本机的图像了,如果没有图像执行下如下命令

  1. #/usr/local/php/bin/php /cacti/poller.php 

 

构建cacti监控平台, LAMP使用脚本自动化编译安装_cacti_07

 

 

 OK,到这里已经安装配置好了,后面会再写篇博客介绍cacti的使用,