19.3/19.4/19.6 安裝zabbix

安裝zabbix

  • 官網下載地址
  • wget repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
  • rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm
  • yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql
  • 會連帶安裝httpd和php
  • 若是mysql以前沒有安裝的話,須要根據lamp那一章的mysql安裝方法安裝mysql
  • vim /etc/my.cnf //須要增長配置
character_set_server = utf8
  • 重啓mysqld服務後,進入mysql命令行,建立zabbix庫
    • create database zabbix character set utf8;
  • 再建立用戶
    • grant all on zabbix.* to 'zabbix'@'127.0.0.1' identified by 'aming-zabbix';
  • 導入數據
  • cd /usr/share/doc/zabbix-server-mysql-3.2.7
  • gzip -d create.sql.gz
  • mysql -uroot -pxxx zabbix < create.sql
  • systemctl start httpd; systemctl enable httpd
  • vim /etc/zabbix/zabbix_server.conf //修改或增長
DBHost=127.0.0.1 //在DBName=zabbix上面增長
DBPassword=aming-zabbix //在DBuser下面增長
  • systemctl start zabbix-server
  • systemctl enable zabbix-server
  • netstat -lntp |grep zabbix //查看監聽端口
  • 瀏覽器訪問http://ip/zabbix/ web界面下面配置zabbix
  • 用戶名Admin 密碼zabbix
  • 進入後臺第一件事情就是修改密碼

安裝zabbix

  • 準備工做
    • 兩臺機器,一臺做爲服務端(監控中心),另一臺做爲它的客戶端
  • 背景
    • zabbix,能夠直接yum安裝,但須要安裝epel擴展源,在epel擴展源裏面安裝的zabbix的版本,最高是2.2版本,比較舊,因此使用官方提供的repo源
  • 這裏實驗使用的zabbix3.2版本
  1. 首先訪問官網下載地址,選擇對應的版本,並提供對應的發行版,而後選擇download,跳轉到一個界面,找到這個rpm包(包含release),也就是說yum源在這個裏面,下圖所示,複製該連接

輸入圖片說明

  1. 在兩臺機器都須要下載它
A機器
[root@hf-01 ~]# wget wget http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm

B機器
[root@hf-02 ~]# wget wget http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
  1. 下載完以後,安裝rpm包(實際上就是安裝了一個yum源倉庫)
A機器安裝rpm包
[root@hf-01 ~]# rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm

B機器安裝rpm包
[root@hf-02 ~]# rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm
  1. 查看/etc/yum.repos.d/目錄下面會看到增長了一個zabbix.repo
[root@hf-01 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo  zabbix.repo
CentOS-CR.repo         CentOS-Media.repo      epel.repo.1
CentOS-Debuginfo.repo  CentOS-Sources.repo    epel-testing.repo
[root@hf-01 ~]# cat /etc/yum.repos.d/zabbix.repo 
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/3.2/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch 
baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
[root@hf-01 ~]#
  1. 再有了這個zabbix.repo源以後,就能夠yum安裝zabbix了,並安裝下面的包
  • yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql
    • zabbix-agent //客戶端軟件
    • zabbix-get //服務端用的一個工具,這個工具能夠經過命令行的形式,得到客戶端某些監控項目的數據
    • zabbix-server-mysql //這個是服務端跟mysql打交道相關的組件
    • zabbix-web //webUI ,web界面
    • zabbix-web-mysql //web和mysql相關組件
  • PS:在服務端是須要安裝這些全部的包,而在客戶端僅僅須要安裝 zabbix-agent 包便可
A機器
[root@hf-01 ~]# yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql

B機器
[root@hf-02 ~]# yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql
  1. zabbix 是基於php環境的,也就是說用的是php 的代碼編寫的web界面,也就說這個zabbix 至關於一個站點
  2. 安裝mysql,若是mysql以前沒有安裝的話,須要根據lamp那一章的mysql安裝方法安裝mysql
  • 固然yum安裝mysql、或mariadb也能夠
  • 安裝mysql
A機器(由於以前安裝過mysql)
[root@hf-01 ~]# !ps
ps aux| grep mysql
root      1203  0.0  0.1 115388  1684 ?        S    01:22   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/hf-01.pid
mysql     1498  0.1 45.3 973552 458376 ?       Sl   01:22   0:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/hf-01.err --pid-file=/data/mysql/hf-01.pid
root      2586  0.0  0.0 112676   980 pts/1    R+   02:10   0:00 grep --color=auto mysql
[root@hf-01 ~]#
  1. 修改mysql的配置文件,修改/etc/my.cnf文件,設定默認的字符集
A機器
[root@hf-01 ~]# vim /etc/my.cnf
在[mysqld]下添加字符集

character_set_server = utf8
保存退出
  • 若是不設置字符集,那麼zabbix的web界面,設置成中文的話,可能就會出現亂碼
  1. 修改完mysql的配置文件後,重啓mysql服務
  • /etc/init.d/mysqld restart
A機器
[root@hf-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@hf-01 ~]#
  1. 重啓mysqld服務後,進入mysql命令行(第一次進入mysql命令行),建立zabbix庫,並指定編碼爲utf8
    • create database zabbix character set utf8;
A機器
[root@hf-01 ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@hf-01 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> create database zabbix character set utf8;        //建立zabbix庫,並指定編碼爲utf8
Query OK, 1 row affected (0.00 sec)

mysql>
  1. 再建立用戶,這個用戶是須要讓web(php)代碼鏈接mysql的,監控中心zabbix mysql服務,啓動的前提就是要鏈接mysql;客戶端採集了數據,須要把這些數據傳遞給服務端,那麼服務端怎麼把這些數據寫入到MySQL去,這時就涉及到了mysql 的用戶,涉及到了mysql 的數據庫;用哪個用戶,去寫入那一個庫。
  • grant all on zabbix.* to 'zabbix'@'127.0.0.1' identified by 'hanfeng-zabbix';
mysql> grant all on zabbix.* to 'zabbix'@'127.0.0.1' identified by 'hanfeng-zabbix';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@hf-01 ~]#
  1. 退出mysql的命令行後,導入原始的數據,若缺乏這些數據,zabbix就沒有辦法工做,就沒有辦法在web界面下展示給咱們
  • zabbix-server-mysql-3.2.11 這塊用tab鍵補全,有可能版本號不一樣
A機器
[root@hf-01 ~]# cd /usr/share/doc/zabbix-server-mysql-3.2.11/
[root@hf-01 zabbix-server-mysql-3.2.11]#
  1. 在/usr/share/doc/zabbix-server-mysql-3.2.11/目錄下有一個create.sql.gz壓縮包,將它解壓,解壓完以後生成了一個.sql的文件
A機器
[root@hf-01 zabbix-server-mysql-3.2.11]# ls
AUTHORS  ChangeLog  COPYING  create.sql.gz  NEWS  README
[root@hf-01 zabbix-server-mysql-3.2.11]# gzip -d create.sql.gz
[root@hf-01 zabbix-server-mysql-3.2.11]# ls
AUTHORS  ChangeLog  COPYING  create.sql  NEWS  README
[root@hf-01 zabbix-server-mysql-3.2.11]#
  1. 而後拿到create.sql文件,將它導入到zabbix庫裏面去
A機器
[root@hf-01 zabbix-server-mysql-3.2.11]# mysql -uroot -phanfeng zabbix < create.sql 
Warning: Using a password on the command line interface can be insecure.
[root@hf-01 zabbix-server-mysql-3.2.11]#
  1. 啓動zabbix服務,啓動zabbix報錯
  • systemctl start zabbix-server
[root@hf-01 ~]# systemctl start zabbix-server
[root@hf-01 ~]#
  1. 啓動httpd服務,在啓動http服務以前,先查看nginx服務是否啓動,若啓動了nginx,就監聽了80端口,就須要去停掉nginx服務,再去啓動httpd服務
[root@hf-01 ~]# ps aux |grep nginx
root      2866  0.0  0.0 112676   984 pts/1    R+   03:06   0:00 grep --color=auto nginx
[root@hf-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1517/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1191/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1517/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2749/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1191/sshd           
[root@hf-01 ~]# systemctl start httpd
[root@hf-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1517/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1191/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1517/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2749/mysqld         
tcp6       0      0 :::80                   :::*                    LISTEN      2874/httpd          
tcp6       0      0 :::22                   :::*                    LISTEN      1191/sshd           
[root@hf-01 ~]#
  • 這時就發現httpd服務監聽了80端口
  1. 若須要開機啓動httpd服務和zabbix服務,則須要將它加入到服務的列表中去
  • systemctl enable httpd
  • systemctl enable zabbix-server
A機器
[root@hf-01 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@hf-01 ~]# systemctl enable zabbix-server
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
[root@hf-01 ~]#
  • 若nginx設置了開機啓動,則須要取消開機啓動
    • systemctl disable nginx
    • chkconfig nginx off
  1. 查看zabbix服務進程
[root@hf-01 ~]# ps aux |grep zabbix
zabbix    2863  0.0  0.3 251752  3332 ?        S    03:04   0:00 /usr/sbin/zabbix_server -c /etc/zabbix/zabbix_server.conf
root      2926  0.0  0.0 112676   984 pts/1    R+   03:15   0:00 grep --color=auto zabbix
[root@hf-01 ~]#
  1. 查看zabbix監聽的端口
[root@hf-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1517/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1191/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1517/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      2749/mysqld         
tcp6       0      0 :::80                   :::*                    LISTEN      2874/httpd          
tcp6       0      0 :::22                   :::*                    LISTEN      1191/sshd           
[root@hf-01 ~]#
  • 錯誤:
    • 這裏看到服務啓動了,但並無zabbix監聽的端口
  1. 查看zabbix並無監聽端口後,首先檢查zabbix的錯誤日誌,錯誤日誌路徑/var/log/zabbix/zabbix_server.log
[root@hf-01 ~]# tail /var/log/zabbix/zabbix_server.log
  2863:20180103:033242.719 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  2863:20180103:033242.719 database is down: reconnecting in 10 seconds
  2863:20180103:033252.720 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  2863:20180103:033252.720 database is down: reconnecting in 10 seconds
  2863:20180103:033302.721 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  2863:20180103:033302.721 database is down: reconnecting in 10 seconds
  2863:20180103:033312.722 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  2863:20180103:033312.723 database is down: reconnecting in 10 seconds
  2863:20180103:033322.724 [Z3001] connection to database 'zabbix' failed: [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
  2863:20180103:033322.724 database is down: reconnecting in 10 seconds
[root@hf-01 ~]#
  • 雖然zabbix服務有進程,但沒有完全的起來,說明不可以鏈接mysql,因此還須要更改zabbix的配置文件
  1. 更改zabbix的配置文件,配置文件爲/etc/zabbix/zabbix_server.conf
A機器
[root@hf-01 ~]# vim /etc/zabbix/zabbix_server.conf

搜索/DBHost
在DBName=zabbix上面增長
DBHost=127.0.0.1

在DBuser下面增長
DBPassword=hanfeng-zabbix

保存退出
  • 由於mysql和zabbix裝在同一臺機器上,這裏的 DBHost能夠寫localhost 或者127.0.0.1;若是是在生產環境上,有時候爲了提高zabbix的性能,可能會把MySQL裝在其餘機器上,這是的DBHost,就須要寫上mysql的ip
  1. 更改完zabbix配置文件,就能夠啓動zabbix服務了,由於以前啓動了zabbix服務,這裏只需restart重啓下便可
A機器
[root@hf-01 ~]# systemctl restart zabbix-server
[root@hf-01 ~]#
  1. 這時再來查看下zabbix進程,有29個(正常狀況下,都是這麼多進程)
A機器
[root@hf-01 ~]# ps aux |grep zabbix |wc -l
29
[root@hf-01 ~]#
  1. 查看監聽的端口,zabbix監聽的端口爲10051端口
A機器
[root@hf-01 ~]# netstat -lntp |grep zabbix
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      3303/zabbix_server  
tcp6       0      0 :::10051                :::*                    LISTEN      3303/zabbix_server  
[root@hf-01 ~]#
  1. 接下來就是配置web界面了(須要關閉防火牆和規則,不然瀏覽器沒法訪問)
  • 在瀏覽器輸入IP地址(即監控中心的IP地址),如192.168.74.129/zabbix 去訪問

輸入圖片說明

  • 不少人會有疑問,僅僅啓動了httpd 服務,沒有去作任何的配置,由於yum安裝zabbix,它默認安裝httpd 及它的配置文件,一切都會幫你配置好了
  1. 再打開zabbix界面後,點擊 Next step ,會出現一個提示,出現OK的都不用管,會看到提示須要去設置php的timezone時區,以下圖

輸入圖片說明

  1. 設置php的timezone時區,在php的配置文件中設置,默認php的配置文件在/etc/php.ini 裏面
A機器
[root@hf-01 ~]# vim /etc/php.ini

搜索 /timezone
將 ;date.timezone 改成以下

date.timezone = Asia/Shanghai
保存退出
  1. 在定義完php的配置文件,還須要重啓apache
[root@hf-01 ~]# systemctl restart httpd
[root@hf-01 ~]#

29.這時再來刷新瀏覽器,會發現都變成OK了php

輸入圖片說明

30.這時再來 Next step ,來配置數據庫相關信息mysql

  • host :MySQL所在機器IP
  • port:0爲默認端口3306,若是有改動就修改
  • name:庫的名字
  • user:庫的用戶
  • password:用戶的密碼

輸入圖片說明

  1. 而後再選擇Next step ,配置站點名字相關

輸入圖片說明

  • host:站點主機名
  • port:端口,若是有改動就改,沒改動,默認就是10051
  • name:站點的名字,你喜歡就好
  1. 配置完成後會有一個信息顯示,若是配置錯誤,能夠back返回上一步從新修改

輸入圖片說明

  1. 安裝成功後,會有提示界面,以下圖

輸入圖片說明

  1. 登陸管理頁,默認的帳號是Admin,密碼爲zabbixnginx

  2. 在登陸進zabbix以後,首先須要去更改密碼(由於默認的密碼你們都知道的),如果在線上環境不更改密碼,很容易被別人拿到後臺權限web

  • 更改密碼,先選擇 Administration ,而後選擇 Users

輸入圖片說明

  • 而後找到Admin,並點進去,選擇 Change password 修改密碼和選擇語言(支持中文),而後保存退出

輸入圖片說明

  • 這時候密碼改了,但看到界面仍是英文版,刷新瀏覽器便可,會看到變成中文版

輸入圖片說明

  • 這時在退出zabbix,再從新進入下,使用新的密碼,會看到從新進入了,這就是zabbix的web界面了

客戶端安裝zabbix(B機器)

  • 在客戶端上也須要下載zabbix的yum源
  • wget repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
  • rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm
  • yum install -y zabbix-agent
  • vim /etc/zabbix/zabbix_agentd.conf //修改以下配置
Server=127.0.0.1修改成Server=192.168.133.130 //定義服務端的ip(被動模式)
 ServerActive=127.0.0.1修改成ServerActive=192.168.133.130 //定義服務端的ip(主動模式)
Hostname=Zabbix server修改成Hostname=aming-123 //這是自定義的主機名,一會還須要在web界面下設置一樣的主機名
  • systemctl start zabbix-agent
  • systemctl enable zabbix-agent

  1. 首先客戶端下載yum源
B機器
[root@hf-02 ~]# wget wget http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
  1. 下載完以後,安裝rpm包(實際上就是安裝了一個yum源倉庫)
B機器
[root@hf-02 ~]# rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm
  1. 客戶端,編輯zabbix的配置文件,文件爲/etc/zabbix/zabbix_agentd.conf,服務端和客戶端通訊,就須要指定一個白名單IP
B機器
[root@hf-02 ~]# vi /etc/zabbix/zabbix_agentd.conf

搜索 /Server
Server=192.168.202.130
# 定義服務端的ip(被動模式),配置文件中默認是本機的IP

搜索 /ServerActive
ServerActive=192.168.133.130    
#定義服務端的ip(主動模式)
#這個涉及到一個主動和被動模式;若是這裏不填寫服務中心的IP,那麼就只能使用被動模式;這個IP是主動模式時候用的

搜索 /Hostname
Hostname=Zabbix server修改成Hostname=hf-02 
#這是自定義的主機名,一會還須要在web界面下設置一樣的主機名

保存退出
  1. 啓動zabbix服務
[root@hf-02 ~]# systemctl start zabbix-agent
[root@hf-02 ~]#
  1. 客戶端查看zabbix服務,監聽的端口,客戶端監聽的端口是10050
[root@hf-02 ~]# ps aux |grep zabbix
zabbix    2566  0.0  0.1  80592  1280 ?        S    13:16   0:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix    2567  0.0  0.1  80592  1296 ?        S    13:16   0:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix    2568  0.0  0.1  80592  1836 ?        S    13:16   0:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix    2569  0.0  0.1  80592  1836 ?        S    13:16   0:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix    2570  0.0  0.1  80592  1836 ?        S    13:16   0:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix    2571  0.0  0.2  80720  2180 ?        S    13:16   0:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root      2573  0.0  0.0 112664   972 pts/0    R+   13:17   0:00 grep --color=auto zabbix
[root@hf-02 ~]# netstat -lntp
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      1301/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1997/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      2566/zabbix_agentd  
tcp6       0      0 :::22                   :::*                    LISTEN      1301/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1997/master         
tcp6       0      0 :::10050                :::*                    LISTEN      2566/zabbix_agentd  
[root@hf-02 ~]#
  1. 將zabbix加入到啓動列表裏面,讓它開機啓動
  • systemctl enable zabbix-agent
[root@hf-02 ~]# systemctl enable zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@hf-02 ~]#
  1. 這樣客戶端的zabbix就配置完成
相關文章
相關標籤/搜索