一、cacti概述
二、cacti工作流程
三、cacti安装
四、配置cacti监控本机
环境:
操作系统:CentOS 6.4 x86_64
软件:Cacti-0.8.7e
官方网站:http://www.cacti.net
一、cacti概述
Cacti 在英文中的意思是仙人掌的意思,Cacti是一套基于PHP、MySQL、SNMP及RRDTool开发的网络流量监测图形分析工具。它通过snmpget来获取数据,使用 RRDtool绘画图形,它的界面非常漂亮,能让你根本无需明白rrdtool的参数能轻易的绘出漂亮的图形。而且你完全可以不需要了解RRDtool复杂的参数。它提供了非常强大的数据和用户管理功能,可以指定每一个用户能查看树状结 构、host以及任何一张图,还可以与LDAP结合进行用户验证,同时也能自己增加模板,让你添加自己的snmp_query和script!功能非常强大完善,界面友好。可以说,Cacti将rrdtool的所有“缺点”都补足了!下图是Cacti运行的主界面,
二、cacti工作流程
下面我们来简单的说明一下Cacti的工作流程,
步骤一,snmp协议收集远程服务器的数据
步骤二,将snmp收集的数据内容保存到rrd数据库中
步骤三,若用户查看某台设备上的流量或其它状态信息
步骤四,在mysql数据库中查找该设备对应的rra数据库文件的名称
步骤五,通过rrdtool命令进行绘图即可
好了,在前面的几篇博客中我们讲解了SNMP协议,也讲解了RRDTool工具的使用,现在再来学习Cacti就非常的简单了,下面我们来安装并使用Cacti。
三、cacti安装
注,Cacti的运行是基于LAMP环境的首先我们来安装LAMP环境。(LAMP环境安装有两种方法,有时间的博友可以使用源码安装,不想用源码安装的博友可以参考我的安装方法全部用yum安装。)下面我们来具体演示一下,
1、更换系统默认的yum源
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@node1 ~]
# rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
Retrieving http:
//pkgs
.repoforge.org
/rpmforge-release/rpmforge-release-0
.5.3-1.el6.rf.x86_64.rpm
warning:
/var/tmp/rpm-tmp
.1lW03k: Header V3 DSA
/SHA1
Signature, key ID 6b8d79e6: NOKEY
Preparing...
########################################### [100%]
1:rpmforge-release
########################################### [100%]
[root@allentuns ~]
# ll /etc/yum.repos.d/
total 24
-rw-r--r--. 1 root root 739 Mar 20 2013 mirrors-rpmforge
-rw-r--r--. 1 root root 717 Mar 20 2013 mirrors-rpmforge-extras
-rw-r--r--. 1 root root 728 Mar 20 2013 mirrors-rpmforge-testing
drwxr-xr-x. 2 root root 4096 May 23 15:00 old
-rw-r--r--. 1 root root 1128 Mar 20 2013 rpmforge.repo
|
2、时间同步
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@node1 ~]
# yum -y install ntp
[root@node1 ~]
# ntpdate -u asia.pool.ntp.org
4 Jun 10:25:26 ntpdate[1375]: step
time
server 202.73.36.32 offset -28799.877146 sec
[root@node1 ~]
# date
Wed Jun 4 10:25:29 EDT 2014
[root@node1 ~]
# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp
: overwrite `
/etc/localtime
'?
yes
[root@node1 ~]
# date
Wed Jun 4 22:32:39 CST 2014
[root@node1 ~]
# crontab -l
*
/1
* * * *
/usr/sbin/ntpdate
-u asia.pool.ntp.org
[root@node1 ~]
# service crond restart
Stopping crond: [ OK ]
Starting crond: [ OK ]
|
3、安装lamp环境
1
|
[root@node1 ~]
# yum install -y httpd php php-mysql php-snmp php-xml php-gd mysql mysql-server gd gd-devel
|
设置开机自启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
[root@node1 ~]
# service httpd start
Starting httpd: [ OK ]
[root@node1 ~]
# chkconfig httpd on
[root@node1 ~]
# service mysqld start
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot
time
you have to copy
support-files
/mysql
.server to the right place
for
your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To
do
so, start the server,
then
issue the following commands:
/usr/bin/mysqladmin
-u root password
'new-password'
/usr/bin/mysqladmin
-u root -h node1 password
'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which
will also give you the option of removing the
test
databases and anonymous user created by default. This is
strongly recommended
for
production servers.
See the manual
for
more
instructions.
You can start the MySQL daemon with:
cd
/usr
;
/usr/bin/mysqld_safe
&
You can
test
the MySQL daemon with mysql-
test
-run.pl
cd
/usr/mysql-test
; perl mysql-
test
-run.pl
Please report any problems with the
/usr/bin/mysqlbug
script!
[ OK ]
Starting mysqld: [ OK ]
[root@node1 ~]
# chkconfig mysqld on
[root@node1 ~]
# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1048
/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1124
/master
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1625
/mysqld
tcp 0 0 :::80 :::* LISTEN 1425
/httpd
tcp 0 0 :::22 :::* LISTEN 1048
/sshd
tcp 0 0 ::1:25 :::* LISTEN 1124
/master
|
测试phpinfo测试页面
1
2
3
4
5
|
[root@node1 ~]
# cat >> /var/www/html/index.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
|
测试php页面
好了!如果大家能看到这个phpinfo的页面,说明我们的apache和php是安装成功的
接着我们来测试我们的mysql数据是否成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@node1 ~]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and
/or
its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and
/or
its
affiliates. Other names may be trademarks of their respective
owners.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
|
test
|
+--------------------+
3 rows
in
set
(0.00 sec)
mysql> \q
Bye
|
好了,到这里Apache+PHP+MySQL就安装成功了。下面我们来看重点
net-snmp和RRDTool
4、安装net-snmp
1
|
[root@node1 ~]
# yum install -y net-snmp net-snmp-utils
|
修改snmp的配置文件
1
2
3
4
5
6
|
[root@node1 ~]
# cd /etc/snmp/
[root@node1 snmp]
# cp snmpd.conf snmpd.conf.bak
[root@node1 snmp]
# vim snmpd.conf
#修改55行view值
55
#view systemview included .1.3.6.1.2.1.1
56 view systemview included .1.3.6.1.2.1
|
接下来启动net-snmp服务并测试
1
2
3
4
5
6
7
8
9
10
11
|
root@node1 snmp]
# service snmpd start
Starting snmpd: [ OK ]
[root@node1 snmp]
# snmpnetstat -v 2c -c public -Ca -Cp tcp localhost
Active Internet (tcp) Connections (including servers)
Proto Local Address Remote Address (state)
tcp *.
ssh
*.* LISTEN
tcp *.mysql *.* LISTEN
tcp localhost.smtp *.* LISTEN
tcp localhost.smux *.* LISTEN
tcp localhost.
ssh
localhost.52311 ESTABLISHED
[root@node1 snmp]
#
|
好了,到这里我们net-snmp安装完成,下面我们来安装RRDTool绘图工具。
5、安装RRDTool
1
|
[root@node1 ~]
# yum -y install rrdtool
|
接下来测试RRDTool
1
2
3
4
5
6
7
8
9
10
|
[root@node1 ~]
# rrdtool -v
RRDtool 1.3.8 Copyright 1997-2009 by Tobias Oetiker <tobi@oetiker.ch>
Compiled Aug 21 2010 10:57:18
Usage: rrdtool [options]
command
command_options
Valid commands: create, update, updatev, graph, graphv, dump, restore,
last, lastupdate, first, info, fetch, tune,
resize, xport
RRDtool is distributed under the Terms of the GNU General
Public License Version 2. (www.gnu.org
/copyleft/gpl
.html)
For
more
information
read
the RRD manpages
|
好了,到这里我们基本环境准备就全部完成了,下面我们来安装与配置Cacti。
6.安装与配置Cacti
6.1、 解压cacti压缩包并移动到指定的路径下
1
2
3
4
5
6
7
8
9
|
[root@node1 ~]
# cd /usr/local/src/
[root@node1 src]
# ls
cacti-0.8.7e.
tar
.gz
[root@node1 src]
# tar xf cacti-0.8.7e.tar.gz -C /var/www/html/
[root@node1 src]
# cd /var/www/html/
[root@node1 html]
# ls
cacti-0.8.7e index.php
[root@node1 html]
# ln -sv cacti-0.8.7e cacti
`cacti
' -> `cacti-0.8.7e'
|
6.2、其中cacti.sql是保存了cacti到所有表的语句,但是没有创建数据库的语句,所以下面我们来配置一下Cacti数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@node1 ~]
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection
id
is 4
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and
/or
its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and
/or
its
affiliates. Other names may be trademarks of their respective
owners.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
mysql> create database cactidb;
mysql> use cactidb;
mysql>
source
/var/www/html/cacti/cacti
.sql
mysql> \q
Bye
|
6.3、因为cacti要访问数据库,所以我们得给cacti创建一个授权用户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[root@node1 ~]# mysql
Welcome to the MySQL monitor. Commands end
with
; or \g.
Your MySQL connection id
is
6
Server version:
5.1
.
73
Source distribution
Copyright (c)
2000
,
2013
, Oracle and/or its affiliates. All rights reserved.
Oracle
is
a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to clear the current input statement.
mysql> GRANT ALL ON cactidb.* TO cactiuser@localhost IDENTIFIED BY
'cactiuser'
;
Query OK,
0
rows affected (
0.00
sec)
mysql> flush privileges;
Query OK,
0
rows affected (
0.00
sec)
mysql> \q
Bye
[root@node1 ~]# mysql -ucactiuser -pcactiuser
Welcome to the MySQL monitor. Commands end
with
; or \g.
Your MySQL connection id
is
7
Server version:
5.1
.
73
Source distribution
Copyright (c)
2000
,
2013
, Oracle and/or its affiliates. All rights reserved.
Oracle
is
a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cactidb |
| test |
+--------------------+
3
rows
in
set
(
0.00
sec)
mysql> \q
Bye
|
6.4、下面我们来修改cacti程序的配置文件
1
2
3
4
5
6
7
8
9
10
11
|
[root@node1 ~]
# cd /var/www/html/cacti/include/
[root@node1 include]
# cp config.php config.php.bak
[root@node1 include]
# vim config.php
#需要修改的行
/*
make
sure these values refect your actual database
/host/user/password
*/
$database_type =
"mysql"
;
$database_default =
"cactidb"
;
$database_hostname =
"localhost"
;
$database_username =
"cactiuser"
;
$database_password =
"cactiuser"
;
$database_port =
"3306"
;
|
6.5、下面我们来创建一个cacti用户(安装初始化cacti)并修改相关目录权限,
1
2
3
|
[root@node1 ~]
# cd /var/www/html/cacti
[root@node1 cacti]
# useradd cactiuser
[root@node1 cacti]
# chown -R cactiuser:cactiuser log/ rra/
|
6.6、浏览器访问cacti的初始化页面并安装cacti
cacti的初始化界面,如下图
点击【Next】继续
接下来让你选择全新安装还是升级安装,我们这里肯定选择【New Install】,点击【Next】继续,
接下来会检查所有配置选项,若有不符合要求的会用红色标出,我们这里全部符合要求,点击【Finish】继续,
直接跳转到cacti的登录页面,到这里我们cacti初始化就全部完成了,下面我们登录一下,默认用户名和密码都为”admin”
第一下次登录时让你修改默认的用户名和密码,自己设置一个即可。
首次登陆cacti的登陆界面,如下图
至此cacti的安装就完成了,接下来我们来配置cacti监控本地主机的硬件信息
设置cacti收集数据的默认的任务计划。
这里任务计划是由poller.php程序执行的,需要说明的是poller.php是由php写的,单线程的功能相对较弱,为此cacti官方单独开发了一个多线程Spine工具,适合大规模监控应用。好了下面我们就来配置一下任务计划并查看。
1
2
3
|
[root@node1 ~]
# echo '*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php &>/dev/null' > /var/spool/cron/cactiuser
[root@node1 ~]
# crontab -u cactiuser -l
*
/5
* * * *
/usr/bin/php
/var/www/html/cacti/poller
.php &>
/dev/null
|
为了防止这个任务计划不执行,首次我们最好手动验证一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
[root@node1 ~]
# su cactiuser
[cactiuser@node1 root]$
/usr/bin/php
/var/www/html/cacti/poller
.php
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 866
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 872
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 1183
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 1961
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 3027
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 3592
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 3667
PHP Deprecated: Assigning the
return
value of new by reference is deprecated
in
/var/www/html/cacti-0
.8.7e
/lib/adodb/adodb
.inc.php on line 3687
PHP Warning: strtotime(): It is not safe to rely on the system
's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected '
America
/New_York
' for '
EDT
/-4
.0
/DST
' instead
in
/var/www/html/cacti-0
.8.7e
/include/global_constants
.php on line 156
PHP Warning:
date
(): It is not safe to rely on the system
's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected '
America
/New_York
' for '
EDT
/-4
.0
/DST
' instead
in
/var/www/html/cacti-0
.8.7e
/include/global_constants
.php on line 156
PHP Warning: strtotime(): It is not safe to rely on the system
's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected '
America
/New_York
' for '
EDT
/-4
.0
/DST
' instead
in
/var/www/html/cacti-0
|