Zabbix4.0環境搭建

Zabbix4.0環境部署

  • LAMP環境部署
  • 安裝Zabbix依賴
  • 安裝Zabbix環境
  • 修改Zabbix默認配置
  • Zabbix網頁監控

Zabbix4.0環境搭建

實驗環境

Zabbix_Server

[root@Zabbix_Server_56 ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@Zabbix_Server_56 ~]# uname -r
3.10.0-862.el7.x86_64
[root@Zabbix_Server_56 ~]# hostname
Zabbix_Server_56.7
[root@Zabbix_Server_56 ~]# ip a | grep 192.168.
    inet 192.168.56.7/24 brd 192.168.56.255 scope global noprefixroute dynamic eth0

LAMP環境部署

注意:Zabbix是建立在LAMP或者LNMP環境之上,在此爲了方便就使用yum安裝LAMP環境,Zabbix3.0以上對php的版本要求在5.4以上

[root@Zabbix_Server_56 ~]# yum -y install httpd httpd-devel mariadb mariadb-server mariadb-devel php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml php-bcmath php-ldap*

查看應用版本信息

[root@Zabbix_Server_56 ~]# rpm -qa httpd php mariadb            #安裝完成後檢查應用版本
mariadb-5.5.60-1.el7_5.x86_64
php-5.4.16-45.el7.x86_64                                        #如php版本不是5.4以上,應升級版本
httpd-2.4.6-80.el7.centos.1.x86_64

啓動並加入開機自啓

[root@Zabbix_Server_56 ~]# systemctl start httpd
[root@Zabbix_Server_56 ~]# systemctl enable httpd
[root@Zabbix_Server_56 ~]# systemctl start mariadb
[root@Zabbix_Server_56 ~]# systemctl enable mariadb
[root@Zabbix_Server_56 ~]# ss -anplt | grep httpd
LISTEN     0      128         :::80                      :::*                   users:(("httpd",pid=1551,fd=4),("httpd",pid=1550,fd=4),("httpd",pid=1549,fd=4),("httpd",pid=1548,fd=4),("httpd",pid=1547,fd=4),("httpd",pid=1545,fd=4))
[root@Zabbix_Server_56 ~]# ss -naplt | grep mysql
LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=1816,fd=13))

網頁測試LAMP環境

[root@Zabbix_Server_56 ~]# vim /var/www/html/index.php
<?php
phpinfo()
?>
[root@Zabbix_Server_56 ~]# systemctl stop iptables
[root@Zabbix_Server_56 ~]# systemctl stop firewalld
[root@Zabbix_Server_56 ~]# setenforce 0
[root@Zabbix_Server_56 ~]# curl http://127.0.0.1 -I         #本地測試
HTTP/1.1 200 OK
Date: Mon, 15 Oct 2018 09:50:07 GMT
Server: Apache/2.4.6 (CentOS) PHP/5.4.16
X-Powered-By: PHP/5.4.16
Content-Type: text/html; charset=UTF-8

安裝Zabbix依賴

創建Zabbix用戶組

[root@Zabbix_Server_56 ~]# useradd -s /sbin/nologin zabbix  -g zabbix

創建Zabbix數據庫以及創建數據庫賬戶和授權

[root@Zabbix_Server_56 ~]# mysqladmin -u root password 123456           #設置數據庫root密碼
[root@Zabbix_Server_56 ~]# mysql -u root -p
Enter password: 
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;       #創建名爲zabbix的數據庫
Query OK, 1 row affected (0.01 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON zabbix.* TO 'zabbix'@'localhost' IDENTIFIED BY '3edc#EDC'; #把名爲zabbix的數據庫授權給zabbix數據庫用戶,密碼爲3edc#EDC,只能在本地登錄
Query OK, 0 rows affected (0.00 sec)

數據庫加固

[root@Zabbix_Server_56 ~]# 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):   #輸入root密碼
OK, successfully used password, moving on...

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

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n                   #是否更改root密碼
 ... skipping.

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] y                     #是否刪除匿名賬戶
 ... Success!

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] y               是否禁止root從遠程登錄
 ... Success!

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] y      #是否刪除test測試數據庫並取消對它的訪問權限
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

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!

安裝zabbix依賴包

[root@Zabbix_Server_56 ~]# yum -y install net-snmp net-snmp-devel curl curl-devel libxml2 libxml2-devel libevent-devel.x86_64 javacc.noarch  javacc-javadoc.noarch javacc-maven-plugin.noarch javacc*

部署JAVA環境

注意:Zabbix監控tomcat需要通過JMX方式監控,JMX監控方式依賴JAVA環境,在此安裝JDK1.8,以下是JDK下載頁面,下載完成上傳到服務器
https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
[root@Zabbix_Server_56 src]# tar xf jdk-8u161-linux-x64.tar.gz  
[root@Zabbix_Server_56 src]# mv jdk1.8.0_161 /usr/local/java 
[root@Zabbix_Server_56 src]# vim /etc/profile                   #在profile文件最後添加java的環境變量
#################JAVA#################
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
[root@Zabbix_Server_56 src]# source /etc/profile
[root@Zabbix_Server_56 src]# java -version                      #查看java版本信息
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

安裝Zabbix環境

下載Zabbix4.0

wget https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/4.0.0/zabbix-4.0.0.tar.gz

解壓並導入SQl

[root@Zabbix_Server_56 src]# tar xf zabbix-4.0.0.tar.gz 
[root@Zabbix_Server_56 src]# cd zabbix-4.0.0/database/mysql/
[root@Zabbix_Server_56 mysql]# ls
data.sql  images.sql  Makefile.am  Makefile.in  schema.sql
注意:數據庫導入順序不能修改,#把以下sql導入zabbix數據庫,用戶名爲zabbix,密碼爲3edc#EDC
[root@Zabbix_Server_56 mysql]# mysql -u zabbix -p3edc#EDC zabbix < schema.sql   
[root@Zabbix_Server_56 mysql]# mysql -u zabbix -p3edc#EDC zabbix < images.sql 
[root@Zabbix_Server_56 mysql]# mysql -u zabbix -p3edc#EDC zabbix < data.sql

編譯安裝

[root@Zabbix_Server_56 mysql]# cd /usr/local/src/zabbix-4.0.0
[root@Zabbix_Server_56 zabbix-4.0.0]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-java --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2 --enable-mbstring
[root@Zabbix_Server_56 zabbix-4.0.0]# make && make install

拷貝zabbix啓動文件

[root@Zabbix_Server_56 zabbix-4.0.0]# cp -rf /usr/local/src/zabbix-4.0.0/misc/init.d/tru64/zabbix_* /etc/init.d/
[root@Zabbix_Server_56 zabbix-4.0.0]# chmod +x /etc/init.d/zabbix_*
[root@Zabbix_Server_56 zabbix-4.0.0]# vim /etc/init.d/zabbix_server
在第二行和第三行添加以下字段以支持開機自啓
  2 #chkconfig:35 95 96
  3 #description:Zabbix_Server
 25 DAEMON=/usr/local/zabbix/sbin/zabbix_server                 #25行原路徑爲/usr/local/sbin/zabbix_server修改爲/usr/local/zabbix/sbin/zabbix_server
[root@Zabbix_Server_56 zabbix-4.0.0]# vim /etc/init.d/zabbix_agentd 
  2 #chkconfig:35 95 96
  3 #description:Zabbix_Agentd
 25 DAEMON=/usr/local/zabbix/sbin/zabbix_agentd

修改Zabbix默認配置

修改Zabbix Server主配置文件

[root@Zabbix_Server_56 /]# cd /usr/local/zabbix/etc/
[root@Zabbix_Server_56 etc]# grep -Ev "#|^$" zabbix_server.conf
LogFile=/tmp/zabbix_server.log                                  #日誌文件路徑
DBHost=localhost                                                #連接數據庫的地址
DBName=zabbix                                                   #連接的數據庫
DBUser=zabbix                                                   #連接數據庫的用戶
DBPassword=3edc#EDC                                             #連接數據庫的密碼
StartPollers=5                                                  #Zabbix進程數,這裏爲實驗環境,所以進程數都較小
StartPollersUnreachable=1                                       #檢查unreachable host(包括IPMI)的進程數
StartTrappers=5                                                 #Trappers(zabbix採集器)進程數,Trappers用於接收其它host用zabbix_sender、active_agents發送的數據,至少需要一個Trapper進程用來在前端顯示Zabbix Server可用性
StartPingers=5                                                  #用於ICMP ping的進程數
StartDiscoverers=1                                              #用於自動發現(Discovery)的discovery的進程數
StartHTTPPollers=1                                              #用於HTTP檢查的進程數
CacheSize=1G                                                    #存儲Host、Item和Trgger數據的內存空間
HistoryCacheSize=256M                                           #存儲History數據的內存大小
HistoryIndexCacheSize=256M                                      #存儲歷史索引緩存共享內存的大小
Timeout=10                                                      #超時時間
AlertScriptsPath=/usr/local/zabbix/etc/alertscripts             #腳本目錄,例如:短信告警腳本SMS,郵件告警腳本mail,微信告警腳步Wechat
FpingLocation=/usr/sbin/fping                                   #Fping路徑
LogSlowQueries=3000                                             
AllowRoot=1                                                     #值爲1就允許root啓動,爲0則不允許
User=zabbix                                                     #zabbix進程啓動用戶
Include=/usr/local/zabbix/etc/zabbix_server.conf.d/*.conf       #鏈接文件
[root@Zabbix_Server_56 etc]# mkdir /usr/local/zabbix/etc//alertscripts      #默認是沒有該文件,需創建
[root@Zabbix_Server_56 etc]# yum install fping -y               

修改Zabbix Agentd配置文件

[root@Zabbix_Server_56 etc]# grep -Ev "#|^$" zabbix_agentd.conf 
LogFile=/tmp/zabbix_agentd.log                                  #日誌路徑
Server=192.168.56.7                                             #被動模式,由server來拉取數據
ServerActive=192.168.56.7                                       #主動模式,主動提交數據給Server
Hostname=192.168.56.7                                           #主機名稱
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf       #鏈接文件

修改php配置文件

注意:php.ini默認的值和zabbix網頁界面不匹配
sed -i 's/post_max_size = 8M/post_max_size = 16M/g' /etc/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /etc/php.ini
sed -i 's/;date.timezone =/date.timezone =PRC/' /etc/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /etc/php.ini
sed -i 's/max_input_time = 60/max_input_time = 300/g' /etc/php.ini
sed -i 's/memory_limit = 128M/memory_limit = 256M/g' /etc/php.ini

拷貝zabbix web網頁到httpd網頁目錄下並做修改

[root@Zabbix_Server_56 etc]# mkdir /var/www/html/zabbix                 #創建zabbix網頁路徑
[root@Zabbix_Server_56 etc]# cp -rf /usr/local/src/zabbix-4.0.0/frontends/php/* /var/www/html/zabbix
[root@Zabbix_Server_56 /]# cd /var/www/html/zabbix/conf
[root@Zabbix_Server_56 conf]# ls
maintenance.inc.php  zabbix.conf.php.example
[root@Zabbix_Server_56 conf]# cp -rf zabbix.conf.php.example zabbix.conf.php
[root@Zabbix_Server_56 conf]# cat zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global $DB, $HISTORY;

$DB['TYPE']             = 'MYSQL';
$DB['SERVER']           = 'localhost';
$DB['PORT']             = '0';
$DB['DATABASE']         = 'zabbix';
$DB['USER']             = 'zabbix';
$DB['PASSWORD']         = '3edc#EDC';                                   #這個地方需要修改爲zabbix數據庫的賬號密碼
// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA']           = '';

$ZBX_SERVER             = 'localhost';
$ZBX_SERVER_PORT        = '10051';
$ZBX_SERVER_NAME        = '';

$IMAGE_FORMAT_DEFAULT   = IMAGE_FORMAT_PNG;

// Uncomment this block only if you are using Elasticsearch.
// Elasticsearch url (can be string if same url is used for all types).
//$HISTORY['url']   = [
//      'uint' => 'http://localhost:9200',
//      'text' => 'http://localhost:9200'
//];
// Value types stored in Elasticsearch.
//$HISTORY['types'] = ['uint', 'text'];

啓動Zabbix,測試訪問

創建軟連接
[root@Zabbix_Server_56 conf]# mkdir /usr/local/etc
[root@Zabbix_Server_56 conf]# ln -s /usr/local/zabbix/etc/zabbix_server.conf /usr/local/etc/
[root@Zabbix_Server_56 conf]# ln -s /usr/local/zabbix/etc/zabbix_agentd.conf /usr/local/etc/

啓動Zabbix並加入開機自啓
[root@Zabbix_Server_56 conf]# /etc/init.d/zabbix_server start
Zabbix server started.
[root@Zabbix_Server_56 conf]# /etc/init.d/zabbix_agentd start
Zabbix agent started.
[root@Zabbix_Server_56 conf]# chkconfig zabbix_server on
[root@Zabbix_Server_56 conf]# chkconfig zabbix_agentd on

重啓httpd
[root@Zabbix_Server_56 conf]# systemctl restart httpd

Zabbix4.0環境搭建
Zabbix4.0環境搭建
Zabbix4.0環境搭建

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