文章目录
部署参考zabbix官网:https://www.zabbix.com/documentation/4.0/zh/manual/appendix/install/db_scripts
server1做server端,server2做agent端、server3做nginx
1.布置后端(zabbix-server、zabbix-agent)
1.1配置yum源并安装
[root@server11 ~]# rpm -ivh http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
Retrieving http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@server11 ~]# cd /etc/yum.repos.d/
[root@server11 yum.repos.d]# vim 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@server11 yum.repos.d]# yum install zabbix-server-mysql
1.2.用户端创建数据库
[root@server11 yum.repos.d]# yum install mariadb-server
[root@server11 yum.repos.d]# systemctl start mariadb.service
[root@server11 yum.repos.d]# systemctl enable --now mariadb.service
[root@server11 yum.repos.d]# mysql
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
MariaDB [(none)]> create user 'zabbix'@'localhost' identified by 'westos';
MariaDB [(none)]> grant all privileges on zabbix.* to 'zabbix'@'localhost';
[root@server11 mysql]# zcat /usr/share/doc/zabbix-server-mysql-4.0.27/create.sql.gz | mysql -uzabbix -pwestos zabbix #导入数据库数据,有些慢,不能ctrl+c
#服务器端
[root@server11 mysql]# vim /etc/zabbix/zabbix_server.conf
#改DBPassword=westos
[root@server11 yum.repos.d]# systemctl enable --now zabbix-server.service
[root@server11 yum.repos.d]# yum install -y zabbix-agent
[root@server11 yum.repos.d]# systemctl enable --now zabbix-agent
[root@server11 yum.repos.d]# cat /var/log/zabbix/zabbix_server.log
#后端已布置好
#防火墙和selinux都要关掉,不然起不开zabbix-agent
2.布置前端(zabbix-web)
[root@server11 mysql]# netstat -antlp
tcp6 0 0 :::10050 :::* LISTEN 15528/zabbix_agentd
tcp6 0 0 :::10051 :::* LISTEN 15419/zabbix_server
#先下载两个依赖php-bcmath-5.4.16-46.el7.x86_64.rpm 和php-mbstring-5.4.16-46.el7.x86_64.rpm
[root@server11 ~]# ls
php-bcmath-5.4.16-46.el7.x86_64.rpm php-mbstring-5.4.16-46.el7.x86_64.rpm
[root@server11 ~]# yum install zabbix-web-mysql php-bcmath-5.4.16-46.el7.x86_64.rpm php-mbstring-5.4.16-46.el7.x86_64.rpm -y
[root@server11 ~]# cd /etc/httpd/conf.d
[root@server11 conf.d]# ls
autoindex.conf php.conf README userdir.conf welcome.conf zabbix.conf
#前段代码都在/usr/share/zabbix
[root@server11 conf.d]# vim zabbix.conf
php_value date.timezone Asia/Shanghai #改时区
[root@server11 conf.d]# systemctl enable --now httpd
网页访问http://192.168.100.241/zabbix/setup.php
数据库密码:westos
server端的Name:Zabbix server
默认用户名:Admin
密码:zabbix
个人设置中文(有乱码)需要改字体
[root@server11 conf.d]# cd /usr/share/zabbix/assets/fonts/
[root@server11 fonts]# ls
graphfont.ttf
#百度下载字体simkai.ttf
[root@server11 fonts]# ls
graphfont.ttf simkai.ttf
[root@server11 include]# vim /usr/share/zabbix/include/defines.inc.php#替换成 simkai
:%s/graphfont/simkai/g
新建server12.server端被动,agent端主动
server端被动,agent端主动:agent主动发送数据给server端,减少server端的压力,发挥最大性能。
被监控端,都要安装zabbix-agent
安装agent
[root@server12 ~]# cd /etc/yum.repos.d/
[root@server12 yum.repos.d]# ls
dvd.repo redhat.repo
[root@server12 yum.repos.d]# rm -fr *
[root@server12 yum.repos.d]# ls
[root@server12 yum.repos.d]# ls
dvd.repo redhat.repo zabbix.repo
[root@server12 yum.repos.d]# yum install -y zabbix-agent
配置和启动agent
[root@server12 yum.repos.d]# cd /etc/zabbix/
[root@server12 zabbix]# ls
zabbix_agentd.conf zabbix_agentd.d
[root@server12 zabbix]# vim zabbix_agentd.conf
#改3个地方,server12agent端主机端口
Server=192.168.100.241#server1端的ip,指向server端
ServerActive=192.168.100.241##server端主动
Hostname=server12 #agent自己的主机名hostname
[root@server12 zabbix]# systemctl enable --now zabbix-agent.service
[root@server12 zabbix]# cat /var/log/zabbix/zabbix_agentd.log #日至
此时,agent端没有连接到server端,因为server端有访问控制,允许agent访问时,agent才能访问server端。解决方案如下
- zabbix网页http://192.168.100.241/zabbix/hosts.php,配置->主机->创建主机:(server端有server2时,才能被访问,agent端没有连接到server12,才能知道采集谁的信息)
主机名称server12(必须是server2)
选择群组:Linux servers
agent代理程序的接口:192.168.100.242#被监控主机的ip,端口10050
增加监控项:添加server2的模板:Template OS Linux,一定要点添加,更新
ZBX变绿了就成功连接了
监控项:不需要一个个添加,有模板可以选择
,模板是链接上去的,选择完模板后一定要点添加,才能加入到主机监控项内。
自动发现、自动注册(新建server3)
[root@server13 yum.repos.d]# yum install -y zabbix-agent
[root@server13 yum.repos.d]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.100.241
ServerActive=192.168.100.241
Hostname=server13
[root@server13 zabbix]# systemctl enable --now zabbix-agent.service
自动发现
:先启用动作,再启动自动发现且里面改一下ip段,(发现速度比较慢),server3会被自动发现。
用完后先关闭动作,删除主机中的server13,禁用自动发现
自动注册(比较快)
:动作里:自动注册->创建动作->操作:添加
添加到主机群组:Linux servers
与模板关联: Template OS Linux
[root@server13 zabbix]# systemctl restart zabbix-agent.service
网页上主机就会发现server13
之后停用掉自动注册auto_reg
3.Zabbix API
Zabbix API允许你以编程方式检索和修改Zabbix的配置,并提供对历史数据的访问。
它广泛用于:
创建新的应用程序以使用Zabbix;
将Zabbix与第三方软件集成;
自动执行常规任务。
%想做什么东西,去找官方文档,先部署出来,再去反推理解
官方文档https://www.zabbix.com/documentation/4.0/zh/manual/api
[root@server11 yum.repos.d]# curl -XPOST http://192.168.100.241/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
}'
{"jsonrpc":"2.0","result":"c99c064302c273a4a417ed73d60f2cb8","id":1}
#如果你正确提供了凭据,API返回的响应将包含用户身份验证令牌
[root@server11 yum.repos.d]# curl -XPOST http://192.168.100.241/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
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 228 100 68 100 160 177 418 --:--:-- --:--:-- --:--:-- 417
{
"id": 1,
"jsonrpc": "2.0",
"result": "01809088efba878bb97c09221c6a30ca"
}
host.get
[root@server11 ~]# vim zabbix.api
[root@server11 ~]# cat zabbix.api
curl -s -XPOST http://192.168.100.241/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":"01809088efba878bb97c09221c6a30ca" #为刚才生成key
}'|python -m json.tool
[root@server11 ~]# sh zabbix.api
{
"id": 2,
"jsonrpc": "2.0",
"result": [
{
"host": "Zabbix server",
"hostid": "10084",
"interfaces": [
{
"interfaceid": "1",
"ip": "127.0.0.1"
}
]
},
{
"host": "server12",
"hostid": "10271",
"interfaces": [
{
"interfaceid": "2",
"ip": "192.168.100.242"
}
]
},
{
"host": "server13",
"hostid": "10272",
"interfaces": [
{
"interfaceid": "3",
"ip": "192.168.100.243"
}
]
}
]
}
host.delete
[root@server11 ~]# cat zabbix.api
curl -s -XPOST http://192.168.100.241/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10272" #server13的hostid
],
"id": 1,
"auth":"01809088efba878bb97c09221c6a30ca"
}'|python -m json.tool
#网页查看,servr13已被删除
[root@server11 ~]# sh zabbix.api
{
"id": 1,#在网页主机那可以查看
"jsonrpc": "2.0",
"result": {
"hostids": [
"10272" #hostip,在网页主机那可以查看
]
}
}
#网页查看主机。server13已删除
创建一个具有IP接口的“Linux Server”主机,将其添加到主机组中,链接一个模板并且把MAC地址设置到主机资产清单里 请求:
[root@server11 ~]# vim zabbix.api
[root@server11 ~]# cat zabbix.api
curl -s -XPOST http://192.168.100.241/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server13",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.100.243",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"#在主机群组中查看Linux servers的groupid
}
],
"templates": [
{
"templateid": "10001" #在模板Template OS Linux的url里面会显示模板的id号
}
]
},
"auth": "01809088efba878bb97c09221c6a30ca",
"id": 1
}' | python -m json.tool
[root@server11 ~]# sh zabbix.api
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10274"
]
}
}
#网页查看主机。server13已创建
4.zabbix监控nginx
- 步骤:server3安装nginx,他的状态由agent取到,给server,在server端创建item和图形graph,展示到php webui
[root@server12 yum.repos.d]# systemctl enable --now httpd.service
[root@server12 yum.repos.d]# echo server12 > /var/www/html/index.html
[root@zhenji yum.repos.d]# curl 192.168.100.242
server12
- 网页:配置->主机-server12-模板-添加Template App HTTP Service
[root@server13 anzhuang]# ls
nginx-1.18.0.tar.gz
[root@server13 anzhuang]# tar zxf nginx-1.18.0.tar.gz
[root@server13 anzhuang]# cd nginx-1.18.0
[root@server13 nginx-1.18.0]# yum install -y gcc pcre-devel openssl-devel
[root@server13 nginx-1.18.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@server13 nginx-1.18.0]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" #debug屏蔽掉,速度快
[root@server13 nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@server13 nginx-1.18.0]# make && make install
[root@server13 nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/ #软链接
[root@server13 nginx]# which nginx
/usr/local/bin/nginx
[root@server13 nginx]# ls
[root@server13 nginx]# cd /usr/local/nginx/conf
[root@server13 conf]# ls
[root@server13 conf]# vim nginx.conf
#添加状态模块
location /status {
stub_status on;#激活
access_log off;#不纪录日志
allow 127.0.0.1;
deny all;
}
[root@server13 conf]# nginx -t #检查语法错误
[root@server13 ~]# nginx
[root@server13 conf]# nginx -s reload
[root@server13 conf]# curl http://127.0.0.1/status##记录访问请求数
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
#压测:ab -c10 -n 1000 http://172.25.0.3/index.html
[root@server13 zabbix_agentd.d]# cat userparameter_nginx.conf
UserParameter=nginx.active,curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
[root@server13 zabbix_agentd.d]# curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
[root@server13 zabbix_agentd.d]# systemctl restart zabbix-agent.service
此时agent端取到了nginx的数据,下面做server端接收agent端的数据
[root@server11 yum.repos.d]# yum install zabbix-get -y
[root@server11 ~]# zabbix_get -s 192.168.100.243 -p 10050 -k "nginx.active"
1
[root@server11 ~]# zabbix_get -s 192.168.100.243 -p 10050 -k "nginx.handled"
4
[root@server11 ~]# zabbix_get -s92.168.100.243 -p 10050 -k "nginx.request"
5
此时server端接收agent端的数据,下面将其展示到zbbix的webUI上
-
选主机server13监控项->创建监控项:(3个都创建)
名称:nginx.active
键值一定是:自定义的nginx.active
-
主机server13->主机-》图形-》创建图形(3个都创建)-》添加nginx.active