穩坐CACTI,遙知千里

    Cacti是一套基於PHP,MySQL,SNMP及RRDTool開發的網絡流量監測圖形分析工具。使用cacti可以有效的監控設備的具體情況。以下篇幅,筆者將講述如何安裝cacti以及使用cacti來監控各種網絡設備。

實現需求:某公司的Web服務器,使用的是lamp技術,公司擬對Web服務器(包括操作系統、mysql、apache等信息)、公司的二層交換機、防火牆進行監控。

實驗拓撲:

top

實驗設備:監控主機(Linux 5.4)

Web服務器(Linux 5.4 lamp環境)

H3c二層交換機 quidway 2000 series(2403H-EI)

Juniper防火牆(ssg 5)

一、Web服務器配置

 1. LAMP環境搭建

(1)、準備環境

首先需要爲Web主機配置正確的ip地址和網關等參數,另外需要配置好yum倉庫。

(2)、安裝相關的組件

//安裝Apahce, PHP, MySQL以及php連接mysql庫組件。
#yum -y install httpd php mysql mysql-server php-mysql
//安裝apache擴展,讓apache更好的支持其他的軟件。
#yum -y install httpd-manual mod_ssl mod_perl mod_auth_mysql
//安裝php的擴展,apache本身並不支持php文件,要安裝對應的php軟件,然後進行http.conf配置;讓apache能解析.php文件。
#yum -y install php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc
//安裝MySQL的擴展,更好的實現mysql的功能。
#yum -y install mysql-connector-odbc mysql-devel libdbi-dbd-mysql

(3)、修改apache的配置文件

# vim /etc/httpd/conf/httpd.conf

391 DirectoryIndex index.php index.html index.html.var

//啓動各項服務並設置爲開機啓動

# service httpd start &&chkconfig httpd on

# service mysqld start &&chkconfig mysqld on

(4)、測試相關服務情況

//測試apache的運行情況,查看是否正常調用php

# cd /var/www/html

# vim index.php

<?php

phpinfo();

?>

//使用客戶端查看情況

linux.1.1.1

//測試apache與mysql的連接性

<?php

$lin=mysql_connect('127.0.0.1','root','');

if($lin)

echo "ok";

else

echo "failed";

?>

//使用客戶端查看情況

linux.1.1.2

2. SNMP設置

(1)、安裝snmp的組件

# yum install net-snmp

(2)、修改相關的配置文件

# vim /etc/snmp/snmpd.conf

62 access notConfigGroup "" any noauth exact all none none

打開85行

85 view all included .1 80

# service snmpd start && chkconfig snmpd on

3. MySQL配置

爲了便於mysql被網管到,需要對mysql進行相關的設置

# mysql

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 35

Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

//管理mysql需要相關的權限,將所需要的權限賦予網管主機

mysql&gt; grant process,super on *.* to 'cacti'@'192.168.10.0' identified by 'cacti';

Query OK, 0 rows affected (0.01 sec)

//授權網管主機進行網管

mysql&gt; grant all privileges on cacti.* to cacti@"192.168.10.0" identified by "cacti";

Query OK, 0 rows affected (0.03 sec)

//刷新

mysql&gt; flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql&gt; \q

Bye

//重啓服務

# service mysqld restart

Stopping MySQL: [ OK ]

Starting MySQL: [ OK ]

#

二、H3C交換機配置

1. 基本配置

//配置主機名稱

[Quidway]sysname sw

//爲vlan1配置管理IP地址

[sw]inter vl 1

[sw-Vlan-interface1]ip add 192.168.10.2 255.255.255.0

[sw-Vlan-interface1]quit

//配置與防火牆相連接口

[sw]inter eth0/10

[sw-Ethernet0/10]port link-t tr

[sw-Ethernet0/10]port tr per vl all

請等待...:........................................ 完成.

[sw-Ethernet0/10]quit

2. SNMP配置

[sw]snmp //開啓SNMP

[sw]snmp community write private

[sw]snmp community read public //溝通的重要密鑰

[sw]snmp sys-info version v2c //版本一定要和主監控設備的版本一致

[sw]snmp target-host tr add u 192.168.10.2 pa sec public v2 //配置遠端的追蹤目標

三、Juniper防火牆配置

 1. 基本配置

//配置主機名稱

ssg5-serial-&gt; set hostname firewall

//配置與交換機相連的接口組的地址

firewall -&gt; set inter bgroup0 zone trust

firewall -&gt; set inter bgroup0 ip 192.168.10.2 255.255.255.0

//開啓bgroup0的各項網管功能。可以不開啓

firewall -&gt; set inter bgroup0 manage

//配置外口的地址

firewall -&gt; set inter eth0/0 zone Untrust

firewall -&gt; set inter eth0/0 ip 100.100.100.1 255.255.255.0

//保存配置

firewall -&gt; save

Save System Configuration ...

Done

2. SNMP配置

配置snmp的名稱,並創建新的團體。

juniper.1.1.1

juniper.1.1.2

查看新創建的團體信息

juniper.1.1.3

四、監控主機配置

 1. 搭建Cacti

(1)、安裝相關組件

//首先需要將所需要的組件上傳到監控主機。筆者上傳的所有文件均位於管理員的家目錄。

monitor.1.1.1

# yum install httpd php php-mysql php-snmp mysql-server perl-DBD-MySQL php-pdo net-snmp net-snmp-libs net-snmp-utils net-snmp-devel ruby ruby-devel –y

//啓動相關的服務,並設置爲開機啓動

# service httpd start && chkconfig httpd on

# service mysqld start && chkconfig mysqld on

(2)、修改snmp

//修改本機的snmp配置,使本機也可以被網管到

# vim /etc/snmp/snmpd.conf

62 access notConfigGroup "" any noauth exact all none none

打開85行

85 view all included .1 80

//啓動snmp服務,並設置爲開機啓動

# service snmpd start && chkconfig snmpd on

(3)、配置rrdtool

//回到安裝目錄,查看相關文件的信息

# cd ~/cacti

# ll

total 9056

-rw-r--r-- 1 root root 13083 Feb 8 02:53 ApacheStats_0.8.2.zip

-rw-r--r-- 1 root root 2236916 Feb 8 02:53 cacti-0.8.7g.tar.gz

-rw-r--r-- 1 root root 207069 Feb 8 02:53 cacti-plugin-0.8.7g-PA-v2.8.tar.gz

-rw-r--r-- 1 root root 4960221 Feb 8 02:53 feition.tar.bz2

-rw-r--r-- 1 root root 442525 Feb 8 02:53 monitor-latest.tgz

-rw-r--r-- 1 root root 1154026 Feb 8 02:53 rrdtool-1.4.4-1.el5.wrl.i386.rpm

-rw-r--r-- 1 root root 55839 Feb 8 02:53 rrdtool-perl-1.4.4-1.el5.wrl.i386.rpm

-rw-r--r-- 1 root root 18906 Feb 8 02:53 rrdtool-ruby-1.4.4-1.el5.wrl.i386.rpm

-rw-r--r-- 1 root root 6701 Feb 8 02:53 sendMsg.zip

-rw-r--r-- 1 root root 25570 Feb 8 02:53 settings-latest.tgz

-rw-r--r-- 1 root root 90488 Feb 8 02:53 thold-latest.tgz

#

//安裝rrdtools

# yum localinstall rrdtool* --nogpgcheck –y

//拆解 cacti的數據包到相關的目錄中

#tar -zxvf cacti-0.8.7g.tar.gz -C /var/www/html

//切換到相關目錄中

# cd /var/www/html/

//爲了便於記憶,修改目錄的名稱。

# mv cacti-0.8.7g/ cacti

(4)、MySQL配置

//打開mysql

# mysql

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

//創建cacti數據庫

mysql&gt; CREATE DATABASE cacti;

Query OK, 1 row affected (0.00 sec)

//賦予本地相關的權限

mysql&gt; grant all privileges on cacti.* to cacti@localhost identified by 'cacti';

Query OK, 0 rows affected (0.01 sec)

mysql&gt; grant all privileges on cacti.* to [email protected] identified by 'cacti';

Query OK, 0 rows affected (0.00 sec)

//賦予遠端相關的權限

mysql&gt; grant all privileges on cacti.* to [email protected] identified by 'cacti';

Query OK, 0 rows affected (0.00 sec)

//刷新

mysql&gt; flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql&gt; \q

Bye

#

(5)、其它配置

//創建計劃任務

# crontab –e

*/5 * * * * php /var/www/html/cacti/poller.php &gt;/dev/null 2&gt;&1

#

//查看創建的計劃任務

# crontab -l

*/5 * * * * php /var/www/html/cacti/poller.php &gt;/dev/null 2&gt;&1

#

//創建cacti的帳號,並賦予其密碼

# useradd cacti

# passwd cacti

Changing password for user cacti.

New UNIX password:

BAD PASSWORD: it is too simplistic/systematic

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

#

//將cacti的數據庫導入到mysql中

#mysql -ucacti -pcacti cacti &lt;/var/www/html/cacti/cacti.sql

# cd /var/www/html/

# cd cacti/

//修改所有文件的擁有者信息

# chown -R root.root *

//修改配置文件

# vim include/config.php

26 $database_type = "mysql";

27 $database_default = "cacti";

//注意此處一定要寫localhost

28 $database_hostname = "localhost";

//數據庫的用戶名和密碼一定是cacti

29 $database_username = "cacti";

30 $database_password = "cacti";

31 $database_port = "3306";

#

//重啓apache

# service httpd restart

#

2. 安裝插件管理器

#cd ~/cacti

//解壓有關插件

# tar -zxvf cacti-plugin-0.8.7g-PA-v2.8.tar.gz

# cp -R cacti-plugin-arch/* /var/www/html/cacti/

# cd /var/www/html/cacti/

//導入sql文件

# mysql -ucacti -pcacti cacti &lt;pa.sql

//打補丁

# patch -p1 -N &lt;cacti-plugin-0.8.7g-PA-v2.8.diff

//修改url的路徑

# vim include/config.php

42 $url_path = "/cacti/";

# cd ~/cacti/

//解壓縮

# tar -zxvf monitor-latest.tgz

# tar -zxvf thold-latest.tgz

#tar -zxvf settings-latest.tgz

//移動文件到相關的目錄

#mv monitor-0.9/ /var/www/html/cacti/plugins/monitor

#mv thold-0.41/ /var/www/html/cacti/plugins/thold

#mv settings-0.6/ /var/www/html/cacti/plugins/settings

#

3. 配置管理界面

進入管理界面,輸入http://192.168.10.1/cacti

monitor.1.1.2

monitor.1.1.3

monitor.1.1.4

monitor.1.1.5

monitor.1.1.6 

勾選相關的組件

monitor.0.1.1

monitor.0.1.2

安裝有關組件

monitor.1.2.3

monitor.1.2.4

設置版本和設置報警信息

monitor.1.2.6

monitor.0.1.3

五、監控Web服務器

在添加Web服務器之前,可以在監控主機上使用以下命令來測試,Web服務器是否可以被網管到。如果出現很多數據證明可以被網管,否則就是無法網管,需要查找原因。

# snmpwalk -v2c -c public 192.168.10.3

1. 添加設備

monitor.1.2.7

monitor.1.2.8

添加相關的管理項,並創建新的視圖

monitor.1.2.10

monitor.1.2.14

2. 添加樹

monitor.1.2.16

monitor.1.2.17

將已經配置好的主機添加到剛剛創建的樹中

monitor.1.2.18

3. 查看監控主機

monitor.1.2.20

六、監控 Web服務器的Apache

 1. 在監控主機上配置

//解壓縮文件

# unzip ApacheStats_0.8.2.zip

# cd ApacheStats_0.8.2

# ll

total 116

-rw-r--r-- 1 root root 102879 Oct 5 2009 cacti_host_template_webserver_-_apache.xml

-rw-r--r-- 1 root root 4108 Oct 5 2009 ss_apache_stats.php

#

//將文件移動到相關的目錄中

# mv ss_apache_stats.php /var/www/html/cacti/scripts/

2. 導入模版

monitor.1.3.1

monitor.1.3.2

3. 添加到設備

monitor.1.3.3

4. 繪圖

monitor.1.3.6

monitor.1.3.7

5. 查看

monitor.1.3.9

七、監控 Web服務器的MySQL

1. 在監控主機上配置

 (1)、解壓縮

//解壓縮

# tar -zxvf mysql-cacti-templates-1.1.2.tar.gz

# cd mysql-cacti-templates-1.1.2

//將文件移動到相關目錄

# mv ss_get_mysql_stats.php /var/www/html/cacti/scripts/

#

(2)、修改相關配置文件

# cd /var/www/html/cacti/scripts/

# vim ss_get_mysql_stats.php

修改30、31、34行數據

30 $mysql_user = 'cacti';

31 $mysql_pass = 'cacti';

34 $cache_dir = '/var/www/html/cacti/cache';

(3)、修改權限

# cd /var/www/html/cacti/

# mkdir cache

# chmod 777 -R cache/

# chown -R apache.apache cache/

2. 導入模版

monitor.1.4.2

monitor.1.4.3

3. 添加到設備

monitor.1.4.5

4. 繪圖

monitor.1.4.7

monitor.1.4.8

5. 查看

monitor.1.4.9

八、監控二層交換機

在監控之前,需要知道二層交換機的OID,否則將無法實現監控。筆者從網上已經查到了H3C交換機2403H-EI的OID值。在配置之前,需要在監控主機上,測試是否能夠網管到二層交換機。

# snmpwalk -v2c -c public 192.168.10.2 1.3.6.1.4.1.2011.6.1.2.1.1.2.0

//出現以下數據,說明正常,可以網管

SNMPv2-SMI::enterprises.2011.6.1.2.1.1.2.0 = Gauge32: 16760832

1. 複製並修改數據模版

(1)、複製數據模版

monitor.2.1.1

monitor.2.1.2

(2)、修改複製的數據模版

monitor.2.1.3

monitor.2.1.4

2. 複製並修改繪圖模版

(1)、複製模版

monitor.2.1.6

monitor.2.1.8

(2)、修改模版

monitor.2.1.9

複製過來的數據條目刪除,創建新的數據條目

monitor.2.1.10

monitor.2.1.11

3. 添加設備

添加新的設備

monitor.2.1.12

monitor.2.1.13

創建繪圖

monitor.2.1.14

monitor.2.1.18

4. 創建樹

monitor.2.1.19

monitor.2.1.20

5. 將設備添加到樹中

monitor.2.1.22

6. 查看

monitor.2.1.24

九、監控防火牆

在監控之前,需要知道防火牆的OID,否則將無法實現監控。筆者從網上已經查到了Juniper防火牆SSG 的OID值。在配置之前,需要在監控主機上,測試是否能夠網管到防火牆。

# snmpwalk -v2c -c public 192.168.10.254 .1.3.6.1.4.1.3224.16.3.2.0

//出現以下數據,說明正常,可以網管

SNMPv2-SMI::enterprises.3224.16.3.2.0 = INTEGER: 4

1. 複製並修改數據模版

(1)、複製數據模版

monitor.3.1.1

monitor.3.1.2

(2)、修改複製的數據模版

monitor.3.1.4

2. 複製並修改繪圖模版

 (1)、複製模版

monitor.3.1.6

monitor.3.1.8

(2)、修改模版

複製過來的數據條目刪除,創建新的數據條目

monitor.3.1.10

monitor.3.1.11

monitor.3.1.12

3. 添加設備

添加新的設備

monitor.3.2.1

monitor.3.2.2

創建繪圖

monitor.3.2.7

monitor.3.2.8

4. 創建樹

monitor.3.2.9

monitor.3.2.10

5. 將設備添加到樹中

monitor.3.2.12

6. 查看

monitor.3.2.14

十、報警測試

將Webserver服務器down掉,結果5分鐘後,出現了報警聲音,警告已經丟失了目標主機。

monitor.3.2.15

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章