Board logo

标题: Zabbix5.0.12_调用Python3脚本监控Linux下的MySQL [打印本页]

作者: admin    时间: 2022-8-20 14:25     标题: Zabbix5.0.12_调用Python3脚本监控Linux下的MySQL

笺注: 这是在 CentOS8_LNMP_编译安装Zabbix5.0.12 的基础上进行的。 被监控主机的MySQL的安装可参考 LNMP一键安装包(lamp_Apache2.4用户验证+phpMyAdmin) 笺注:使用以下方法,被监控主机不用安装zabbix-agent 被监控主机的信息: [root@localhost ~]# cat /etc/issue |head -1 CentOS release 6.9 (Final) [root@localhost ~]# [root@localhost ~]# uname -r 2.6.32-696.el6.x86_64 [root@localhost ~]# [root@localhost ~]# ifconfig eth0 |grep "inet addr" |awk '{print $2}' |cut -d: -f 2 192.168.168.130 [root@localhost ~]# 被监控主机配置MySQL数据库:( 使用数据库管理员root@localhost;假如MySQL的端口为默认的TCP 3306,就可以不写 ) [root@localhost ~]# mysql -u"root" -p"123" -h"localhost" -P"3306" Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.48-log Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> 查看当前登录的数据库用户: mysql> Select user(); +----------------+ | user() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec) mysql> 查看MySQL的版本: mysql> Select version(); +------------+ | version() | +------------+ | 5.5.48-log | +------------+ 1 row in set (0.00 sec) mysql> 创建一个库db1: mysql> Create database db1 character set utf8 collate utf8_general_ci; Query OK, 1 row affected (0.11 sec) mysql> mysql> show create database db1; +----------+--------------------------------------------------------------+ | Database | Create Database | +----------+--------------------------------------------------------------+ | db1 | CREATE DATABASE `db1` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+--------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> 注释:库db1的字符集为 utf8_general_ci 在库db1中创建表、插入测试的数据: mysql> use db1; Database changed mysql> mysql> Create table Table1(Name varchar(20),Age tinyint); Query OK, 0 rows affected (0.42 sec) mysql> INSERT into Table1(Name,Age) values ('Zhuohua',-8); Query OK, 1 row affected (0.10 sec) mysql> INSERT into Table1(Name,Age) values ('Python',3); Query OK, 1 row affected (0.00 sec) mysql> INSERT into Table1(Name,Age) values ('小明',16); Query OK, 1 row affected (0.00 sec) mysql> 输出库db1的表Table1中的所有记录: select * from db1.Table1; 图片1.png 在MySQL下查看当前使用的是哪个库: mysql> Select database(); +------------+ | database() | +------------+ | db1 | +------------+ 1 row in set (0.01 sec) mysql> 输出当前库的表Table1中的所有记录: Select * from Table1; 图片2.png ###### Select Name,Age,ABS(Age) from Table1; 图片3.png 注释:ABS()函数会返回数值的绝对值。 Select abs(Age) from Table1 where Name = 'Zhuohua'; 图片4.png Select Name,LENGTH(Name) AS 字符串的长度 from Table1; 图片5.png 注释:LENGTH()函数会返回字符串的长度;假如MySQL的字符编码是UTF-8,那么一个汉字将返回 3 ###### MySQL服务器再授权数据库用户zhuohua@'192.168.168.154'(密码168),仅仅在使用IP地址192.168.168.154时可以进行访问,对库db1有完全控制的权限: mysql> grant all on db1.* to zhuohua@'192.168.168.154' identified by '168'; Query OK, 0 rows affected (0.01 sec) mysql> 查看远程数据库用户zhuohua@'192.168.168.154'的权限: mysql> show grants for zhuohua@'192.168.168.154'; +----------------------------------------------------------------------------------------------------------------------+ | Grants for zhuohua@192.168.168.154 | +----------------------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'zhuohua'@'192.168.168.154' IDENTIFIED BY PASSWORD '*242E46A1E8D30FE06F7CE37B55BFC25BA981D70C' | | GRANT ALL PRIVILEGES ON `db1`.* TO 'zhuohua'@'192.168.168.154' | +----------------------------------------------------------------------------------------------------------------------+ 2 rows in set (0.01 sec) mysql> 被监控主机的防火墙配置:(打开TCP 3306端口) [root@localhost ~]# cat /etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Sat Feb 8 10:48:31 2020 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1:156] -A INPUT -i lo -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Completed on Sat Feb 8 10:48:31 2020 [root@localhost ~]# 保存好配置文件后,重启iptables: [root@localhost ~]# service iptables restart iptables:应用防火墙规则:[确定] [root@localhost ~]# ############# ############# Zabbix服务器的Python3的安装可参考:CentOS8安装Python3 第三方库PyMySQL的安装可参考:Python3脚本管理Linux下的MySQL Zabbix服务器使用Python3脚本获取被监控主机的MySQL版本: [root@centos8 ~]# cat /script/xx.py #coding=utf-8 import pymysql def db_connect(): #连接数据库;MySQL服务器的IP地址(192.168.168.130)、用户名、用户密码、其中一个库的名称(db1)、端口号、字符集 db=pymysql.connect("192.168.168.130", "zhuohua", "168", "db1",3306,charset="utf8") cursor = db.cursor() Sql_1 = "select version()" cursor.execute(Sql_1) Result_1 = cursor.fetchone() print(Result_1[0]) print("-" * 10) print(f"数据库的版本信息:{Result_1[0]}") db.close() #关闭数据库连接 def func_main(): try: db_connect() except Exception as e: print(f"数据库连接失败,原因: {e}") if __name__ == "__main__": func_main() 脚本运行的结果: [root@centos8 ~]# python3 /script/xx.py 5.5.48-log ---------- 数据库的版本信息:5.5.48-log [root@centos8 ~]# ############# Zabbix服务器使用Python3脚本输出SQL语句执行后的返回值: [root@centos8 ~]# cat /script/xx.py #coding=utf-8 import pymysql def db_connect(): db=pymysql.connect("192.168.168.130", "zhuohua", "168", "db1",3306,charset="utf8") cursor = db.cursor() Sql_1 = "Select abs(Age) from Table1 where Name = 'Zhuohua'" cursor.execute(Sql_1) Results = cursor.fetchone() #print (Results) #(8,) #print (type(Results)) # print (Results[0]) #print (type(Results[0])) # db.close() def func_main(): try: db_connect() except Exception as e: print(e) if __name__ == "__main__": func_main() 脚本运行的结果: [root@centos8 ~]# python3 /script/xx.py 8 [root@centos8 ~]# 设置脚本权限: [root@centos8 ~]# chmod a+x /script/xx.py [root@centos8 ~]# ############# Zabbix服务器使用Python3脚本输出SQL语句执行后的返回值: [root@centos8 ~]# cat /script/yy.py #coding=utf-8 import pymysql def db_connect(): db=pymysql.connect("192.168.168.130", "zhuohua", "168", "db1",3306,charset="utf8") cursor = db.cursor() Sql_1 = "Select Age from Table1 where Name = '小明'" cursor.execute(Sql_1) Results = cursor.fetchone() print (Results[0]) db.close() def func_main(): try: db_connect() except Exception as e: print(e) if __name__ == "__main__": func_main() 脚本运行的结果: [root@centos8 ~]# python3 /script/yy.py 16 [root@centos8 ~]# 设置脚本权限: [root@centos8 ~]# chmod a+x /script/yy.py [root@centos8 ~]# ############# ############# 修改Zabbix服务器的Agent配置文件: [root@centos8 ~]# find / -name zabbix_agentd.conf /root/zabbix-5.0.12/conf/zabbix_agentd.conf /usr/local/zabbix/etc/zabbix_agentd.conf [root@centos8 ~]# [root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf # UnsafeUserParameters=0 修改为:(启用该功能) UnsafeUserParameters=1 接着插入: UserParameter=command_1,/usr/bin/python3 /script/xx.py UserParameter=command_2,/usr/bin/python3 /script/yy.py 如下图: 图片6.png 保存好配置文件后,重启一下Zabbix本机的客户端和服务端: pkill -9 -U zabbix /usr/local/zabbix/sbin/zabbix_agentd /usr/local/zabbix/sbin/zabbix_server ###### 在Zabbix服务器测试,验证能否获取本机(127.0.0.1)的键值: [root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_1 8 [root@centos8 ~]# [root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_2 16 [root@centos8 ~]# ################## 配置》主机: 在主机Zabbix server中创建监控项: 图片7.png 给“Zabbix服务器使用Python3脚本输出SQL语句执行后的返回值”创建监控项: 自定义名称: Oracle command_1 键值: command_1 信息类型: 数字(无正负) 图片8.png 备注:其他地方保持默认,点击底下的“添加”按键。 给监控项“Oracle command_1”创建图形“Oracle command_1 image”: 图片9.png 备注:其他地方保持默认,点击底下的“添加”按键。 查看图形“Oracle command_1 image”: 监测》主机》左击主机名称Zabbix server》图形: 图片10.png 选择时间范围: 图片11.png 使用“过滤器”,选择显示的图形:(默认会显示所有图形) 图片12.png 图片13.png 图片14.png ###### 给监控项“Oracle command_1”创建触发器:(返回值大于或等于10就告警) 自定义名称: AGE is greater than or equal to 10 图片15.png 插入表达式:(监控项: Zabbix server: Oracle command_1 图片16.png 自动生成的表达式:(触发器的表达式要用到监控项中的键值) {Zabbix server:command_1.last()}>=10 图片17.png 备注:其他地方保持默认,点击底下的“添加”按键。 再次查看图形“Oracle command_1 image”:(理应可以看到触发器“AGE is greater than or equal to 10”) 图片18.png ############# ############# 在库db1的表Table1中更改数据: mysql> use db1; Database changed mysql> mysql> Update Table1 set Age = 18 where Name = 'Zhuohua'; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> Select * from Table1 where Name = 'Zhuohua'; +---------+------+ | Name | Age | +---------+------+ | Zhuohua | 18 | +---------+------+ 1 row in set (0.00 sec) mysql> 再次查看图形“Oracle command_1 image”: 图片19.png 图片20.png 监控项“Oracle command_1”的返回值大于或等于10时,仪表板会如下图显示:(显示对应触发器的名称) AGE is greater than or equal to 10 图片21.png 图片22.png ############# ############# 再次在库db1的表Table1中更改数据: mysql> use db1; Database changed mysql> mysql> Update Table1 set Age = -9 where Name = 'Zhuohua'; Query OK, 1 row affected (0.13 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> Select Age from Table1 where Name = 'Zhuohua'; +------+ | Age | +------+ | -9 | +------+ 1 row in set (0.00 sec) mysql> Select Abs(Age) from Table1 where Name = 'Zhuohua'; +----------+ | Abs(Age) | +----------+ | 9 | +----------+ 1 row in set (0.00 sec) mysql> 再次查看图形“Oracle command_1 image”: 图片23.png 图片24.png 监控项“Oracle command_1”的返回值小于9时,仪表板会如下图显示: 触发器“AGE is greater than or equal to 10”会自动消失 图片25.png 相关文章: MySQL的SQL语句 Zabbix调用Python3脚本监控Linux下的MySQL Zabbix5.0.12_调用Python3脚本监控Windows下的MSSQL Zabbix5.0.12_调用Shell脚本监控Linux下的MySQL Zabbix5.0.12_使用模板监控MySQL主从同步状态

图片附件: 图片1.png (2022-8-20 14:26, 37.77 KB) / 下载次数 52
http://blog.zhuohua.store/attachment.php?aid=20272&k=f6c71f028d157a11ceaac31d86a167fc&t=1714353050&sid=ndFcgn



图片附件: 图片2.png (2022-8-20 14:26, 36.83 KB) / 下载次数 45
http://blog.zhuohua.store/attachment.php?aid=20273&k=4fa8d54bc7aed2fc25acf964f83b4f0b&t=1714353050&sid=ndFcgn



图片附件: 图片3.png (2022-8-20 14:27, 44.09 KB) / 下载次数 48
http://blog.zhuohua.store/attachment.php?aid=20274&k=f40228a7936d9630ad1e7f4755e862dc&t=1714353050&sid=ndFcgn



图片附件: 图片4.png (2022-8-20 14:27, 49.57 KB) / 下载次数 43
http://blog.zhuohua.store/attachment.php?aid=20275&k=d0310886b58740b41bb8c3ddf4b6c34f&t=1714353050&sid=ndFcgn



图片附件: 图片5.png (2022-8-20 14:27, 73.12 KB) / 下载次数 40
http://blog.zhuohua.store/attachment.php?aid=20276&k=67f17319a477ec3f75dece19e96f6d1d&t=1714353050&sid=ndFcgn



图片附件: 图片6.png (2022-8-20 14:32, 44.97 KB) / 下载次数 40
http://blog.zhuohua.store/attachment.php?aid=20277&k=b3b2bcda3ef68c979fbeea94f8c5844e&t=1714353050&sid=ndFcgn



图片附件: 图片7.png (2022-8-20 14:33, 14.42 KB) / 下载次数 51
http://blog.zhuohua.store/attachment.php?aid=20278&k=9110563f795d48e76116346af2fc5529&t=1714353050&sid=ndFcgn



图片附件: 图片8.png (2022-8-20 14:33, 27.98 KB) / 下载次数 54
http://blog.zhuohua.store/attachment.php?aid=20279&k=9a54610350d709f56fc1ea5c8dbc8907&t=1714353050&sid=ndFcgn



图片附件: 图片9.png (2022-8-20 14:34, 76.3 KB) / 下载次数 50
http://blog.zhuohua.store/attachment.php?aid=20280&k=5673307b3992bb1cbb6a23e09b282937&t=1714353050&sid=ndFcgn



图片附件: 图片10.png (2022-8-20 14:34, 46.06 KB) / 下载次数 46
http://blog.zhuohua.store/attachment.php?aid=20281&k=3218339e72b951011a82e5e8cd6ac5e4&t=1714353050&sid=ndFcgn



图片附件: 图片11.png (2022-8-20 14:35, 61.54 KB) / 下载次数 53
http://blog.zhuohua.store/attachment.php?aid=20282&k=151479fcb88f939ff5e4d4cdecae5c07&t=1714353050&sid=ndFcgn



图片附件: 图片12.png (2022-8-20 14:35, 14.79 KB) / 下载次数 48
http://blog.zhuohua.store/attachment.php?aid=20283&k=c3c9533d8ad6b2082ebda63d8a491e13&t=1714353050&sid=ndFcgn



图片附件: 图片13.png (2022-8-20 14:35, 72.07 KB) / 下载次数 47
http://blog.zhuohua.store/attachment.php?aid=20284&k=073e98fb5c91460249126e9380478cf8&t=1714353050&sid=ndFcgn



图片附件: 图片14.png (2022-8-20 14:35, 34.08 KB) / 下载次数 20
http://blog.zhuohua.store/attachment.php?aid=20285&k=c107dd6cf57f9f663428b5efdd7ec771&t=1714353050&sid=ndFcgn



图片附件: 图片15.png (2022-8-20 14:36, 38.92 KB) / 下载次数 14
http://blog.zhuohua.store/attachment.php?aid=20286&k=5a76caa8ca9e033256f8f354ecc08bca&t=1714353050&sid=ndFcgn



图片附件: 图片16.png (2022-8-20 14:36, 22.04 KB) / 下载次数 15
http://blog.zhuohua.store/attachment.php?aid=20287&k=90b656ce6dd1695e76157e3779b095d3&t=1714353050&sid=ndFcgn



图片附件: 图片17.png (2022-8-20 14:36, 12.34 KB) / 下载次数 21
http://blog.zhuohua.store/attachment.php?aid=20288&k=528623f1c0c83965344b7a0e80d2e2ad&t=1714353050&sid=ndFcgn



图片附件: 图片18.png (2022-8-20 14:37, 40.68 KB) / 下载次数 18
http://blog.zhuohua.store/attachment.php?aid=20289&k=7dc8bc7a1f5f15a11ac95df6fd3e16c6&t=1714353050&sid=ndFcgn



图片附件: 图片19.png (2022-8-20 14:38, 78.24 KB) / 下载次数 21
http://blog.zhuohua.store/attachment.php?aid=20290&k=a1669a041ae9ddfa8fcec4ece1f404b9&t=1714353050&sid=ndFcgn



图片附件: 图片20.png (2022-8-20 14:38, 45.4 KB) / 下载次数 22
http://blog.zhuohua.store/attachment.php?aid=20291&k=e214aec3880790a5b4bc68f4756f92be&t=1714353050&sid=ndFcgn



图片附件: 图片21.png (2022-8-20 14:38, 50.11 KB) / 下载次数 24
http://blog.zhuohua.store/attachment.php?aid=20292&k=b480cc5711dcee4eb612719c1c1be80e&t=1714353050&sid=ndFcgn



图片附件: 图片22.png (2022-8-20 14:38, 19.56 KB) / 下载次数 20
http://blog.zhuohua.store/attachment.php?aid=20293&k=7a6cfdf809c47676c8de0643ef92a992&t=1714353050&sid=ndFcgn



图片附件: 图片23.png (2022-8-20 14:40, 78.31 KB) / 下载次数 19
http://blog.zhuohua.store/attachment.php?aid=20294&k=08281f75d4ac798e6a4473d6653ca842&t=1714353050&sid=ndFcgn



图片附件: 图片24.png (2022-8-20 14:40, 51.12 KB) / 下载次数 20
http://blog.zhuohua.store/attachment.php?aid=20295&k=f3742b4d3cf0a0d2fb1aca9f10c0af91&t=1714353050&sid=ndFcgn



图片附件: 图片25.png (2022-8-20 14:40, 43.91 KB) / 下载次数 18
http://blog.zhuohua.store/attachment.php?aid=20296&k=8069997327702eb872f267cd338687bc&t=1714353050&sid=ndFcgn






欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2