閱讀目錄:

1. 關閉防火牆

2. 安裝Zabbix下載源

3. ZabbixClient  --- 安裝zabbix-agent代理

4. ZabbixServer --- 安裝zabbix-server服務

5. ZabbixServer --- 配置zabbix-server的資料庫

6. ZabbixServer --- 配置zabbix-server的服務引數

7. ZabbixServer --- 使用zabbix-server

8. 登入Zabbix的Web端

9. 在Web配置zabbix監控agent

10. 安裝Grafana以及Zabbix外掛

11. 登入Grafana並且配置

序章:

Zabbix的一個很優秀的分散式監控伺服器, 它有兩部分組成:

1. “zabbix-server”用來收集並且在web端展示資料

2. “zabbix-agent”用來採集資料,傳送給server

在安裝Zabbix時,用了3臺虛擬機器來測試監控的資料是否正常採集、展示:

Server:

ZabbixServer --- IP : 192.168.246.133

Client:

ZabbixClient_1 --- IP : 192.168.246.134

ZabbixClient_2 --- IP : 192.168.246.135

1. 關閉防火牆

Linux的防火牆是咱們新手的噩夢,很多情況會出現能ping通,但是訪問不了Web頁面。所以開始就幹掉它!

// 關閉防火牆
[root@localhost ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]

// 開機關閉防火牆

[root@localhost ~]# chkconfig iptables off

2. 安裝Zabbix下載源

zabbix官方提供了下載源,直接執行下面命令,下載安裝就可以了

# rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm

提示安裝成功後,檢視Linux下載源,是否正確安裝,如果安裝成功,Linux下會有zabbix.repo原始檔

[root@localhost Desktop]# ll /etc/yum.repos.d/
total 28
-rw-r--r--. 1 root root 1991 Aug 4 00:13 CentOS-Base.repo
-rw-r--r--. 1 root root 647 Aug 4 00:13 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 289 Aug 4 00:13 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 Aug 4 00:13 CentOS-Media.repo
-rw-r--r--. 1 root root 6259 Aug 4 00:13 CentOS-Vault.repo
-rw-r--r--. 1 root root 401 Sep 11 2014 zabbix.repo

3. ZabbixClient --- 安裝zabbix-agent代理

分別在ZabbixClient_1與ZabbixClient_2安裝並配置如下設定:

關閉防火牆

[root@localhost Desktop]# /etc/init.d/iptables stop
[root@localhost Desktop]# chkconfig iptables off

安裝Zabbix官方下載源

[root@localhost Desktop]# rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm

安裝zabbix-agent,在下載的時候,會自動安裝zabbix-agent的基礎依賴包

[root@localhost Desktop]# yum install zabbix-agent

進入zabbix-agent的Config檔案

[root@localhost Desktop]# vim /etc/zabbix/zabbix_agentd.conf

配置zabbix-agent的引數,主要修改Server的IP和Hostname,

Server=192.168.246.133
Hostname=localhost

啟動zabbix-agent

[root@localhost Desktop]# /etc/init.d/zabbix-agent start

4. ZabbixServer --- 安裝zabbix-server服務

關閉防火牆

[root@localhost Desktop]# /etc/init.d/iptables stop
[root@localhost Desktop]# chkconfig iptables off

安裝Zabbix官方下載源

[root@localhost Desktop]# rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm

通過zabbix提供的下載源,下載並安裝zabbix-server-mysql和zabbix-web-mysql,這個兩個mysql資料庫是用來存放採集到的資料,Zabbix的web端會通過這些資料,來展示繪圖。而zabbix-get則是用來收集資料的指令。

# yum install zabbix-server-mysql zabbix-web-mysql zabbix-get

在下載這兩個包的時候,會提示你安裝基礎依賴包,其中這些基礎包中,還有zabbix-server的服務端的包,選擇“y”繼續即可。

// 以下是需要安裝的基礎依賴包
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
zabbix-server-mysql x86_64 2.4.6-1.el6 zabbix 1.5 M
zabbix-web-mysql noarch 2.4.6-1.el6 zabbix 14 k
Installing for dependencies:
OpenIPMI-libs x86_64 2.0.16-14.el6 base 473 k
fping x86_64 2.4b2-16.el6 zabbix-non-supported 31 k
iksemel x86_64 1.4-2.el6 zabbix-non-supported 47 k
net-snmp x86_64 1:5.5-54.el6_7.1 updates 308 k
php-bcmath x86_64 5.3.3-46.el6_6 updates 39 k
php-mbstring x86_64 5.3.3-46.el6_6 updates 459 k
php-mysql x86_64 5.3.3-46.el6_6 updates 86 k
zabbix x86_64 2.4.6-1.el6 zabbix 161 k
zabbix-server x86_64 2.4.6-1.el6 zabbix 22 k
zabbix-web noarch 2.4.6-1.el6 zabbix 4.5 M
Updating for dependencies:
net-snmp-libs x86_64 1:5.5-54.el6_7.1 updates 1.5 M
php x86_64 5.3.3-46.el6_6 updates 1.1 M
php-cli x86_64 5.3.3-46.el6_6 updates 2.2 M
php-common x86_64 5.3.3-46.el6_6 updates 529 k
php-gd x86_64 5.3.3-46.el6_6 updates 111 k
php-pdo x86_64 5.3.3-46.el6_6 updates 79 k
php-xml x86_64 5.3.3-46.el6_6 updates 107 k Transaction Summary
================================================================================

5. ZabbixServer --- 配置zabbix-server的資料庫

啟動mysql,我的CentOS在安裝的時候,就把mysql的軟體給安裝了,如果沒有mysql,則需要去安裝mysql,我這裡就直接啟動mysql就可以了

# /etc/init.d/mysqld start

成功啟動mysql的話,輸入mysql,就可以進入mysql的命令行了。進入命令列,建立zabbix資料庫

[root@localhost ~]# mysql
mysql> create database zabbix;
Query OK, 1 row affected (0.00 sec)

在mysql中,檢視剛剛建立的zabbix是否建立成功,如下圖資料庫中,有zabbix代表已經建立成功

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| zabbix |
+--------------------+
4 rows in set (0.00 sec)

接下要要執行zabbix官方提供是sql語句。在安裝基礎依賴包的時候,已經在檔案裡面了。現在可以直接執行

[root@localhost ~]# cd /usr/share/doc/zabbix-server-mysql-2.4.6/create/
[root@localhost create]# mysql -uroot zabbix < schema.sql
[root@localhost create]# mysql -uroot zabbix < images.sql
[root@localhost create]# mysql -uroot zabbix < data.sql

6. ZabbixServer --- 配置zabbix-server的服務引數

配置在以下路徑裡面 :

# vim /etc/zabbix/zabbix_server.conf

如果沒有特殊需要的話,使用預設配置就可以了,我的mysql賬戶是root,所以需要修改DBuser

LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
DBName=zabbix
DBUser=root
DBSocket=/var/lib/mysql/mysql.sock
StartPollers=5
CacheSize=256M
SNMPTrapperFile=/var/log/snmptt/snmptt.log
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts

修改zabbix的時區,有些系統並不是北京時間,如果是國外的時間,在展示出來的圖的時間就是錯的,為了保守起見,設定一下zabbix的時區

# vim /etc/httpd/conf.d/zabbix.conf

修改以下欄位

php_value date.timezone Asia/Shanghai

修改zabbix的php的時區

# vim /etc/php.ini

修改以下欄位

date.timezone = Asia/Shanghai

啟動Zabbix服務,啟動httpd:

[root@localhost create]# /etc/init.d/zabbix-server start
[root@localhost create]# /etc/init.d/httpd start

7. 使用zabbix-get

安裝好了zabbix-agent和zabbix-server。在Server端試試能不能收到agent端的資料,可以使用zabbix-get。在安裝mysql的時候,已經將zabbix-get安裝過了。可以使用zabbix_get -help命令來檢視該命令怎麼使用

[root@localhost create]# zabbix_get -help
Zabbix get v2.4.6 (revision 54796) (10 August 2015) usage: zabbix_get [-hV] -s <host name or IP> [-p <port>] [-I <IP address>] -k <key> Options:
-s --host <host name or IP> Specify host name or IP address of a host
-p --port <port number> Specify port number of agent running on the host. Default is 10050
-I --source-address <IP address> Specify source IP address -k --key <key of metric> Specify key of item to retrieve value for -h --help Display help information
-V --version Display version number Example: zabbix_get -s 127.0.0.1 -p 10050 -k "system.cpu.load[all,avg1]"

測試是否能收到資料,使用以下命令,發現是可以收到的。

// 測試是否可以收到系統的資訊
[root@localhost create]# zabbix_get -s 192.168.246.134 -p 10050 -k "system.uname"
Linux localhost.localdomain 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64

// 測試是否可以收到CPU的資料

[root@localhost create]# zabbix_get -s 192.168.246.134 -p 10050 -k "system.cpu.load"
0.020000

8. 登入Zabbix的Web端

在Server端,啟動Zabbix服務,啟動httpd:

[root@localhost create]# /etc/init.d/zabbix-server start
[root@localhost create]# /etc/init.d/httpd start

登入Zabbix的Web客戶端,在瀏覽器中輸入“http://192.168.246.133/zabbix/index.php”,預設會跳轉至Zabbix的setup引導頁面:

點選Next

點選Next

點選Test connection,否則無法點選Next,  測試連線成功後,點選Next

設定埠,可以直接使用預設設定的埠10051

點選Next

點選Next

點選Finish,完成初始化引導

輸入預設的賬號密碼,賬號:admin, 密碼zabbix

9. 在Web配置zabbix監控agent

點選下圖的Create host

按下圖配置,主意要輸入agent的IP和埠,埠預設是10050

點選select,選擇Linux OS和zabbix Server的模板,點選Select->Add->Add,如下圖

添加了以後就已經自動啟動了,可以點選Graphs,檢視是否有資料過來

點選CPU jumps

點選preview

就可以看到agent那邊有資料傳過來了

10. 安裝Grafana

在ZabbixServer伺服器上,安裝Grafana

yum install https://grafanarel.s3.amazonaws.com/builds/grafana-2.1.1-1.x86_64.rpm

下載Grafana的Zabbix外掛,在git上下載,如果無法執行下面命令的話,可以先“# yum install git”安裝這個命令

[root@localhost ~]# git clone https://github.com/linglong0820/grafana-zabbix

將外掛複製到Grafana檔案下

[root@localhost ~]# cp -r grafana-zabbix/zabbix/  /usr/share/grafana/public/app/plugins/datasource/

啟動Grafana

[root@localhost ~]# service grafana-server start
[root@localhost ~]# chkconfig grafana-server on

11. 登入Grafana並且配置

啟動Grafana後,在瀏覽器中輸入“http://192.168.246.133:3000/login”就可以看到頁面了。

輸入預設使用者名稱和密碼,使用者名稱:admin, 密碼:admin, 點選登入

按如下配置

【主意1】Zabbix的API地址為“http://192.168.246.133/zabbix/api_jsonrpc.php”,其中IP的安裝zabbix-server的伺服器IP

【主意2】Zabbix的API賬號密碼就是Zabbix的Web端登入賬號和密碼,賬號:admin, 密碼zabbix

點選Add後,點選Test Connection測試是否可以連線。提示“Success”說明連線成功,點選Save

點選Dashboard,去主目錄設定, 點選new,新建一個Home

新建一個Graph

點選下列紅框處->選擇Edit->選擇資料庫ZabbixClient

然後配置一下,就可以出現華麗麗的資料了,下圖都是官方的示意圖: