運維監控之cacti
Cacti是一套基於PHP,MySQL,SNMP及RRDTool開發的網絡流量監測圖形分析工具。
首先我們需要搭建lamp環境和安裝snmp
安裝cacti所需要的軟件包
[root@chh1 cacti]# ll
total 9064
-rw-r--r-- 1 root root 2236916 Oct 11 11:21 cacti-0.8.7g.tar.gz
-rw-r--r-- 1 root root 207069 Oct 11 11:21 cacti-plugin-0.8.7g-PA-v2.8.tar.gz
-rw-r--r-- 1 root root 4960221 Oct 11 11:21 feition.tar.bz2
-rw-r--r-- 1 root root 442525 Oct 11 11:21 monitor-latest.tgz
-rw-r--r-- 1 root root 1154026 Oct 11 11:21 rrdtool-1.4.4-1.el5.wrl.i386.rpm
-rw-r--r-- 1 root root 55839 Oct 11 11:21 rrdtool-perl-1.4.4-1.el5.wrl.i386.rpm
-rw-r--r-- 1 root root 18906 Oct 11 11:21 rrdtool-ruby-1.4.4-1.el5.wrl.i386.rpm
-rw-r--r-- 1 root root 6701 Oct 11 11:21 sendMsg.zip
-rw-r--r-- 1 root root 25570 Oct 11 11:21 settings-latest.tgz
-rw-r--r-- 1 root root 12114 Oct 11 11:21 test.png
-rw-r--r-- 1 root root 9091 Oct 11 11:21 test1.png
-rw-r--r-- 1 root root 90488 Oct 11 11:21 thold-latest.tgz
安裝軟件包
[root@chh1 cacti]# 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
啓動apache和mysql並設爲開機啓動
[root@chh1 cacti]# service httpd start
Starting httpd: vh [ OK ]
[root@chh1 cacti]# chkconfig httpd on
[root@chh1 cacti]# service mysqld start
[root@chh1 cacti]# chkconfig mysqld on
cacti的架構示意圖
最底層是數據採集層,我們可以通過snmp或者腳本來獲取需要監控的對象的數據,中間是數據的存儲型有環形數據庫RRDtool和mysql構成,上層是展示層通過apache和php以及cacti來展示監控對象的狀態。
安裝cacti的軟件包
給mysql的root用戶設置口令
[root@chh1 cacti]# chkconfig mysqld on
[root@chh1 cacti]# mysqladmin -u root -p password '123'
Enter password:
安裝rrd環裝數據庫
[root@chh1 cacti]# yum localinstall rrd* --nogpgcheck -y
解壓cacti到/var/www/html/
[root@chh1 cacti]# tar -zxvf cacti-0.8.7g.tar.gz -C /var/www/html/
[root@chh1 html]# mv cacti-0.8.7g/ cacti
創建cacti賬號
[root@chh1 html]# useradd cacti
[root@chh1 html]# passwd cacti
在mysql數據庫中添加cacti數據庫
mysql> create database cacti;
Query OK, 1 row affected (0.00 sec)
給數據庫授權apache調用php訪問cacti時對mysql可以訪問
將cacti數據庫授權給cacti用戶
mysql> grant all privileges on cacti.* to catci@localhost identified by 'cacti';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on cacti.* to [email protected] identified by 'cacti';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on cacti.* to [email protected] identified by 'cacti';
Query OK, 0 rows affected (0.00 sec)
刷新一下
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
導入cacti中的表格
[root@chh1 html]# mysql -ucacti -pcacti cacti </var/www/html/cacti/cacti.sql
修改cacti和mysql結合的文件
[root@chh1 cacti]# vim include/config.php
給log和rra目錄授權
[root@chh1 cacti]# chown -R cacti log rra
創建計劃任務每五分鐘獲取一下數據
準備工作基本做完,可以訪問一下
啓動snmpd服務
[root@chh1 cacti]# service snmpd start
關於Cacti設置
保存一下我們就可以看到信息
產生圖形監控
實驗監控拓撲
監控win server 2003
安裝snmp
監控linux-web
在cacti上導入監控apache的腳本文件
[root@chh1 ~]# mv ss_apache_stats.php /var/www/html/cacti/scripts/
打開apache的狀態輸出
[root@chh Server]# vim /etc/httpd/conf/httpd.conf
重啓apache
由於沒有監控apache的模板,所以我們需要導入模板。
導入模板(模板下載地址:http://forums.cacti.net/about25227.html&highlight=apachestats)
繪製圖像
然後將apache放到樹上面去。
安裝插件管理器
有了插件我們可以設置監控對象的預值報警,也可以直接查看他們的狀態。
[root@chh1 cacti]# tar -zxvf cacti-plugin-0.8.7g-PA-v2.8.tar.gz
將cacti-plugin-arch中的文件拷貝到cacti目錄中去
進入控制檯,對管理員啓用插件管理器
進入cacti的目錄
cd /var/www/html/cacti/
mysql -ucacti -pcacti cacti <pa.sql
打補丁
[root@chh1 cacti]# patch -p1 -N <cacti-plugin-0.8.7g-PA-v2.8.diff
編輯php的配置文件
vim include/config.php
改變url路徑爲 /cacti/
重啓一下apache
[root@chh1 include]# service httpd restart
然後來到cacti
拆解插件
tar -zxvf monitor-latest.tgz
tar -zxvf monitor-latest.tgz
tar -zxvf thold-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
到控制檯中啓用插件 在“plugin management”中
email郵件報警的配置
我們可以發送一下測試郵件
發送成功
郵件日誌
我的163郵箱記錄
預值自動報警的設置
保存一下
設置當用戶高於3個時就報警。
Win server監控
監控磁盤C的狀態
高於900M自動報警
163郵箱上的報警郵件
下面我們來看看飛信報警
實現飛信報警
1.下載最新版飛信機器人。
http://bbs.it-adv.net/viewthread.php?tid=1081&extra=page%3D1
根據情況下載,因爲會經常更新。 下載兩個一個是fetion程序,一個是Linux下的庫包linuxso-0101113.tar.gz
飛信機器人庫路徑處理
[root@chh1 linuxso_20101113]# cp lib* /lib/
測試飛信發送
下面我們編寫腳本,讓飛信機器人實現自動發送
新建sendsms文件夾
[root@chh1 ~]# mkdir /var/www/html/sendsms
[root@chh1 cacti]# mv linuxso_20101113/ /var/www/html/sendsms/linuxso
[root@chh1 ~]# cd /var/www/html/cacti/plugins/thold/
編輯發送腳本
[root@chh1 thold]# chmod a+x sendsms.sh
測試一下我們的腳本
[root@chh1 thold]# vim alert.log 隨便寫點東西
[root@chh1 thold]# ./sendsms.sh 執行發送消息測試
實現飛信與預值thold的結合
[root@chh1 thold]# vim thold_functions.php
當達到預值就會有報警信息發送到我們的手機上,下面來看一下我們的飛信消息。
這時163郵件也收到很多報警郵件