Zabbix5.0.12_调用Python3脚本监控Windows下的MSSQL
笺注:
这是在 CentOS8_LNMP_编译安装Zabbix5.0.12 的基础上进行的。
被监控主机是在 使用Navicat远程管理MSSQL2014 的基础上进行的。
笺注:使用以下方法,被监控主机不用安装zabbix-agent
被监控主机配置MSSQL数据库:
创建库db1:
CREATE DATABASE db1;
在库db1中创建表Table1:
Use db1;
Create table Table1 (id int primary key not null identity (1,1),Name nvarchar(20),Chinese numeric(12,2))
插入测试的数据:
INSERT into Table1(Name,Chinese) values ('Zhuohua',8.8);
INSERT into Table1(Name,Chinese) values ('小明',16);
输出库db1的表Table1中的所有记录:
Use db1;
Select * from Table1;
#############
#############
Zabbix服务器的Python3的安装可参考:CentOS8安装Python3
第三方库pymssql的安装可参考:CentOS8使用Python3脚本管理MSSQL2008R2
Zabbix服务器使用Python3脚本获取被监控主机的MSSQL版本:
[root@centos8 ~]# cat /script/xx.py
#coding=utf-8
import pymssql
def db_connect():
#连接数据库;MSSQL服务器的IP地址(192.168.168.133)、端口号(1433)、用户名(sa)、用户密码、其中一个库的名称
db=pymssql.connect('192.168.168.133:1433','sa','Jacky888','db1')
cursor = db.cursor()
Sql_1 = "select @@VERSION as version"
cursor.execute(Sql_1)
Result_1 = cursor.fetchone()
print('数据库的版本信息:')
print(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
数据库的版本信息:
Microsoft SQL Server 2014 - 12.0.2000.8 (X64)
Feb 20 2014 20:04:26
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
[root@centos8 ~]#
#############
Zabbix服务器使用Python3脚本输出SQL语句执行后的返回值:
[root@centos8 ~]# cat /script/xx.py
#coding=utf-8
import pymssql
def db_connect():
db=pymssql.connect('192.168.168.133:1433','sa','Jacky888','db1')
cursor = db.cursor()
Sql_1 = "Select Chinese from Table1 where Name = 'Zhuohua'"
cursor.execute(Sql_1)
Results = cursor.fetchone()
#print (Results) #(Decimal('8.80'),)
#print (type(Results)) #<class 'tuple'>
#print (Results[0]) #8.80
#print (type(Results[0])) #<class 'decimal.Decimal'>
Results_1 = float(Results[0]) #转换为浮点数
print (Results_1)
#print (type(Results_1)) #<class 'float'>
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.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 pymssql
def db_connect():
db=pymssql.connect('192.168.168.133:1433','sa','Jacky888','db1')
cursor = db.cursor()
Sql_1 = "Select Chinese from Table1 where Name = '小明'"
cursor.execute(Sql_1)
Results = cursor.fetchone()
#print (Results) #(Decimal('16.00'),)
#print (type(Results)) #<class 'tuple'>
#print (Results[0]) #16.00
#print (type(Results[0])) #<class 'decimal.Decimal'>
Results_1 = float(Results[0]) #转换为浮点数
print (Results_1)
#print (type(Results_1)) #<class 'float'>
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.0
[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
如下图:
保存好配置文件后,重启一下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
ZBX_NOTSUPPORTED: Timeout while executing a shell script.
[root@centos8 ~]#
解决方法:
修改Zabbix服务器的Agent配置文件:
[root@centos8 ~]# vi /usr/local/zabbix/etc/zabbix_agentd.conf
# Timeout=3
修改为:
Timeout=30
保存好配置文件后,重启一下Zabbix本机的客户端和服务端:
pkill -9 -U zabbix
/usr/local/zabbix/sbin/zabbix_agentd
/usr/local/zabbix/sbin/zabbix_server
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_1
8.8
[root@centos8 ~]#
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_2
16.0
[root@centos8 ~]#
##################
配置》主机:
在主机Zabbix server中创建监控项:
给“Zabbix服务器使用Python3脚本输出SQL语句执行后的返回值”创建监控项:
自定义名称: Oracle command_1
键值: command_1
信息类型: 浮点数
备注:其他地方保持默认,点击底下的“添加”按键。
监控项“Oracle command_1”添加成功后,可以测试一下:
点击“Get value and test”:
可以获取到键值的返回值就可以了:
给监控项“Oracle command_1”创建图形“Oracle command_1 image”:
备注:其他地方保持默认,点击底下的“添加”按键。
查看图形“Oracle command_1 image”:
监测》主机》左击主机名称Zabbix server》图形:
选择时间范围:
使用“过滤器”,选择显示的图形:(默认会显示所有图形)
######
给监控项“Oracle command_1”创建触发器:(返回值大于或等于10.8就告警)
自定义名称: Chinese is greater than or equal to 10.8
插入表达式:(监控项: Zabbix server: Oracle command_1 )
自动生成的表达式:(触发器的表达式要用到监控项中的键值)
{Zabbix server:command_1.last()}>=10.8
备注:其他地方保持默认,点击底下的“添加”按键。
再次查看图形“Oracle command_1 image”:(理应可以看到触发器“Chinese is greater than or equal to 10.8”)
#############
#############
在库db1的表Table1中更改数据:
Use db1;
Update Table1 set Chinese = 10.8 where Name = 'Zhuohua';
Select * from Table1;
再次查看图形“Oracle command_1 image”:
监控项“Oracle command_1”的返回值大于或等于10.8时,仪表板会如下图显示:(显示对应触发器的名称)
Chinese is greater than or equal to 10.8
#############
#############
再次在库db1的表Table1中更改数据:
Use db1;
Update Table1 set Chinese = 10 where Name = 'Zhuohua';
Select * from Table1;
再次查看图形“Oracle command_1 image”:
监控项“Oracle command_1”的返回值小于10.8时,仪表板会如下图显示:
触发器“Chinese is greater than or equal to 10.8”会自动消失
相关文章:
MSSQL的SQL语句
Zabbix调用Python3脚本监控MSSQL
Zabbix5.0.12_调用Python3脚本监控Linux下的MySQL |