Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络流量监测图形分析工具
Cacti和Nagios是现在使用比较多的网络监控软件了,对于这两款监控软件的区别,应该说是侧重点的不同。 Cacti比较着重于直观数据的监控,易于生成图形,用来监控网络流量、cpu使用率、硬盘使用率等可以说很在合适不过。 而Nagios则比较注重于主机和服务的监控,并且有很强大的发送报警信息的功能。 把两者结合起来,既可以使报警机制高效及时,又可以很容易的查看各项数据的情况。上篇已经跟大家介绍了nagios的安装,这篇将跟大家讲下cacti的安装。
一、在LNMP(linux+nginx+mysql+php)上搭建cacti
1.修改selinux
vim /etc/selinux/config
修改为
关闭防火墙
service iptables stop
chkconfig iptables off
重启将selinux生效reboot
安装编译工具及库文件
for i in ` rpm -q make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch |grep 'not installed' | awk '{print $2}' `; do yum -y install $i; done
安装libmcrypt
cd /usr/local/src
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install
安装cmake
cd /usr/local/src
tar zxvf cmake-2.8.7.tar.gz
cd cmake-2.8.7
./configure
make && make install
安装pcre
cd /usr/local/src
mkdir /usr/local/pcre #创建安装目录
tar zxvf pcre-8.30.tar.gz
cd pcre-8.30
./configure --prefix=/usr/local/pcre
make && make install
安装mysql
groupadd mysql #添加mysql组
useradd -g mysql mysql -s /bin/false #创建mysql加入到mysql组,不允许mysql登录
mkdir -p /data/mysql #创建MySQL数据库存放目录
chown -R mysql:mysql /data/mysql #设置MySQL数据库目录权限
mkdir -p /usr/local/mysql #创建MySQL安装目录
cd /usr/local/src
tar zxvf mysql-5.5.21.tar.gz
cd mysql-5.5.21
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql-DSYSCONFDIR=/etc #配置
make
make install
cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf #拷贝配置文件
vim /etc/my.cnf #在 [mysqld] 部分增加datadir = /data/mysql #添加MySQL数据库路径
./scripts/mysql_install_db --user=mysql #生成mysql系统数据库
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld #增加执行权限
chkconfig mysqld on #加入开机启动
vim /etc/rc.d/init.d/mysqld #编辑
basedir = /usr/local/mysql #MySQL程序安装路径
datadir = /data/mysql #MySQl数据库存放目录
service mysqld start #启动
vim /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
使环境变量立即生效
source /etc/profile
下面两行把myslq的库文件链接到系统默认位置,编译类似PHP等软件可不指定mysql的库文件地址。
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
reboot #重启
mysql_secure_installation #设置Mysql密码
或者直接修改密码/usr/local/mysql/bin/mysqladmin -u root -p password "123456" #修改密码
service mysqld restart #重启
(6)、安装 nginx
groupadd www #添加组
useradd -g www www -s /bin/false #创建账户www并加入到组,不允许www直接登录
cd /usr/local/src
tar zxvf nginx-1.0.14.tar.gz
cd nginx-1.0.14
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.30
#--with-pcre=/usr/local/src/pcre-8.30指源码包解压的路径,不是安装的路径,否则会报错
make && make install
/usr/local/nginx/sbin/nginx #启动nginx
设置nginx开启启动
vim /etc/rc.d/init.d/nginx #编辑启动文件添加下面内容
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=
/usr
/local
/nginx
/sbin
/nginx
nginx_config=
/usr
/local
/nginx
/conf
/nginx.conf
nginx_pid=
/usr
/local
/nginx
/logs
/nginx.pid
RETVAL=
0
prog=
"nginx"
# Source function library.
.
/etc
/rc.d
/init.d
/functions
# Source networking configuration.
.
/etc
/sysconfig
/network
# Check that networking is up.
[
${NETWORKING} =
"no"
]
&&
exit
0
[
-x
$nginxd
]
||
exit
0
# Start nginx daemons functions.
start
(
)
{
if
[
-e
$nginx_pid
];
then
echo
"nginx already running...."
exit
1
fi
echo
-n $
"Starting $prog: "
daemon
$nginxd
-c
${nginx_config}
RETVAL=
$?
echo
[
$RETVAL =
0
]
&&
touch
/var
/lock
/subsys
/nginx
return
$RETVAL
}
# Stop nginx daemons functions.
stop
(
)
{
echo
-n $
"Stopping $prog: "
killproc
$nginxd
RETVAL=
$?
echo
[
$RETVAL =
0
]
&&
rm
-f
/var
/lock
/subsys
/nginx
/usr
/local
/nginx
/logs
/nginx.pid
}
reload
(
)
{
echo
-n $
"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc
$nginxd
-HUP
RETVAL=
$?
echo
}
# See how we were called.
case
"$1"
in
start
)
start
;;
stop
)
stop
;;
reload
)
reload
;;
restart
)
stop
start
;;
status
)
status
$prog
RETVAL=
$?
;;
*
)
echo $
"Usage: $prog {start|stop|restart|reload|status|help}"
exit
1
esac
exit
$RETVAL
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限
chkconfig nginx on #设置开机启动
/etc/rc.d/init.d/nginx restart
service nginx restart
安装php
cd /usr/local/src
tar -zvxf php-5.3.10.tar.gz
cd php-5.3.10
mkdir -p /usr/local/php5 #建立php安装目录
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv --with-freetype --with-jpeg --with-png --with-zlib --with-libxml --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic --enable-suhosin --enable-session --with-mcrypt --with-curl
make && make install
cp php.ini-production /usr/local/php5/etc/php.ini #复制php配置文件到安装目录
rm -rf /etc/php.ini #删除系统自带配置文件
ln -s /usr/local/php5/etc/php.ini /etc/php.ini #添加软链接
ln -s /usr/local/php5/bin/php /usr/bin/php
cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf #拷贝模板文件为php-fpm配置文件
vim /usr/local/php5/etc/php-fpm.conf #编辑
user = www #设置php-fpm运行账号为www
group = www #设置php-fpm运行组为www
pid = run/php-fpm.pid #取消注释
设置 php-fpm开机启动
cp /usr/local/src/php-5.3.10/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #拷贝php-fpm到启动目录
chmod +x /etc/rc.d/init.d/php-fpm #添加权限
chkconfig php-fpm on #设置开机启动
vim /usr/local/php5/etc/php.ini #编辑配置文件
找到:;date.timezone = 修改为:date.timezone = PRC #设置时区
找到:expose_php = On 修改为:expose_php = OFF #禁止显示php版本的信息
找到:display_errors = On 修改为:display_errors = OFF #关闭错误提示
配置nginx支持php
vim /usr/local/nginx/conf/nginx.conf
修改/usr/local/nginx/conf/nginx.conf 配置文件,需做如下修改
user www www; #首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php5/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
index index.php index.html index.htm; #添加index.php
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
/etc/init.d/nginx restart #重启nginx
cd /usr/local/nginx/html/ #进入nginx默认网站根目录
rm -rf /usr/local/nginx/html/* #删除默认测试页
chown www.www /usr/local/nginx/html/ -R #设置目录所有者
chmod 755 /usr/local/nginx/html/ -R #设置目录权限
重启nginx、mysqld、php-fpm服务
安装配置Cacti Web服务
(1)安装rrdtool、net-snmp以及相关依赖包
for i in ` rpm -q rrdtool net-snmp rrdtool-devel net-snmp-devel net-snmp-utils net-snmp-python net-snmp-perl lm_sensors-devel file-devel rpm-devel file intltool libart_lgpl libart_lgpl-devel elfutils pango-devel* cairo-devel* mysql-devel Mod_auth_mysql php-mysql cairo dejavu-fonts-common.noarch dejavu-lgc-sans-mono-fonts.noarch dejavu-sans-mono-fonts.noarch fontpackages-filesystem.noarch libXft libXrender libthai pango pixman perl-rrdtool net-snmp net-snmp-utils tcp_wrappers-devel |grep 'not installed' | awk '{print $2}' `; do yum -y install $i; done
修改snmpd配置文件,
vim /etc/snmp/snmpd.conf
找到先对应的行修改,修改为下面内容
com2sec notConfigUser 127.0.0.1 public #127.0.0.1为cacti监控服务器
#view systemview included .1.3.6.1.2.1.25.1.1
access notConfigGroup "" any noauth exact all none none
view all included .1 80
启动snmpd
service snmpd start
chkconfig snmpd on
安装Cacti
cd /usr/local/src
tar zxvf cacti-0.8.7i.tar.gz
cd cacti-0.8.7i
cp -r * /usr/local/nginx/html
chown www.www /usr/local/nginx/html/ -R #设置目录所有者
chmod 755 /usr/local/nginx/html/ -R #设置目录权限
配置Cacti数据库
mysql -u root -p #输入密码进入MySQL控制台
create database cactidb; #创建数据库cactidb
insert into mysql.user(Host,User,Password) values('localhost','cactiuser',password('123456')); #创建数据库用户cactiuser 密码 123456
grant all on cactidb.* to 'cactiuser'@'%' identified by '123456' with grant option; #授权用户cactiuser对数据库cactidb完全访问
flush privileges; #刷新系统授权表,使设置生效
use cactidb
source /usr/local/nginx/html/cacti.sql #导入cacti数据库文件
vim /usr/local/nginx/html/include/config.php #配置数据库连接
$database_type = "mysql";
$database_default = "cactidb"; #数据库名称
$database_hostname = "localhost"; #主机名称,默认即可
$database_username = "cactiuser"; #数据库用户名
$database_password = "123456"; #数据库密码
$database_port = "3306"; #MySQL数据库默认连接端口
$database_ssl = false;
打开http://Cacti-IP/ 出现安装界面
点击一步
每个选项下面都是[OK: FILE FOUND],表示配置文件路径全部正确
完成之后出现登录界面
默认的用户名和密码都是admin
第一次需要密码修改,修改后点save
因为cacti主要是通过poller.php来触发数据的采集,在crontab设置cacti每隔5分钟采集一次数据
crontab -e
*/5 * * * * php /usr/local/nginx/html/poller.php &> /dev/null
重启crond
/usr/bin/php /usr/local/nginx/html/poller.php #手动刷新数据