Cacti使用安装详解_MySQL
cacti是用php语言实现的一个软件,它的主要功能是用snmp服务获取数据,然后用rrdtool储存和更新数据,当用户需要查看数据的时候用rrdtool生成图表呈现给用户。因此,snmp和rrdtool是cacti的关键。Snmp关系着数据的收集,rrdtool关系着数据存储和图表的生成。
Mysql配合PHP程序存储一些变量数据并对变量数据进行调用,如:主机名、主机ip、snmp团体名、端口号、模板信息等变量。
snmp抓到数据不是存储在mysql中,而是存在rrdtool生成的rrd文件中(在cacti根目录的rra文件夹下)。rrdtool对数据的更新和存储就是对rrd文件的处理,rrd文件是大小固定的档案文件(Round Robin Archive),它能够存储的数据笔数在创建时就已经定义。关于RRDTool的知识请参阅RRDTool教学。
前期准备工作:
Cacti官网:http://www.cacti.net/download_cacti.php
找到Linux/Unix in tar.gz format连接图标,
下载地址: http://www.cacti.net/downloads/cacti-0.8.8b.tar.gz
下载地址: http://www.cacti.net/downloads/spine/cacti-spine-0.8.8b.tar.gz
确保前期安装需要准备的软件:
- net-snmp-devel
- mysql
- mysql-devel
- openssl-devel
准备所需要的软件包
Apache http://www.apache.org/
Mysql http://www.mysql.com/
Php http://www.php.net
Rrdtool http://oss.oetiker.ch/rrdtool/
Net-snmp http://www.net-snmp.org/
Cacti http://www.cacti.net/
其中apache、mysql、php通过yum安装
下面简述安装过程:
一、安装RRDTool和SNMP
1 安装RRDTool
yum -y install rrdtool
启动rrdtool服务并且加入启动列表
service rrdcached start 失败报错:rrdcached: 未被识别的服务
chkconfig rrdcached on 失败
wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.8.tar.gz
也chkconfig失败
2 安装snmp支持工具
可以yum安装,也可以源码安装,比如
Wget http://sourceforge.net/projects/net-snmp/files/net-snmp/5.6.2.1/net-snmp-5.6.2.1.tar.gz/download
yum -y install net-snmp
yum -y install net-snmp-utils
启动snmpd服务并且加入启动列表
service snmpd start
chkconfig snmpd on
3 安装LAMP
[root@cacti ~]# yum -y install httpd mysql-server php php-mysql mysql-devel
[root@cacti ~]# chkconfig httpd on
[root@cacti ~]# chkconfig mysqld on
[root@cacti ~]# /etc/init.d/httpd start
[root@cacti ~]# /etc/init.d/mysqld start
启动完httpd之后,可以打开 http://10.xx.1.xx/,界面如下:
二、然后下载cacti
到这里去下载 wget http://www.cacti.net/downloads/cacti-0.8.8b.tar.gz
下载当前最新版 wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.8b.tar.gz
和cacti-spine,http://www.cacti.net/downloads/spine/
下载当前最新版 cacti-spine,wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.8b.tar.gz
三、开始安装cacti-spine
1,建立cacti用户
[root@squid-2 ~]# useradd -m cacti
[root@squid-2 ~]# passwd cacti
2,首先安装几个编译cacti-spine需要的devel包:
yum install net-snmp-devel -y
yum install mysql-devel -y
yum install openssl-devel -y
3,安装gcc和libtool
yum install gcc -y
yum install libtool -y
4,下载cactid并解压:
tar -xf cacti-spine-0.8.8b.tar.gz
编译安装:
cd cacti-spine-0.8.8b
aclocal
libtoolize --force
autoheader
autoconf
automake
./configure
make
make install
5,安装好以后,我们需要再配置一下:
vim /etc/spine.conf
DB_Host localhost
DB_Database cacti
DB_User cacti_user
DB_Pass cacti05261743
DB_Port 3306,
6,启动一下看看
#/usr/local/spine/bin/spine
一般显示
SPINE: Using spine config file [/etc/spine.conf]
SPINE: Version 0.8.8a starting
SPINE: Time: 0.1190 s, Threads: 5, Hosts: 2
7,创建mysql账号:
- mysql> set names utf8;
- Query OK, 0 rows affected (0.00 sec)
- mysql> create database cacti;
- Query OK, 1 row affected (0.09 sec)
- mysql>
- mysql> grant all on cacti.* to cacti_user@/
登录mysql,删除掉’’@localhost以及’’@hostname这2个用户名为空串的mysql账号,就OK了。
- [cacti@squid-2 ~]$ /usr/local/spine/bin/spine
- SPINE: Using spine config file [/etc/spine.conf]
- MYSQL: Connection Failed: Error:/'2002/', Message:/'Can/'t connect to local MySQL server through socket /'/var/lib/mysql/mysql.sock/' (2)/'
- MYSQL: Connection Failed: Error:/'2002/', Message:/'Can/'t connect to local MySQL server through socket /'/var/lib/mysql/mysql.sock/' (2)/'
- MYSQL: Connection Failed: Error:/'2002/', Message:/'Can/'t connect to local MySQL server through socket /'/var/lib/mysql/mysql.sock/' (2)/'
- MYSQL: Connection Failed: Error:/'2002/', Message:/'Can/'t connect to local MySQL server through socket /'/var/lib/mysql/mysql.sock/' (2)/'
- MYSQL: Connection Failed: Error:/'2002/', Message:/'Can/'t connect to local MySQL server through socket /'/var/lib/mysql/mysql.sock/' (2)/'
- 05/16/2014 04:11:43 PM - SPINE: Poller[0] FATAL: Connection Failed, Error:/'2002/', Message:/'Can/'t connect to local MySQL server through socket /'/var/lib/mysql/mysql.sock/' (2)/' (Spine init)
找到正在运行的mysql.sock的路径,做一个软连接:
- [root@mysqlvm2 bin]# find / -name mysql.sock
- /usr/local/mysql/mysql.sock
- [root@mysqlvm2 bin]#
- [root@mysqlvm2 bin]# ln -s /usr/local/mysql/mysql.sock /var/lib/mysql/mysql.sock
- [root@mysqlvm2 bin]#
再次启动,有报错如下:
- [root@mysqlvm2 bin]# /usr/local/spine/bin/spine
- SPINE: Using spine config file [/etc/spine.conf]
- 05/16/2014 04:16:40 PM - SPINE: Poller[0] FATAL: MySQL Error:/'1146/', Message:/'Table /'cacti.settings/' doesn/'t exist/
需要导入默认的cacti.sql,解压缩.tar.gz,获得cacti.sql
- [cacti@squid-2 cacti-0.8.8b] tar -xvf cacti-0.8.8b.tar.gz
- [cacti@squid-2 cacti-0.8.8b] cd cacti-0.8.8b
- [root@mysqlvm2 bin]# type mysql
- mysql is hashed (/usr/local/mysql/bin/mysql)
- [cacti@squid-2 cacti-0.8.8b]$
- mysql -u cacti_user -pcacti05261743 -h127.0.0.1 cacti /cacti/cacti-0.8.8b/cacti.sql
- [cacti@squid-2 cacti-0.8.8b]$ /usr/local/spine/bin/spine
- SPINE: Using spine config file [/etc/spine.conf]
- SPINE: Version 0.8.8b starting
- SPINE: Time: 0.1386 s, Threads: 5, Hosts: 2
- [cacti@squid-2 cacti-0.8.8b]$
启动成功了。
8,最后,等cacti安装完之后在cacti的web配置界面选择setting
settings->paths->Spine Poller File Path [/usr/local/spine/bin/spine]
settings->poller->Poller Type [spine]
console->settings->poller->Maximum Threads per Process [5]
save完成。
四、我们开始安装正主cacti
1 配置
解压并放到apache文档目录(/var/www/html)
tar -xf cacti-0.8.8b.tar.gz
mv cacti-0.8.8b /var/www/html/cacti
cd /var/www/html/
编辑CentOS cacti配置文件,连接MySQL服务的用户名,密码,端口等。
- vim /var/www/html/cacti/include/config.php
- #
- $database_type = /"mysql/";
- $database_default = /"cacti/";
- $database_hostname = /"localhost/";
- $database_username = /"cacti_user/";
- $database_password = /"cacti_user05161610/";
- $database_port = /"3306/";
- $database_ssl = false;
2添加计划任务(使cacti每5分钟获得一次数据)
#crontab -e
*/1 * * * * /usr/bin/php /var/www/html/cacti/poller.php > /dev/null 2>&1 //让系统每1分钟收集
3 数据库配置
在前面的第三步已经配置好了,创建数据库cacti以及用户。
4 测试,建立一个login.jsp文件
[root@mysqlvm2 cacti]# vim login.php
simple demo
phpinfo();
?>
如下图所示:
打开网址页面:
启动完httpd之后,可以打开 http://10.xx.1.xx/,界面如下:
遇到的小问题,打开index.php是空页面,如下所示:
去查看日志,日志没有任何信息:
问题在哪里呢?
找到apache的配置文件httpd.conf
- [root@mysqlvm2 www]# vim /etc/httpd/conf/httpd.conf
- Search error_log
- 日志路径 /var/log/httpd/error_log
- [root@mysqlvm2 cacti]# cd /var/log/httpd/
- [root@mysqlvm2 httpd]# ll
- 总计 20
- -rw-r--r-- 1 root root 9228 05-16 18:39 access_log
- -rw-r--r-- 1 root root 2049 05-16 18:14 error_log
- -rw-r--r-- 1 root root 0 05-16 17:18 ssl_access_log
- -rw-r--r-- 1 root root 1422 05-16 18:14 ssl_error_log
- -rw-r--r-- 1 root root 0 05-16 17:18 ssl_request_log
逐个查看log信息,在ssl_error_log里面发现错误信息:
- [root@mysqlvm2 httpd]# tail -f ssl_error_log
- [Fri May 16 18:00:36 2014] [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
- [Fri May 16 18:00:36 2014] [warn] RSA server certificate CommonName (CN) `localhost.localdomain/' does NOT match server name!?
- [Fri May 16 18:14:39 2014] [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
- [Fri May 16 18:14:39 2014] [warn] RSA server certificate CommonName (CN) `localhost.localdomain/' does NOT match server
- [Fri May 16 18:14:39 2014] [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
- [Fri May 16 18:14:39 2014] [warn] RSA server certificate CommonName (CN) `localhost.localdomain/' does NOT match server name!?
- [Fri May 16 18:48:48 2014] [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
- [Fri May 16 18:48:48 2014] [warn] RSA server certificate CommonName (CN) `localhost.localdomain/' does NOT match server
- [Fri May 16 18:48:48 2014] [warn] RSA server certificate is a CA certificate (BasicConstraints: CA == TRUE !?)
- [Fri May 16 18:48:48 2014] [warn] RSA server certificate CommonName (CN) `localhost.localdomain/
经过朋友的提醒,还需要安装php-mysql组件
[root@mysqlvm2 httpd]# yum install -y php-mysql
之后,再打开页面,http://10.254.1.12/cacti/install/,就有信息如下:
点击next,继续下一步,其他的都死默认选项,默认登录用户名密码是admin:
五:Cacti监控mysql报表流量图:
wget https://mysql-cacti-templates.googlecode.com/files/better-cacti-templates-1.1.8.tar.gz
tar –xvf better-cacti-templates-1.1.8.tar.gz
cd better-cacti-templates-1.1.8
将better-cacti-templates-1.1.8/scripts下的 ss_get_mysql_stats.php 这个脚本 这个脚本需要放在cacti的服务端。
比如cacti部署在/var/www/html目录下,那么就cp到/var/www/html/cacti/scripts/下
cp /root/better-cacti-templates-1.1.8/scripts/ss_get_mysql_stats.php /var/www/html/cacti/scripts/
修改ss_get_mysql_stats.php 文件 第30行
$mysql_user = 'cacti_user';
$mysql_pass = 'cacti_user05161610';
$cache_dir = "/xok.la/cacti/cache/";
chown -R apache.apache /var/www/html/cacti/scripts
chmod -R 755 /var/www/html/cacti/scripts
使用 http 访问 cacti主机 导入
/opt/mysql-cacti-templates-1.1.2/cacti_host_template_x_db_server_ht_0.8.6i.xml
然后创建mysql用户:
grant process,super on *.* to 'cacti_user'@'%' identified by 'cacti_user05161610';
上一篇: 详解Javascript中的闭包
下一篇: 3种PHP实现数据采集方法