哪个网站做兼职,青岛开发区 网站建设,wordpress google 地图插件,linux wordpress 安装前言
监控系统除了监控os和数据库性能相关的指标外#xff0c;业务数据也是重点监控的对象。
一线驻场的运维同学应该深有体会#xff0c;每天需要向甲方或者公司反馈现场的数据情况#xff0c;正常情况下一天巡检两次#xff0c;早上上班后和下午下班前各一次。监控项目…前言
监控系统除了监控os和数据库性能相关的指标外业务数据也是重点监控的对象。
一线驻场的运维同学应该深有体会每天需要向甲方或者公司反馈现场的数据情况正常情况下一天巡检两次早上上班后和下午下班前各一次。监控项目多而杂如果每次都手动拷贝脚本到数据库中查询不仅麻烦而且效率极低因此有必要配置一个监控系统辅助业务数据的巡检工作。
监控基本方式是通过sql查询数据库相关内容状态和信息来实现这里以监控示例业务表为例讲解此监控项建立的过程希望能有一定的指导作用方便各位根据自己的实际情况建立监控内容。
0 环境准备
zabbix部署教程
1 业务场景模拟
示例数据
数据库sakila下载地址https://dev.mysql.com/doc/index-other.html下载后导入数据库即可完成创建。 EER图 假设业务监控指标的需求为获取Lethbridge城市当前的累计营业额。
sql如下
mysql
select sum(p.amount) amountfrom sakila.customer c,sakila.payment p,sakila.store s,sakila.address a,sakila.city ct where c.customer_id p.customer_idand c.store_id s.store_id and s.address_id a.address_id and a.city_id ct.city_id and ct.city Lethbridge;----------
| amount |
----------
| 36997.53 |
----------
1 row in set (0.02 sec)2 创建监控项脚本文件
此文件用于添加监控脚本。
[rootmysql001 scripts]# touch /var/lib/zabbix/percona/scripts/business_data.sh
[rootmysql001 scripts]# chmod x /var/lib/zabbix/percona/scripts/business_data.sh
[rootmysql001 scripts]# ll /var/lib/zabbix/percona/scripts/business_data.sh
-rwxr-xr-x 1 root root 587 Feb 25 18:15 /var/lib/zabbix/percona/scripts/business_data.sh3 修改zabbix agent配置文件
将监控项脚本文件添加到配置文件中。
[rootmysql001 scripts]# vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/userparameter_percona_mysql.conf
#添加
UserParameterbusiness_data[*],/var/lib/zabbix/percona/scripts/business_data.sh $14 监控用户赋权
给监控用户赋予sakila数据库的查询权限。
mysql grant select on sakila.* to pmplocalhost;
Query OK, 0 rows affected (0.01 sec)mysql show grants for pmplocalhost;
---------------------------------------------------------------
| Grants for pmplocalhost |
---------------------------------------------------------------
| GRANT PROCESS, REPLICATION CLIENT ON *.* TO pmplocalhost |
| GRANT SELECT ON sakila.* TO pmplocalhost |
---------------------------------------------------------------
2 rows in set (0.00 sec)5 测试监控脚本
过滤前
[mysqlmysql001 ~]$ mysql -upmp -pPmp123456. -e select sum(p.amount) amount from sakila.customer c, sakila.payment p, sakila.store s, sakila.address a, sakila.city ct where c.customer_id p.customer_id and c.store_id s.store_id and s.address_id a.address_id and a.city_id ct.city_id and ct.city Lethbridge\G
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
amount: 36997.53过滤后
[mysqlmysql001 ~]$ mysql -upmp -pPmp123456. -e select sum(p.amount) amount from sakila.customer c, sakila.payment p, sakila.store s, sakila.address a, sakila.city ct where c.customer_id p.customer_id and c.store_id s.store_id and s.address_id a.address_id and a.city_id ct.city_id and ct.city Lethbridge\G 2/dev/null | awk NR2 {print $2}
36997.53过滤后的结果一定要为一个数值或字符串不能包含间隔或多列要不然监控项不好配置。
count()等这种聚合函数无脑在脚本后加 2/dev/null | awk ‘NR2 {print $2}’ 就行。
命令解析
2/dev/null去除MySQL命令行直接输入密码警告awk ‘NR2’取第二行数据awk ‘{print $2}’取第二列数值。
6 配置监控项脚本
[rootmysql001 scripts]# vim /var/lib/zabbix/percona/scripts/business_data.sh
#添加#!/bin/bash
#定义一个本地用户登录mysql数据库的变量
case $1 in#case1: Lethbridge_AmountLethbridge_Amount)mysql -upmp -pPmp123456. -e select sum(p.amount) amount from sakila.customer c, sakila.payment p, sakila.store s, sakila.address a, sakila.city ct where c.customer_id p.customer_id and c.store_id s.store_id and s.address_id a.address_id and a.city_id ct.city_id and ct.city Lethbridge\G 2/dev/null | awk NR2 {print $2};;#case2: number of actorsNumber_of_Actors)mysql -upmp -pPmp123456. -e select count(*) from sakila.actor\G 2/dev/null | awk NR2 {print $2};;
esac此脚本不仅可以添加mysql数据库的监控os和oracle等其他服务的监控都是可以添加的只需要往下添加case选项就可以。
7 重启zabbix代理
[rootmysql001 scripts]# systemctl restart zabbix_agentd
[rootmysql001 scripts]# systemctl status zabbix_agentd
● zabbix_agentd.service - SYSV: Zabbix Monitoring AgentLoaded: loaded (/etc/rc.d/init.d/zabbix_agentd; bad; vendor preset: disabled)Active: active (running) since Sun 2024-02-25 21:04:38 CST; 9s agoDocs: man:systemd-sysv-generator(8)Process: 12525 ExecStop/etc/rc.d/init.d/zabbix_agentd stop (codeexited, status0/SUCCESS)Process: 12539 ExecStart/etc/rc.d/init.d/zabbix_agentd start (codeexited, status0/SUCCESS)Main PID: 12548 (zabbix_agentd)CGroup: /system.slice/zabbix_agentd.service├─12548 /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf├─12549 /usr/local/zabbix/sbin/zabbix_agentd: collector [idle 1 sec]├─12550 /usr/local/zabbix/sbin/zabbix_agentd: listener #1 [waiting for connection]├─12551 /usr/local/zabbix/sbin/zabbix_agentd: listener #2 [waiting for connection]├─12552 /usr/local/zabbix/sbin/zabbix_agentd: listener #3 [waiting for connection]└─12553 /usr/local/zabbix/sbin/zabbix_agentd: active checks #1 [idle 1 sec]Feb 25 21:04:38 mysql001 systemd[1]: Starting SYSV: Zabbix Monitoring Agent...
Feb 25 21:04:38 mysql001 zabbix_agentd[12539]: Starting Zabbix Agent: [ OK ]
Feb 25 21:04:38 mysql001 systemd[1]: Started SYSV: Zabbix Monitoring Agent.只需要重启一次即可以后脚本文件business_data.sh添加监控项不需要再重启zabbix代理。
8 测试
到zabbix server的bin目录下执行zabbix_get测试
[rootzabbix6 bin]# ./zabbix_get -s 192.168.131.99 -k business_data[Lethbridge_Amount]
36997.53测试成功
9 创建主机
登录zabbix server Web页面在配置-主机-创建主机下添加主机 配置主机名、群组和zabbix代理端ip。
10 添加监控项
配置监控项名称、键值、数据类型和更新间隔。
注意此处设置更新间隔为1s如果配置的监控项目比较多且数据库的压力比较大可以根据实际环境适当调大更新间隔。
监控项报错错误信息 数据为浮点型监控项配置了数值型。
修改监控项配置 10 结果呈现 至此完成此监控项添加。
【关联文章】
1zabbix部署教程 2zabbix监控MySQL 3PMP自定义监控项 4zabbix监控业务数据