1. 程式人生 > 其它 >Centos7下安裝部署Zabbix-server 5.0.1

Centos7下安裝部署Zabbix-server 5.0.1

技術標籤:監控

目錄

1. Zabbix簡介

  • zabbix是一個基於WEB介面的提供分散式系統監視以及網路監視功能的企業級的開源解決方案。
  • zabbix能監視各種網路引數,保證伺服器系統的安全運營;並提供靈活的通知機制以讓系統管理員快速定位/解決存在的各種問題。
  • zabbix由2部分構成,zabbix server與可選元件zabbix agent。
  • zabbix server可以通過SNMP,zabbix-agent,ping,埠監視等方法提供對遠端伺服器/網路狀態的監視,資料收集等功能,它可以執行在Linux,Solaris,HP-UX,AIX,FreeBSD,Open BSD,OS X等平臺上。

2. 伺服器準備

2.1 主機資訊+IP地址

#一臺虛擬機器
hostname:Zabbix-server
ip:server_ip_address

2.2 檢視主機的系統版本資訊

[[email protected] ~]#  cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 

2.3 關掉Selinux和配置防火牆

2.3.1 關掉Selinux
[[email protected] ~]#  setenforce 0  &&   getenforce
Permissive
[[email protected] ~]# sed -i  "s/SELINUX=enforcing/SELINUX=disabled/g"  /etc/sysconfig/selinux 
[[email protected] ~]#  cat /etc/sysconfig/selinux 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     disabled - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of disabled.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
2.3.2 配置防火牆
[[email protected] ~]# firewall-cmd --state && firewall-cmd --list-ports
running
80/tcp

# 假設zabbix-agent埠為10050
[[email protected] ~]# firewall-cmd --zone=public --permanent --add-port=10050/tcp && firewall-cmd --reload     &&    firewall-cmd --list-ports
success
success
80/tcp 10050/tcp

#假設zabbix-server埠為10051
[[email protected] ~]# firewall-cmd --zone=public --permanent --add-port=10051/tcp && firewall-cmd --reload     &&    firewall-cmd --list-ports
success
success
80/tcp 10051/tcp 10050/tcp 

#假設httpd埠為8480
[[email protected] ~]# firewall-cmd --zone=public --permanent --add-port=8480/tcp && firewall-cmd --reload     &&    firewall-cmd --list-ports
success
success
80/tcp 10051/tcp 8480/tcp 10050/tcp

3. 安裝Zabbix-server需要的環境(LAMP: httpd服務+資料庫服務+php)

3.1 LAMP介紹

  • LAMP:Linux+Apache+Mysql/MariaDB+Perl/PHP/Python一組常用來搭建動態網站或者伺服器的開源軟體,本身都是各自獨立的程式,但是因為常被放在一起使用,擁有了越來越高的相容度,共同組成了一個強大的Web應用程式平臺。
  • LNMP:LNMP指的是一個基於CentOS/Debian編寫的Nginx、PHP、MySQL、phpMyAdmin、eAccelerator一鍵安裝包。可以在VPS、獨立主機上輕鬆的安裝LNMP生產環境。

3.2 安裝apache也就是httpd服務

3.2.1 直接yum安裝
[[email protected] tools]#  rpm -qa|grep httpd
[[email protected] tools]# yum install -y httpd
[[email protected] tools]#  rpm -qa|grep httpd
httpd-2.4.6-97.el7.centos.x86_64
httpd-tools-2.4.6-97.el7.centos.x86_64
3.2.2 設定httpd服務開機自啟動
[[email protected] tools]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
3.2.3 修改httpd服務的預設監聽埠80為8480
[[email protected] tools]# cd /etc/httpd/conf
[[email protected] conf]# vi httpd.conf +42
[[email protected] conf]# grep 8480 httpd.conf
Listen 8480
3.2.4 啟動httpd服務
[[email protected] conf]# systemctl start httpd && sudo echo $?
0
[[email protected] conf]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-01-19 12:42:20 CST; 1min 43s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 125278 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
 Main PID: 125293 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─125293 /usr/sbin/httpd -DFOREGROUND
           ├─125294 /usr/sbin/httpd -DFOREGROUND
           ├─125295 /usr/sbin/httpd -DFOREGROUND
           ├─125296 /usr/sbin/httpd -DFOREGROUND
           ├─125297 /usr/sbin/httpd -DFOREGROUND
           └─125298 /usr/sbin/httpd -DFOREGROUND

1月 19 12:42:20 Zabbix-server systemd[1]: Starting The Apache HTTP Server...
1月 19 12:42:20 Zabbix-server httpd[125293]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, ...message
1月 19 12:42:20 Zabbix-server systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

[[email protected] conf]# lsof -i:8480
COMMAND    PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
httpd   125293   root    4u  IPv6 6790155      0t0  TCP *:8480 (LISTEN)
httpd   125294 apache    4u  IPv6 6790155      0t0  TCP *:8480 (LISTEN)
httpd   125295 apache    4u  IPv6 6790155      0t0  TCP *:8480 (LISTEN)
httpd   125296 apache    4u  IPv6 6790155      0t0  TCP *:8480 (LISTEN)
httpd   125297 apache    4u  IPv6 6790155      0t0  TCP *:8480 (LISTEN)
httpd   125298 apache    4u  IPv6 6790155      0t0  TCP *:8480 (LISTEN)

3.3 安裝資料庫—儲存zabbix採集到的資料

3.3.1 直接yum或者rpm安裝

注意:因為7版本的mysql要收費,所以我們這裡安裝mariadb代替mysql

[[email protected] tools] rpm -qa|grep  mariadb 
[[email protected] tools]#  yum install -y mariadb mariadb-server
[[email protected] tools]# rpm -qa|grep  mariadb 
mariadb-libs-5.5.68-1.el7.x86_64
mariadb-server-5.5.68-1.el7.x86_64
mariadb-5.5.68-1.el7.x86_64
3.3.2 設定mysql服務開機自啟動
[[email protected] tools]#  systemctl enable mariadb.service  
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
3.3.3 啟動mysql服務
[[email protected] ~]# systemctl start mariadb.service 
[[email protected] ~]#  systemctl status mariadb.service  
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2021-01-19 13:04:26 CST; 1h 21min ago
 Main PID: 127665 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─127665 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─127830 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/l...

1月 19 13:04:24 Zabbix-server systemd[1]: Starting MariaDB database server...
1月 19 13:04:24 Zabbix-server mariadb-prepare-db-dir[127630]: Database MariaDB is probably initialized in /var/lib/mysql already, noth...done.
1月 19 13:04:24 Zabbix-server mariadb-prepare-db-dir[127630]: If this is not the case, make sure the /var/lib/mysql is empty before ru...-dir.
1月 19 13:04:24 Zabbix-server mysqld_safe[127665]: 210119 13:04:24 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
1月 19 13:04:24 Zabbix-server mysqld_safe[127665]: 210119 13:04:24 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
1月 19 13:04:26 Zabbix-server systemd[1]: Started MariaDB database server.
Hint: Some lines were ellipsized, use -l to show in full.

[[email protected] ~]#  lsof -i:3306
COMMAND    PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
mysqld  127830 mysql   14u  IPv4 6797577      0t0  TCP *:mysql (LISTEN)

[[email protected] ~]#  mysql #預設是沒有密碼的
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
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 version();
+----------------+
| version()      |
+----------------+
| 5.5.60-MariaDB |
+----------------+
1 row in set (0.00 sec)

MariaDB [(none)]> exit;
Bye

3.3.4 初始化 mariadb 並配置 root 密碼

注意:要先輸入當前密碼,一般初次安裝的預設密碼為空

[[email protected] tools]# mysql_secure_installation
.
.
.

4. 正式安裝Zabbix-server

注意:zabbix-server也要監控自身,所以也要同時安裝zabbix-agent

4.1 下載zabbix的rpm安裝包並進行安裝

[[email protected] tools]# rpm -qa|grep zabbix
[[email protected] ~]#  cd /home/tools/ && wget https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

[[email protected] tools]# ll zabbix-release-5.0-1.el7.noarch.rpm
-rw-r--r--. 1 root root 14532 5月  11 2020 zabbix-release-5.0-1.el7.noarch.rpm

[[email protected] tools]# ll /etc/yum.repos.d/
總用量 38
-rw-r--r--. 1 root root 1664 12月  9 2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 12月  9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 12月  9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 12月  9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 12月  9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 12月  9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 12月  9 2015 CentOS-Vault.repo
-rw-r--r--. 1 root root  951 10月  3 2017 epel.repo
-rw-r--r--. 1 root root 1050 10月  3 2017 epel-testing.repo

[[email protected] tools]# yum install -y zabbix-release-5.0-1.el7.noarch.rpm 
[[email protected] tools]# rpm -qa|grep zabbix
zabbix-release-5.0-1.el7.noarch
[[email protected] tools]# ll /etc/yum.repos.d/
總用量 40
-rw-r--r--. 1 root root 1664 12月  9 2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 12月  9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 12月  9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 12月  9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 12月  9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 12月  9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 12月  9 2015 CentOS-Vault.repo
-rw-r--r--. 1 root root  951 10月  3 2017 epel.repo
-rw-r--r--. 1 root root 1050 10月  3 2017 epel-testing.repo
-rw-r--r--. 1 root root  853 5月  11 2020 zabbix.repo  ###安裝rpm包之後產生的###

4.2 鑑於國內網路情況,將zabbix官方下載源替換成阿里雲的

[[email protected] tools]# sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

4.3 安裝 zabbix-server-mysql 和 zabbix-agent

[[email protected] tools]# yum install zabbix-server-mysql zabbix-agent -y
[[email protected] tools]#  rpm -qa |grep zabbix 
zabbix-server-mysql-5.0.7-1.el7.x86_64
zabbix-release-5.0-1.el7.noarch
zabbix-agent-5.0.7-1.el7.x86_64

4.4 安裝 Software Collections

注意: 安裝 Software Collections是便於後續安裝高版本的 php,預設 yum 安裝的 php 版本為 5.4 過低。

[[email protected] tools]# rpm -qa |grep centos-release-scl 
[[email protected] tools]# yum install centos-release-scl -y
[[email protected] tools]# rpm -qa |grep centos-release-scl 
centos-release-scl-rh-2-3.el7.centos.noarch
centos-release-scl-2-3.el7.centos.noarch
[[email protected] tools]# ll /etc/yum.repos.d/
總用量 48
-rw-r--r--. 1 root root 1664 12月  9 2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 12月  9 2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 12月  9 2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 12月  9 2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 12月  9 2015 CentOS-Media.repo
-rw-r--r--. 1 root root  998 12月 11 2018 CentOS-SCLo-scl.repo ###新產生的###
-rw-r--r--. 1 root root  971 10月 29 2018 CentOS-SCLo-scl-rh.repo ###新產生的###
-rw-r--r--. 1 root root 1331 12月  9 2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 12月  9 2015 CentOS-Vault.repo
-rw-r--r--. 1 root root  951 10月  3 2017 epel.repo
-rw-r--r--. 1 root root 1050 10月  3 2017 epel-testing.repo
-rw-r--r--. 1 root root  897 1月  19 12:35 zabbix.repo

4.5 修改 /etc/yum.repos.d/zabbix.repo

啟用下載 zabbix 的前端源,將[zabbix-frontend]下的 enabled 改為 1

[[email protected] tools]# vi /etc/yum.repos.d/zabbix.repo

4.6 安裝 zabbix 前端和php相關環境

[[email protected] tools]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y 

[[email protected] tools]# rpm -qa |grep zabbix
zabbix-release-5.0-1.el7.noarch
zabbix-agent-5.0.7-1.el7.x86_64   
zabbix-web-mysql-scl-5.0.7-1.el7.noarch  #####
zabbix-apache-conf-scl-5.0.7-1.el7.noarch  #####
zabbix-server-mysql-5.0.7-1.el7.x86_64
zabbix-web-5.0.7-1.el7.noarch ##### 
zabbix-web-deps-scl-5.0.7-1.el7.noarch #####

執行上面的命令時rh-php72-php作為依賴被安裝

[[email protected] tools]#  rpm -qa |grep rh-php72-php   
rh-php72-php-zip-7.2.24-1.el7.x86_64
rh-php72-php-process-7.2.24-1.el7.x86_64
rh-php72-php-mysqlnd-7.2.24-1.el7.x86_64
rh-php72-php-mbstring-7.2.24-1.el7.x86_64
rh-php72-php-xml-7.2.24-1.el7.x86_64
rh-php72-php-bcmath-7.2.24-1.el7.x86_64
rh-php72-php-json-7.2.24-1.el7.x86_64
rh-php72-php-cli-7.2.24-1.el7.x86_64
rh-php72-php-gd-7.2.24-1.el7.x86_64
rh-php72-php-fpm-7.2.24-1.el7.x86_64
rh-php72-php-pdo-7.2.24-1.el7.x86_64
rh-php72-php-ldap-7.2.24-1.el7.x86_64
rh-php72-php-common-7.2.24-1.el7.x86_64
rh-php72-php-pear-1.10.5-1.el7.noarch

4.7 設定服務開機自啟動

[[email protected] zabbix]# systemctl enable  rh-php72-php-fpm 
[[email protected] zabbix]# systemctl enable  zabbix-server.service 
[[email protected] zabbix]# systemctl enable  zabbix-agent.service 

4.8 使用 root 使用者進入 mysql,並建立 zabbix 資料庫,注意資料庫編碼

[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-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;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> create user [email protected] identified by 'zabbix123456'; ##這裡和後面配置檔案設定密碼保持一致##
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant all privileges on zabbix.* to [email protected];
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ejucms             |
| mysql              |
| performance_schema |
| zabbix             |
+--------------------+

4.9 向zabbix庫匯入表 — 初始化zabbix資料

[[email protected] ~]# rpm -qa |grep zabbix-server-mysql
zabbix-server-mysql-5.0.7-1.el7.x86_64
[[email protected] ~]#  rpm -ql  zabbix-server-mysql     
/etc/logrotate.d/zabbix-server
/etc/zabbix/zabbix_server.conf
/usr/lib/systemd/system/zabbix-server.service
/usr/lib/tmpfiles.d/zabbix-server.conf
/usr/lib/zabbix/alertscripts
/usr/lib/zabbix/externalscripts
/usr/sbin/zabbix_server_mysql
/usr/share/doc/zabbix-server-mysql-5.0.7
/usr/share/doc/zabbix-server-mysql-5.0.7/AUTHORS
/usr/share/doc/zabbix-server-mysql-5.0.7/COPYING
/usr/share/doc/zabbix-server-mysql-5.0.7/ChangeLog
/usr/share/doc/zabbix-server-mysql-5.0.7/NEWS
/usr/share/doc/zabbix-server-mysql-5.0.7/README
/usr/share/doc/zabbix-server-mysql-5.0.7/create.sql.gz #######
/usr/share/doc/zabbix-server-mysql-5.0.7/double.sql
/usr/share/man/man8/zabbix_server.8.gz
/var/log/zabbix
/var/run/zabbix

[[email protected] ~]# cd /usr/share/doc/zabbix-server-mysql-5.0.7/
[[email protected] zabbix-server-mysql-5.0.7]# ll
總用量 2784
-rw-r--r--. 1 root root      98 12月 14 18:27 AUTHORS
-rw-r--r--. 1 root root 1165388 12月 21 17:38 ChangeLog
-rw-r--r--. 1 root root   17990 12月 14 18:27 COPYING
-rw-r--r--. 1 root root 1644549 12月 21 18:24 create.sql.gz   ###也可以先使用gunzip命令解壓再匯入###
-rw-r--r--. 1 root root     282 12月 14 18:27 double.sql
-rw-r--r--. 1 root root      52 12月 14 18:27 NEWS
-rw-r--r--. 1 root root    1317 12月 14 18:27 README

[[email protected] zabbix-server-mysql-5.0.7]# zcat /usr/share/doc/zabbix-server-mysql-5.0.7/create.sql.gz | mysql -uzabbix -p zabbix
Enter password: 
[[email protected] zabbix-server-mysql-5.0.7]# echo $?
0

4.10 修改/etc/zabbix/zabbix_server.conf 配置檔案

[[email protected] zabbix-server-mysql-5.0.7]# cd /etc/zabbix/
[[email protected] zabbix]# ll
總用量 40
drwxr-xr-x. 2 apache apache    32 1月  19 12:40 web
-rw-r--r--. 1 root   root   15101 12月 21 18:27 zabbix_agentd.conf
drwxr-xr-x. 2 root   root       6 12月 21 18:27 zabbix_agentd.d
-rw-r-----. 1 root   zabbix 21531 12月 21 18:27 zabbix_server.conf

配置檔案修改前:


[[email protected] zabbix]#  egrep -v "^$|^#"  zabbix_server.conf 
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBName=zabbix
DBUser=zabbix
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1

配置檔案修改後:

和主機連線91行(取消註釋)

[[email protected] zabbix]#  egrep -v "^$|^#"  zabbix_server.conf 
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=localhost ###主機連線地址 第91行###
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix123456 ### 設定資料庫密碼 第124行###
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1

4.11 修改/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf 配置檔案

[[email protected] zabbix]# vi  /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf 
#最後一行新增以下內容修改時區
php_value[date.timezone] = Asia/Shanghai

4.12 啟動相關服務

[[email protected] zabbix]# systemctl restart mariadb.service 
[[email protected] zabbix]# systemctl restart httpd 
[[email protected] zabbix]# systemctl restart rh-php72-php-fpm 
[[email protected] zabbix]# systemctl restart zabbix-server.service 
[[email protected] zabbix]# systemctl restart zabbix-agent.service 

4.13 檢查相關服務啟動是否正常

[[email protected] tools]# netstat -lntup 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1461/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1693/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      128031/zabbix_agent           
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      128039/zabbix_serve 
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      128029/php-fpm: mas 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      127830/mysqld       
tcp        0      0 0.0.0.0:8480              0.0.0.0:*               LISTEN      21789/nginx: master 
tcp6       0      0 :::22                   :::*                    LISTEN      1461/sshd