zabbix監控之zabbix源碼部署 (安裝篇)

【本文檔所介紹的內容適用於公司測試/生產環境等常見的zabbix監控系統部署】

一:Zabbix部署前環境要求:

1.1相關軟件以及系統

系統要求:Centos 6.6 (64位)

運行環境要求: php環境(LAMP或 LNMP)

相關中間件:zabbix-2.4.6.tar.gz

1.2 檢查系統自帶是否安裝Zabbix軟件包

rpm -qa | grep zabbix

如有安裝,請使用以下命令卸載相關程序

yum remove zabbix

二:zabbix正式部署安裝

2.1 zabbix 運行的WEB環境安裝 (zabbix的安裝需要LAMP或者LNMP環境)

(1)安裝Apahce, PHP, MySQL以及php連接mysql庫組件

yum -y install httpd php mysql mysql-server php-mysql gcc gcc-c++ 

(2)安裝php擴展

yum -y install php-gd gd-devel php-xml php-mbstring php-ldap php-pear php-xmlrpc php-bcmath libcurl-devel libxml2-devel curl-devel openssl-devel

上述安裝完成後,需要配置Apache主配置文件和php.ini文件以支持zabbix安裝,如下所示:

vim /etc/httpd/conf/httpd.conf
找到:ServerName 80
ServerName xxxx.com:80    //取消掉註釋符號,改爲"ServerName localhost:80";
vim /etc/php.ini
修改內容如下
date.timezone = Asia/Chongqing
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
post_max_size = 32M

(3)安裝MySQL鏈接驅動擴展

yum -y install mysql-devel

(4)啓動相關服務(mysql,httpd)並設置數據庫密碼

service httpd start
service mysqld start
mysqladmin -u root password 123.com

創建一個php測試頁面

vim /var/www/html/info.php

內容如下:

<?php
$conn=mysqli_connect('localhost','root','123.com');
if ($conn)
  echo "<h2>success...</h2>";
else
  echo "<h2>Failure...</h2>";
?>
<?php
phpinfo();
?>

輸入http://ip/info.php  訪問,出現如下頁面說明zabbix運行環境ok了

wKiom1b1CA-gLkA7AACS-a7vsF4897.png

以上爲RPM方式安裝LAMP環境,如要源碼安裝LAMP請訪問:http://blief.blog.51cto.com/6170059/1658994 

備註:

Zabbix對PHP參數、PHP模塊有特殊要求,安裝zabbix時PHP需要開啓以下支持:

模塊名稱
開啓方式
bcmath  --enable-bcmath
mbstring
--enable-mbstring
sockets 
--enable-sockets
gd   
--with-gd
libxml 
--with-libxml-dir=path
xmlwriter
--with-libxml-dir=path
xmlreader--with-libxml-dir=path
ctype
默認支持
session默認支持
gettext
默認支持


2.2 zabbix 安裝

首先到Zabbix官網(www.zabbix.com)下載Zabbix源碼包,這裏以zabbix-2.4.6包爲例;

(1)安裝zabbix所需的組件(server,agent)

yum -y install  net-snmp-devel

(2)安裝zabbix服務端程序

2.1.1 編譯安裝zabbix服務端(server服務端)

<--install zabbix-server-->

* 創建zabbix服務運行用戶(server,agent)

groupadd -r zabbix
useradd -r -g zabbix  -d /usr/local/zabbix -M -s /sbin/nologin zabbix

* 編譯安裝zabbix-server

tar zxvf  zabbix-2.4.6.tar.gz  -C /usr/local/src
cd  /usr/local/src/zabbix-2.4.6
./configure \
--prefix=/usr/local/zabbix-server \
--enable-server \                  #開啓zabbix server服務
--enable-proxy \                   #開啓zabbix 代理監控功能,常用於分佈式監控
--with-mysql \                     #使用MySQL
--with-libxml2 \                   #開啓虛擬機監控
--with-libcurl \                   #開啓web監控
--with-net-snmp                    #開啓網絡監控
make && make install

* 安裝完成後(如有報錯請根據錯誤提示排錯)還需要將創建zabbix系統服務啓動腳本,內容如下:

#!/bin/sh

# Zabbix
# Copyright (C) 2001-2015 Zabbix SIA
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Start/Stop the Zabbix server daemon.
# Place a startup script in /sbin/init.d, and link to it from /sbin/rc[023].d 
. /etc/rc.d/init.d/functions

BASEDIR=/usr/local/zabbix-server
exec="${BASEDIR}/sbin/zabbix_server"
prog=zabbix_server
pidfile=/var/run/zabbix_server.pid
config="${BASEDIR}/etc/zabbix_server.conf"
lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon   $exec -c $config
    retval=$?
    echo
    if [ $retval -eq 0 ]; then
        touch $lockfile || retval=4
    fi
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc  -p ${pidfile} $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p ${pidfile} $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
start)
     rh_status_q && exit 0
     $1
     ;;
stop)
     rh_status_q || exit 0
     $1
     ;;
restart)
     $1
     ;;
reload)
     rh_status_q || exit 7
     $1
     ;;
status)
     rh_status
     ;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
     exit 2
esac

說明

(zabbix系統服務啓動腳本默認存放在源碼解壓包下的/misc/init.d子目錄下,也可以使用自帶的系統服務啓動腳本)

zabbix_server啓動後默認沒有偵聽狀態(即 netstat -nltp | grep zabbix_server)所以需要另外添加偵聽狀態:

echo "zabbix-trapper 10051/tcp    # Zabbix Trapper" >>/etc/services
echo "zabbix-trapper 10051/udp    # Zabbix Trapper" >>/etc/services

備註:

zabbix-agent 10050 :通常爲zabbix客戶端偵聽端口 & zabbix-trapper 10051 :通常爲zabbix服務端偵聽端口 

2.2.2 導入zabbix圖形模板數據及zabbix服務端配置

編譯安裝完zabbix後需要導入zabbix監控模板數據和修改zabbix服務端配置文件

* 將監控模板數據導入到數據庫

1.登錄數據庫創建zabbix數據庫並授權

 mysql -u root -p123.com
> create database zabbix;
> grant all on zabbix.* to zabbix@localhost identified by '123.com';
> flush privileges;

2.導入zabbix監控模板數據到zabbix數據庫

cd  /usr/local/src/zabbix-2.4.6/database/mysql/
mysql -uzabbix -p123.com zabbix < schema.sql
mysql -uzabbix -p123.com zabbix < images.sql
mysql -uzabbix -p123.com zabbix < data.sql

備註:

zabbix的模板數據文件主要存放源碼解壓包下的/database子目錄下,該目錄下存放着各種類型的數據庫的模板文件,有MySQL,oracle等等常用的數據模板文件

* 配置zabbix服務端文件

1.默認情況下zabbix服務端配置文件和客戶端配置(開啓了agent功能)文件都存放在zabbix安裝目錄下的/etc文件夾下,如下:

[root@lvs01 mysql]# ll /usr/local/zabbix/etc/
total 40
-rw-r--r--. 1 root root 13051 Mar 24 04:04 zabbix_proxy.conf
drwxr-xr-x. 2 root root  4096 Mar 24 04:04 zabbix_proxy.conf.d
-rw-r--r--. 1 root root 13296 Mar 24 04:04 zabbix_server.conf
drwxr-xr-x. 2 root root  4096 Mar 24 04:04 zabbix_server.conf.d

2.備份原有zabbix_server.conf文件,新添加一個zabbix_server.conf文件,內容如下:

LogFile=/var/log/zabbix_server.log
PidFile=/var/run/zabbix_server.pid
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123.com
DBSocket=/var/lib/mysql/mysql.sock
StartDiscoverers=5
CacheSize=256M
StartDBSyncers=5
HistoryCacheSize=128Mvi
TrendCacheSize=64M
HistoryTextCacheSize=256M
ValueCacheSize=128M
Timeout=30
AlertScriptsPath=/usr/local/zabbix/alertscripts
ExternalScripts=/usr/local/zabbix/externalscripts
LogSlowQueries=10000
StartProxyPollers=3

3.重啓zabbix服務

service zabbix_server restart

備註:

因爲zabbix服務運行賬戶是zabbix,所以有可能對"/var/run"和"/var/log"沒有寫入權限,如果沒有權限寫入,使用"chmod o+rwx"授予權限即可

2.2.3 部署zabbix web服務站點

導入完監控模板數據文件和配置完zabbix server端配置文件後,剩下就需要配置zabbix站點web服務,如下:

cd  /usr/local/src/zabbix-2.4.6
mkdir /var/www/html/zabbix
cp -r frontends/php/* /var/www/html/zabbix

複製位於conf下的zabbix.conf.php.example文件,操作如下:

cd  /var/www/html/zabbix/conf
cp zabbix.conf.php.example zabbix.conf.php

接下來就開始初始化zabbix,如下:

輸入http://ip/zabbix, (這裏爲:http://172.51.96.175/zabbix)即可訪問zabbix初始化頁面  


wKiom1b0xySTjln8AAG5Ssm8H2g247.png


點擊“next”會出現如下頁面

wKiom1b0yE7in413AAIUs-fRIGg241.png

這裏:zabbix會檢查zabbix運行的web環境,當出現“ok”的狀態的時候,才允許“next”,否則會提示“filed”,如有出現filed可根據提示排錯


wKiom1b0ytLRI3j7AAHqUIcg7FQ712.png


這裏需要設置數據庫連接信息,設置完成後,測試一下是否可以連接到數據庫,否則就無法進行下一步;

wKioL1b0zCOg7y1TAAG7DX5xyFk759.png

注意:這裏是設置zabbix的主機名,數據傳輸端口以及zabbix名稱的地方;關於主機名一定主機本身能夠解析到該主機名,否則容易出現服務啓動錯誤


wKiom1b0zIPgU3VtAAHGMmorTx0047.png

設置完成後,可以看到設置信息,如上


wKioL1b0zWCBKghYAAGHSnD6MZM112.png

到了這裏,當出現“ok”狀態說明zabbix初始化完成,這一步有可能會出現“filed”提示沒有權限寫入,如果出現沒有權限寫入就根據提示授予可讀寫權限即可;

當初始化完之後,會出現zabbix監控主頁,如下所示

wKioL1b0z9vgBj4HAAEfbauKWFg711.png

注意:

如果出現“zabbix server is not running ”即zabbix狀態不是“yes”而是“no”,解決辦法如下:

1. 重啓zabbix_server 服務; 2. 關閉seLinux或執行“setsebool -P httpd_can_network_connect on


至此zabbix服務端就安裝完成了,下面在服務器上安裝zabbix_agent就可以實現對server的監控了

(3)安裝zabbix客戶端程序

1.編譯安裝zabbix客戶端(agentd端)

tar zxvf  zabbix-2.4.6.tar.gz  -C /usr/local/src
cd  /usr/local/src/zabbix-2.4.6
./configure \
--prefix=/usr/local/zabbix-agent \
--enable-agent \                  #開啓zabbix agentd 
make && make install

* 安裝完成後(如有報錯請根據錯誤提示排錯)還需要將創建zabbix_agentd系統服務啓動腳本,內容如下:

#!/bin/sh

# Zabbix
# Copyright (C) 2001-2015 Zabbix SIA
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# Start/Stop the Zabbix server daemon.
# Place a startup script in /sbin/init.d, and link to it from /sbin/rc[023].d 
. /etc/rc.d/init.d/functions

BASEDIR=/usr/local/zabbix-agent
exec="${BASEDIR}/sbin/zabbix_agentd"
prog=zabbix_agentd
pidfile=/var/run/zabbix_agentd.pid
config="${BASEDIR}/etc/zabbix_agentd.conf"
lockfile=/var/lock/subsys/$prog

start() {
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6
    echo -n $"Starting $prog: "
    daemon   $exec -c $config
    retval=$?
    echo
    if [ $retval -eq 0 ]; then
        touch $lockfile || retval=4
    fi
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc  -p ${pidfile} $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p ${pidfile} $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
start)
     rh_status_q && exit 0
     $1
     ;;
stop)
     rh_status_q || exit 0
     $1
     ;;
restart)
     $1
     ;;
reload)
     rh_status_q || exit 7
     $1
     ;;
status)
     rh_status
     ;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload}"
     exit 2
esac

說明

(zabbix系統服務啓動腳本默認存放在源碼解壓包下的/misc/init.d子目錄下,也可以使用自帶的系統服務啓動腳本)

2.添加zabbix_agentd偵聽狀態:

echo "zabbix-agent 10050/tcp      # Zabbix Agentd" >>/etc/services
echo "zabbix-agent 10050/udp      # Zabbix Agentd" >>/etc/services

2. 修改zabbix_agentd主配置文件

備份原有zabbix_server.conf文件,新添加一個zabbix_server.conf文件,內容如下:

LogFile=/var/log/zabbix_agentd.log    #設置log存放路徑
PidFile=/var/run/zabbix_agentd.pid    #設置pid文件存放路徑
Server=127.0.0.1                      #設置zabbix-server端連接地址
ServerActive=127.0.0.1                #設置zabbix-server連接地址
Hostname=localhost                    #設置主機名,注意與zabbix-server無關
UserParameter=custom.vfs.dev.read.ops[*],cat /proc/diskstats | grep $1 | head -1 |awk '{print $$4}'
UserParameter=custom.vfs.dev.read.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}'
UserParameter=custom.vfs.dev.write.ops[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}'
UserParameter=custom.vfs.dev.write.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}'
UserParameter=custom.vfs.dev.io.active[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}'
UserParameter=custom.vfs.dev.io.ms[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}'
UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}'
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}'

3.重啓zabbix服務

service zabbix_server restart

(4)登錄zabbix開啓server監控:

如下點擊“disable”按鈕就可以監控對server的監控了

wKiom1b1DPLzZw44AAFaYNmcc_c798.png

開啓後,我們發現狀態變爲“enable”說明zabbix_server已經開始對server監控了

wKiom1b1DWrSbGefAAHTpiNugOU090.png


以上就是整個zabbix服務端以及客戶端的安裝過程,關於zabbix監控使用是一個比較複雜的過程,後面還會繼續講到zabbix監控(比如主機性能監控,分佈式監控等等)總之:zabbix監控是一個循環漸進的過程,在實際應用環境中用到只是zabbix一部分,以後將會繼續講到zabbix

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