Zabbix监控实例
文章目录
1.zabbix监控windows
1.zabbix监控windows
环境:
1.zabbix server的ip:10.0.0.61
2.被监控windows的ip:10.0.0.1
1.下载Zabbix Agent
https://www.zabbix.com/downloads/4.0.14/zabbix_agents-4.0.14-win-amd64.zip
2.Windows安装zabbix agent
编辑配置文件D:\zabbix_agents-4.0.14-win-amd64\conf\zabbix_agentd.conf
Server=10.0.0.61
3.运行cmd,将zabbix命令注册为服务(使用管理员) services.msc
"d:\zabbix_agents-4.0.14-win-amd64\bin\zabbix_agentd.exe" --config d:\zabbix_agents-4.0.14-win-amd64\conf\zabbix_agentd.conf --install
#找到运行,输入services.msc
4.cmd运行
查看监听netstat -an|find "10050"
---------------------------------------------------
5.zabbix_get获取windows信息
zabbix_get -s xxx -k system.uname
zabbix_get -s xxx -k vm.memory.size[free]
zabbix_get -s xxx -k vfs.fs.size[C:,pfree]
[root@manage ~]# zabbix_get -s 10.0.0.1 -k system.uname
Windows 计算机 10.0.19042 Microsoft Windows 10 家庭中文版 x64
[root@manage ~]# zabbix_get -s 10.0.0.1 -k vm.memory.size[free]
2080636928
[root@manage ~]# zabbix_get -s 10.0.0.1 -k vfs.fs.size[C:,pfree]
22.248787
6.windows防火墙放开10050端口
1.防火墙关闭的可以跳过这步
2.高级设置->入站规则->新建规则->端口->10050->允许连接->所有
7.zabbix页面上添加windows主机
1.直接应用windows模板
2.验证Windows监控是否正常
编写一个最简单的bat脚本D:\zabbix_agents-4.0.14-win-amd64\echo.bat
@echo off
echo "100"
zabbix配置添加D:\zabbix_agents-4.0.14-win-amd64\conf\zabbix_agentd.conf
UserParameter=echo.test,D:\zabbix_agents-4.0.14-win-amd64\echo.bat
[root@manage ~]# zabbix_get -s 10.0.0.1 -k echo.test
100
windows基于主机创建监控项
配置----->主机------>创建监控项
2.zabbix监控nginx状态
zabbix监控Nginx
1.监控Nginx什么内容
监控Nginx中7种状态
2.怎么监控
1.开启Nginx stub status 模块
2.使用curl命令获取stub_status的数据
3.将获取数据的方法封装为一个又一个的监控项
3.实施监控
#1.开启nginx stub_status状态
[root@web001 ~]# cat /etc/nginx/conf.d/status.cwq.com.conf
server {
listen 80;
server_name status.cwq.com;
location /nginx_status {
stub_status;
access_log off;
allow 127.0.0.1;
deny all;
}
}
[root@web001 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web001 ~]# systemctl restart nginx
#检测是否可以获取到值
[root@web001 ~]# curl -HHost:status.cwq.com 127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
#2.编写脚本,提取nginx status的指标
[root@web001 ~]# cd /etc/zabbix/zabbix_agentd.d/
[root@web001 zabbix_agentd.d]# mkdir scripts
[root@web001 zabbix_agentd.d]# cd scripts/
[root@web001 scripts]# cat nginx_status.sh
Nginx_status_file=/tmp/nginx_status.tmp
Nginx_status_name=status.cwq.com
Nginx_status_path=/nginx_status
curl -sH host:${Nginx_status_name} http://127.0.0.1:80${Nginx_status_path} > ${Nginx_status_file}
case $1 in
active)
echo $[ $(awk 'NR==1 {print $NF}' ${Nginx_status_file}) -1 ]
;;
accepts)
echo $[ $(awk 'NR==3 {print $1}' ${Nginx_status_file}) - 1 ]
;;
handled)
echo $[ $(awk 'NR==3 {print $2}' ${Nginx_status_file}) - 1 ]
;;
requests)
echo $[ $(awk 'NR==3 {print $3}' ${Nginx_status_file}) - 1 ]
;;
reading)
awk 'NR==4 {print $2}' ${Nginx_status_file}
;;
writing)
awk 'NR==4 {print $4}' ${Nginx_status_file}
;;
waiting)
awk 'NR==4 {print $NF}' ${Nginx_status_file}
;;
*)
echo "USAGE: $0 [active|accepts|handled|requests|reading|writing|waiting]"
;;
esac
[root@web001 scripts]# chmod +x nginx_status.sh
[root@web001 ~]# rm -f /tmp/nginx_status.tmp
#3.配置zabbix-agent, 添加自定义监控项
[root@web001 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx_status[*], /bin/bash /etc/zabbix/zabbix_agentd.d/scripts/nginx_status.sh "$1"
#4.重启zabbix
[root@web001 ~]# systemctl restart zabbix-agent
#5.zabbix-server使用zabbix-get获取监控项的值
[root@manage ~]# zabbix_get -s 10.0.0.7 -k nginx_status[accepts]
5
#6.登录zabbix-web
1.创建模板
2.创建监控项
3.创建图形
4.创建触发器
5.关联对应主机
1.创建模板
2.创建监控项
3.创建图形
4.创建触发器
5.添加对应主机
#压测看下数据
[root@web001 ~]# ab -n 10000 -c 2000 http://127.0.0.1/
3.zabbix监控nginx状态码
(200 301 302 401 403 404 500 501 502)
#1.提取日志
[root@web001 ~]# awk '{print $9}' /var/log/nginx/access.log | grep -c "502"
165381
#2.自定义监控。支持传参
[root@web001 ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx_status[*], /bin/bash /etc/zabbix/zabbix_agentd.d/scripts/nginx_status.sh "$1"
UserParameter=nginx_status_code[*], awk '{print $9}' /var/log/nginx/access.log|grep -c "$1"
[root@web001 ~]# systemctl restart zabbix-agent
#3.zabbix服务端使用zabbix-get获取
[root@manage ~]# zabbix_get -s 10.0.0.7 -k nginx_status_code[502]
165381
#4.登录zabbix-web
1.创建模板
2.创建监控项
3.创建图形
4.创建触发器
5.关联对应主机
1.创建模板
2.创建监控项
3.创建图形
4.关联主机
4.监控php状态
#1.在下面文件中增加一行
[root@web01 ~]# cat /etc/php-fpm.d/www.conf
....
pm.status_path = /fpm_status
....
#重启php-fpm
[root@web001 ~]# systemctl restart php-fpm
#配置nginx,将http请求转发给php处理
[root@web001 ~]# cat /etc/nginx/conf.d/status.test.com.conf
server {
listen 80;
server_name status.test.com;
location /fpm_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#重启nginx
[root@web001 ~]# systemctl restart nginx
#测试一下获取值
[root@web001 ~]# curl -H Host:status.test.com http://127.0.0.1/fpm_status
pool: www
process manager: dynamic
start time: 29/Aug/2021:16:49:54 +0800
start since: 233
accepted conn: 1 #当前池接受的请求数
listen queue: 0 #请求队列,如果这个值不为0,那么需要增加FPM的进程数量
max listen queue: 0 #请求队列最高的数量
listen queue len: 128 #socket等待队列长度
idle processes: 4 #空闲进程数量
active processes: 1 #活跃进程数量
total processes: 5 #总进程数量
max active processes: 1 #最大的活跃进程数量(FPM启动开始计算)
max children reached: 0 #最大的活跃进程数量(FPM启动开始计算)
#4.编写取值脚本
[root@web001 ~]# cat /etc/zabbix/zabbix_agentd.d/scripts/fpm_status.sh
#!/usr/bin/bash
fpm_status_file=/tmp/fpm_status.tmp
fpm_status_name=status.test.com
fpm_status_path=/fpm_status
curl -sH host:${fpm_status_name} http://127.0.0.1:80${fpm_status_path} > ${fpm_status_file}
case $1 in
accepted_conn)
awk '/accepted conn:/{print $NF}' ${fpm_status_file}
;;
listen_queue)
awk '/^listen queue:/{print $NF}' ${fpm_status_file}
;;
max_listen_queue)
awk '/^max listen queue:/{print $NF}' ${fpm_status_file}
;;
active_processes)
awk '/^active processes:/{print $NF}' ${fpm_status_file}
;;
idle_processes)
awk '/^idle processes:/{print $NF}' ${fpm_status_file}
;;
total_processes)
awk '/^total processes:/{print $NF}' ${fpm_status_file}
;;
max_active_processes)
awk '/^max active processes:/{print $NF}' ${fpm_status_file}
;;
max_children_reached)
awk '/^max children reached:/{print $NF}' ${fpm_status_file}
;;
*)
echo "USAGE: $0 [accepted_conn|listen_queue|max_listen_queue|active_processes|idle_processes|total_processes|max_active_processes|max_children_reached]"
;;
esac
#5.自定义监控项,支持传参
[root@web001 ~]# cat /etc/zabbix/zabbix_agentd.d/fpm.conf
UserParameter=fpm.status[*], /bin/bash /etc/zabbix/zabbix_agentd.d/scripts/fpm_status.sh "$1"
#重启zabbix-agent
[root@web001 ~]# systemctl restart zabbix-agent
#6.zabbix-server使用zabbix-get获取
[root@manage ~]# zabbix_get -s 10.0.0.7 -k fpm.status[]
USAGE: /etc/zabbix/zabbix_agentd.d/scripts/fpm_status.sh [accepted_conn|listen_queue|max_listen_queue|active_processes|idle_processes|total_processes|max_active_processes|max_children_reached]
[root@manage ~]# zabbix_get -s 10.0.0.7 -k fpm.status[accepted_conn]
3
[root@manage ~]# zabbix_get -s 10.0.0.7 -k fpm.status[max_listen_queue]
0
#7.登录zabbix-web
1.创建模板
2.创建监控项
3.创建图形
4.创建触发器
5.关联对应主机
1.创建模板
2.创建监控项
#这些值全部加监控项
[accepted_conn|listen_queue|max_listen_queue|active_processes|idle_processes|total_processes|max_active_processes|max_children_reached]
3.创建图形
4.创建触发器
5.为对应主机添加模板
5.监控php-fpm的9000端口
创建触发器
6.监控mysql状态
#Mysql的基础监控
1.进程存活
2.检测端口
#Mysql的高级监控说明
Mysql提供show global status可以实现对Mysql的高级监控
高级监控包含监控Mysql连接数,增删改查数,流量等
#其他的监控mysql的方式
天兔
percona
[root@db01 ~]# mysqladmin password 123456
[root@db01 ~]# mysql -uroot -p123456 -e 'show global status'
#监控命令详解mysql -uroot -p123456 -e 'show global status'
Threads_connected:连接数
Com_select:查询总量
Com_insert:插入总量
Com_update:更新总量
Com_delete:删除总量
Bytes_received: 流入总流量
Bytes_sent:流出总流量
Slow_queries:慢查询总量
#1.准备脚本
[root@web001 scripts]# cat /etc/zabbix/zabbix_agentd.d/scripts/mysql_status.sh
#!/usr/bin/bash
port=$1
key=$2
mysql -uroot -p123456 -P${port} -e "show global status" |grep "${key}\s" |awk '{print $2}'
[root@web001 scripts]# chmod +x mysql_status.sh
#2.测试监控Shell脚本
[root@web001 scripts]# sh /etc/zabbix/zabbix_agentd.d/scripts/mysql_status.sh 3306 Threads_connected
1
#3.Zabbix自定义监控项
[root@web001 scripts]# cat /etc/zabbix/zabbix_agentd.d/mysql.conf
UserParameter=mysql.status[*], /bin/bash /etc/zabbix/zabbix_agentd.d/scripts/mysql_status.sh "$1" "$2" 2>/dev/null
#4.重启zabbix-agent
[root@web001 zabbix_agentd.d]# systemctl restart zabbix-agent
#5.在zabbix-server
[root@manage ~]# zabbix_get -s 10.0.0.7 -k mysql.status[3306,Threads_connected]
1
[root@manage ~]# zabbix_get -s 10.0.0.7 -k mysql.status[3306,Com_insert]
0
#使用Template DB MySQL模板监控Mysql 利用自带的模板改一改
mysql.status[3306,Threads_connected]
mysql.status[3306,Com_select]
mysql.status[3306,Bytes_received]
mysql.status[3306,Bytes_sent]
1.创建模板,基于已有的mysql模板全克隆一个
2.修改监控项
3.添加模板到对应的主机
4.看监测,最新数据,图形
写一个测试的程序测一下
[root@web01 ~]# cat mysql.sh
for i in {1..1000}
do
mysql -uroot -poldxu.com -h 127.0.0.1 -e "create database IF NOT EXISTS db;
use db;
DROP TABLE IF EXISTS test${i};
create table test${i}(id int);
insert into db.test${i} values (123456);
select * from db.test${i};
delete from test${i} where id=123456;"
done
7.监控mysql主从状态
Mysql主从监控的必要性
1.如果发生主从同步异常,业务会出问题
2.如果从库是用来备份的,会导致数据丢失
Mysql主从同步监控说明
1.从库运行show slave status\G可以来查看主从同步信息
Slave IO Running 可以观察从库的IO进程是否正常,IO进程用于同步二进制日志
Slave SQL Running 可以观察从库的SQL进程是否正常,SQL进程用于执行二进制日志
Seconds Behind Master代表主从同步的延时时间
#在10.0.0.51上配置从库
[root@db01 ~]# vim /etc/my.cnf
log-bin
server-id=51
[root@db01 ~]# systemctl restart mariadb
[root@db01 ~]# mysql -uroot -p123456
MariaDB [(none)]> grant replication slave, replication client on *.* to 'rep'@'%' identified by 'Rep123.com';
#备份master数据库中数据
[root@db01 ~]# mysqldump -uroot -p123456 --all-databases --single-transaction --master-data=1 --flush-logs > /root/db-$(date +%F)-all.sql
[root@db01 ~]# ll db-2021-08-29-all.sql
-rw-r--r-- 1 root root 515778 Aug 29 18:37 db-2021-08-29-all.sql
#将备份数据传输到slave
[root@db01 ~]# scp -rp ./db-2021-08-29-all.sql root@10.0.0.7:~
[root@web01 ~]# vim /etc/my.cnf
server-id=7
[root@web001 ~]# systemctl restart mariadb
#恢复数据
[root@web001 ~]# mysql -uroot -p123456 < db-2021-08-29-all.sql
[root@web001 ~]# mysql -uroot -p123456
MariaDB [(none)]> change master to
-> master_host='10.0.0.51',
-> master_user='rep',
-> master_password='Rep123.com';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.51
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mariadb-bin.000002
Read_Master_Log_Pos: 245
Relay_Log_File: mariadb-relay-bin.000004
Relay_Log_Pos: 531
Relay_Master_Log_File: mariadb-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 245
Relay_Log_Space: 872
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 51
1 row in set (0.00 sec)
#监控Mysql主从的Shell脚本mysql_slave_status.sh
[root@db01 ~]# cat /etc/zabbix/zabbix_agentd.d/scripts/mysql_slave_status.sh
#!/usr/bin/bash
port=$1
key=$2
mysql -uroot -p123456 -P${port} -e "show slave status\G"|grep "${key}\:"|awk '{print $2}' 2>/dev/null
[root@web001 ~]# mysql -uroot -p123456 -P3306 -e "show slave status\G"|grep "Slave_IO_Running\:"|awk '{print $2}'
Yes
[root@web001 ~]# mysql -uroot -p123456 -P3306 -e "show slave status\G"|grep "Slave_SQL_Running\:"|awk '{print $2}'
Yes
#手动执行脚本取值
[root@web001 ~]# sh +x /etc/zabbix/zabbix_agentd.d/scripts/mysql_slave_status.sh 3306 Slave_IO_Running
Yes
[root@web001 ~]# sh +x /etc/zabbix/zabbix_agentd.d/scripts/mysql_slave_status.sh 3306 Slave_SQL_Running
Yes
[root@web001 ~]# sh +x /etc/zabbix/zabbix_agentd.d/scripts/mysql_slave_status.sh 3306 Seconds_Behind_Master
0
#封装监控项
[root@web001 ~]# cat /etc/zabbix/zabbix_agentd.d/mysql.conf
UserParameter=mysql.slave.status[*], /bin/bash /etc/zabbix/zabbix_agentd.d/scripts/mysql_slave_status.sh "$1" "$2" 2>/dev/null
#重启zabbix-agent
[root@web001 ~]# systemctl restart zabbix-agent
#在zabbix-server端手动取值
[root@manage ~]# zabbix_get -s 10.0.0.7 -k mysql.slave.status[3306,Slave_IO_Running]
Yes
[root@manage ~]# zabbix_get -s 10.0.0.7 -k mysql.slave.status[3306,Slave_SQL_Running]
Yes
[root@manage ~]# zabbix_get -s 10.0.0.7 -k mysql.slave.status[3306,Seconds_Behind_Master]
0
#创建监控模板Template Mysql Slave Status
mysql.slave.status[3306,Slave_IO_Running]
mysql.slave.status[3306,Slave_SQL_Running]
mysql.slave.status[3306,Seconds_Behind_Master]
1.创建模板
2.创建监控项
3.创建触发器
1.触发器一: 两个状态都要为Yes
{Template MySQL Slave Status:mysql.slave.status[3306,Slave_IO_Running].str(Yes)}=0 or {Template MySQL Slave Status:mysql.slave.status[3306,Slave_SQL_Running].str(Yes)}=0
2.触发器二: 延时不能超过100
{Template MySQL Slave Status:mysql.slave.status[3306,Seconds_Behind_Master].last()}>100
4.将模板添加到对应的主机