部署 Zabbix 集中監控系統 (基於 LAMP 架構)

Zabbix 概述

         Zabbix 是一個高度集成的企業級開源網絡監控解決方案,與 Cacti、Nagios 類似,提供分佈式監控以及集中的 Web 管理界面。由 zabbix serve r端與 zabbix agent 組成。被監控對象只有支持 SNMP 協議或者運行 Zabbix_agents 代理程序即可。Zabbix 的官方網址爲 http://www.zabbix.com/ ,軟件可以自由下載使用。

         Zabbix 具備常見商業監控軟件所具備的功能:主機性能檢測網絡設備性能監控、數據庫性能監控、ftp等同於協議的監控,能夠利用靈活的可定製警告機制,運行用戶對事件發送基於 E-mail 的警告,可以保證相關維護人員對問題作出快速響應,還可以利用存儲數據提供傑出的報表及實時的圖形化數據處理。

Zabbix 特點

   支持自動發現網絡設備和服務器
    支持分佈式監控
    可設置報警閥值
    可通過多種方式進行數據收集
    可定製的報警方式
    實時的繪圖功能
    通過web監控系統與設置

案例環境,如表所示。

主機                      操作系統                  IP地址                         主要操作

Zabbix服務器       Centos 7                     192.168.66.146           搭建LAMP 架構,部署zabbix-sever

Linux客戶機          Centos 7                     192.168.66.145           部署zabbix-agent


解決中文亂碼的軟件包鏈接:

鏈接:https://pan.baidu.com/s/1ObFt9zepWO9l05k-f79ZfQ
提取碼:nxy3

         Zabbix 通過 C/S 模式採集數據,通過 B/S 模式在 Web 端展示和配置。其中 Zabbix_server 可以運行在 CentOS 、RHEL 、SUSE 、Ubuntu 等 Linux 系統上,還需要使用 LAMP 平臺來承載數據庫和 Web 界面。

一. 在監控端部署 LAMP 架構

1.關閉防火牆

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service                  
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0

2.安裝 LAMP 所需的軟件包

[root@localhost ~]# yum install -y httpd mariadb-server mariadb php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash

3.修改配置文件

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf                 //httpd 的主配置文件

95 ServerName www.yun.com:80                                                 //設置servername

163 <IfModule dir_module>
164     DirectoryIndex index.html index.php                                 //頁面首頁類型支持 index.php

編輯 /etc/php.ini 配置文件,設置時區

[root@localhost ~]# vim /etc/php.ini

878 date.timezone = PRC                               //設置中國時區

4.啓動 httpd 服務和 maruadb 服務,並查看端口。

[root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# netstat -ntap | egrep '(3306|80)'                   //查看3306和80端口
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      4630/mysqld        
tcp6       0      0 :::80                   :::*                    LISTEN      4351/httpd  

5.初始化數據庫的配置    

[root@localhost ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):                   //回車進入
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y                                 //設置root密碼
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
  ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n              //是否刪除匿名用戶 n
  ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n         //運行遠程登錄
  ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n              //刪除測試數據庫 n
  ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y                         //重新加載
  ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

6.數據庫初始化完成後,即可登錄數據庫

[root@localhost ~]# mysql -uroot –p                //使用root 用戶登錄數據庫
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;         //創建zabbix 數據庫,被設置字符集爲簡體中文
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;                //查看數據庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> grant all privileges on *.* to 'zabbix'@'%' identified by 'admin123';  

//創建zabbix用戶,所有庫、表都給zabbix 管理,密碼爲:admin123
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;                //刷新數據庫
Query OK, 0 rows affected (0.00 sec)

使用創建用戶zabbix登錄數據庫

[root@localhost html]# mysql -uzabbix -p
Enter password:
ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: YES)

使用 zabbix 用戶登錄數據庫失敗,其原因是有空用戶名稱佔用導致本地無法連接,解決方法爲刪除空用戶名

以root 用戶登錄數據庫,刪除空用戶名

[root@localhost html]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> select user,host from mysql.user;                //查看當前用戶
+--------+-----------------------+
| user   | host                  |
+--------+-----------------------+
| zabbix | %                     |
| root   | 127.0.0.1             |
| root   | ::1                   |
|        | localhost             |                                    //存在兩個空用戶名
| root   | localhost             |
|        | localhost.localdomain |
| root   | localhost.localdomain |
+--------+-----------------------+
7 rows in set (0.00 sec)

MariaDB [(none)]> drop user ''@localhost;                      //刪除空用戶
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> drop user ''@localhost.localdomain;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit
Bye

此時退出,再以 zabbix 用戶登錄數據庫

[root@localhost html]# mysql -uzabbix -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)

7. 測試 php ,能否使用 zabbix 用戶連接數據庫

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.php                  編輯php首頁

<?php
   phpinfo();
?>

使用瀏覽器訪問監控端的 IP 地址,即192.168.66.146. 查看 php 首頁

0

將 php 首頁改爲測試連接數據庫

[root@localhost html]# vim index.php

<?php
$link=mysql_connect('192.168.66.146','zabbix','admin123');      
if($link) echo "<h1>Success!!!</h1>";
else echo "Fail!!";
mysql_close();
?>

使用 zabbix 用戶連接數據庫,連接成功輸出:Success!!!  ,連接失敗則輸出:Fail!! 信息

再次使用瀏覽器訪問測試

1

到此 LAMP 平臺部署已經完成,接下來在監控端部署 Zabbix _server 的服務。

二 . 部署 Zabbix_server 服務

1 .安裝與 php 相關的軟件包 

[root@localhost html]# yum install php-bcmath php-mbstring –y

安裝 zabbix 的yum 源,自動產生 repo 文件

[root@localhost html]# rpm -ivh http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
獲取http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.85LcDm: 頭V4 RSA/SHA512 Signature, 密鑰 ID a14fe591: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
    1:zabbix-release-3.5-1.el7         ################################# [100%]

安裝 zabbix-server-mysql 和 zabbix-web-mysql

[root@localhost html]# yum install zabbix-server-mysql zabbix-web-mysql –y

2. 初始化數據庫模塊,生成數據庫文件,注意密碼不要輸成 root

[root@localhost html]# zcat /usr/share/doc/zabbix-server-mysql-4.0.0/create.sql.gz | mysql -uzabbix -p zabbix
Enter password:         //密碼爲 admin123

3.(1)更改配置文件(這些過濾的內容是配置文件中自動配置好的,不需要更改,只有數據庫登錄密碼需要更改)

[root@localhost html]# grep -n '^'[a-Z] /etc/zabbix/zabbix_server.conf
38:LogFile=/var/log/zabbix/zabbix_server.log                              //日誌文件路徑
49:LogFileSize=0                                                                                  //日誌文件大小
72:PidFile=/var/run/zabbix/zabbix_server.pid                             //pid 文件路徑     
82:SocketDir=/var/run/zabbix
101:DBName=zabbix                                                                         //數據庫名稱
117:DBUser=zabbix                                                                           //數據庫用戶
357:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log    
475:Timeout=4                                                                                   //超時信息
518:AlertScriptsPath=/usr/lib/zabbix/alertscripts                       //腳本文件路徑
529:ExternalScripts=/usr/lib/zabbix/externalscripts                   //擴展性腳本文件
565:LogSlowQueries=3000                                                               //慢日誌文件

更改數據庫登錄密碼

[root@localhost html]# vim /etc/zabbix/zabbix_server.conf

125 DBPassword=admin123

(2)修改時區

[root@localhost html]# vim /etc/httpd/conf.d/zabbix.conf

20          php_value date.timezone Asia/Shanghai               //修改爲上海的時區

(3)修正圖表中文亂碼

[root@localhost html]# vim /usr/share/zabbix/include/defines.inc.php

:%s /graphfont/kaiti/g          //將 graphfont 全文替換爲 kaiti

複製相應的字體文件到 /usr/share/zabbix/fonts/

[root@localhost html]# yum install lrzsz –y       //lrzsz 工具可以在windows 和Linux 系統之間傳輸文件,

[root@localhost html]# mkdir /aaa
[root@localhost html]# cd /aaa
[root@localhost aaa]# ls
[root@localhost aaa]# rz

[root@localhost aaa]# ls
php-bcmath-5.4.16-42.el7.x86_64.rpm    STKAITI.TTF
php-mbstring-5.4.16-42.el7.x86_64.rpm
[root@localhost aaa]# cp STKAITI.TTF /usr/share/zabbix/fonts/

4啓動 zabbix-server 服務,並查看端口

[root@localhost aaa]# systemctl start zabbix-server.service
[root@localhost aaa]# systemctl enable zabbix-server.service                  //開機自啓動
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
[root@localhost aaa]# netstat -an | grep 10051                                             //zabbix 的端口爲10051
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN    
tcp6       0      0 :::10051                :::*                    LISTEN    

由於修改過配置文件,重啓 httpd 服務

[root@localhost aaa]# systemctl restart httpd.service

5.服務重啓後,訪問 http://192.168.66.146/zabbix  ,安裝 zabbix 後登錄。

2

測試數據庫的連通性,測試連通成功會顯示 OK 字樣。如下圖所示

3

4

添加主機信息

5

6

7

使用默認用戶 Admin ,密碼 zabbix  登錄

8

啓動中文界面,Zabbix Web 管理界面自帶多種語言包,默認使用的語言爲英語。將 Zabbix 語言切換到中文版本

9

10

三 . 部署 zabbix_agent 服務(在被監控端搭建)

Agent 的作用就是獲得 host 數據,然後把收集的數據發送給 Server(主動模式)或者是 Server 主動來拿取數據(被動模式)。

1.關閉防火牆

[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0

2. 安裝 yum 源,安裝 zabbix-agent 服務

[root@localhost ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
獲取http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.hITfvK: 頭V4 RSA/SHA512 Signature, 密鑰 ID a14fe591: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
    1:zabbix-release-3.5-1.el7         ################################# [100%]

[root@localhost ~]# yum install zabbix-agent –y

3.更改 zabbix_agent 配置文件

[root@localhost ~]# grep -n '^'[a-Z] /etc/zabbix/zabbix_agentd.conf          
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
98:Server=127.0.0.1
139:ServerActive=127.0.0.1
150:Hostname=Zabbix server
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf

指定服務器的 IP地址

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

98 Server=192.168.66.146

139 ServerActive=192.168.66.146

150 Hostname=test

4.啓動 zabbix-agent 服務並查看端口

[root@localhost ~]# systemctl start zabbix-agent.service
[root@localhost ~]# systemctl enable zabbix-agent.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@localhost ~]# netstat -natp | grep 10050
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      54896/zabbix_agentd
tcp6       0      0 :::10050                :::*                    LISTEN      54896/zabbix_agentd

server 端的端口爲:10051,agent 端口爲:10050

此時 zabbix-server 和 zabbix-agent 都配置完成,並啓動服務,到zabbix 的 web 頁面,添加被監控主機。

創建主機。Host  是 Zabbix 監控的基本載體,所有監控項目都是基於 Host 。可從“配置” –> “主機”-> “創建主機”。

11

12

13

14

15

四 . 部署郵件發送服務(在監控端配置郵件報警)

1.安裝 mailx 郵件軟件包

[root@localhost aaa]# yum install mailx –y

更改配置文件

[root@localhost aaa]# vim /etc/mail.rc               //注意如使用網易或qq郵箱需要開啓客戶端授權碼進行第三方登錄

set from=1947…@qq.com                             //qq郵箱地址
set smtp=smtp.qq.com
set smtp-auth-user=1947…[email protected]
set smtp-auth-password=ahixbfxiuztjcfjb        //第三方授權碼
set smtp-auth=login

qq郵箱的第三方授權碼獲得方法如下,進入qq郵箱,選擇“設置” ,然後“賬戶”

17

發送郵件測試

[root@localhost aaa]# echo "this is zabbix" | mail -s "testmail" 1947…[email protected]         //測試發送郵件,查看是否能收到

2.編寫發郵件腳本

[root@localhost aaa]# cd /usr/lib/zabbix/
[root@localhost zabbix]# ls
alertscripts  externalscripts
[root@localhost zabbix]# cd alertscripts/
[root@localhost alertscripts]# ls
[root@localhost alertscripts]# vim mail.sh

#!/bin/bash
#send mail
messages=`echo $3 | tr '\r\n' '\n'`
subject=`echo $2 | tr '\r\n' '\n'`
echo "${messages}" | mail -s "${subject}" $1 >>/tmp/mailx.log 2>&1

給腳本執行權限

[root@localhost alertscripts]# mv mail.sh mailx.sh
[root@localhost alertscripts]# touch /tmp/mailx.log
[root@localhost alertscripts]# chown -R zabbix.zabbix /tmp/mailx.log
[root@localhost alertscripts]# chmod +x /usr/lib/zabbix/alertscripts/mail.sh
[root@localhost alertscripts]# chown -R zabbix.zabbix /usr/lib/zabbix/

測試發郵件腳本

[root@localhost alertscripts]# ./mailx.sh 1947…[email protected] "yum" "heihei"

總結:

Zabbix 是一個企業級的、開源的、分佈式的監控套件,可以監控網絡和服務的狀態。

Zabbix 可以利用數據提供圖形化的報告,還具有靈活的告警機制。

Zabbix 可以使用 Zabbix Web 管理頁面進行管理配置。

Zabbix 自帶多種監控模板可以直接使用


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