1. 程式人生 > 其它 >企業實戰之監控升級視覺化zabbix4.0 系列 一

企業實戰之監控升級視覺化zabbix4.0 系列 一

@commandersu 2021-07-20 09:35 字數 18395 閱讀 1694


zabbix簡介前言

常見開源的的監控系統有zabbix ,nagios ,cacti

nagios的簡單介紹

1,重在監控告警,在這一方面zabbix比他更優秀,告警配置視覺化,web化
2,nagios新增監控需要更改配置檔案。
3,沒有監控的歷史資料,圖形支援差,也就是說只是知道報警,但是沒有記錄原因
4,不支援分佈時候監控系統。
比如:不支援分散式部署

Cacti的簡單介紹

1,重在採集伺服器。網路裝置的監控資料並繪圖
2,依賴於snmp協議
3,不好自定義監控
4,告警支援不友好

zabbix的簡單介紹

1,所有監控配置都web化,web採用php開發
2,支援分散式監控
3,支援多種方式資料採集:簡單監控,agent監控,snmp介面,jmx介面監控
4,告警配置web化:郵件,微信,釘釘,簡訊
5,zaabix和grafana的結合方便監控資料的視覺化
比如:支援

zabbix監控的搭建簡單理論

1,zabbix server會去採集監控資料,採集的監控資料會寫入到sql資料庫
2,zabbix的web後端採用的php語言開發,所有的配置資訊,使用者認證等都會寫入到sql資料庫。
3,企業級zabbix的搭建依賴環境:主流採用LNMP(centos7+nginx+mysql+php)
4,環境下使用者請求流程
使用者->nginx—>php-fpm->執行php程式->操作mysql

zabbix工作原理圖

zabbix 由以下幾個元件部分構成:

  1. >1 Zabbix Server
  2. 負責接收 agent 傳送的報告資訊的核心元件,所有配置,統計資料及操作資料均由其組
  3. 織進行;
  4. ---
  5. >2 Database Storage
  6. 專用於儲存所有配置資訊,以及由 zabbix 收集的資料;
  7. ---
  8. >3 Web interface
  9. zabbix GUI 介面,通常與 Server 執行在同一臺主機上;
  10. ---
  11. >4 Proxy
  12. 可選元件,常用於分佈監控環境中,代理 Server 收集部分被監控端的監控資料
  13. 並統一發往 Server 端;
  14. ---
  15. >5 Agent
  16. 部署在被監控主機上,負責收集本地資料併發往 Server 端或 Proxy 端;
  17. 注:zabbix node 也是 zabbix server 的一種
  18. ---
  19. >程序
  20. 預設情況下zabbix包含5個程式: zabbix_agentd zabbix_get zabbix_proxy
  21. zabbix_senderzabbix_server,另外一個 zabbix_java_gateway 是可選,這個需要另
  22. 外安裝
  23. ---
  24. 下面來分別介紹下他們各自的作用:
  25. abbix_agentd
  26. 戶端守護程序,此程序收集客戶端資料,例如 cpu 負載、記憶體、硬碟使用情況等。
  27. ---
  28. >zabbix_get
  29. zabbix 工具,單獨使用的命令,通常在 server 或者proxy端執行獲取遠端客戶端資訊的
  30. 命令。 通常使用者排錯。 例如在server端獲取不到客戶端的記憶體資料, 我們可以使用
  31. zabbix_get獲取客戶端的內容的方式來做故障排查。
  32. ---
  33. >zabbix_sender
  34. zabbix 工具,用於傳送資料給 server 或者proxy,通常用於耗時比較長的檢查。很多檢
  35. 查非常耗時間,導致 zabbix 超時。於是我們在指令碼執行完畢之後,使用 sender 主動提
  36. 交資料。
  37. ---
  38. >zabbix_server
  39. zabbix 服務端守護程序。zabbix_agentdzabbix_getzabbix_sender
  40. zabbix_proxyzabbix_java_gateway 的資料最終都是提交到 server
  41. 備註:當然不是資料都是主動提交給 zabbix_server,也有的是 server 主動去取資料。
  42. ---
  43. >zabbix_proxy
  44. zabbix 代理守護程序。功能類似server,唯一不同的是它只是一箇中轉站,它需要把收
  45. 集到的資料提交/被提交到 server 裡。
  46. ---
  47. >zabbix_java_gateway
  48. zabbix2.0 之後引入的一個功能。顧名思義:Java 閘道器,類似 agentd,但是隻用於
  49. Java方面。需要特別注意的是,它只能主動去獲取資料,而不能被動獲取資料。 它的數
  50. 據最終會給到server或者proxy
  51. ---

zabbix監控環境中相關術語

  1. #主機(host) :
  2. 要監控的網路裝置,可由 IP DNS 名稱指定;
  3. ---
  4. #主機組(host group):
  5. 主機的邏輯容器,可以包含主機和模板,但同一個組織內的主機和模板不能互相連結;
  6. 主機組通常在給使用者或使用者組指派監控許可權時使用;
  7. ---
  8. #監控項(item) :
  9. 一個特定監控指標的相關的資料;這些資料來自於被監控物件;item zabbix 進行數
  10. 據收集的核心,相對某個監控物件,每個 item 都由"key"標識;
  11. ---
  12. #觸發器(trigger) :
  13. 一個表示式,用於評估某監控物件的特定 item 內接收到的資料是否在合理範圍內,也就
  14. 是閾值;接收的資料量大於閾值時,觸發器狀態將從"OK"轉變為"Problem",當資料再
  15. 次恢復到合理範圍,又轉變為"OK"
  16. ---
  17. #事件(event) :
  18. 觸發一個值得關注的事情,比如觸發器狀態轉變,新的 agent 或重新上
  19. 線的 agent 的自動註冊等;
  20. ---
  21. #動作(action) :
  22. 指對於特定事件事先定義的處理方法,如傳送通知,何時執行操作;
  23. ---
  24. #報警媒介型別(media) :
  25. 傳送通知的手段或者通道,如 EmailJabber 或者 SMS 等;
  26. ---
  27. #模板 (template) :
  28. 用於快速定義被監控主機的預設條目集合, 通常包含了 item triggergraph
  29. screen application 以及 low-level discovery rule;模板可以直接連結至某個主機;
  30. ---
  31. #前端(frontend) :
  32. Zabbix web 介面

zabbix各種相關元件關係圖

一搭建zabbix

1搭建環境

作業系統主機名ip記憶體
centos7.5 zabbix 192.168.200.173 1G

1.1搭建nginx

  1. [root@zabbix ~]# cat /etc/redhat-release
  2. CentOS Linux release 7.5.1804 (Core)
  3. [root@zabbix ~]# setenforce 0
  4. setenforce: SELinux is disabled
  5. [root@zabbix ~]# systemctl stop firewalld
  6. [root@zabbix ~]# systemctl stop NetworkManager
  7. [root@zabbix ~]# ls
  8. anaconda-ks.cfg nginx-1.16.1.tar.gz
  9. [root@zabbix ~]# yum install -y wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
  10. #安裝支援程式我們原始碼編譯
  11. [root@zabbix ~]# tar xf nginx-1.16.1.tar.gz -C /usr/src/
  12. [root@zabbix ~]# cd /usr/src/nginx-1.16.1/
  13. [root@zabbix nginx-1.16.1]# ls
  14. auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
  15. [root@zabbix nginx-1.16.1]# ./configure --prefix=/usr/local/nginx && make && make install
  16. #編譯安裝
  17. [root@zabbix nginx-1.16.1]# ln -s /usr/local/nginx/sbin/* /usr/bin/ #將命令連結出來
  18. [root@zabbix nginx-1.16.1]#which nginx
  19. #檢視有了nginx的命令
  20. /usr/bin/nginx
  21. [root@zabbix nginx-1.16.1]# cd /usr/local/nginx/conf/
  22. [root@zabbix conf]# ls
  23. fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
  24. fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
  25. fastcgi_params koi-win nginx.conf scgi_params.default win-utf
  26. [root@zabbix conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
  27. #精簡配置檔案
  28. [root@zabbix conf]# cat nginx.conf
  29. worker_processes 1;
  30. events {
  31. worker_connections 1024;
  32. }
  33. http {
  34. include mime.types;
  35. default_type application/octet-stream;
  36. sendfile on;
  37. keepalive_timeout 65;
  38. server {
  39. listen 80;
  40. server_name localhost;
  41. location / {
  42. root html;
  43. index index.html index.htm;
  44. }
  45. error_page 500 502 503 504 /50x.html;
  46. location = /50x.html {
  47. root html;
  48. }
  49. }
  50. }
  51. [root@zabbix conf]# nginx -t
  52. #檢測一下配置檔案有沒有語法錯誤
  53. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  54. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  55. [root@zabbix conf]# /usr/local/nginx/sbin/nginx
  56. #啟動nginx
  57. [root@zabbix conf]# ss -antup | grep 80
  58. tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=18266,fd=6),("nginx",pid=18265,fd=6))
  59. #檢視下nginx的埠有沒有開
  60. #使用systemctl管理nginx
  61. [root@zabbix conf]# vim /usr/lib/systemd/system/nginx.service
  62. [Unit] #這個模組主要是對服務的描述
  63. Description=nginx #描述服務是什麼
  64. After=network.target # 描述服務的類別
  65. [Service] #是服務的具體執行引數
  66. Type=forking #後臺執行的形式
  67. ExecStart=/usr/local/nginx/sbin/nginx #啟動命令
  68. [Install] #服務安裝的相關設定
  69. WantedBy=multi-user.target
  70. #測試一下啊systemctl管理(如果這裡有問題,請重啟下伺服器就可以了)
  71. [root@zabbix conf]# ss -antup | grep 80
  72. tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1213,fd=6),("nginx",pid=1212,fd=6))
  73. [root@zabbix conf]# systemctl stop nginx
  74. [root@zabbix conf]# ss -antup | grep 80
  75. [root@zabbix conf]#
  76. #開啟
  77. [root@zabbix conf]# systemctl start nginx
  78. [root@zabbix conf]# ss -antup | grep 80
  79. tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1249,fd=6),("nginx",pid=1248,fd=6))

1.2關於systemctl管理服務引數說明

  • [Service]部分是服務的關鍵,是服務的一些具體執行引數的設定
  • Type=forking是後臺執行的形式,
  • PIDFile為存放PID的檔案路徑,
  • ExecStart為服務的執行命令,
  • ExecReload為重啟命令,
  • ExecStop為停止命令,
  • rivateTmp=True表示給服務分配獨立的臨時空間,
    *注意:[Service]部分的啟動、重啟、停止命令全部要求使用絕對路徑,使用相對路徑則會報錯;

1.3在瀏覽器上驗證下nginx

二搭建php

php和nginx結合使用是有2中方式的,
1是socket方式(這個方式需要兩個服務在同一個伺服器上)
2是網路方式,這樣的可以不在同一個服務其上 (預設是網路)

2.1編譯安裝php

  1. [root@zabbix conf]# cd ~
  2. [root@zabbix ~]# wget http://hk1.php.net/distributions/php-5.6.40.tar.gz #現在5.64安裝包
  3. [root@zabbix ~]# yum -y install epel-release #用yum安裝一個epel源,這個檔案是擴充套件的一些服務的源
  4. [root@zabbix ~]# yum -y clean all
  5. [root@zabbix ~]# yum makecache
  6. [root@zabbix ~]# yum -y install gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel libmcrypt libmcrypt-devel
  7. [root@zabbix ~]# wget https://www.php.net/distributions/php-5.6.40.tar.gz
  8. [root@zabbix ~]# echo "$?"
  9. 0
  10. [root@zabbix ~]# ls
  11. anaconda-ks.cfg nginx-1.16.1.tar.gz php-5.6.40.tar.gz
  12. [root@zabbix ~]# tar xf php-5.6.40.tar.gz -C /usr/src/
  13. [root@zabbix ~]# cd /usr/src/php-5.6.40/
  14. [root@zabbix php-5.6.40]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-ctype --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-fpm
  15. #出現一下的式樣就ok了
  16. Generating files
  17. configure: creating ./config.status
  18. creating main/internal_functions.c
  19. creating main/internal_functions_cli.c
  20. +--------------------------------------------------------------------+
  21. | License: |
  22. | This software is subject to the PHP License, available in this |
  23. | distribution in the file LICENSE. By continuing this installation |
  24. | process, you are bound by the terms of this license agreement. |
  25. | If you do not agree with the terms of this license, you must abort |
  26. | the installation process at this point. |
  27. +--------------------------------------------------------------------+
  28. Thank you for using PHP.
  29. [root@zabbix php-5.6.40]# make && make install

php主要編譯安裝說明

  • [x]--prefix指定php的安裝目錄
  • [x]--with-config-file-path指定php的配置檔案位置
  • [x]--with-mysql、--with-mysqli讓php可以操作mysql
  • [x]--enable-fpm主要是nginx要來呼叫php語言得使用php-fpm

2.2啟動php

  1. [root@zabbix php-5.6.40]# tail -1 /etc/profile #新增環境變數
  2. export PATH=$PATH:/usr/local/php/sbin/:/usr/local/php/bin/
  3. [root@zabbix php-5.6.40]# source /etc/profile #讓他立即生效
  4. [root@zabbix php-5.6.40]# php-fpm -t #檢測一下配置檔案有沒有錯誤,現在報錯是因為沒有配置檔案在裡面,我們需要複製一份過去,
  5. [04-Nov-2019 10:04:15] ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)
  6. [04-Nov-2019 10:04:15] ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'
  7. [04-Nov-2019 10:04:15] ERROR: FPM initialization failed
  8. [root@zabbix php-5.6.40]# ls php.ini-
  9. php.ini-development php.ini-production
  10. # deve是開發環境的,production是生產環境的
  11. [root@zabbix php-5.6.40]# cp php.ini-production /usr/local/php/etc/php.ini
  12. [root@zabbix php-5.6.40]# cd /usr/local/php/etc/
  13. [root@zabbix etc]# ls
  14. pear.conf php-fpm.conf.default php.ini
  15. [root@zabbix etc]# cp php-fpm.conf.default php-fpm.conf
  16. [root@zabbix etc]# ls
  17. pear.conf php-fpm.conf php-fpm.conf.default php.ini
  18. [root@zabbix etc]# php-fpm -t
  19. [04-Nov-2019 10:16:00] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
  20. [root@zabbix etc]# php-fpm -v
  21. PHP 5.6.40 (fpm-fcgi) (built: Nov 4 2019 09:37:11)
  22. Copyright (c) 1997-2016 The PHP Group
  23. Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
  24. #使用systemctl將php管理起來,並啟動
  25. [root@zabbix etc]# cat /usr/lib/systemd/system/php-fpm.service
  26. [Unit]
  27. Description=php-fpm
  28. After=network.target
  29. [Service]
  30. Type=forking
  31. ExecStart=/usr/local/php/sbin/php-fpm
  32. [Install]
  33. WantedBy=multi-user.target
  34. [root@zabbix etc]# systemctl start php-fpm
  35. [root@zabbix etc]# ss -antup | grep 9000
  36. tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=8872,fd=0),("php-fpm",pid=8871,fd=0),("php-fpm",pid=8870,fd=7))

2.3修改nginx的配置,讓php和nginx連用起來

  1. [root@zabbix etc]# cd /usr/local/nginx/conf/
  2. [root@zabbix conf]# cat nginx.conf
  3. worker_processes 1;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. sendfile on;
  11. keepalive_timeout 65;
  12. server {
  13. listen 80;
  14. server_name localhost;
  15. location / {
  16. root html;
  17. index index.html index.htm index.php; #必須加上.php結尾的主頁,要不然nginx呼叫動態的時候找不到回直接報錯就不找了
  18. }
  19. location ~ \.php$ {
  20. root html;
  21. fastcgi_pass 127.0.0.1:9000;
  22. fastcgi_index index.php;
  23. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #去哪裡尋找php檔案。
  24. include fastcgi_params;
  25. }
  26. error_page 500 502 503 504 /50x.html;
  27. location = /50x.html {
  28. root html;
  29. }
  30. }
  31. }
  32. [root@zabbix conf]# nginx -t
  33. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  34. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  35. [root@zabbix conf]# nginx -s reload
  36. [root@zabbix conf]# cd /usr/local/nginx/html/
  37. [root@zabbix html]# touch index.php #裡邊什麼都不用寫
  38. [root@zabbix html]# cat test.php
  39. <?php
  40. echo "mr.su it's ok";
  41. ?>

2.4驗證php+nginx是否連線成功

三mysql資料庫安裝

3.1搭建mysql資料庫

  1. [root@zabbix html]# cd ~
  2. [root@zabbix ~]# wget https://www.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39.tar.gz
  3. [root@zabbix ~]# ls
  4. anaconda-ks.cfg mysql-5.6.39.tar.gz nginx-1.16.1.tar.gz php-5.6.40.tar.gz
  5. [root@zabbix ~]# yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel
  6. [root@zabbix ~]# useradd -M -s /sbin/nologin mysql
  7. [root@zabbix ~]# tar xf mysql-5.6.39.tar.gz -C /usr/src/
  8. [root@zabbix ~]# cd /usr/src/mysql-5.6.39/
  9. [root@zabbix mysql-5.6.39]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1
  10. [root@zabbix mysql-5.6.39]# echo "$?"
  11. 0
  12. [root@zabbix mysql-5.6.39]# make && make install
  13. [root@zabbix mysql-5.6.39]# echo "$?"
  14. 0
  15. [root@zabbix mysql-5.6.39]# cp support-files/mysql.server /etc/init.d/mysqld
  16. [root@zabbix mysql-5.6.39]# chmod +x /etc/init.d/mysqld
  17. [root@zabbix mysql-5.6.39]# tail -1 /etc/profile #修改環境變數,本質是要伺服器找到命令,所以做軟連線也是可以的
  18. export PATH=$PATH:/usr/local/mysql/bin/
  19. #修改配置檔案
  20. [root@zabbix mysql-5.6.39]# cat /etc/my.cnf
  21. [mysqld]
  22. bind-address=0.0.0.0
  23. port=3306
  24. datadir=/data/mysql
  25. user=mysql
  26. skip-name-resolve
  27. long_query_time=2
  28. slow_query_log_file=/data/mysql/mysql-slow.log
  29. expire_logs_days=2
  30. innodb-file-per-table=1
  31. innodb_flush_log_at_trx_commit = 2
  32. log_warnings = 1
  33. max_allowed_packet = 512M
  34. connect_timeout = 60
  35. net_read_timeout = 120
  36. [mysqld_safe]
  37. log-error=/data/mysql/mysqld.log
  38. pid-file=/data/mysql/mysqld.pid

重要的編譯選項說明
- [x] CMACK_INSTALL_PREFIX指定安裝的目錄
- [x] MYSQL_DATADIR指定Mysql的資料目錄

3.2初始化資料庫

  1. [root@zabbix mysql-5.6.39]# mkdir -p /data/mysql #建立資料目錄
  2. [root@zabbix mysql-5.6.39]# chown -R mysql:mysql /usr/local/mysql /data/mysql/ #分別對資料目錄,和執行目錄授權
  3. [root@zabbix mysql-5.6.39]# yum install -y perl-Module-Install #初始資料庫的一個依賴程式
  4. [root@zabbix mysql-5.6.39]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql/ #初始完成以後輸出的特別像亂碼,往上看,找到兩個ok就初始化完成了
  5. [root@zabbix mysql-5.6.39]# cat /usr/lib/systemd/system/mysqld.service #使用systemctl管理mysql
  6. [Unit]
  7. Description=mysqld
  8. After=network.target
  9. [Service]
  10. Type=forking
  11. ExecStart=/etc/init.d/mysqld start
  12. [Install]
  13. WantedBy=multi-user.target
  14. [root@zabbix mysql-5.6.39]# systemctl start mysqld
  15. [root@zabbix mysql-5.6.39]# ss -antup | grep 3306
  16. tcp LISTEN 0 80 *:3306 *:* users:(("mysqld",pid=26225,fd=10))

3.3給mysql建立zabbix賬號並驗證連線

  1. [root@zabbix mysql-5.6.39]# cd ~
  2. [root@zabbix ~]# mysqladmin -h 127.0.0.1 -u root password 'zabbixpwd'
  3. Warning: Using a password on the command line interface can be insecure.
  4. [root@zabbix ~]# echo "$?"
  5. 0
  6. [root@zabbix ~]# mysql -uroot -pzabbixpwd -h 127.0.0.1
  7. Warning: Using a password on the command line interface can be insecure.
  8. Welcome to the MySQL monitor. Commands end with ; or \g.
  9. Your MySQL connection id is 3
  10. Server version: 5.6.39 Source distribution
  11. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  12. Oracle is a registered trademark of Oracle Corporation and/or its
  13. affiliates. Other names may be trademarks of their respective
  14. owners.
  15. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  16. mysql> mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.200.%' IDENTIFIED BY 'zabbixpwd' WITH GRANT OPTION;
  17. Query OK, 0 rows affected (0.00 sec)
  18. mysql> flush privileges;
  19. Query OK, 0 rows affected (0.00 sec)
  20. mysql> exit
  21. [root@zabbix ~]# mysql -uroot -pzabbixpwd -h 192.168.200.173 #驗證密碼在遠處登入
  22. mysql> exit
  23. [root@zabbix ~]# cat /usr/local/nginx/html/test_mysql.php #建立測試nginx+php+mysql 的測試檔案
  24. <?php
  25. $link=mysql_connect("127.0.0.1","root","zabbixpwd");
  26. if(!$link) echo "FAILD!連線錯誤,使用者名稱密碼不對";
  27. else echo "OK!可以連線";
  28. ?>

3.4通過http://192.168.200.173/test_mysqsl.php

四搭建zabbix4.0

4.1下載並編譯安裝zabbix

  1. [root@zabbix ~]# yum install -y libevent-devel wget tar gcc gcc-c++ make net-snmp-devel libxml2-devel libcurl-devel #yum安裝zabbix的支援程式
  2. [root@zabbix ~]# useradd -M -s /sbin/nologin zabbix
  3. [root@zabbix ~]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz #下載zabbix4.0的原始碼包
  4. [root@zabbix ~]# ls
  5. anaconda-ks.cfg mysql-5.6.39.tar.gz nginx-1.16.1.tar.gz php-5.6.40.tar.gz zabbix-4.0.3.tar.gz
  6. [root@zabbix ~]# tar xf zabbix-4.0.3.tar.gz -C /usr/src/
  7. [root@zabbix ~]# cd /usr/src/zabbix-4.0.3/
  8. [root@zabbix zabbix-4.0.3]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql=/usr/local/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2
  9. #出現以下的就ok了
  10. ***********************************************************
  11. * Now run 'make install' *
  12. * *
  13. * Thank you for using Zabbix! *
  14. * <http://www.zabbix.com> *
  15. ***********************************************************
  16. [root@zabbix zabbix-4.0.3]# make && make install
  17. [root@zabbix zabbix-4.0.3]# tail -1 /etc/profile #修改變數檔案,使其能找到命令
  18. export PATH=$PATH:/usr/local/zabbix/sbin/:/usr/local/zabbix/bin/
  19. [root@zabbix zabbix-4.0.3]# source /etc/profile #立即生效
  20. [root@zabbix zabbix-4.0.3]# zabbix_server --version
  21. zabbix_server (Zabbix) 4.0.3
  22. Revision 87993 20 December 2018, compilation time: Nov 13 2019 09:44:17
  23. Copyright (C) 2018 Zabbix SIA
  24. License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
  25. This is free software: you are free to change and redistribute it according to
  26. the license. There is NO WARRANTY, to the extent permitted by law.

選項說明

  • --prefix指定安裝目錄
  • --enable-server安裝zabbix server
  • --enable-agent安裝zabbix agent
  • --with-mysql用mysql來儲存

4.2初始化zabbix資料庫,啟動zabbix

  1. [root@zabbix zabbix-4.0.3]# mysql -h 127.0.0.1 -uroot -pzabbixpwd
  2. mysql> create database zabbix character set utf8 collate utf8_bin;
  3. Query OK, 1 row affected (0.00 sec)
  4. mysql> grant all privileges on zabbix.* to zabbix@'127.0.0.1' identified by 'zabbixpwd';
  5. Query OK, 0 rows affected (0.04 sec)
  6. mysql> flush privileges;
  7. Query OK, 0 rows affected (0.04 sec)
  8. mysql> set names utf8; #設定編碼
  9. Query OK, 0 rows affected (0.00 sec)
  10. mysql> use zabbix; #切換資料庫準備導表
  11. Database changed
  12. mysql> source /usr/src/zabbix-4.0.3/database/mysql/schema.sql #開始導表,不要順序亂了
  13. mysql> source /usr/src/zabbix-4.0.3/database/mysql/data.sql
  14. mysql> source /usr/src/zabbix-4.0.3/database/mysql/images.sql #導表完成
  15. #修改zabbix的配置檔案
  16. [root@zabbix zabbix-4.0.3]# cd /usr/local/zabbix/etc/
  17. [root@zabbix etc]# ls
  18. zabbix_agentd.conf zabbix_agentd.conf.d zabbix_server.conf zabbix_server.conf.d
  19. [root@zabbix etc]# cp zabbix_server.conf zabbix_server.conf.bak
  20. [root@zabbix etc]# ls
  21. zabbix_agentd.conf zabbix_agentd.conf.d zabbix_server.conf zabbix_server.conf.bak zabbix_server.conf.d
  22. [root@zabbix etc]# cat zabbix_server.conf
  23. LogFile=/usr/local/zabbix/zabbix_server.log
  24. DBHost=127.0.0.1
  25. DBName=zabbix
  26. DBUser=zabbix
  27. DBPassword=zabbixpwd
  28. DBPort=3306
  29. Timeout=30
  30. AlertScriptsPath=/usr/local/zabbix/alertscripts #放置我們微信告警,以及郵件告警指令碼的位置。
  31. ExternalScripts=/usr/local/zabbix/externalscripts
  32. LogSlowQueries=3000
  33. #一定要把配置檔案裡的#號都刪掉
  34. #啟動zabbix
  35. [root@zabbix etc]# chown zabbix:zabbix -R /usr/local/zabbix/ #給目錄降權
  36. [root@zabbix etc]# zabbix_server #啟動zabbix,命令就是zabbix
  37. [root@zabbix etc]# ss -lntup | grep 10051 #檢視埠,server端已經啟動
  38. tcp LISTEN 0 128 *:10051
  39. [root@zabbix etc]# mkdir -p /usr/local/nginx/html/zabbix
  40. [root@zabbix etc]# cp -a /usr/src/zabbix-4.0.3/frontends/php/* /usr/local/nginx/html/zabbix/ #將所有的zabbix的網頁複製到nginx下

4.3在web介面登入zabbix

使用ip登入。ip/zabbix

  1. #修改php的檔案
  2. [root@zabbix etc]# vim /usr/local/php/etc/php.ini
  3. #將以下幾行修改成這個樣子
  4. 660post_max_size = 32M
  5. 372max_execution_time = 350
  6. 382max_input_time = 350
  7. 936date.timezone = Asia/Shanghai
  8. 702always_populate_raw_post_data = -1
  9. [root@zabbix etc]# systemctl restart php-fpm #重啟php
  10. [root@zabbix etc]# ss -antup | grep 9000
  11. tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=42895,fd=0),("php-fpm",pid=42894,fd=0),("php-fpm",pid=42893,fd=7))

  1. root@zabbix ~]# ls
  2. anaconda-ks.cfg mysql-5.6.39.tar.gz nginx-1.16.1.tar.gz php-5.6.40.tar.gz zabbix-4.0.3.tar.gz zabbix.conf.php
  3. [root@zabbix ~]# mv zabbix.conf.php /usr/local/nginx/html/zabbix/conf/
  4. [root@zabbix ~]# cat /usr/local/nginx/html/zabbix/conf/zabbix.conf.php #裡面改就是我們剛剛形成的一些內容
  5. <?php
  6. // Zabbix GUI configuration file.
  7. global $DB;
  8. $DB['TYPE'] = 'MYSQL';
  9. $DB['SERVER'] = '127.0.0.1';
  10. $DB['PORT'] = '3306';
  11. $DB['DATABASE'] = 'zabbix';
  12. $DB['USER'] = 'zabbix';
  13. $DB['PASSWORD'] = 'zabbixpwd';
  14. // Schema name. Used for IBM DB2 and PostgreSQL.
  15. $DB['SCHEMA'] = '';
  16. $ZBX_SERVER = 'localhost';
  17. $ZBX_SERVER_PORT = '10051';
  18. $ZBX_SERVER_NAME = '';
  19. $IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;

4.4禁用沒用的使用者

4.5修改超戶的密碼

4.6zabbix網頁漢化補丁包

  1. [root@zabbix ~]# ls /usr/local/nginx/html/zabbix/fonts/ #這個目錄裡放著的就是zabbix的字型,但是沒有中文字型所以會出現亂碼,我們的解決方法就是下載個字型覆蓋到這
  2. DejaVuSans.ttf
  3. [root@zabbix ~]# wget https://raw.githubusercontent.com/chenqing/ng-mini/master/font/msyh.ttf #下載字型的網址。
  4. [root@zabbix ~]# ls
  5. anaconda-ks.cfg msyh.ttf mysql-5.6.39.tar.gz nginx-1.16.1.tar.gz php-5.6.40.tar.gz zabbix-4.0.3.tar.gz
  6. [root@zabbix ~]# mv msyh.ttf /usr/local/nginx/html/zabbix/fonts/
  7. [root@zabbix ~]# ls /usr/local/nginx/html/zabbix/fonts/
  8. DejaVuSans.ttf msyh.ttf
  9. [root@zabbix ~]# vim /usr/local/nginx/html/zabbix/include/defines.inc.php #修改php檔案裡的預設,將DejaVuSans字型,改成msyh微軟雅黑字型
  10. 65 define('ZBX_GRAPH_FONT_NAME', 'msyh'); // font file name
  11. 110 define('ZBX_FONT_NAME', 'msyh'); #將65行和110行替換成這個樣子。
  12. #php插入的不像java之類的需要編譯類檔案等,直接就可以使用。

4.7漢化完成