企鵝學步--Zabbix 監控服務器搭建

監控服務器: Hostname: station3.example.com   IP: 192.168.1.3   OS: CentOS5.7 32bit

一、yum安裝LAMP
1.1安裝主程序
# yum -y install httpd php mysql mysql-server php-mysql
1.2安裝apache擴展 
# yum -y install httpd-manual mod_ssl mod_perl mod_auth_mysql
1.3安裝php擴展 
# yum -y install php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc php-bcmath
1.4安裝mysql擴展
# yum -y install mysql-connector-odbc mysql-devel libdbi-dbd-mysql
1.5編譯的apache加載php

# vim httpd.conf
增加391 DirectoryIndex index.php index.html index.html.var        #php目錄索引
1.6開機啓動
# service httpd start
# chkconfig httpd on
# service mysqld start
# chkconfig mysqld on

1.7設置mysql數據庫root帳號密碼。
# mysqladmin -u root password 'redhat'   
1.8測試apache與php、mysql的連接:
# cd /var/www/html
# vim index.php
it works!
<?php
$link=mysql_connect('localhost','root','redhat');
if($link)
   echo "success";
else
   echo "fail";
?>
測試成功
二、Zabbix 安裝
2.1yum安裝zabbix需要的其他組件
# yum -y install net-snmp-devel curl-devel 
2.2添加zabbix用戶
# groupadd zabbix           添加zabbix組             
# useradd zabbix -g zabbix   新建zabbix用戶並將其加入到zabbix組 
2.3解壓軟件
# tar zxvf zabbix-2.0.3.tar.gz -C /usr/src
# cd /usr/src/zabbix-2.0.3 
2.4爲zabbix創建數據庫並添加用戶
# /etc/init.d/mysqld start
Starting MySQL                      [  OK  ]
# mysql -uroot -predhat
mysql> create database zabbix character set utf8;#新建一個數據庫叫zabbix  
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'redhat';   
#給zabbix這個數據庫授權,只允許zabbix這個用戶通過本地登錄,zabbix用戶的密碼爲redhat 
mysql> flush privileges;
2.5將zabbix源碼包中的數據導入到新建的zabbix數據庫
注:這一步是zabbix2.3與其他舊版不同的,在版本2.3裏數據庫的結構和名字都變了,而且導入也要嚴格按照順序來。
# mysql -uzabbix -p'redhat' zabbix < /usr/src/zabbix-2.0.3/database/mysql/schema.sql      
#這個是zabbix的數據庫表結構,要先導入。  
# mysql -uzabbix -p'redhat' zabbix < /usr/src/zabbix-2.0.3/database/mysql/p_w_picpaths.sql  
# mysql -uzabbix -p'redhat' zabbix < /usr/src/zabbix-2.0.3/database/mysql/data.sql

2.6編譯安裝
# yum -y install gcc*  先安裝gcc庫,否則編譯報錯找不到C編譯器
# ./configure --prefix=/usr/local/zabbix --enable-server --enable-proxy --enable-agent --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libcurl
*******************************
  Now run 'make install'    
*******************************
[root@station3 zabbix-2.0.3]# make install

2.7 爲zabbix server添加端口
[root@station3 ~]# vim /etc/services 
添加如下信息
zabbix-agent    10050/tcp                       # Zabbix Agent 
zabbix-agent    10050/udp                       # Zabbix Agent 
zabbix-trapper  10051/tcp                       # Zabbix Trapper 
zabbix-trapper  10051/udp                       # Zabbix Trapper

2.8 配置文件目錄軟連接
# ls /usr/local/zabbix/etc
zabbix_agent.conf    zabbix_agentd.conf    zabbix_proxy.conf    zabbix_server.conf
zabbix_agent.conf.d  zabbix_agentd.conf.d  zabbix_proxy.conf.d  zabbix_server.conf.d
# ln -s /usr/local/zabbix/etc /etc/zabbix

2.9更改配置文件中數據庫相關的用戶名密碼
# vim /etc/zabbix/zabbix_server.conf 
修改以下三項: 
DBName=zabbix 
DBUser=zabbix 
DBPassword=redhat 
注:DBPassword 默認是被註釋掉的

# vim /etc/zabbix/zabbix_agentd.conf
Hostname=station3.example.com
ServerActive=192.168.1.3:20051


2.10給zabbix服務端程序做軟鏈接
# ll /usr/local/zabbix/bin
total 428
-rwxr-xr-x 1 root root 195326 Oct 24 13:59 zabbix_get
-rwxr-xr-x 1 root root 230852 Oct 24 13:59 zabbix_sender
# ll /usr/local/zabbix/sbin
total 5048
-rwxr-xr-x 1 root root  547886 Oct 24 13:59 zabbix_agent
-rwxr-xr-x 1 root root  622208 Oct 24 13:59 zabbix_agentd
-rwxr-xr-x 1 root root 1878726 Oct 24 13:59 zabbix_proxy
-rwxr-xr-x 1 root root 2097879 Oct 24 13:59 zabbix_server

# cd /usr/local/zabbix/bin/  
# for i in *;do ln -s /usr/local/zabbix/bin/${i} /usr/bin/${i};done  
# cd /usr/local/zabbix/sbin/  
# for i in *;do ln -s /usr/local/zabbix/sbin/${i} /usr/sbin/${i};done

2.11添加數據庫Lib文件位置到/etc/ld.so.conf中,並使其生效
# echo "/usr/local/mysql-5.1.48/lib/mysql/" >> /etc/ld.so.conf  
# ldconfig

2.12拷貝相應的web程序到相關WEB服務目錄下
# cp -r /usr/src/zabbix-2.0.3/frontends/php/ /var/www/html/zabbix/
# chown -R zabbix.zabbix /var/www/html/zabbix

2.13拷貝zabbix服務端和客戶端啓動腳本到/etc/init.d目錄下.
注:這個地方存放腳本的目錄結構也有變化,沒有redhat這個目錄,所以我cp的fedora目錄裏的啓動腳本,具體區別可以閱讀README。關鍵是是看zabbix_xxxx的文件結構符合redhat的Sys V結構。
# ll /usr/src/zabbix-2.0.3/misc/init.d/fedora/core5
total 8
-rwxr-xr-x 1 1005 1005 541 Oct  3 22:41 zabbix_agentd
-rwxr-xr-x 1 1005 1005 543 Oct  3 22:41 zabbix_server
# cp /usr/src/zabbix-2.0.3/misc/init.d/fedora/core5/zabbix_server /etc/init.d
# cp /usr/src/zabbix-2.0.3/misc/init.d/fedora/core5/zabbix_agentd /etc/init.d
2.14修改啓動腳本
# vim /etc/init.d/zabbix_server 
ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server"  修改zabbix_server實際位置
# service zabbix_server start
Starting Zabbix Server:                                    [  OK  ]
# vim /etc/init.d/zabbix_agentd
ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd"
# service zabbix_agentd start
Starting Zabbix Agent:                                     [  OK  ]
[root@station3 ~]# chkconfig zabbix_server on
[root@station3 ~]# chkconfig zabbix_agentd on
2.15修改PHP配置文件php.ini內容
# vim /etc/php.ini 
date.timezone = Asia/Shanghai 
post_max_size = 32M 
max_execution_time = 300 
max_input_time = 300
memory_limit = 128M
mbstring.func_overload = 2
# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

四、瀏覽器安裝
(1)瀏覽器打開剛安裝的zabbix  http://192.168.1.3/zabbix

 

 

154942156.jpg

 

155210298.jpg

 # yum -y install php-bcmath     yum安裝的php直接
自己編譯php,那麼在編譯的時候加--enable-bcmath
cd php-5.2.7/ext/bcmath
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config 
make && make install (make 可以得到so路徑)
so路徑拷貝到  extension路徑
vim php.ini 
extension=bcmath.so
重啓apache
檢測php組件,全部OK纔可以點擊next

155351889.jpg

 (3)連接zabbix數據庫 

155538493.jpg

 (4)設置zabbix服務IP和端口,name可以忽略

155651852.jpg

155831233.jpg

如果不能保存配置文件,點擊Down將php生成的文件下載到本地,然後在上傳到/var/www/html/zabbix/conf下,也可以用本地記事本打開zabbix.conf.php,複製其中的內容,在 vim /var/www/html/zabbix/conf/zabbix.conf.php,將剛纔複製的內容粘貼,保存退出,在點擊try連接測試,出現下圖的OK即可。

(5)輸入用戶名密碼登錄
默認的用戶名:admin   密碼:zabbix

 

 

201044155.jpg

 三、排錯過程

安裝完成,但有過問題,zabbix server竟然不能監控自己。 即使重啓服務,任然報錯。not running, not monitored.

# service zabbix_server restart

#service zabbix_agentdd restart  

202324195.jpg

 排錯思路:1. 服務  2.端口   3.配置文件 4. 日誌

檢查對應進程都已經啓動
# ps aux | grep zabbix
zabbix    4229  0.0  0.1   6016   736 ?        S    09:31   0:00 /usr/local/zabbix/sbin/zabbix_agentd
zabbix    4242  0.0  0.1   6016   800 ?        S    09:31   0:00 /usr/local/zabbix/sbin/zabbix_agentd
zabbix    4243  0.0  0.1   6016   608 ?        S    09:31   0:00 /usr/local/zabbix/sbin/zabbix_agentd
zabbix    4244  0.0  0.1   6016   608 ?        S    09:31   0:00 /usr/local/zabbix/sbin/zabbix_agentd
zabbix    4245  0.0  0.1   6016   608 ?        S    09:31   0:00 /usr/local/zabbix/sbin/zabbix_agentd
zabbix    4246  0.0  0.1   6032   812 ?        S    09:31   0:00 /usr/local/zabbix/sbin/zabbix_agentd
zabbix    4248  0.0  0.4  10264  2484 ?        S    09:31   0:00 /usr/local/zabbix/sbin/zabbix_server
root     15127  0.0  0.1   4880   668 pts/1    R+   12:56   0:00 grep zabbix
查看對應的日誌錯誤,默認在/tmp/zabbix-*.log
# ll /tmp | grep zabbix
-rw-rw-r-- 1 zabbix zabbix    900 Oct 25 12:55 zabbix_agentd.log
-rw-rw-r-- 1 zabbix zabbix      4 Oct 25 09:31 zabbix_agentd.pid
-rw-rw-r-- 1 zabbix zabbix 362401 Oct 25 13:30 zabbix_server.log
-rw-rw-r-- 1 zabbix zabbix      4 Oct 25 09:31 zabbix_server.pid
# tail /tmp/zabbix_server.log
4248:20121025:133318.754 [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'root'@'localhost' (using password: NO)
  4248:20121025:133318.755 Database is down. Reconnecting in 10 seconds.
這是未將/usr/local/zabbix/etc/zabbix.conf.php中DB參數修改,連接失敗所致。
[root@station3 ~]# vim /etc/zabbix/zabbix_server.conf 
修改以下三項: 
DBName=zabbix 
DBUser=zabbix 
DBPassword=redhat 
# cat /tmp/zabbix_server.log
17376:20121025:143052.725 cannot send list of active checks to [127.0.0.1]: host [station3.example.com] not found
是因爲zabbix_agentd.conf未設置IP:Port
# vim /etc/zabbix/zabbix_agentd.conf
ServerActive=192.168.1.3:20051 

在configuration-> Hosts面板中Status,啓用monitored, 才能監控server。

202929494.jpg

 

 

203038680.jpg

 

 

203134626.jpg

 

 特別鳴謝:linuxsong對我的啓發,算是拋磚引玉吧。期待大家的指點,多謝! 

 

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