1.安装cx_Oracle
a.官网下载客户端组件包
oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm
oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
b.使用rpm命令安装上面给的三个包
rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm
rpm -ivh oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm
rpm -ivh oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm
c.配置系统环境变量
vi ~/.bash_profile
添加:
export LD_LIBRARY_PATH=/usr/lib/oracle/12.2/client64/lib/:$LD_LIBRARY_PATH
source ~/.bash_profile ##生效
vi /etc/ld.so.conf.d/oracle.conf
添加:
/usr/lib/oracle/12.2/client64/lib/
执行ldconfig ##生效
vi /etc/profile.d/oracle.sh
添加:
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export PATH=$PATH:$ORACLE_HOME/bin
export PATH=$PATH:$ORACLE_HOME/lib
chmod +x /etc/profile.d/oracle.sh
/etc/profile.d/oracle.sh ##生效
ln -s /usr/include/oracle/12.2/client64 $ORACLE_HOME/include
ln -s /usr/lib/oracle/12.2/client64/lib/libclntsh.so.12.1 libclntsh.so.11.1 ##否则安装cx_Oracle会报错
检查环境是否配置完成:ls $ORACLE_HOME
d.下载cx_Oracle-5.1.2.tar.gz
tar -xzvf cx_Oracle-5.1.2.tar.gz
cd cx_Oracle-5.1.2
python setup.py install
e.验证
[root@lepus lepus]# python
Python 2.7.5 (default, Jul 13 2018, 13:06:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
2.登录被监控数据库创建监控账号
create user lepus_monitor identified by "root123"; grant connect,resource to lepus_monitor; grant select on v_$parameter to lepus_monitor ; grant select on v_$sysstat to lepus_monitor ; grant select on v_$instance to lepus_monitor ; grant select on v_$database to lepus_monitor ; grant select on product_component_version to lepus_monitor ; grant select on v_$session to lepus_monitor ; grant select on v_$dataguard_stats to lepus_monitor ; grant select on dba_data_files to lepus_monitor ; grant select on dba_free_space to lepus_monitor ;
3.Lepus oracle驱动验证
[root@lepus lepus]# python test_driver_oracle.py Oracle python drivier is ok!
4.登录监控平台,新建Oracle监控
5.监控平台执行验证
[root@lepus lepus]# python check_oracle.py 2018-11-26 13:34:15 [INFO] check oracle controller start. 2018-11-26 13:34:16 [INFO] check oracle controller finished. [root@lepus lepus]#
附上python连接Oracle测试脚本
方法一: import cx_Oracle dsn_tnsstr = cx_Oracle.makedsn('10.1.21.31','1521', 'pdb1') #dsn_tns = dsn_tnsstr.replace('SID', 'SERVICE_NAME') username="lepus_monitor" userpwd="root123" conn = cx_Oracle.connect(username,userpwd,dsn_tns) sql="select FILE_NAME,TABLESPACE_NAME from dba_data_files" curs = conn.cursor() result = curs.execute(sql) g = lambda k:"%-8s" % k i=1 for row in result.fetchmany(2): print str(i)+'\t', for k in map(g,row): print k+'\t', print i+=1 curs.close() 方法二: import cx_Oracle host="10.1.21.31" port="1521" dsn="pdb1" url=host+':'+port+'/'+dsn #dsn_tnsstr = cx_Oracle.makedsn('10.1.21.31','1521', 'pdb1') #dsn_tns = dsn_tnsstr.replace('SID', 'SERVICE_NAME') username="lepus_monitor" userpwd="root123" #conn = cx_Oracle.connect(username,userpwd,dsn_tns) conn=cx_Oracle.connect(username,userpwd,url) sql="select FILE_NAME,TABLESPACE_NAME from dba_data_files" curs = conn.cursor() result = curs.execute(sql) g = lambda k:"%-8s" % k i=1 for row in result.fetchmany(2): print str(i)+'\t', for k in map(g,row): print k+'\t', print i+=1 curs.close() quit()