组件说明
zabbix server:负责接收agent发送的报告信息的核心组件,所有配置,统计数据及操作数据都由它组织进行
database storage: 专用于存储所有配置信息,以及由zabbix收集的数据
web interface: zabbix的 GUI 接口
proxy:可选组件,常用于监控节点很多的分布式环境中,代理server收集部分数据转发到server,可以减轻server的压力
agent:部署在被监控的主机上,负责收集主机本地数据如cpu、内存、数据库等数据发往 server 端或 proxy 端
另外,zabbix server,proxy,agent 都有自己的配置文件以及log(日志)文件,重要的参数需要在这里配置
监控系统运行的流程
agentd数据数据模式
agentd收集数据分为主动和被动两种模式:
主动监测通信过程
zabbix首先向ServerActive配置的IP请求获取active items,获取并提交active tiems数据值server或者proxy。zabbix获取一次active items,根据配置文件中的RefreshActiveChecks的频率进行,如果获取失败,那么将会在60秒之后重试
获取ACTIVE ITEMS列表
Agent打开TCP连接(主动检测变成Agent打开)
Agent请求items检测列表
Server返回items列表
Agent 处理响应
关闭TCP连接
Agent开始收集数据
主动检测提交数据过程如下:
Agent建立TCP连接
Agent提交items列表收集的数据
Server处理数据,并返回响应状态
关闭TCP连接
被动监测通信过程
被动模式每次都需要打开一个tcp连接,当监控项越来越多时,就困难会出现server端性能问题
主,被应用规则
常用的监控架构平台
server-agentd模式:
这个是最简单的架构,常用于监控主机比较少的情况下
server-proxy-agentd模式:
这个常用于比较多的机器,使用proxy进行分布式监控,有效的减轻server端的压力。
部署环境
zabbix服务器:20.0.0.10(监控端)
客户机: 20.0.0.11(被监控端)
主机重命名
[root@server1 ~]# hostnamectl set-hostname zabbix-server
[root@server1 ~]# su
[root@server2 ~]# hostnamectl set-hostname zabbix-client
[root@server2 ~]# su
关闭防火墙及核心防护
[root@zabbix-server ~]# systemctl stop firewalld
[root@zabbix-server ~]# setenforce 0 核心防护关闭
[root@zabbix-server ~]# systemctl disable firewalld
[root@zabbix-server ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
安装依赖包
[root@zabbix-server ~]# yum -y install httpd mariadb-server mariadb php php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mhash
修改配置文件
[root@zabbix-server ~]# ln -s /etc/httpd/conf/httpd.conf /etc/httpd.conf
[root@zabbix-server ~]# vi /etc/httpd.conf
ServerName www.example.com:80 //95行,删除前面#注释符,域名
DirectoryIndex index.php index.html //164行,添加选中内容
[root@zabbix-server ~]# vi /etc/php.ini
添加修改
date.timezone = PRC //878行,添加PRC
[root@zabbix-server ~]# systemctl start httpd
[root@zabbix-server ~]# systemctl start mariadb
[root@zabbix-server ~]# netstat -anpt | grep 80
tcp6 0 0 :::80 :::* LISTEN 57066/httpd
[root@zabbix-server ~]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 57328/mysqld
初始化数据库
[root@zabbix-server ~]# 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@zabbix-server ~]# mysql -uroot -p123123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
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)]>
[root@zabbix-server ~]# vi /var/www/html/index.php
<?php
phpinfo();
?>
[root@zabbix-server ~]# mysql -uroot -p123123
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)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to 'zabbix'@'%' identified by 'admin123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by 'admin123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
示例
验证php与mysql数据库的联动性
[root@zabbix-server ~]# vi /var/www/html/index.php
<?php 数据库连接 ip地址 用户名 密码
$link=mysql_connect('20.0.0.10','zabbix','admin123');
if($link) echo "Success!!
"; 条件判断连接成功
else echo "Fail!!"; 连接失败
mysql_close();
?>
添加zabbix源
[root@zabbix-server ~]# 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
添加必要插件(依赖包)
[root@zabbix-server ~]# yum -y install php-mbstring php-bcmath
下载zabbix软件源,下载zabbix服务端软件
[root@zabbix-server ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
[root@zabbix-server ~]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent --nogpgcheck
导入zabbix监控数据库
[root@zabbix-server ~]# zcat /usr/share/doc/zabbix-server-mysql-4.0.27/create.sql.gz | mysql -uzabbix -padmin123 zabbix
查看库用户信息
[root@zabbix-server ~]# mysql -uroot -pabc123
MariaDB [(none)]> use zabbix; 使用zabbix数据库
MariaDB [zabbix]> show tables; 查看表
MariaDB [zabbix]> select* from users\G 查看库用户信息
[root@zabbix-server ~]# vi /etc/zabbix/zabbix_server.conf
38 LogFile=/var/log/zabbix/zabbix_server.log 日志文件路径
49 LogFileSize=0 默认文件大小
72 PidFile=/var/run/zabbix/zabbix_server.pid pid进程文件路径
82 SocketDir=/var/run/zabbix socket文件路径,对外访问接口
100 DBName=zabbix 数据库名称
116 DBUser=zabbix 数据库用户
124 DBPassword=admin123 添加登录密码
356 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log snmp协议,触发文件路径
473 Timeout=4 超时时间
516 AlertScriptsPath=/usr/lib/zabbix/alertscripts 警告脚本路径
527 ExternalScripts=/usr/lib/zabbix/externalscripts 额外脚本
563 LogSlowQueries=3000 慢日志(日志查询时间3秒,对数据的补充,占用系统内存)
含义
修改时区
[root@zabbix-server ~]# vi /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai //时区修改为亚洲上海
修改字体
[root@zabbix-server ~]# vi /usr/share/zabbix/include/defines.inc.php
:%s /graphfont/kaiti/g //直接输入
导入字体
复制C盘:\Windows\Fonts 楷体 常规 到/usr/share/zabbix/assets/fonts/ 并改名为kaiti.ttf
[root@server1 ~]# cd /usr/share/zabbix/assets/fonts/
[root@server1 fonts]# ll
总用量 11512
lrwxrwxrwx. 1 root root 33 1月 20 20:07 graphfont.ttf -> /etc/alternatives/zabbix-web-font
-rw-r--r--. 1 root root 11787328 1月 20 20:32 kaiti.ttf
开启服务端zabbix服务
[root@server1 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.
[root@server1 fonts]# systemctl start zabbix-server
[root@server1 fonts]# netstat -anpt | grep zabbix
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 58263/zabbix_server
tcp6 0 0 :::10051 :::* LISTEN 58263/zabbix_server
[root@server1 fonts]# systemctl restart httpd
网页访问http://20.0.0.10/zabbix/
点击下一步
点击下一步
下一步
设置客户端zabbix-client
配置zabbix源并安装zabbix
[root@zabbix-client ~]# vi /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
[root@zabbix-client ~]# yum -y install zabbix-agent --nogpgcheck
编辑客户端代理文件
[root@zabbix-client ~]# vi /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=20.0.0.10 指向服务器ip
123 StartAgents=3 开启参数
139 ServerActive=20.0.0.10 指向服务器ip
150 Hostname=client1 当前主机在zabbix中的名称,对应server平台创建客户端名称一致
268 Include=/etc/zabbix/zabbix_agentd.d/*.conf 包含的配置文件
开启客户端zabbix服务
[root@zabbix-client ~]# systemctl enable zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@zabbix-client ~]# systemctl start zabbix-agent
[root@zabbix-client ~]# netstat -anpt | grep zabbix
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 55955/zabbix_agentd
tcp6 0 0 :::10050 :::* LISTEN 55955/zabbix_agentd
转到server上修改配置文件,开启服务
[root@server1 ~]# vi /etc/zabbix/zabbix_agentd.conf
98 Server=20.0.0.11
139 ServerActive=20.0.0.11
150 Hostname=server
[root@server1 ~]# systemctl enable zabbix-agent
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@server1 ~]# systemctl start zabbix-agent
[root@server1 ~]# netstat -anpt | grep zabbix
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 60415/zabbix_agentd
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 59263/zabbix_server
tcp6 0 0 :::10050 :::* LISTEN 60415/zabbix_agentd
tcp6 0 0 :::10051 :::* LISTEN 59263/zabbix_server
在服务端设置监控内容信息
zabbix界面创建监控的客户端
创建客户机端
创建模块,监控内容
创建server客户端的监控内容
此处一定点击添加
client1客户端的监控内容
创建成功
server
[root@zabbix-server ~]# vi /etc/mail.rc
//末尾添加
set from=1349271266@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=1609325444@qq.com
set smtp-auth-password=xxxxxx
set smtp-auth=login
编写发邮件的脚本
[root@server1 ~]# cd /usr/lib/zabbix/alertscripts/
[root@server1 alertscripts]# vi mail.sh //添加以下信息
#!/bin/bash
#send mail
message=`echo $3 | tr '\r\n' '\n'` 内容信息
subject=`echo $2 | tr '\r\n' '\n'` 主题
echo "${message}" | mail -s "${subject}" $1 >>/tmp/mail.log 2>&1
附:
tr:删除,字符转换
\r\n:换行
2>&1:混合输出
[root@server1 alertscripts]# touch /tmp/mailx.log //创建日志文件
[root@server1 alertscripts]# cd
[root@server1 ~]# chown -R zabbix.zabbix /tmp/mailx.log //设置日志文件属主属组
[root@server1 ~]# chmod +x /usr/lib/zabbix/alertscripts/mail.sh //授权脚本
[root@server1 ~]# chown -R zabbix.zabbix /usr/lib/zabbix/ //设置脚本属主属组
测试报警效果
[root@server1 ~]# cd /usr/lib/zabbix/alertscripts/
[root@server1 alertscripts]# ./mailx.sh 1349271266@qq.com "test" "2021-01-20"
修改触发条件
创建动作
此处添加一定要点
完成后页面
创建恢复操作
模拟故障,测试邮件发送
设置网卡故障
server上重启zabbix服务
[root@zabbix-server alertscripts]# cd
[root@zabbix-server ~]# systemctl restart zabbix-server
[root@zabbix-server ~]# netstat -anpt | grep zabbix
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 60415/zabbix_agentd
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 65404/zabbix_server
tcp6 0 0 :::10050 :::* LISTEN 60415/zabbix_agentd
tcp6 0 0 :::10051 :::* LISTEN 65404/zabbix_server