天天看點

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

目錄

  • 環境
  • 一、zabbix伺服器的安裝
    • 1.部署LAMP架構
      • 1.1 安裝依賴包
      • 1.2 修改配置檔案
      • 1.3 啟動apache和mariadb
      • 1.4 初始化資料庫
      • 1.5 驗證php網頁是否可以打開
      • 1.6 對zabbix使用者授權
      • 1.7 驗證php和mysql的關聯性
    • 2.部署zabbix伺服器
      • 2.1 添加zabbix源
      • 2.2 安裝zabbix服務端軟體
      • 2.3 導入zabbix監控資料庫
      • 2.4 解決亂碼問題
      • 2.5 啟動zabbix
    • 3.zabnix網頁配置
    • 4.設定linux版zabbix用戶端
      • 4.1 修改配置
      • 4.2 啟動服務
      • 4.3 web建立被控主機
      • 4.4 安裝郵件服務
    • 5.添加規則
      • 5.1 報警媒介
      • 5.2 建立動作
      • 5.3 配置主機
      • 5.4 停止用戶端服務,測試

環境

zabbix   192.168.30.7/24  centos7.6
linux    192.168.30.8/24  centos7.6
windows  192.168.30.100/24
           

一、zabbix伺服器的安裝

1.部署LAMP架構

1.1 安裝依賴包

[[email protected] ~]# 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
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
軟體包 httpd-2.4.6-97.el7.centos.x86_64 已安裝并且是最新版本
軟體包 1:mariadb-server-5.5.68-1.el7.x86_64 已安裝并且是最新版本
軟體包 1:mariadb-5.5.68-1.el7.x86_64 已安裝并且是最新版本
軟體包 php-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 php-mysql-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 php-gd-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 libjpeg-turbo-1.2.90-8.el7.x86_64 已安裝并且是最新版本
軟體包 libjpeg-turbo-devel-1.2.90-8.el7.x86_64 已安裝并且是最新版本
軟體包 libjpeg-turbo-static-1.2.90-8.el7.x86_64 已安裝并且是最新版本
軟體包 libjpeg-turbo-utils-1.2.90-8.el7.x86_64 已安裝并且是最新版本
軟體包 php-ldap-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 php-odbc-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 1:php-pear-1.9.4-22.el7.noarch 已安裝并且是最新版本
軟體包 php-xml-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 php-xmlrpc-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 php-common-5.4.16-48.el7.x86_64 已安裝并且是最新版本
無須任何處理
[[email protected] ~]# 

           

1.2 修改配置檔案

[[email protected] ~]# vim /etc/httpd/conf/httpd.conf 
ServerName www.aa.com:80
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
[[email protected] ~]# vim /etc/php.ini
date.timezone = PRC

           

1.3 啟動apache和mariadb

[[email protected] ~]# systemctl start httpd.service
[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# netstat -antp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      22334/httpd         
[[email protected] ~]# netstat -antp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      22596/mysqld        
[[email protected] ~]# 

           

1.4 初始化資料庫

[[email protected] ~]# 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] n    //是否設定密碼
 ... 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] 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] 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] n    //是否删除test庫
 ... 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.


           

1.5 驗證php網頁是否可以打開

[[email protected] ~]# vi /var/www/html/index.php
<?php
phpinfo();
?>
           
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

1.6 對zabbix使用者授權

[[email protected] ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
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)]> GRANT all ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> 

           

1.7 驗證php和mysql的關聯性

[[email protected] ~]# vi /var/www/html/index.php

<?php
$link=mysql_connect('192.168.30.7','zabbix','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
mysql_close();
?>
~     
           
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

2.部署zabbix伺服器

2.1 添加zabbix源

[[email protected] ~]# vi /etc/yum.repos.d/CentOS-Base.repo 

[Ali]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

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

[zabbix]
name=Zabbix Official Repository - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/\$basearch/
enabled=1
gpgcheck=0

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - \$basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/\$basearch/
enabled=1
gpgcheck=0
[[email protected] ~]# yum clean all    //清理緩存
已加載插件:fastestmirror, langpacks
正在清理軟體源: Ali epel zabbix zabbix-non-supported
Cleaning up list of fastest mirrors
Other repos take up 1.0 G of disk space (use --verbose for details)
[[email protected] ~]# yum makecache   //添加緩存
已加載插件:fastestmirror, langpacks
Determining fastest mirrors
Ali                                                                                                       | 3.6 kB  00:00:00     
epel                                                                                                      | 4.7 kB  00:00:00     
zabbix                                                                                                    | 2.9 kB  00:00:00     
zabbix-non-supported                                                                                      | 2.9 kB  00:00:00     
(1/16): Ali/7/x86_64/group_gz                                                                             | 153 kB  00:00:00     
(2/16): Ali/7/x86_64/primary_db                                                                           | 6.1 MB  00:00:01     
(3/16): Ali/7/x86_64/filelists_db                                                                         | 7.2 MB  00:00:01     
(4/16): Ali/7/x86_64/other_db                                                                             | 2.6 MB  00:00:00     
(5/16): epel/x86_64/group_gz                                                                              |  96 kB  00:00:00     
(6/16): epel/x86_64/updateinfo                                                                            | 1.0 MB  00:00:00     
(7/16): epel/x86_64/prestodelta                                                                           | 1.2 kB  00:00:00     
(8/16): epel/x86_64/filelists_db                                                                          |  12 MB  00:00:02     
(9/16): epel/x86_64/primary_db                                                                            | 6.9 MB  00:00:02     
(10/16): epel/x86_64/other_db                                                                             | 3.4 MB  00:00:00     
(11/16): zabbix/x86_64/primary_db                                                                         | 231 kB  00:00:00     
(12/16): zabbix/x86_64/other_db                                                                           | 313 kB  00:00:00     
(13/16): zabbix-non-supported/x86_64/primary_db                                                           | 3.7 kB  00:00:00     
(14/16): zabbix/x86_64/filelists_db                                                                       | 176 kB  00:00:00     
(15/16): zabbix-non-supported/x86_64/filelists_db                                                         | 1.7 kB  00:00:00     
(16/16): zabbix-non-supported/x86_64/other_db                                                             | 3.3 kB  00:00:00     
中繼資料緩存已建立
[[email protected] ~]# 

           

2.2 安裝zabbix服務端軟體

[[email protected] ~]# yum install -y php-mbstring php-bcmath   //添加必要插件
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
軟體包 php-mbstring-5.4.16-48.el7.x86_64 已安裝并且是最新版本
軟體包 php-bcmath-5.4.16-48.el7.x86_64 已安裝并且是最新版本
無須任何處理

           
[[email protected] ~]# rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
擷取http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.dz2aRz: 頭V4 RSA/SHA512 Signature, 密鑰 ID a14fe591: NOKEY
準備中...                          ################################# [100%]
正在更新/安裝...
   1:zabbix-release-3.5-1.el7         ################################# [100%]

[[email protected] ~]# yum install zabbix-server-mysql zabbix-web-mysql  -y  --nogpgcheck
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
軟體包 zabbix-server-mysql-4.0.33-1.el7.x86_64 已安裝并且是最新版本
軟體包 zabbix-web-mysql-4.0.33-1.el7.noarch 已安裝并且是最新版本
無須任何處理
[[email protected] ~]# 

           

2.3 導入zabbix監控資料庫

[[email protected] ~]# zcat /usr/share/doc/zabbix-server-mysql-4.0.33/create.sql.gz | mysql -uzabbix -padmin123 zabbix  //導入zabbix監控資料庫
[[email protected] ~]# vim /etc/zabbix/zabbix_server.conf   //檢視zabbix未注釋的生效行
[[email protected] ~]# 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
82:SocketDir=/var/run/zabbix
100:DBName=zabbix
116:DBUser=zabbix
124:DBPassword=admin123
356:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
471:Timeout=4
514:AlertScriptsPath=/usr/lib/zabbix/alertscripts
525:ExternalScripts=/usr/lib/zabbix/externalscripts
561:LogSlowQueries=3000
[[email protected] ~]# 

           

2.4 解決亂碼問題

[[email protected] fonts]# vim /usr/share/zabbix/include/defines.inc.php
:%s /graphfont/kaiti/g 
[[email protected] ~]# cd /usr/share/zabbix/assets/fonts/
[[email protected] fonts]# ls
graphfont.ttf
[[email protected] fonts]# rz -E
rz waiting to receive.
[[email protected] fonts]# rz -E
rz waiting to receive.
[[email protected] fonts]# ls
 COUR.TTF  graphfont.ttf
[[email protected] fonts]# mv COUR.TTF simkai.ttf
[[email protected] fonts]# ls
graphfont.ttf  simkai.ttf

           

2.5 啟動zabbix

[[email protected] fonts]# 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.
[[email protected] fonts]# systemctl start zabbix-server
[[email protected] fonts]# netstat -anpt | grep zabbix 
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      25764/zabbix_server 
tcp6       0      0 :::10051                :::*                    LISTEN      25764/zabbix_server 
[[email protected] fonts]# systemctl restart httpd.service
[[email protected] fonts]# netstat -antp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      25847/httpd         
[[email protected] fonts]# 

           
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

3.zabnix網頁配置

健康檢查

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

連接配接資料庫

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

zabbix伺服器名稱

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

資訊總覽

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

4.設定linux版zabbix用戶端

[[email protected] ~]# yum install -y zabbix-agent
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
軟體包 zabbix-agent-4.0.33-1.el7.x86_64 已安裝并且是最新版本
無須任何處理
[[email protected] ~]# 

           

4.1 修改配置

[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf 
[[email protected] ~]# 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=192.168.30.7
139:ServerActive=192.168.30.7
150:Hostname=client
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf

           

4.2 啟動服務

[[email protected] ~]# 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.
[[email protected] ~]# systemctl restart zabbix-agent.service
[[email protected] ~]# netstat -anpt | grep zabbix
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      19600/zabbix_agentd 
tcp6       0      0 :::10050                :::*                    LISTEN      19600/zabbix_agentd 
           

4.3 web建立被控主機

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

4.4 安裝郵件服務

[[email protected] fonts]# yum -y install mailx
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
軟體包 mailx-12.5-19.el7.x86_64 已安裝并且是最新版本
無須任何處理

           

測試

[[email protected] assets]# cd /usr/lib/zabbix/alertscripts/
[[email protected] alertscripts]# ls
mailx.sh
[[email protected] alertscripts]# cat mailx.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
[[email protected] alertscripts]# vim /etc/mail.rc
[[email protected] alertscripts]# vi mailx.sh 
[[email protected] alertscripts]# touch /tmp/mailx.log 
[[email protected] alertscripts]# chown -R zabbix.zabbix  /tmp/mailx.log 
[[email protected] alertscripts]# chmod +x /usr/lib/zabbix/alertscripts/mailx.sh
[[email protected] alertscripts]# chown -R zabbix.zabbix /usr/lib/zabbix/

[[email protected] alertscripts]# ./mailx.sh [email protected] "123""123"

           
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

5.添加規則

5.1 報警媒介

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

5.2 建立動作

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

5.3 配置主機

Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

5.4 停止用戶端服務,測試

[email protected] ~]# systemctl stop zabbix-agent.service
//用戶端停止
           
[[email protected] zabbix]# tail -f /var/log/zabbix/zabbix_server.log    //伺服器日志
 34013:20210903:000022.790 Zabbix agent item "agent.ping" on host "client" failed: first network error, wait for 15 seconds
 34015:20210903:000037.751 Zabbix agent item "proc.num[,,run]" on host "client" failed: another network error, wait for 15 seconds
 34015:20210903:000052.758 Zabbix agent item "system.cpu.load[percpu,avg5]" on host "client" failed: another network error, wait for 15 seconds
 34015:20210903:000107.766 temporarily disabling Zabbix agent checks on host "client": host unavailable

           
Zabbix安裝配置及郵件報警環境一、zabbix伺服器的安裝

繼續閱讀