文章目录
1、概述及安装
zabbix官方文档:https://www.zabbix.com/documentation/4.0/zh/manual/installation
全方位监控:适用于所有IT基础架构,服务,应用和云资源的监控解决方案。
网络监控,服务器监控,云监控,应用监控,服务监控。
监控平台研发过程中关注的三方面:
- 数据采集,c语言,其网络通信最快;
- 数据库,数据采集需要存到数据库,前端php代码要从后端数据库上将数据展示出来;
- 前端展示,数据采集存到数据库后,需要可视化,zabbix用的是php语言;
- 报警。
经测试,Zabbix 可以运行在下列平台:
Linux
IBM AIX
FreeBSD
NetBSD
OpenBSD
HP-UX
Mac OS X
Solaris
Windows:自 XP 以来的所有桌面和服务器版本(仅限 Zabbix agent)
## 添加 Zabbix 软件仓库
[root@server1 yum.repos.d]# rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@server1 yum.repos.d]# ls
redhat.repo rhel7.6.repo zabbix.repo
[root@server1 yum.repos.d]# ll zabbix.repo
-rw-r--r-- 1 root root 410 Jul 13 19:43 zabbix.repo
[root@server1 yum.repos.d]# cat zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=0
[root@server1 yum.repos.d]# yum repolist
## 安装 Server/proxy/前端
[root@server1 yum.repos.d]# yum install -y zabbix-server-mysql
[root@server1 yum.repos.d]# yum install -y mariadb-server
## 创建数据库
[root@server1 yum.repos.d]# systemctl start mariadb.service
[root@server1 yum.repos.d]# systemctl enable --now mariadb.service
[root@server1 yum.repos.d]# mysql
mysql> create database zabbix character set utf8 collate utf8_bin; #字符集utf8,
mysql> create user 'zabbix'@'localhost' identified by 'westos';
mysql> grant all privileges on zabbix.* to 'zabbix'@'localhost';
## 导入数据
[root@server1 zabbix-server-mysql-4.0.27]# pwd
/usr/share/doc/zabbix-server-mysql-4.0.27
[root@server1 zabbix-server-mysql-4.0.27]# ls
AUTHORS ChangeLog COPYING create.sql.gz NEWS README
[root@server1 zabbix-server-mysql-4.0.27]# zcat create.sql.gz | wc -l ##zcat 查看压缩文件 create.sql.gz是zabbix提供的初始化的数据库的结构,zabbix自带数据库,也自带前端,前端也是直接连上预设的数据库。
14476
[root@server1 zabbix-server-mysql-4.0.27]# zcat create.sql.gz | mysql -uzabbix -pwestos zabbix ##直接导入到数据库zabbix
[root@server1 zabbix-server-mysql-4.0.27]# mysql
MariaDB [(none)]> use zabbix
MariaDB [zabbix]> show tables;
## 为 Zabbix server/proxy 配置数据库,启动 Zabbix server 进程
[root@server1 ~]# cd /etc/zabbix/
[root@server1 zabbix]# ls
zabbix_server.conf
[root@server1 zabbix]# vim zabbix_server.conf
DBPassword=westos
[root@server1 zabbix]# systemctl enable --now zabbix-server
[root@server1 zabbix]# ps ax ##zabbix开启很多进程
[root@server1 zabbix]# cat /var/log/zabbix/zabbix_server.log ##没有错误
[root@server1 zabbix]# yum install -y zabbix-agent
[root@server1 zabbix]# systemctl enable --now zabbix-agent ##本机server端的CI结构配置完毕
zabbix在开启时默认监控本机localhost
2、Zabbix 前端配置
2.1 配置前端
[root@server1 zabbix]# getenforce
Disabled
[root@server1 zabbix]# netstat -antlp
tcp6 0 0 :::10050 :::* LISTEN 4044/zabbix_agentd
tcp6 0 0 :::10051 :::* LISTEN 3932/zabbix_server
[root@server1 ~]# ls
php-bcmath-5.4.16-46.el7.x86_64.rpm php-mbstring-5.4.16-46.el7.x86_64.rpm ##可以从aliyun镜像站内搜索下载
[root@server1 ~]# yum install -y zabbix-web-mysql php-bcmath-5.4.16-46.el7.x86_64.rpm php-mbstring-5.4.16-46.el7.x86_64.rpm
[root@server1 ~]# cd /etc/httpd/
[root@server1 httpd]# cd conf.d/
[root@server1 conf.d]# rpm -q httpd ##前端通过apahe发布,根据依赖性已经下载了apache
httpd-2.4.6-88.el7.x86_64
[root@server1 conf.d]# vim zabbix.conf
php_value date.timezone Asia/Shanghai
[root@server1 conf.d]# systemctl enable --now httpd
password:westos
username: Admin 要大写
password:zabbix
2.2 更改字体为中文
更改主机图形的预览显示字体为中文
[root@server1 fonts]# pwd
/usr/share/zabbix/assets/fonts
[root@server1 fonts]# ls
graphfont.ttf simkai.ttf ##下载simkai.ttf中文字体
[root@server1 fonts]# ll graphfont.ttf
lrwxrwxrwx 1 root root 33 Jul 13 21:45 graphfont.ttf -> /etc/alternatives/zabbix-web-font
[root@server1 fonts]# ll /etc/alternatives/zabbix-web-font
lrwxrwxrwx 1 root root 38 Jul 13 21:45 /etc/alternatives/zabbix-web-font -> /usr/share/fonts/dejavu/DejaVuSans.ttf
[root@server1 fonts]# cd /usr/share/zabbix/
[root@server1 zabbix]# cd include/
[root@server1 include]# vim defines.inc.php
:%s/graphfont/simkai/g
2.3 手动添加server
[root@server1 yum.repos.d]# scp zabbix.repo server2:/etc/yum.repos.d/
[root@server2 ~]# yum repolist
[root@server2 ~]# yum install -y zabbix-agent ##被监控端都要安装zabbix-agent
[root@server2 ~]# cd /etc/zabbix/
[root@server2 zabbix]# ls
zabbix_agentd.conf zabbix_agentd.d
[root@server2 zabbix]# vim zabbix_agentd.conf
98 Server=192.168.0.1 ## 注意,防火墙不要挡掉192.168.0.1的10051端口,agent自己监听的端口是10050
139 ServerActive=192.168.0.1
##agent支持两种采集类型,主动,被动,默认当前两个都支持,agent发给server 或者 server端来找agent。
##默认当前是server端来找agent,优化的时候改为server端被动,agent主动,server端的性能得到提高。
## 全部填写server端的地址,最终怎么采集取决于server端。
150 Hostname=server2 ##每个agent填写自己的主机名
[root@server2 zabbix]# hostname
server2
[root@server2 zabbix]# cat /etc/hosts ##所有的节点要有解析
192.168.0.100 foundation.example.com
192.168.0.1 server1
192.168.0.2 server2
192.168.0.3 server3
[root@server2 zabbix]# systemctl enable --now zabbix-agent.service
[root@server2 zabbix]# cat /var/log/zabbix/zabbix_agentd.log
[root@server2 zabbix]# netstat -antlp
server2 成功连接10050端口
2.4 自动发现server
[root@server1 yum.repos.d]# scp zabbix.repo server3:/etc/yum.repos.d/
[root@server3 ~]# yum install -y zabbix-agent
[root@server3 ~]# cd /etc/zabbix/
[root@server3 zabbix]# vim zabbix_agentd.conf
98 Server=192.168.0.1
139 ServerActive=192.168.0.1
150 Hostname=server3
[root@server3 zabbix]# systemctl enable --now zabbix-agent.service
[root@server3 zabbix]# cat /var/log/zabbix/zabbix_agentd.log
14061:20210714:103019.124 no active checks on server [192.168.0.1:10051]: host [server3] not found
2.5 针对特定主机 自动注册
客户端主动发起,不是server端自动扫描
[root@server3 zabbix]# systemctl restart zabbix-agent.service
ping需要时间
3、API
官方文档:https://www.zabbix.com/documentation/4.0/zh/manual/api/reference/host/delete
3.1 检索主机
我们现在有一个有效的用户身份验证令牌,可以用来访问Zabbix中的数据。 例如,让我们使用 host.get 方法检索所有已配置主机的ID,主机名和接口 :
[root@server1 ~]# curl -s -XPOST http://192.168.0.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
> {
> "jsonrpc": "2.0",
> "method": "user.login",
> "params": {
> "user": "Admin",
> "password": "zabbix"
> },
> "id": 1,
> "auth": null
> }' | python -m json.tool
[root@server1 ~]# vim zabbix.api
curl -s -XPOST http://192.168.0.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 2,
"auth": "b26f520d820e5ec9963eb276df6d0a31"
}' | python -m json.tool
[root@server1 ~]# sh zabbix.api
响应对象将包含有关主机的请求的数据:
3.2 删除主机
[root@server1 ~]# vim zabbix.api
curl -s -XPOST http://192.168.0.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10273" ##server3主机id
],
"id": 2,
"auth": "b26f520d820e5ec9963eb276df6d0a31"
}' | python -m json.tool
[root@server1 ~]# sh zabbix.api
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10273"
]
}
}
请求
响应
server3成功删除
3.2 创建主机
[root@server1 ~]# vim zabbix.api
curl -s -XPOST http://192.168.0.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server3",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.0.3",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"
}
],
"templates": [
{
"templateid": "10001"
}
]
},
"id": 2,
"auth": "b26f520d820e5ec9963eb276df6d0a31"
}' | python -m json.tool
[root@server1 ~]# sh zabbix.api
{
"id": 2,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10274"
]
}
}
创建一个具有IP接口的“Linux Server”主机,将其添加到主机组中,链接一个模板并且把MAC地址设置到主机资产清单里 .
请求
响应
server3创建成功
4、zappix监控nginx
在server3上搭建nginx环境
[root@server2 zabbix]# yum install -y httpd
[root@server2 zabbix]# systemctl enable --now httpd
[root@server2 html]# echo server2 > index.html
[root@server3 ~]# systemctl restart zabbix-agent.service
[root@server3 ~]# ls
nginx-1.18.0.tar.gz
[root@server3 ~]# tar zxf nginx-1.18.0.tar.gz
[root@server3 ~]# cd nginx-1.18.0/
[root@server3 nginx-1.18.0]# yum install -y gcc pcre-devel openssl-devel
[root@server3 nginx-1.18.0]# vim auto/cc/gcc
171 # debug
#CFLAGS="$CFLAGS -g"
[root@server3 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@server3 nginx-1.18.0]# make && make install
[root@server3 nginx-1.18.0]# cd /usr/local/nginx/
[root@server3 nginx]# du -sh
980K .
[root@server3 nginx]# cd sbin/
[root@server3 sbin]# pwd
/usr/local/nginx/sbin
[root@server3 sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@server3 sbin]# which nginx
/usr/local/bin/nginx
[root@server3 sbin]# cd ..
[root@server3 nginx]# cd conf/
[root@server3 conf]# vim nginx.conf
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
[root@server3 conf]# nginx -t ##检测语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server3 conf]# nginx ##开启
[root@server3 conf]# curl localhost
<title>Welcome to nginx!</title>
[root@server3 conf]# curl http://127.0.0.1/status
Active connections: 1
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 0
[root@foundation50 ~]# ab -c10 -n 1000 http://192.168.0.3/index.html
[root@server3 conf]# curl http://127.0.0.1/status
并发10个 ,完成1000个
Active connections: 1 并发连接数
accepts server端允许多少次连接
handled 允许多少次tcp三次握手
requests 处理了多少次请求
布置前端
[root@server3 ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@server3 zabbix_agentd.d]# ls
userparameter_mysql.conf
[root@server3 zabbix_agentd.d]# grep -v ^# /etc/zabbix/zabbix_agentd.conf | uniq
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@server3 zabbix_agentd.d]# cp userparameter_mysql.conf userparameter_nginx.conf
[root@server3 zabbix_agentd.d]# vim userparameter_nginx.conf
UserParameter=nginx.active,curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
UserParameter=nginx.accept,curl -s http://127.0.0.1/status | awk 'NR==3{print $1}'
UserParameter=nginx.handled,curl -s http://127.0.0.1/status | awk 'NR==3{print $2}'
UserParameter=nginx.request,curl -s http://127.0.0.1/status | awk 'NR==3{print $3}'
[root@server3 zabbix_agentd.d]# systemctl restart zabbix-agent
[root@server1 ~]# yum install -y zabbix-get ##在server端经过get指令
[root@server1 ~]# zabbix_get -s 192.168.0.3 -p 10050 -k "nginx.active"
1
[root@server1 ~]# zabbix_get -s 192.168.0.3 -p 10050 -k "nginx.accept"
1033
[root@server1 ~]# zabbix_get -s 192.168.0.3 -p 10050 -k "nginx.handled"
1034
[root@server1 ~]# zabbix_get -s 192.168.0.3 -p 10050 -k "nginx.request"
1035
zabbix server(10051) --> agent(10050)
zabbix server访问 agent,server每隔一段时间主动去连接agent,agent去取监控项。
创建监控项
主机 server3 创建图形