Zabbix调用Python3脚本监控Linux下的Oracle(二)
笺注:
Zabbix服务器安装Python3可参考 CentOS8安装Python3
这是在 使用Navicat连接Oracle11gR2 的基础上进行的。
笺注:使用以下方法,被监控主机不用安装zabbix-agent
被监控主机的信息:
[root@oracle ~]# cat /etc/issue |head -1
Oracle Linux Server release 6.9
[root@oracle ~]#
[root@oracle ~]# uname -r
4.1.12-61.1.28.el6uek.x86_64
[root@oracle ~]#
[root@oracle ~]# ifconfig eth1 |grep "inet addr" |awk '{print $2}' |awk -F: '{print $2}'
192.168.168.135
[root@oracle ~]#
被监控主机本地登录Oracle:(Oracle已启动时)
[root@oracle ~]# su - oracle
[oracle@oracle ~]$ sqlplus /nolog
SQL*Plus: Release 11.2.0.1.0 Production on Mon Aug 12 06:49:09 2019
Copyright (c) 1982, 2009, Oracle. All rights reserved.
SQL> conn /as sysdba;
Connected.
SQL>
SQL> show user;
USER is "SYS"
SQL>
切换用户:
SQL> connect happy/mima;
Connected.
SQL>
SQL> show user;
USER is "HAPPY"
SQL>
查看Oracle的版本:
SQL> SELECT version FROM product_component_version WHERE substr(product, 1, 6) = 'Oracle';
VERSION
--------------------------------------------------------------------------------
11.2.0.1.0
SQL>
输出数据库最大连接数:
SQL> Select value from v$parameter where name='processes';
VALUE
--------------------------------------------------------------------------------
150
SQL>
输出数据库当前打开的连接数:
SQL> Select count(*) from v$session;
COUNT(*)
----------
28
SQL>
输出数据库当前处于激活状态的连接数:
SQL> Select count(*) from v$session where status='ACTIVE';
COUNT(*)
----------
22
SQL>
创建表TABLE_1:
SQL> Create table TABLE_1(ID number(3),NAME varchar2(10));
Table created.
SQL>
#############
#############
Windows客户端使用Navicat连接Oracle11gR2:
往表TABLE_1插入一条记录:
INSERT into TABLE_1 (ID,NAME) values (1,'李大杰');
输出表TABLE_1中的所有记录:
Select * FROM TABLE_1;
#############
#############
Zabbix服务器使用Python3脚本查看被监控主机的Oracle版本:
[root@centos8 ~]# cat /script/xx.py
#coding=utf-8
import cx_Oracle
def db_connect():
#远程连接数据库(IP 192.168.168.135);使用数据库用户happy
db=cx_Oracle.connect('happy/mima@192.168.168.135:1521/ORCL')
cursor = db.cursor()
Sql_1 = "SELECT version FROM product_component_version WHERE substr(product, 1, 6) = 'Oracle'"
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
11.2.0.1.0
----------
数据库的版本信息:11.2.0.1.0
[root@centos8 ~]#
笺注:Zabbix服务器安装第三方库等等可参考 CentOS8使用Python3脚本远程管理Oracle11gR2
#############
Zabbix服务器使用Python3脚本输出被监控主机的数据库当前处于激活状态的连接数:
[root@centos8 ~]# cat /script/xx.py
#coding=utf-8
import cx_Oracle
def db_connect():
db=cx_Oracle.connect('happy/mima@192.168.168.135:1521/ORCL')
cursor=db.cursor()
Sql_1 = "Select count(*) from v$session where status='ACTIVE'"
cursor.execute(Sql_1)
Results=cursor.fetchone()
#print (Results) #(22,)
#print (type(Results)) #<class 'tuple'>
print (Results[0])
#print (type(Results[0])) #<class 'int'>
db.close()
def main():
try:
db_connect()
except Exception as e:
print(e)
if __name__ == "__main__":
main()
脚本运行的结果:
[root@centos8 ~]# python3 /script/xx.py
22
[root@centos8 ~]#
设置脚本权限:
[root@centos8 ~]# chmod a+x /script/xx.py
[root@centos8 ~]# ll /script/xx.py
-rwxr-xr-x 1 root root 488 8月 12 16:13 /script/xx.py
[root@centos8 ~]#
#############
Zabbix服务器使用Python3脚本输出SQL语句执行后的返回值:
[root@centos8 ~]# cat /script/yy.py
#coding=utf-8
import cx_Oracle
def db_connect():
db=cx_Oracle.connect('happy/mima@192.168.168.135:1521/ORCL')
cursor=db.cursor()
Sql_1 = "SELECT ID from TABLE_1 WHERE NAME = '李大杰'"
cursor.execute(Sql_1)
Results=cursor.fetchone()
#print (Results) #(1,)
#print (type(Results)) #<class 'tuple'>
print (Results[0])
#print (type(Results[0])) #<class 'int'>
db.close()
def main():
try:
db_connect()
except Exception as e:
print(e)
if __name__ == "__main__":
main()
脚本运行的结果:
[root@centos8 ~]# python3 /script/yy.py
1
[root@centos8 ~]#
设置脚本权限:
[root@centos8 ~]# chmod a+x /script/yy.py
[root@centos8 ~]# ll /script/yy.py
-rwxr-xr-x 1 root root 491 8月 12 16:14 /script/yy.py
[root@centos8 ~]#
#############
#############
修改Zabbix服务器的Agent配置文件:
[root@centos8 ~]# find / -name zabbix_agentd.conf
/root/zabbix-4.4.5/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本机的客户端和服务端:
[root@centos8 ~]# pkill -9 -U zabbix
[root@centos8 ~]#
[root@centos8 ~]# /usr/local/zabbix/sbin/zabbix_agentd
[root@centos8 ~]# /usr/local/zabbix/sbin/zabbix_server
[root@centos8 ~]#
######
在Zabbix服务器测试,验证能否获取本机(127.0.0.1)的key:
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_1
22
[root@centos8 ~]#
[root@centos8 ~]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k command_2
1
[root@centos8 ~]#
##################
在主机Zabbix server中创建监控项:
给“被监控主机的数据库当前处于激活状态的连接数”创建监控项:
自定义名称: Oracle command_1
键值: command_1
信息类型: 数字(无正负)
备注:其他地方保持默认,点击底下的“添加”按键。
######
给监控项“Oracle command_1”创建图形“Oracle command_1 image”:
备注:其他地方保持默认,点击底下的“添加”按键。
查看图形“Oracle command_1 image”:
监测》图形:
##################
在主机Zabbix server中再创建监控项:
给“Zabbix服务器使用Python3脚本输出SQL语句执行后的返回值”创建监控项:
自定义名称: Oracle command_2
键值: command_2
信息类型: 数字(无正负)
备注:其他地方保持默认,点击底下的“添加”按键。
######
给监控项“Oracle command_2”创建触发器:(返回值大于或等于5就告警)
自定义名称: ID is greater than or equal to 5
插入表达式:(监控项: Zabbix server: Oracle command_2 )
自动生成的表达式:(触发器的表达式要用到监控项中的键值)
{Zabbix server:command_2.last()}>=5
备注:其他地方保持默认,点击底下的“添加”按键。
新创建的触发器“ID is greater than or equal to 5”:(使用“过滤器”搜索)
#############
#############
更改表TABLE_1的字段ID的值:
UPDATE TABLE_1 SET ID = 5 WHERE NAME = '李大杰';
SELECT * FROM TABLE_1;
监控项“Oracle command_2”的返回值大于或等于5时,仪表板会如下图显示:(显示对应触发器的名称)
ID is greater than or equal to 5
#############
#############
再次更改表TABLE_1的字段ID的值:
UPDATE TABLE_1 SET ID = 3 WHERE NAME = '李大杰';
SELECT * FROM TABLE_1;
监控项“Oracle command_2”的返回值小于5时,仪表板会如下图显示:
触发器“ID is greater than or equal to 5”会自动消失
相关文章:
Zabbix调用Python3脚本监控Linux下的Oracle(一)
Linux常用命令(一) |