1:Zabbix 服务端部署
拓扑图
为了实践zabbix-server的各类安装方式,本次实验采用了三种安装方式,在三台服务器上安装 Zabbix Server,分别采用apt、yum、编译安装。
后续的Zabbix数据库服务只部署一台,并在其中创建三个数据库 zabbix_server_apt、zabbix_server_yum、zabbix_server_compile, 分别为三台 Zabbix Server提供数据服务。
Zabbix 4.0 LTS 中文官方文档地址:https://www.zabbix.com/documentation/4.0/zh/manual
1.1:准备 Zabbix 数据库
无论是apt、yum还是编译安装,数据库的准备工作都是一致的。
1.1.1:安装 MySQL
安装mysql-server
和mysql-client
,server端用于提供数据库服务,client端用于连接MySQL。
也可以安装
mariadb-server
和mariadb-client
root@node100:~# apt update
root@node100:~# apt install mysql-server mysql-client
进行mysql的安全初始化。
root@node100:~# mysql_secure_installation
编辑mysqld.cnf
,更改监听地址。
root@node100:~# vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
重启mysql。
root@node100:~# systemctl restart mysql
1.1.2:创建数据库
为了验证前边三种zabbix-server的安装,共创建三个数据库: zabbix_server_apt、zabbix_server_yum、zabbix_server_compile,分别为之前用三种方式安装的zabbix-server提供数据服务,正常情况下只创建一个数据库。
创建数据库:
CREATE DATABASE zabbix_server_apt CHARACTER SET utf8 COLLATE utf8_bin;
CREATE DATABASE zabbix_server_yum CHARACTER SET utf8 COLLATE utf8_bin;
CREATE DATABASE zabbix_server_compile CHARACTER SET utf8 COLLATE utf8_bin;
创建用户并授权。
三个数据库用同一用户
zabbix_server
来管理。
CREATE USER zabbix_server@'192.168.1.%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON zabbix_server_apt.* TO zabbix_server@'192.168.1.%';
GRANT ALL PRIVILEGES ON zabbix_server_yum.* TO zabbix_server@'192.168.1.%';
GRANT ALL PRIVILEGES ON zabbix_server_compile.* TO zabbix_server@'192.168.1.%';
FLUSH PRIVILEGES;
测试数据库的远程连接:
root@node101:~# mysql -uzabbix_server -p -h192.168.1.100
MySQL [(none)]> SHOW DATABASES;
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| zabbix_server_apt |
| zabbix_server_compile |
| zabbix_server_yum |
+-----------------------+
1.2:apt 方式部署 Zabbix 服务端
Zabbix Server 端一般和 Zabbix Web 前端部署在同一台服务器,数据库在另一台服务器部署;同时,在Zabbix Server 端也安装上 Agent,用于监控 Server 端的运行状况。
打开Zabbix官方主页,点击右上角下载,可以进入下载安装Zabbix的页面(https://www.zabbix.com/cn/download);
依次选择ZABBIX版本、OS分布、OS版本、数据库以及WEB SERVER,就可以看到官方的安装步骤:
这里我选择安装4.0 LTS的长期支持版本,安装系统为Ubuntu 18.04,Zabbix数据库采用MySQL,WEB SERVER 只有一个选项Apache,即httpd。
官方安装步骤如下,下面将按照如下步骤,在Ubuntu 18.04系统上采用apt安装Zabbix 4.0 LTS。
Install and configure Zabbix server for your platform
a. Install Zabbix repository
# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb# dpkg -i zabbix-release_4.0-3+bionic_all.deb# apt update
b. 安装Zabbix server,Web前端,agent
# apt install zabbix-server-mysql zabbix-frontend-php zabbix-agent
c. 创建初始数据库
Make sure you have database server up and running.
在数据库主机上运行以下代码。
# mysql -uroot -ppasswordmysql> create database zabbix character set utf8 collate utf8_bin;mysql> create user zabbix@localhost identified by 'password';mysql> grant all privileges on zabbix.* to zabbix@localhost;mysql> quit;
导入初始架构和数据,系统将提示您输入新创建的密码。
# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
d. 为Zabbix server配置数据库
编辑配置文件 /etc/zabbix/zabbix_server.conf
DBPassword=password
e. 为Zabbix前端配置PHP
编辑配置文件 /etc/zabbix/apache.conf, uncomment and set the right timezone for you.
# php_value date.timezone Europe/Riga
f. 启动Zabbix server和agent进程
启动Zabbix server和agent进程,并为它们设置开机自启:
# systemctl restart zabbix-server zabbix-agent apache2# systemctl enable zabbix-server zabbix-agent apache2
g. 配置Zabbix前端
连接到新安装的Zabbix前端: http://server_ip_or_name/zabbix
根据Zabbix文件里步骤操作: Installing frontend
1.2.1:添加 Zabbix 官方 apt 源
使用apt安装Zabbix时,首先需要添加Zabbix软件源,这样apt就可以依据软件仓库来解决各种依赖关系。
依次下载软件源的deb程序包、安装程序包,并更新apt缓存:
root@node101:~# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb
root@node101:~# dpkg -i zabbix-release_4.0-3+bionic_all.deb
root@node101:~# apt update
1.2.2:更改为阿里云 apt 源
因为Zabbix官方软件源在国外,国内使用可能速度较慢,这时可以将软件源更改到国内的Zabbix软件源,比如阿里云。
安装了Zabbix官方软件源的deb程序包后,会在/etc/apt/sources.list.d/
目录中生成zabbix.list
文件,其中内容就是Zabbix官方软件源的地址。
官方 zabbix.list
:
root@node101:~# cat /etc/apt/sources.list.d/zabbix.list
deb http://repo.zabbix.com/zabbix/4.0/ubuntu bionic main
deb-src http://repo.zabbix.com/zabbix/4.0/ubuntu bionic main
使用sed命令替换,更改 Zabbix 源的配置文件 /etc/apt/sources.list.d/zabbix.list
,将 repo.zabbix.com/zabbix/4.0
更改为 mirrors.aliyun.com/zabbix/zabbix/4.0
,再更新apt缓存,就可以使用阿里云的Zabbix软件仓库来进行Zabbix相关的安装了。
# 先备份,养成更改任何文件前都先备份的习惯,方便回滚。
root@node101:~# cp /etc/apt/sources.list.d/zabbix.list{,.bak}
root@node101:~# sed -i 's@repo.zabbix.com/zabbix/4.0@mirrors.aliyun.com/zabbix/zabbix/4.0@g' /etc/apt/sources.list.d/zabbix.list
root@node101:~# apt update
更改后的 zabbix.list
:
root@node101:~# cat /etc/apt/sources.list.d/zabbix.list
deb http://mirrors.aliyun.com/zabbix/zabbix/4.0/ubuntu bionic main
deb-src http://mirrors.aliyun.com/zabbix/zabbix/4.0/ubuntu bionic main
1.2.3:安装 Zabbix server,Web前端,agent
安装Zabbix Server时,需要同时安装Web 前端,同时也需要为Server安装一个Agent,来监控Server服务器的运行状况。
zabbix-server-mysql 表示:安装的是使用MySQL数据库的zabbix-server。
root@node101:~# apt install zabbix-server-mysql zabbix-frontend-php zabbix-agent
1.2.4:数据库导入初始架构和数据
导入数据前,需要为zabbix-server安装mysql客户端工具。
Ubuntu安装命令:
apt install mysql-client
CentOS安装命令:
yum install mysql
node101 apt安装的zabbix-server导入初始数据库:
root@node101:~# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | \
mysql -uzabbix_server -p -h192.168.1.100 zabbix_server_apt
1.2.5:配置并启动 zabbix-server
关于zabbix-server的配置,此处只做一些启动前的基础配置,优化配置需要结合实际生产环境来调整。
node101 apt安装的zabbix_server.conf的配置:
更改
DBHost
、DBName
、DBUser
、DBPassword
、DBPort
、StartPollers
;
root@node101:~# grep '^[a-Z]' /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=192.168.1.100
DBName=zabbix_server_apt
DBUser=zabbix_server
DBPassword=123456
DBPort=3306
StartPollers=10
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
FpingLocation=/usr/bin/fping
Fping6Location=/usr/bin/fping6
LogSlowQueries=3000
配置完成后启动zabbix-server:
root@node101:~# systemctl start zabbix-server && systemctl enable zabbix-server
验证监听端口,zabbix-server默认监听端口为10051
:
root@node101:~# ss -tnlp | grep 10051
1.2.6:更改时区
使用apt安装时,zabbix web前端的apache配置文件路径为/etc/apache2/conf-enabled/zabbix.conf
,其中配置了PHP相关的一些配置,需要将时区更改为自己所在地区。
将mod_php5.c和mod_php7.c的时区都更改了。本机使用的是php-7.2.24。
root@node101:~# vim /etc/apache2/conf-enabled/zabbix.conf
<IfModule mod_php5.c>
php_value date.timezone Asia/Shanghai
</IfModule>
<IfModule mod_php7.c>
php_value date.timezone Asia/Shanghai
</IfModule>
更改后重启apache2。
root@node101:~# systemctl restart apache2
1.2.7:完成最终配置
打开zabbix web界面(http://192.168.1.101/zabbix),完成最终配置。
点击下一步:
确保先决条件都正常:
完成数据库配置:
填写zabbix server信息:
确认配置信息,确认后点击下一步,完成最终配置:
登录zabbix web界面:
默认用户和密码为:Admin/zabbix。
1.2.8:配置并启动本机的 zabbix-agent
zabbix-server本机的zabbix-agent不需要任何配置,直接启动即可。
如果是被监控主机上的zabbix-agent,则需要配置最基础的
Server
、Hostname
、ListenPort
,如果是主动监控模式,还需要配置ServerActive
。
apt安装的zabbix-agent的默认配置:
root@node101:~# grep '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
启动zabbix-agent:
root@node101:~# systemctl start zabbix-agent && systemctl enable zabbix-agent
1.2.9:Web 界面的中文配置
刚部署完成的zabbix服务端,web界面中有中文选项,但是灰色的,无法选择;这是因为zabbix-server所在操作系统没有中文环境导致。
You are not able to choose some of the languages, because locales for them are not installed on the web server.
为Ubuntu系统安装中文环境:
root@node101:~# apt install language-pack-zh*
增加中文语言环境变量:
root@node101:~# vim /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
LANG="zh_CN.UTF-8"
重新设置系统语言环境:
root@node101:~# dpkg-reconfigure locales
在这里插入图片描述
Generating locales (this might take a while)...
en_AG.UTF-8... done
en_AU.UTF-8... done
en_BW.UTF-8... done
en_CA.UTF-8... done
en_DK.UTF-8... done
en_GB.UTF-8... done
en_HK.UTF-8... done
en_IE.UTF-8... done
en_IL.UTF-8... done
en_IN.UTF-8... done
en_NG.UTF-8... done
en_NZ.UTF-8... done
en_PH.UTF-8... done
en_SG.UTF-8... done
en_US.UTF-8... done
en_ZA.UTF-8... done
en_ZM.UTF-8... done
en_ZW.UTF-8... done
zh_CN.UTF-8... done
zh_HK.UTF-8... done
zh_SG.UTF-8... done
zh_TW.UTF-8... done
Generation complete.
重启apache2,在Web界面完成中文设置:
root@node101:~# systemctl restart apache2
已成为中文界面:
1.2.10:解决中文字体问题
设置为中文界面后,在图形显示中,一些监控项指标名称显示为方框,这是由于虽然设置了中文界面,但是Zabbix的Web前端程序中没有对应的字体导致。
Zabbix Web前端的字体存放目录为/usr/share/zabbix/assets/fonts/
,目前其中存放的字体文件为graphfont.ttf
:
root@node101:~# ll /usr/share/zabbix/assets/fonts/
lrwxrwxrwx 1 root root 38 Mar 9 19:35 graphfont.ttf -> /etc/alternatives/zabbix-frontend-font
从Windows系统中找到个人喜欢的字体文件,上传至zabbix server,这里使用楷体:
win7 的字体存放位置为
控制面板\外观和个性化\字体
。
root@node101:/usr/share/zabbix/assets/fonts# ll
lrwxrwxrwx 1 root root 38 Mar 9 19:35 graphfont.ttf -> /etc/alternatives/zabbix-frontend-font
-rw-r--r-- 1 root root 11785184 Jun 11 2009 simkai.ttf
配置Zabbix Web前端来调用simkai字体:
root@node101:~# vim /usr/share/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME', 'simkai'); // font file name
define('ZBX_FONT_NAME', 'simkai');
验证字体是否生效:
1.3:yum 方式部署 Zabbix 服务端
同样在Zabbixx下载安装页面(https://www.zabbix.com/cn/download)选择对应的ZABBIX版本、OS分布、OS版本、数据库和WEB SERVER,查看对应的安装步骤。
官方的在CentOS7系统安装Zabbix 4.0LTS的步骤如下,下面按照如下步骤进行Zabbix Server在CentOS7.2上的安装。
Install and configure Zabbix server for your platform
a. Install Zabbix repository
# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm# yum clean all
b. 安装Zabbix server,Web前端,agent
# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent
c. 创建初始数据库
Make sure you have database server up and running.
在数据库主机上运行以下代码。
# mysql -uroot -ppasswordmysql> create database zabbix character set utf8 collate utf8_bin;mysql> create user zabbix@localhost identified by 'password';mysql> grant all privileges on zabbix.* to zabbix@localhost;mysql> quit;
导入初始架构和数据,系统将提示您输入新创建的密码。
# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
d. 为Zabbix server配置数据库
编辑配置文件 /etc/zabbix/zabbix_server.conf
DBPassword=password
e. 为Zabbix前端配置PHP
编辑配置文件 /etc/httpd/conf.d/zabbix.conf, uncomment and set the right timezone for you.
# php_value date.timezone Europe/Riga
f. 启动Zabbix server和agent进程
启动Zabbix server和agent进程,并为它们设置开机自启:
# systemctl restart zabbix-server zabbix-agent httpd# systemctl enable zabbix-server zabbix-agent httpd
g. 配置Zabbix前端
连接到新安装的Zabbix前端: http://server_ip_or_name/zabbix
根据Zabbix文件里步骤操作: Installing frontend
1.3.1:添加 Zabbix 官方 yum 源
安装Zabbix官方yum源,并清空yum缓存。
[root@node201 ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
[root@node201 ~]# yum clean all
1.3.2:更改为阿里云 yum 源
同样可以更改Zabbix官方yum源为阿里云的yum源,提升下载速率。
很不巧,在做实验时阿里云镜像站正在升级:
1.3.3:安装 Zabbix Server
安装Zabbix server,Web前端,agent:
[root@node201 ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent
1.3.4:数据库导入初始架构和数据
node201 yum安装的zabbix-server导入初始数据库:
root@node201:~# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | \
mysql -uzabbix_server -p -h192.168.1.100 zabbix_server_yum
1.3.5:配置并启动 zabbix-server
node201 yum安装的zabbix_server.conf的配置:
apt和yum安装的同版本zabbix-server,除了fping的程序路径配置不同外,其它默认配置都相同;所以本次实验中除了数据库名称不同外,二者的基础启动配置都相同。
[root@node201 ~]# grep '^[a-Z]' /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=192.168.1.100
DBName=zabbix_server_yum
DBUser=zabbix_server
DBPassword=123456
DBPort=3306
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
启动zabbix-server,并设为开机自启动。
[root@node201 ~]# systemctl start zabbix-server && systemctl enable zabbix-server
验证监听端口:
[root@node201 ~]# ss -tnlp | grep 10051
1.3.6:更改时区
yum安装时,web前端的PHP相关配置在/etc/httpd/conf.d/zabbix.conf
中,在其中修改时区:
[root@node201 ~]# vim /etc/httpd/conf.d/zabbix.conf
<IfModule mod_php5.c>
php_value date.timezone Asia/Shanghai
</IfModule>
修改后重启httpd:
[root@node201 ~]# systemctl restart httpd
1.3.7:完成最终配置
打开zabbix web界面(http://192.168.1.201/zabbix),完成最终配置。
数据库配置:
zabbix server 信息:
登录zabbix web界面:
1.3.8:配置并启动本机的 zabbix-agent
同样,zabbix-server本机的zabbix-agent无需进行任何配置,直接启动即可。
查看默认配置:
[root@node201 ~]# grep '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
启动zabbix-agent:
[root@node201 ~]# systemctl start zabbix-agent && systemctl enable zabbix-agent
1.3.9:解决中文字体问题
更改为中文界面后,图形的监控项名称同样显示方框:
将中文楷体字体文件simkai.ttf
上传至zabbix web界面的字体目录/usr/share/zabbix/assets/fonts/
:
[root@node201 ~]# ll /usr/share/zabbix/assets/fonts/
total 11512
lrwxrwxrwx 1 root root 33 Mar 10 10:33 graphfont.ttf -> /etc/alternatives/zabbix-web-font
-rw-r--r-- 1 root root 11785184 Jun 11 2009 simkai.ttf
配置zabbix web界面调用simkai字体:
[root@node201 ~]# vim /usr/share/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME', 'simkai'); // font file name
define('ZBX_FONT_NAME', 'simkai');
验证字体是否生效:
1.4:编译方式部署 Zabbix 服务端
Zabbix官方的编译安装文档:https://www.zabbix.com/documentation/4.0/manual/installation/install
下面将按照官方文档的步骤进行Zabbix Server的编译安装。
1.4.1:下载 Zabbix 源码包
到Zabbix下载安装界面(https://www.zabbix.com/cn/download),选择Zabbix Sources,并在下方Download ready-to use Zabbix distributives选择想要安装的版本,下载Zabbix源码包。
Zabbix源码包包含了包括zabbix-server、zabbix-agent、zabbix-proxy、zabbix-javagateway等诸多功能,只需在编译时添加相应选项开启对应的功能即可。
1.4.2:安装编译环境
Ubuntu 18.04 系统的Zabbix编译安装环境、运行环境及运维过程中的常用工具安装。
apt update
apt install apache2 apache2-bin apache2-data apache2-utils fontconfig-config fonts-dejavu-core fping libapache2-mod-php libapache2-mod-php7.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libfontconfig1 libgd3 libiksemel3 libjbig0 libjpeg-turbo8 libjpeg8 liblua5.2-0 libodbc1 libopenipmi0 libsensors4 libsnmp-base libsnmp30 libsodium23 libssh2-1 libtiff5 libwebp6 libxpm4 php-bcmath php-common php-gd php-ldap php-mbstring php-mysql php-xml php7.2-bcmath php7.2-cli php7.2-common php7.2-gd php7.2-json php7.2-ldap php7.2-mbstring php7.2-mysql php7.2-opcache php7.2-readline php7.2-xml snmpd ssl-cert ttf-dejavu-core libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev openjdk-8-jdk curl libcurl4-openssl-dev
CentOS 安装以下程序包来解决编译环境:
yum install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php php-hcmath php-mbstring mariadb mariadb-devel -y
1.4.3:编译安装 Zabbix Server
Ubuntu 和 CentOS 系统的编译安装过程相同。
将下载的Zabbix源码包上传并解压至服务器的/usr/local/src
目录。
root@node102:~# cd /usr/local/src
root@node102:/usr/local/src# tar zxvf zabbix-4.0.29.tar.gz
进入源码目录,进行编译。
指定安装目录为
/apps/zabbix_server
,开启server、agent、java gateway,指定数据库为mysql;监控虚拟机时,需要配置
--with-libcurl
,--with-libxml2
选项;--with-libcurl
在SMTP认证以及使用web.page.*
监控项时也需要。
root@node102:/usr/local/src# cd zabbix-4.0.29/
root@node102:/usr/local/src/zabbix-4.0.29# ./configure \
--prefix=/apps/zabbix_server \
--enable-server --enable-agent --enable-java --with-mysql \
--with-net-snmp --with-libcurl --with-libxml2
执行安装:
root@node102:/usr/local/src/zabbix-4.0.29# make install
1.4.4:创建 Zabbix 用户和组
使用apt或yum安装时,会自动创建zabbix用户和组,在编译安装时,需要手动创建用户和组,因为在运行zabbix-server进程时,最好使用zabbix用户。
创建zabbix用户和组。
root@node102:~# addgroup --system --quiet zabbix
root@node102:~# adduser --quiet --system --disabled-login --ingroup zabbix --home /var/lib/zabbix --no-create-home zabbix
以上命令为 Debian-based 系统的创建用户和组的命令,以下为RedHat-based系统的命令:
groupadd --system zabbix useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix
Ubuntu 系统同样也支持
groupadd
和useradd
命令。
1.4.5:创建 zabbix-server,zabbix-agent 启动脚本
为了方便zabbix-server的进程管理和开机自启动,编译安装时需要创建zabbix-server的启动脚本,即service文件。
因为编译安装是在Ubuntu系统上进行的,可以从apt安装的zabbix-server中拷贝一个service文件,进行修改后使用。
原apt安装生成的zabbix-server.service
文件内容:
root@node101:~# cat /lib/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target
After=mysql.service
After=mysqld.service
After=mariadb.service
[Service]
Environment="CONFFILE=/etc/zabbix/zabbix_server.conf"
EnvironmentFile=-/etc/default/zabbix-server
Type=forking
Restart=on-failure
PIDFile=/run/zabbix/zabbix_server.pid
KillMode=control-group
ExecStart=/usr/sbin/zabbix_server -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
原apt安装生成的zabbix-agent.service
文件内容:
root@node101:~# cat /lib/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/etc/zabbix/zabbix_agentd.conf"
EnvironmentFile=-/etc/default/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/run/zabbix/zabbix_agentd.pid
KillMode=control-group
ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.target
拷贝service文件至编译安装的服务器。
root@node101:~# scp /lib/systemd/system/zabbix-server.service 192.168.1.102:/lib/systemd/system/zabbix-server.service
root@node101:~# scp /lib/systemd/system/zabbix-agent.service 192.168.1.102:/lib/systemd/system/zabbix-agent.service
更改后的zabbix-server.service
文件。
因为zabbix-server和数据库未部署在同一服务器,所以
After=mysql.service
、After=mysqld.service
和After=mariadb.service
都可以删掉;更改了配置文件、pid文件,以及zabbix-server程序的路径:
Environment="CONFFILE=/apps/zabbix_server/etc/zabbix_server.conf" PIDFile=/apps/zabbix_server/run/zabbix_server.pid ExecStart=/apps/zabbix_server/sbin/zabbix_server -c $CONFFILE
**【注意】**service文件中的
PIDFile
要和zabbix_server.conf
中的PidFile
路径一致。
root@node102:~# cat /lib/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/apps/zabbix_server/etc/zabbix_server.conf"
EnvironmentFile=-/etc/default/zabbix-server
Type=forking
Restart=on-failure
PIDFile=/apps/zabbix_server/run/zabbix_server.pid
KillMode=control-group
ExecStart=/apps/zabbix_server/sbin/zabbix_server -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
更改后的zabbix-agent.service
文件。
同样更改了配置文件、pid文件和agent启动程序的路径:
Environment="CONFFILE=/apps/zabbix_server/etc/zabbix_agentd.conf" PIDFile=/apps/zabbix_server/run/zabbix_agentd.pid ExecStart=/apps/zabbix_server/sbin/zabbix_agentd -c $CONFFILE
**【注意】**同样,service文件中的
PIDFile
要和zabbix_agentd.conf
中的PidFile
路径一致。
root@node102:~# cat /lib/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/apps/zabbix_server/etc/zabbix_agentd.conf"
EnvironmentFile=-/etc/default/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/apps/zabbix_server/run/zabbix_agentd.pid
KillMode=control-group
ExecStart=/apps/zabbix_server/sbin/zabbix_agentd -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.target
重载systemd:
root@node102:~# systemctl daemon-reload
1.4.6:数据库导入初始架构和数据
node102 编译安装的zabbix-server导入初始数据库:
zabbix源码包提供了用于导入初始架构和数据的sql脚本,准备zabbix-server的数据库时,需要导入
schema.sql
、images.sql
、data.sql
三个脚本,而且要注意导入顺序。root@node102:~# ll /usr/local/src/zabbix-4.0.29/database/mysql/ -rw-r--r-- 1 yqc yqc 4175260 Feb 22 16:57 data.sql -rw-r--r-- 1 yqc yqc 1978341 Dec 14 18:27 images.sql -rw-r--r-- 1 root root 15707 Mar 10 11:04 Makefile -rw-r--r-- 1 yqc yqc 392 Feb 22 16:57 Makefile.am -rw-r--r-- 1 yqc yqc 15888 Feb 22 16:57 Makefile.in -rw-r--r-- 1 yqc yqc 140265 Feb 22 16:57 schema.sql
root@node102:~# cd /usr/local/src/zabbix-4.0.29/database/mysql/
mysql -uzabbix_server -p -h192.168.1.100 zabbix_server_compile < schema.sql
mysql -uzabbix_server -p -h192.168.1.100 zabbix_server_compile < images.sql
mysql -uzabbix_server -p -h192.168.1.100 zabbix_server_compile < data.sql
查看各数据库中的表,以验证导入成功。
1.4.7:配置并启动 zabbix-server
编译安装的zabbix-server的相关文件和目录可以设定得更符合自己的使用习惯一些。
此处将日志、pid、socket、脚本都设置在/apps/zabbix_server
目录中,方便同一管理。
所以要首先创建一些必要的目录,并修改属主为zabbix:
mkdir /apps/zabbix_server/{logs,run,etc/{alertscripts,externalscripts,zabbix_server_conf.d}}
root@node102:~# chown zabbix:zabbix /apps/zabbix_server -R
node102编译安装的zabbix_server.conf的配置:
root@node102:~# grep '^[a-Z]' /apps/zabbix_server/etc/zabbix_server.conf
LogFile=/apps/zabbix_server/logs/zabbix_server.log
PidFile=/apps/zabbix_server/run/zabbix_server.pid
SocketDir=/apps/zabbix_server/run
DBHost=192.168.1.100
DBName=zabbix_server_compile
DBUser=zabbix_server
DBPassword=123456
DBPort=3306
StartPollers=10
ListenIP=0.0.0.0
Timeout=4
AlertScriptsPath=/apps/zabbix_server/etc/alertscripts
ExternalScripts=/apps/zabbix_server/etc/externalscripts
LogSlowQueries=3000
User=zabbix
Include=/apps/zabbix_server/etc/zabbix_server_conf.d
启动zabbix-server并设为开机自启动:
root@node102:~# systemctl start zabbix-server && systemctl enable zabbix-server
验证监听端口:
root@node102:~# ss -tnlp | grep 10051
1.4.8:拷贝 PHP 文件
编译安装时,需要在web server的根目录中创建一个zabbix目录作为访问的uri,并将源码包中的 php文件拷贝到${WEB_SERVER_ROOT}/zabbix中去。
apache2的默认根目录为/var/www/html
。
在/var/www/html
中创建zabbix 资源目录:
root@node102:~# mkdir /var/www/html/zabbix
将/usr/local/src/zabbix-4.0.29/frontends/php/
中的文件拷贝到/var/www/html/zabbix
中:
root@node102:~# cp /usr/local/src/zabbix-4.0.29/frontends/php/* /var/www/html/zabbix/ -R
1.4.9:更改时区
更改php.ini
,添加时区配置:
root@node102:~# vim /etc/php/7.2/apache2/php.ini
date.timezone = Asia/Shanghai
更改完成后重启apache2:
root@node102:~# systemctl restart apache2
1.4.10:完成最终配置
打开zabbix web界面(http://192.168.1.102/zabbix/),完成最终配置。
在检查先决条件时,会有一些报错,是一些php配置问题,涉及post_max_size
、max_execution_time
、max_input_time
三个参数,按照需求完成相应配置即可。
Minimum required size of PHP post is 16M (configuration option "post_max_size").
Minimum required limit on execution time of PHP scripts is 300 (configuration option "max_execution_time").
Minimum required limit on input parse time for PHP scripts is 300 (configuration option "max_input_time").
编辑php.ini
:
root@node102:~# vim /etc/php/7.2/apache2/php.ini
post_max_size = 16M
max_execution_time = 300
max_input_time = 300
更改完成后重启apache2:
root@node102:~# systemctl restart apache2
验证先决条件:
配置数据库:
配置zabbix server信息
配置完成后,还会出现一个报错,这是因为编译安装时不会自动将zabbix的web界面配置文件拷贝到对应的位置;
手动下载配置文件,并拷贝到对应的位置/var/www/html/zabbix/conf/zabbix.conf.php
即可。
Details Cannot create the configuration file.
Unable to create the configuration file.
Alternatively, you can install it manually:
Download the configuration file
Save it as "/var/www/html/zabbix/conf/zabbix.conf.php"
下载zabbix.conf.php
,上传至/var/www/html/zabbix/conf/zabbix.conf.php
,并验证配置文件的内容是否为刚才在web界面上配置的信息:
root@node102:/var/www/html/zabbix/conf# cat zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global $DB;
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = '192.168.1.100';
$DB['PORT'] = '3306';
$DB['DATABASE'] = 'zabbix_server_compile';
$DB['USER'] = 'zabbix_server';
$DB['PASSWORD'] = '123456';
// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = '';
$ZBX_SERVER = 'localhost';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = 'zabbix_server_compile';
$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
刷新页面后提示已成功:
登录zabbix web界面:
1.4.11:配置并启动本机的 zabbix-agent
同理,编译安装的agent也可以将日志、pid、自定义的监控项配置文件等统一放到安装目录中去,方便管理。
编辑zabbix_agentd.conf:
root@node102:~# grep '^[a-Z]' /apps/zabbix_server/etc/zabbix_agentd.conf
PidFile=/apps/zabbix_server/run/zabbix_agentd.pid
LogFile=/apps/zabbix_server/logs/zabbix_agentd.log
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/apps/zabbix_server/etc/zabbix_agentd.conf.d/
启动zabbix-agent,并设为开机启动:
root@node102:~# systemctl start zabbix-agent && systemctl enable zabbix-agent
验证监听端口:
root@node102:~# ss -tnlp | grep 10050
1.4.12:Web 界面的中文配置
为Ubuntu系统安装中文环境:
root@node102:~# apt install language-pack-zh*
增加中文语言环境变量:
root@node102:~# vim /etc/environment
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
LANG="zh_CN.UTF-8"
重新设置系统语言环境:
root@node102:~# dpkg-reconfigure locales
Generating locales (this might take a while)...
en_AG.UTF-8... done
en_AU.UTF-8... done
en_BW.UTF-8... done
en_CA.UTF-8... done
en_DK.UTF-8... done
en_GB.UTF-8... done
en_HK.UTF-8... done
en_IE.UTF-8... done
en_IL.UTF-8... done
en_IN.UTF-8... done
en_NG.UTF-8... done
en_NZ.UTF-8... done
en_PH.UTF-8... done
en_SG.UTF-8... done
en_US.UTF-8... done
en_ZA.UTF-8... done
en_ZM.UTF-8... done
en_ZW.UTF-8... done
zh_CN.UTF-8... done
zh_HK.UTF-8... done
zh_SG.UTF-8... done
zh_TW.UTF-8... done
Generation complete.
重启apache2,在Web界面完成中文设置:
root@node102:~# systemctl restart apache2
已成为中文界面:
1.4.13:解决中文字体问题
将中文楷体字体文件simkai.ttf
上传至zabbix web前端的字体目录/var/www/html/zabbix/assets/fonts/
中:
root@node102:~# ll /var/www/html/zabbix/assets/fonts/
total 12260
drwxr-xr-x 2 root root 4096 Mar 10 18:09 ./
drwxr-xr-x 5 root root 4096 Mar 10 17:32 ../
-rw-r--r-- 1 root root 756072 Mar 10 17:32 DejaVuSans.ttf
-rw-r--r-- 1 root root 11785184 Jun 11 2009 simkai.ttf
配置Zabbix Web前端来调用simkai字体:
root@node102:~# vim /var/www/html/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME', 'simkai'); // font file name
define('ZBX_FONT_NAME', 'simkai');
验证字体是否生效: