Docker安装服务端
参考资料: zabbixdocker镜像地址 https://hub.docker.com/u/zabbix/
参考资料: zabbix官方文档 https://www.zabbix.com/cn/download
官方提供docker镜像服务:https://hub.docker.com/r/zabbix/zabbix-server-mysql
注意:本容器不包含mysql的安装和配置。使用前,需要先安装好mysql。
# 官方启动命令:
docker run --name some-zabbix-server-mysql -e DB_SERVER_HOST="some-mysql-server" -e MYSQL_USER="some-user" -e MYSQL_PASSWORD="some-password" -d zabbix/zabbix-server-mysql:tag
# 启动命令示例:
docker run --name zabbix_server -e DB_SERVER_HOST="192.168.4.35" -e MYSQL_USER="zabbix" -e MYSQL_PASSWORD="zabbix" -d zabbix/zabbix-server-mysql:centos-latest
# 进入容器:
docker exec -it zabbix_server /bin/bash
# 查看容器日志:
docker logs zabbix_server
因为使用docker容器安装zabbix客户端的话,会影响获取数据的正确性(只能获取到容器内的各项参数),所以客户端使用yum进行安装。
yum安装服务端
关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
vi /etc/selinux/config
# SELINUX=permissive
添加zabbix存储库
# 两种方法
# 方法一,自动:
rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noarch.rpm
# 方法二,手动:
# 找到zabbix的官方yum源:http://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/
vi /etc/yum.repos.d/zabbix.repo
# 写入下面内容
[zabbix]
name=zabbix
baseurl=http://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/
gpgcheck=0
enabled=1
安装zabbix-server-mysql和zabbix-web-mysql
yum install -y zabbix-server-mysql zabbix-web-mysql
要使用MySQL支持安装Zabbix代理
yum install zabbix-proxy-mysql -y
安装zabbix-agent客户端
yum install zabbix-agent -y
# 启动
systemctl start zabbix-agent
# 设置开机自启
chkconfig zabbix-agent on
安装并且启动mysql5.6及初始化数据库信息
# 加载yum源
rpm -ivh http://repo.mysql.com/yum/mysql-5.6-community/el/6/x86_64/mysql-community-release-el6-5.noarch.rpm
# 安装
yum install -y mysql-community-server
# 启动
systemctl start mysqld
# 设置开机自启动
chkconfig mysqld on
# 初始化数据库信息
mysql_secure_installation
[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, 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 MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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] n
... skipping.
By default, MySQL 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
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- 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!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
# 创建zabbix数据库,创建zabbix账号
mysql -uroot -p
# 创建一个zabbix库并设置为utf8的字符编码格式
mysql> create database zabbix character set utf8 collate utf8_bin;
# 创建账户并且授权设置密码
# 给来自loclhost的用户zabbxi分配可对数据库zabbix所有表进行所有操作的权限,并且设定密码为zabbix
mysql> grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix';
mysql> update mysql.user set password=password('zabbix') where user='zabbix';
# 刷新权限
mysql> flush privileges;
# 导入默认的zabbix数据库信息
zcat /usr/share/doc/zabbix-server-mysql-4.2.0/create.sql.gz | mysql zabbix -uzabbix -pzabbix
修改zabbix_server.conf的配置文件
vi /etc/zabbix/zabbix_server.conf
# 修改内容如下:
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
# 查看效果
grep ^DB /etc/zabbix/zabbix_server.conf
编辑php.ini
vi /etc/php.ini
# 修改内容如下:
max_execution_time 300
memory_limit 128M
post_max_size 16M
upload_max_filesize 2M
max_input_time 300
time_zone Asia/Shanghai
修改系统时间
# 安装ntp服务
yum install ntpdate -y
# 调整为亚洲上海时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 同步时间服务器
ntpdate us.pool.ntp.org
修改配置文件
vi /etc/httpd/conf.d/zabbix.conf
# 修改内容如下:
php_value date.timezone Asia/Shanghai
启动apache/zabbix-server服务并设置为开机启动
# 启动apache
systemctl start httpd
systemctl enable httpd
# 启动zabbix-server服务并设置为开机启动
systemctl start zabbix-server
systemctl enable zabbix-server
# 查看zabbix-server日志,如有错误,会在这里显示出来。
tailf /var/log/zabbix/zabbix_server.log
访问网站,例如http://本机ip/zabbix
Zabbix·server的默认账号Admin 密码zabbix,如图所示:
基础界面如下
根据提示可以看到上面图形界面的操作都写到一个配置文件中
more /etc/zabbix/web/zabbix.conf.php
移除或改名web目录下面的setup.php文件
mv setup.php setup.php.bak
设置新的密码,界面的主题、语言和登陆后显示的默认界面等
其他:解决中文乱码无法显示的问题:
复制windows下的字体
Windows字体目录地址C:\Windows\Fonts下找到“楷体常规”字体,复制到zabbix服务器中:
查找zabbix所使用的语言包
find / -name *font* -print | grep zabbix
ll `find / -name *font* -print | grep zabbix`
# 可以得到zabbix-web-font所对应的字体文件的位置:
lrwxrwxrwx. 1 root root 38 Apr 4 14:43 /etc/alternatives/zabbix-web-font -> /usr/share/fonts/dejavu/DejaVuSans.ttf
# 备份本身语言包
mv /usr/share/fonts/dejavu/DejaVuSans.ttf /usr/share/fonts/dejavu/DejaVuSans.ttf.bak
# 替换成windows语言包
mv /root/SIMKAI.TTF /usr/share/fonts/dejavu/DejaVuSans.ttf
刷新页面已经恢复
安装安装zabbix-agent客户端
yum install zabbix-agent -y
# 启动
systemctl start zabbix-agent
# 设置开机自启
chkconfig zabbix-agent on
设置参数
vi /etc/zabbix/zabbix_agentd.conf
# 通用配置
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log #错误日志
LogFileSize=0
Include=/etc/zabbix/zabbix_agentd.d/
#被动模式
Server=192.168.4.35 #被动模式下必填,将zabbix-server端的ip填入
ListenIP=0.0.0.0 #监听所有IP
ListenPort=10050 #启动监听端口
StartAgents=3 #被动模式下选择,主动模式则为0
#主动模式
ServerActive=192.168.4.35 #主动模式下填写
Hostname=agent1.zabbix.com #本机的hostname,此值必须与zabbix的网页上创建的主机名一致即可
# 精简更改内容,一般只要更改这3项就可以了
Server=192.168.4.35
ServerActive=192.168.4.35
Hostname=agent1.zabbix.com
# 使用命令行替换
sed -i "s/127.0.0.1/192.168.4.35/g" /etc/zabbix/zabbix_agentd.conf
服务端添加主机
在配置-》主机-》创建主机
请确保这里的主机名和配置Hostname的主机名一致,这里的IP填写的是安装客户端服务器的IP地址。
为了避免DNS解析出问题,可以在服务端的/etc/hosts上,添加下域名解析内容。如果内网可以自动解析,则这一步可以省略。
# vi /etc/hosts
192.168.4.102 agent1.zabbix.com
192.168.3.60 agent2.zabbix.com
192.168.4.200 agent3.zabbix.com
192.168.4.35 server.zabbix.com
zabbix-get安装与使用
有时候会在zabbix下创建脚本,自定义监控项,在zabbix server上创建监控项前可以先使用zabbix_get来判断脚本是否正确。
安装
# 配置yum源
rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noarch.rpm
# 安装
yum install zabbix-get
使用
可以使用zabbix_get -help
查看用法
usage:
zabbix_get -s host-name-or-IP [-p port-number] [-I IP-address] -k item-key
zabbix_get -s host-name-or-IP [-p port-number] [-I IP-address]
--tls-connect cert --tls-ca-file CA-file
[--tls-crl-file CRL-file] [--tls-agent-cert-issuer cert-issuer]
[--tls-agent-cert-subject cert-subject]
--tls-cert-file cert-file --tls-key-file key-file -k item-key
zabbix_get -s host-name-or-IP [-p port-number] [-I IP-address]
--tls-connect psk --tls-psk-identity PSK-identity
--tls-psk-file PSK-file -k item-key
zabbix_get -h
zabbix_get -V
Get data from Zabbix agent.
....
# 格式
zabbix_get -s host-name-or-IP [-p port-number] [-I IP-address] -k item-key
Zabbix get 支持以下命令行参数:
-s --host <host name or IP> 指定主机名或主机的IP地址.
-p --port <port number> 指定主机上运行代理的端口号. 默认端口10050.
-I --source-address <IP address> 指定源IP地址.
-k --key <item key> 指定需要获取值的监控项.
-h --help 帮助提示.
-V --version 显示版本号.
示例:
# 获取CPU信息
zabbix_get -s 192.168.4.102 -p 10050 -k "system.cpu.load[all,avg1]"
# 其中
# -s 客户端的IP
# -p 客户端端口,默认10050
# -k 监控项的key
# 获取系统版本号
zabbix_get -s 192.168.4.102 -k system.uname
# 获取磁盘根分区的使用量
zabbix_get -s 192.168.4.102 -k vfs.fs.size[/,used]
# 获取磁盘根分区的空余量
zabbix_get -s 192.168.4.102 -k vfs.fs.size[/,free]
# 获取磁盘根分区的磁盘使用率
zabbix_get -s 192.168.4.102 -k vfs.fs.size[/,pused]