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将无法运行。
- #!/bin/bash
- #
- # httpd Startup script for the Apache HTTP Server
- #
- # chkconfig: - 85 15
- # description: Apache is a World Wide Web server. It is used to serve \
- # HTML files and CGI.
- # processname: httpd
- # config: /etc/httpd/conf/httpd.conf
- # config: /etc/sysconfig/httpd
- # pidfile: /var/run/httpd.pid
- # Source function library.
- . /etc/rc.d/init.d/functions
- if [ -f /etc/sysconfig/httpd ]; then
- . /etc/sysconfig/httpd
- fi
- # Start httpd in the C locale by default.
- HTTPD_LANG=${HTTPD_LANG-"C"}
- # This will prevent initlog from swallowing up a pass-phrase prompt if
- # mod_ssl needs a pass-phrase from the user.
- INITLOG_ARGS=""
- # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
- # with the thread-based "worker" MPM; BE WARNED that some modules may not
- # work correctly with a thread-based MPM; notably PHP will refuse to start.
- # Path to the apachectl script, server binary, and short-form for messages.
- apachectl=/usr/local/apache/bin/apachectl
- httpd=${HTTPD-/usr/local/apache/bin/httpd}
- prog=httpd
- pidfile=${PIDFILE-/var/run/httpd.pid}
- lockfile=${LOCKFILE-/var/lock/subsys/httpd}
- RETVAL=0
- start() {
- echo -n $"Starting $prog: "
- LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && touch ${lockfile}
- return $RETVAL
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc -p ${pidfile} -d 10 $httpd
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
- }
- reload() {
- echo -n $"Reloading $prog: "
- if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
- RETVAL=$?
- echo $"not reloading due to configuration syntax error"
- failure $"not reloading $httpd due to configuration syntax error"
- else
- killproc -p ${pidfile} $httpd -HUP
- RETVAL=$?
- fi
- echo
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- status)
- status -p ${pidfile} $httpd
- RETVAL=$?
- ;;
- restart)
- stop
- start
- ;;
- condrestart)
- if [ -f ${pidfile} ] ; then
- stop
- start
- fi
- ;;
- reload)
- reload
- ;;
- graceful|help|configtest|fullstatus)
- $apachectl $@
- RETVAL=$?
- ;;
- *)
- echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
- exit 1
- esac
- exit $RETVAL
接下来就可以执行脚本了
- #!/bin/bash
- #
- <<mark
- prepare the source packages
- apr-1.4.6.tar.bz2
- apr-util-1.4.1.tar.bz2
- httpd-2.4.2.tar.bz2
- cmake-2.8.8.tar.gz
- mysql-5.5.25a.tar.gz
- libmcrypt-2.5.7-5.el5.i386.rpm
- libmcrypt-devel-2.5.7-5.el5.i386.rpm
- php-5.4.4
- mark
- ################compileing httpd######################
- #yum -y groupinstall "Development Libraries" "Development Tools" "X Software Development"
- #setenforce 0
- #mkdir /source
- SOURCE=/source
- SysDir=/etc/rc.d/init.d
- PathDir=/etc/profile
- cd $SOURCE
- wget http://apache.etoak.com//apr/apr-1.4.6.tar.bz2
- wget http://apache.etoak.com//apr/apr-util-1.4.1.tar.bz2
- wget http://apache.etoak.com//httpd/httpd-2.4.2.tar.bz2
- wget http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz
- wget http://anduin.linuxfromscratch.org/sources/BLFS/svn/m/mysql-5.5.25a.tar.gz
- wget http://downloads.php.net/stas/php-5.4.4.tar.bz2
- tar xf apr-1.4.6.tar.bz2
- cd apr-1.4.6
- ./buildconf
- ./configure --prefix=/usr/local/apr
- make
- make install
- cd $SOURCE
- tar xf apr-util-1.4.1.tar.bz2
- cd apr-util-1.4.1
- ./buildconf --with-apr=$SOURCE/apr-1.4.6
- ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
- make
- make install
- cd $SOURCE
- yum -y install pcre-devel
- tar xf httpd-2.4.2.tar.bz2
- cd httpd-2.4.2
- ./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
- make
- make install
- cd $SOURCE
- echo "PidFile \"/var/run/httpd.pid\" " >> /etc/httpd/httpd.conf
- cp /root/httpd.sh $SysDir/httpd
- chmod +x $SysDir/httpd
- chkconfig --add httpd
- service httpd start
- echo "PATH=\$PATH:/usr/local/apache/bin" >> $PathDir
- export PATH=$PATH:/usr/local/apache/bin
- echo "MANPATH /usr/local/apache/man " >> /etc/man.config
- ln -sv /usr/local/apache/include /usr/include/httpd
- service httpd start
- #########################compileing mysql###################################
- cd $SOURCE
- tar xf cmake-2.8.8.tar.gz
- cd cmake-2.8.8
- ./bootstrap
- make
- make install
- cd $SOURCE
- groupadd -r mysql
- useradd -g mysql -r -d /data/mysql mysql
- tar xf mysql-5.5.25a.tar.gz
- cd mysql-5.5.25a
- 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
- make
- make install
- cd /usr/local/mysql
- chown -R :mysql .
- mkdir -pv /data/mysql
- chown -R mysql:mysql /data/
- cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
- cp /usr/local/mysql/support-files/mysql.server $SysDir/mysqld
- chmod +x $SysDir/mysqld
- chkconfig --add mysqld
- scripts/mysql_install_db --user=mysql --datadir=/data/mysql
- sed -i '/\[mysqld\]/a \datadir = /data/mysql' /etc/my.cnf
- sed -i 's/thread_concurrency = 8/thread_concurrency = 2/g' /etc/my.cnf
- echo "PATH=\$PATH:/usr/local/mysql/bin" >> $PathDir
- export PATH=$PATH:/usr/local/mysql/bin
- echo "MATHPATH /usr/local/mysql/man" >> /etc/man.config
- ln -sv /usr/local/mysql/include /usr/include/mysql
- echo " /usr/local/mysql/lib " >> /etc/ld.so.conf.d/mysql.conf
- ldconfig
- service mysqld start
- cd
- ##########################compiling php#########################
- cd $SOURCE
- tar xf php-5.4.4.tar.bz2
- cd php-5.4.4
- ./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
- cd $SOURCE
- make
- make install
- cd $SOURCE
- cp php-5.4.4/php.ini-production /etc/php.ini
- echo "AddType application/x-httpd-php .php " >> /etc/httpd/httpd.conf
- echo "AddType application/x-httpd-php-source .phps" >> /etc/httpd/httpd.conf
- sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/g' /etc/httpd/httpd.conf
- service httpd restart
- cd
- echo "The End!"
OK,此时lamp平台架构好了
二、 安装配置cacti
1、cacti虚拟主机
在LAMP平台上,专门为cacti准备了一个虚拟主机cacti.magedu.com,其配置如下:
- #vim /etc/httpd/httpd.conf
- 启用虚拟主机
- # vim /etc/httpd/extra/httpd-vhosts.conf //把原有的虚拟主机注释掉
- 添加内容如下:
- <VirtualHost *:80>
- ServerName cacti.forman.com
- DocumentRoot /web/vhosts/cacti
- <Directory "/web/vhosts/cacti">
- Options Indexes
- AllowOverride none
- Require all granted
- </Directory>
- ErrorLog "logs/cacti-error_log"
- CustomLog "logs/cacti-access_log" common
- </VirtualHost>
- #mkdir /web/vhosts/cacti -pv
2、net-snmp
2.1 安装
由于本机即是监控端(NMS),又是被监控端,因此我们这里直接通过yum安装net-snmp和net-snmp-utils
- # yum -y install net-snmp net-snmp-utils
2.2启动服务
- # service snmpd start
- # chkconfig snmpd on
- 使用如下命令进行测试:
- # snmpwalk -v 2c -c public localhost system
- 如果有正常监控信息输出即为OK。
3、安装rrdtool
- #pwd
- /source
- #wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.7.tar.gz
- # tar xf rrdtool-1.4.7.tar.gz
- # cd rrdtool-1.4.7
- # ./configure --prefix=/usr/local/rrdtool
- # make
- # make install
- #echo "MANPATH /usr/local/rrdtool/share/man" >> /etc/man.config
- #echo "PATH=\$PATH:/usr/local/rrdtool/bin " >> /etc/profile
- #export PATH=$PATH:/usr/local/rrdtool/bin
- #ln -sv /usr/local/rrdtool/include /usr/include/rrdtool
- #echo "/usr/local/rrdtool/lib" >> /etc/ld.so.conf.d/rrdtool.conf
- #ldconfig
- #cd
4、安装cacti
4.1 安装
- #pwd
- /source
- #wget http://www.cacti.net/downloads/cacti-0.8.8a.tar.gz
- # tar xf cacti-0.8.8a.tar.gz -C /web/vhosts
- # cd cacti-0.8.8a
- # mv ./* /web/vhosts/cacti
4.2 初始配置:
为cacti创建数据库,并初始化所需要表:
- # mysqladmin -uroot -p create cacti //此时的密码为mysql的密码
- # cd /web/vhosts/cacti
- # mysql -uroot -p cacti < cacti.sql
创建mysql用户,使用cacti能访问其cacti数据库
- # mysql -uroot -p -e "GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY 'cactiuser'";
- //创建了用户cactiuser 密码也为cactiuser
- # mysqladmin -uroot -p flush-privileges
- # mysql -ucactiuser -p //验证一下是否正确
- cactiuser
接着创建系统用户cactiuser,以之运行cacti的数据获取和日志记录等工作
- # useradd cactiuser
- # chown -R cactiuser:cactiuser /web/vhosts/cacti/{log,rra}
以cactiuser用户的身份,每5钟获取一次数据:
- # echo '*/5 * * * * /usr/local/php/bin/php /web/vhosts/cacti/poller.php &> /dev/null' > /var/spool/cron/cactiuser
php的时区不一致,把php配置文件/etc/php.ini中的时区改为如下格式:
- #vim /etc/php.ini
- date.timezone = Asia/Shanghai
- # hwclock -s //同步系统时间
- # service httpd restart
编辑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的基本工作做完了,后面的工作就是根据自己的需要进行配置了。本文不足之处期待指正