zabbix部署(apache)
Zabbix部署
1:下载源地址与安装zabbix
# 下载源
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
[root@virtual_host ~]# ls /etc/yum.repos.d | grep zabbix
zabbix.repo
# 更换yum源地址
[root@virtual_host ~]# sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
[root@virtual_host ~]# cat /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
---
# 清空缓存
[root@virtual_host ~]# yum clean all
# 安装zabbix连接mysql驱动以及安装zabbix的agent
[root@virtual_host ~]# yum install -y zabbix-server-mysql zabbix-agent
---
Installed:
zabbix-agent.x86_64 0:5.0.20-1.el7 zabbix-server-mysql.x86_64 0:5.0.20-1.el7
-
# (需修改yum源)
cat /etc/yum.repos.d/zabbix.repo
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1 # 此处本为0 改为1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
# 安装 SCL
[root@virtual_host ~]# yum install centos-release-scl -y
---
Installed:
centos-release-scl.noarch 0:2-3.el7.centos
---
# 安装zabbix前端环境
[root@virtual_host ~]# yum install -y zabbix-web-mysql-scl zabbix-apache-conf-scl
--
Installed:
zabbix-apache-conf-scl.noarch 0:5.0.20-1.el7 zabbix-web-mysql-scl.noarch 0:5.0.20-1.el7 --
2:安装并配置数据库(MySQL)
# 部署zabbix所需数据库(可单独拿出来部署)
[root@virtual_host ~]# yum install -y mariadb mariadb-server
---
Installed:
mariadb.x86_64 1:5.5.68-1.el7 mariadb-server.x86_64 1:5.5.68-1.el7
---
# 启动数据库
[root@virtual_host ~]# systemctl enable mariadb.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
# 初始化数据库
[root@virtual_host ~]# 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] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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] n
... skipping.
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
- 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!
# 登录数据库
[root@virtual_host ~]# mysql -uroot -p
passwd:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
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)]>
# 创建数据库
MariaDB [(none)]> # create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
# 创建zabbix用户
MariaDB [(none)]> # create user zabbix@localhost identified by "zabbix";
Query OK, 0 rows affected (0.00 sec)
# 授权zabbix用户
MariaDB [(none)]> # grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)
# 刷新授权
MariaDB [(none)]> # flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退出
MariaDB [(none)]> # exit
Bye
3:使用zabbix-mysql命令导入数据库信息
# 查看数据是否存在
[root@virtual_host ~]# ls /usr/share/doc/zabbix-server-mysql-5.0.20/create.sql.gz
/usr/share/doc/zabbix-server-mysql-5.0.20/create.sql.gz
# 导入数据
[root@virtual_host ~]# zcat /usr/share/doc/zabbix-server-mysql-5.0.20/create.sql.gz | mysql -uzabbix -pzabbix -D zabbix
等待导入即可
# 确认数据
[root@virtual_host ~]# mysql -uzabbix -pzabbix -e "show tables from zabbix;"
+----------------------------+
| Tables_in_zabbix |
+----------------------------+
| acknowledges |
| actions |
| alerts |
| application_discovery |
| application_prototype |
| application_template |
| applications |
| auditlog |
---
4:修改zabbix的配置文件
# 修改zabbix-server配置文件
[root@virtual_host ~]# cat /etc/zabbix/zabbix_server.conf | grep -v "^#" | grep -v "^$"
ListenPort=10051
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBPort=3306
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
# 修改php文件
[root@virtual_host ~]# cat /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
---
php_value[date.timezone] = Asia/Shanghai
# 启动zabbix
[root@virtual_host ~]# systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm.service
[root@virtual_host ~]# systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/rh-php72-php-fpm.service to /usr/lib/systemd/system/rh-php72-php-fpm.service.
# 访问
10.0.0.10/zabbix
账号:Admin
密码:zabbix
5:部署zabbix-agent2并验证
zabbix-agent2采用golang编写 若要与agent公用需要修改端口
# 统一时间
[root@virtual_host ~]# yum install ntpdate -y
[root@virtual_host ~]# ntpdate -u ntp.aliyun.com
# 修改统一时区
[root@virtual_host ~]# mv /etc/localtime{,.bak}
[root@virtual_host ~]# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 下载源
[root@virtual_host ~]# rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
[root@virtual_host ~]# ls /etc/yum.repos.d | grep zabbix
zabbix.repo
# 更换yum源地址
[root@virtual_host ~]# sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
# 安装
[root@virtual_host ~]# yum install -y zabbix-agent2
# 启动
[root@virtual_host ~]# systemctl enable zabbix-agent2.service --now
# 修改agent2配置文件
[root@virtual_host ~]# grep -Ev '^#|^$' /etc/zabbix/zabbix_agent2.conf
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=10.0.0.10 # zabbix-server地址
ListenPort=10050
ListenIP=0.0.0.0
ServerActive=10.0.0.10 # zabbix-server地址
Hostname=zabbix-agent
Include=/etc/zabbix/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.sock
# 重启zabbix-agent2
[root@virtual_host ~]# systemctl restart zabbix-agent2.service
# 安装客户端
[root@virtual_host zabbix]# yum install -y zabbix-get
# 验证
[root@virtual_host zabbix]# zabbix_get -s '10.0.0.11' -p '10050' -k 'agent.ping'
1
6:处理图形乱码问题
# 安装字体样式
[root@virtual_host zabbix]# yum install -y wqy-microhei-fonts
Installed:
wqy-microhei-fonts.noarch 0:0.2.0-0.12.beta.el7
# 覆盖字体
[root@virtual_host ~]# \cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf
\cp 代表去掉 cp -i 默认 只使用cp
# 这样就完成了!
7:监控主机














8:创建自定义监控内容(Key)
监控服务器登录人数:
1:监控登录人数超过3及告警
# 命令行操作 获取agent主机/的大小
[root@virtual_host ~]# zabbix_get -s '10.0.0.11' -p 10050 -k vfs.fs.size[/,used]
1922277376
# 自定义Key获取
# 监控登录人数:
[root@virtual_host ~]# who | wc -l
2
# 去客户端配置
[root@virtual_host ~]# vim /etc/zabbix/zabbix_agent2.conf +287
UnsafeUserParameters=1
[root@virtual_host ~]# cat /etc/zabbix/zabbix_agent2.d/login_monitor.conf
UserParameter=login.user,who|wc -l
# 创建并写入以上内容
# 重启zabbix-agent2
# 服务端测试
[root@virtual_host ~]# zabbix_get -s '10.0.0.11' -p 10050 -k login.user
2
9:自定义模板添加
基于自定义Key去配置模板:
[root@virtual_host ~]# zabbix_get -s '10.0.0.11' -p 10050 -k login.user
2
1:创建模板
2:创建应用集:(类似于文件夹,集合一堆监控项)
3:创建监控项:(自定义item,具体想监控的内容)
4:创建触发器:(当监控项获取到值的时候与触发器比较,决定是否告警)
5:创建图形
6:关联模板到主机






























10:配置邮件告警













测试告警



浙公网安备 33010602011771号