cacti是一套开源的网络监控工具,可以监控主机的状态和负载情况,
添加相应的模板后,可以用来监控apache服务器和mysql服务器的运行状态,
我安装这个主要是为了监控mysql服务器的运行状态的。
cacti的网站:http://www.cacti.net/
主机环境:CentOS 5.5 更新日期:20100717
mysql: mysql-5.0.77-4.el5_5.3
php:    php-5.1.6-27.el5
apache: httpd-2.2.3-43.el5.centos
cacti安装时会要求一些组件,比如snmp, php-xml,php-ldap(可选),
这些倒不用太担心,因为安装LAMP环境的时候多少也装了一些,
即便没有,请使用yum命令,我感觉centos的这个还是挺好的,
虽然软件有时有些老了,但是还是可以用的。手动把snmp的相关程序装下,
不然最后首次配置的时候会找不到一些snmpwalk,snmpget什么的
[root@tigertall bin]# rpm -qa|grep snmp
php-snmp-5.1.6-27.el5
net-snmp-utils-5.3.2.2-9.el5_5.1
net-snmp-5.3.2.2-9.el5_5.1
net-snmp-devel-5.3.2.2-9.el5_5.1
net-snmp-libs-5.3.2.2-9.el5_5.1
[root@tigertall bin]#

如果以上组件有缺失,请使用yum自己安装下。
使用yum安装 cacti
[gaohu@tigertall ~]$ sudo yum install cacti
这个命令会自动安装相关的组件。

1.php配置要求

[gaohu@tigertall ~]$ php -m 
这个命令可以检查php安装了哪些模块。
要求php扩展支持中已经配置了mysql.so,centos yum默认环境下
httpd的conf文件配置在  /etc/httpd/conf/httpd.conf 
默认的加载项(php,mysql)配置目录 /etc/httpd/conf.d。
[root@tigertall conf.d]# ls
bugzilla.conf  manual.conf  php.conf        
proxy_ajp.conf  README      ssl.conf       
welcome.conf cacti.conf     perl.conf   
phpMyAdmin.conf  python.conf    
squid.conf  webalizer.conf
[root@tigertall conf.d]# 
可以看到,很多web相关的配置都在这里。
php的基础配置在 /etc/php.ini。
[root@tigertall conf.d]# cd /etc/php.d
[root@tigertall php.d]# ls
dbase.ini         gd.ini        mcrypt.ini  mysql.ini   
pdo_mysql.ini   pgsql.ini      xmlwriter.ini
dom.ini           ldap.ini      mhash.ini  
ncurses.ini  pdo_pgsql.ini   snmp.ini       xsl.ini
eaccelerator.ini  mbstring.ini  mysqli.ini 
pdo.ini      pdo_sqlite.ini  xmlreader.ini
[root@tigertall php.d]#

php.d目录下设置了一些php扩展组件的配置。
查看mysql.ini,启用extension=mysql.so
查看snmp.in,启用extension=snmp.so
如果要启用导入模板的功能,还要启用文件上传,在/etc/php.ini中启用
file_uloads=on;
注释以下,如果没有注释
session.save_path = /tmp;

 

2.apache配置要求

 在apache配置/etc/httpd/conf.d/php.conf中,
检查一下语句是否启用,一般情况下,
应该是已经默认启用了的。
# PHP is an HTML-embedded scripting language which 
# attempts to make it
# easy for developers to write dynamically
# generated webpages.
LoadModule php5_module modules/libphp5.so
#
# Cause the PHP interpreter to handle files
# with a .php extension.
AddHandler php5-script .php
AddType text/html .php
#
# Add index.php to the list of files that
# will be served as directory
# indexes.
DirectoryIndex index.php

3.mysql配置要求

请记得给root设置一个秘密,以保护自己安全,呵呵。
可以使用如下的命令来更改root的密码:
mysqladmin --user=root password somepassword
mysqladmin --user=root --password reload

4.cacti安装配置

1.使用如下命令来安装cacti,这个会自动安装需要的组件。
[root@tigertall php.d]# yum install cacti

2.创建mysql数据库

[root@tigertall php.d]#
mysqladmin -uroot -phandomse create cacti

3.导
入默认的cacti数据库
如果使用yum默认安装,创建数据库的脚本
默认在
/usr/share/doc/cacti-0.8.7f/cacti.sql
[root@tigertall php.d]# cd /usr/share/doc/cacti-0.8.7f/

执行创建数据库的命令
[root@tigertall 0.8.7.f]#
mysql -uroot -phandomse cacti < cacti.sql

4.创建cacti数据库的用户和密码,
登录到数据库
[root@tigertall 0.8.7.f]#
mysql -u root -p 'pass'
用户授权,请把用户名,数据库和密码换成自己需要的设置。
mysql> GRANT ALL ON cacti.* TO cactiuser@localhost
--> IDENTIFIED BY 'password';
刷新权限

mysql> flush privileges;


5.编辑cacti配置,做数据连接设置
[root@tigertall include]# vi /usr/share/cacti/include/config.php
这里有cacti连接使用的数据库类型,用户名,密码信息

/* make sure these values refect \*/
/* your actual database/host/user/password */
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiuser";
$database_password = "cactiuser";
$database_port = "3306";

/* Default session name - Session name must */

/* contain alpha characters */
#$cacti_session_name = "Cacti";


把上述配置按照自己的需要设置

6.目录权限设置
[root@tigertall include]# cd /usr/share/cacti/
[root@tigertall cacti]# ll rra
lrwxrwxrwx 1 root root 18 Jul 17 15:07 rra -> /var/lib/cacti/rra

[root@tigertall cacti]# ll log
lrwxrwxrwx 1 root root 15 Jul 17 15:07 log -> /var/log/cacti/

从上述可以看到,rra实际上是var/lib/cacti/rra的一个软链接,
log则是/var/log/cacti的一个软链接。
更改上述目录的权限,按需要更改属主,下面生成数据会用到这个用户。
[root@tigertall cacti]# chown -R gaohu rra/ log/


7.配置自动作业时间
编辑crontab。
[root@tigertall log]# crontab -e
增加如下内容,
*/5 * * * * php /usr/share/cacti/poller.php > /dev/null 2>&1

注意:
这个地方不能按照官方文档来,不能加上执行的用户名,
crontab根本没有执行用户这个参数的,官方文档是这样的:
*/5 * * * * gaohu php /usr/share/cacti/poller.php > /dev/null 2>&1

按照这个设置,会报错的:
07/17/2010 10:59:57 PM - POLLER: Poller[0] WARNING:
Cron is out of sync with the Poller Interval! 
The Poller Interval is '300' seconds,
with a maximum of a '300' second Cron,
but 419 seconds have
passed since the last poll!
会告警说设置的是300秒,结果400多秒了还没有执行过。

以上内容意思是,每隔五分钟,执行下php脚本,统计下数据。


一个由此引发的问题见最后。

8.登录服务器
登录http://your-webserver/cacti
初始登录时,用户名和密码为admin,登录后,会要求立即切换密码。




4.Spine安装配置(可选)



spine是一个基于C语言的,非常快速的轮询引擎。它是默认的cmd.php的可选替代。
如果决定使用它的话,
需要自己来显式的安装和配置。cacti本身并不包含该引擎。
安装Spine的最简单的方法就是使用rpm或者使用ports。
如果使用yum来进行安装,需要添加rpmforge的软件仓库。
如果使用源码,可以在cacti的主站下载源码包。下载的地址为:
http://www.cacti.net/downloads/spine/cacti-spine-0.8.7g.tar.gz
1.从cacti网站下载spine源码包

2.解压到本地目录,编译安装
安装spine需要有net-snmp-devel、mysql、mysql-devel、openssl-devel支持,
请查看下是否已经安装。
使用以下命令来编译安装:
tar xvzf cacti-spine-0.8.7g.tar.gz
cd cacti-spine-0.8.7g
./configure
make
make install

默认安装在/usr/local/spine/目录下。

3.复制一份spine的配置文件。
[root@tigertall etc]# cd /usr/local/spine/etc/
[root@tigertall etc]# cp spine.conf.dist  spine.conf

编辑spine.conf文件,设置Cacti数据的连接信息。
DB_Host         localhost # 主机地址
DB_Database     cacti # 数据库
DB_User         gaohu # 数据库用户
DB_Pass         pass # 数据库连接密码
DB_Port         3306 # 数据库连接端口
DB_PreG         1 # 如果要配合0.8.7g之前的0.8.7的cacti工作,
# 则配置成为1。
# 因为我主机安装时cacti 0.8.7e 所以设置成了1。
# 如果都是从g版本的源码安装,可以设置为0
4.设置Spine
a)以admin用户登录到Cacti。
b)从目录菜单中选择设置"Settings",选择"Paths"选项卡。
c)在"Spine Poller File Path"中输入Spine可执行程序的完整路径,
对我来说是/usr/local/spine/spine,保存设置。
d)选择"Poller"选项卡。
e)在"Poller Type"下拉框设置中,选择"spine"。

完成这些设置后,Cacti将会每5分钟执行一次Spine,来代替原来的cmd.php。
如果使用spine的过程中有任何问题,你可以随时将"Poller Type"切换回"cmd.php"。




4.应用补丁(可选)

安装完成后,如果觉得需要,可以打相应的补丁,但是,不要把旧版本的补丁打到新的版本上。


大致的安装过程就是这样了。


5.Cacti的设置

下一篇写吧,呵呵 ^_^。

6.奇怪的问题^_^。

1.安装配置后没有图形
第一次使用的时候,需要手工执行下php /usr/share/cacti/poller.php,
等上几分钟,就可以显示图形了。

2.奇怪的权限
在第3节的配置中,我们使用了gaohu作为cacti的用户。
但是,使用yum安装cacti时候,系统会自动创建cacti用户。
按照cacti的要求,安第一次使用gaohu,
执行php /usr/share/cacti/poller.php的时候,
要使用对上述日志目录有写入授权的用户来操作。
但是执行这个命令的时候,很可能会报
[gaohu@tigertall ~]$ php /usr/share/cacti/poller.php
PHP Warning:  include(/usr/share/cacti/include/config.php):
failed to open stream: Permission denied in
/usr/share/cacti/include/global.php on line 45
PHP Warning:  include(): Failed
opening '/usr/share/cacti/include/config.php' for
inclusion (include_path='.:/usr/share/pear:/usr/share/php')
 in /usr/share/cacti/include/global.php on line 45
FATAL: Cannot connect to MySQL server on 'localhost'.
Please make sure you have specified
a valid MySQL database name in 'include/config.php'

开始只看到了最后的那句。老是以为是mysql有问题,结果看了下端口,没有问题
[gaohu@tigertall cacti]$ sudo netstat -apn|grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:*  LISTEN 3504/mysqld       

又以为是数据库的问题,可能是对cacti数据库的权限不够,
于是给gaohu用户增加了远程的全部权限,还是不行。
后来看到上面的错误,才感觉到奇怪。
PHP Warning:  include(/usr/share/cacti/include/config.php):
failed to open stream: Permission denied in
/usr/share/cacti/include/global.php on line 45


调用poller.php的时候,会调用/usr/share/cacti/include/global.php,
然后跟踪了下路径,发现了很奇怪的事情
[gaohu@tigertall cacti]$ ll include/global.php
-rw-r--r-- 1 root root 7410 May 20 09:07 include/global.php

[gaohu@tigertall cacti]$ ll /usr/share/cacti/include/config.php
lrwxrwxrwx 1 root root 17 Jul 17 15:07
/usr/share/cacti/include/config.php -> /etc/cacti/db.php
[gaohu@tigertall cacti]$ ll /etc/cacti/db.php
-rw-r----- 1 cacti apache 1915 Jul 17 17:03 /etc/cacti/db.php

这个db.php文件,只有cacti用户和apache用户可读写,只有cacti用户可写,
gaohu用户只是可以su的普通用户,不属于apache组,所以自然没有权限。导致报错。
两个解决办法:1.把gaohu加到apache组,
2.改变/etc/catic/db.php 文件的属性。
我是用的第一个,或许第二个会更好一点吧。
其实还有第3个办法,
就是把上面的文件那些切换属主的和编辑定时作业的都切换到cacti用户,
但是因为默认安装的cacti是没有登录终端的,
所以没有办法切换到su - cacti中,这个对一般用户不太好。根据自己的需要选择下吧。

然后应该就可以工作了。


posted on 2010-07-17 18:25  tigertall  阅读(11014)  评论(0编辑  收藏  举报