标签:ibm node 数据源 system tran connect color test 解析
近期梳理Weblogic数据源,数据源较多,但是每一个数据源在weblogic中是xml方式存在,所以想到批量解析xml,把数据放到数据库后来解决。
需要的数据源信息:
WEBLOGIC_HOST
JDBC_NAME
JNDI_NAME
DB_ALIAS
DB_NAME
DB_HOST
INST_PORT
DB_USER
TARGET
URL
DRIVER_TYPE
CAPACITY
数据源xml格式:
<?xml version=‘1.0‘ encoding=‘UTF-8‘?> <jdbc-data-source> <name>jdjd</name> <jdbc-driver-params> <url>jdbc:db2://10.10.112.166:50000/MYDB</url> <driver-name>com.ibm.db2.jcc.DB2Driver</driver-name> <properties> <property> <name>user</name> <value>myuser</value> </property> <property> <name>driverType</name> <value>4</value> </property> <property> <name>databaseName</name> <value>MYDB</value> </property> </properties> <password-encrypted>{AES}sdfhjhsafhj=</password-encrypted> </jdbc-driver-params> <jdbc-connection-pool-params> <test-table-name>SQL SELECT COUNT(*) FROM SYSIBM.SYSTABLES</test-table-name> </jdbc-connection-pool-params> <jdbc-data-source-params> <jndi-name>jdjd</jndi-name> <global-transactions-protocol>OnePhaseCommit</global-transactions-protocol> </jdbc-data-source-params> </jdbc-data-source>
解析xml,取所需信息放入mysql数据库:
#!/usr/local/bin/python2.7 # encoding: utf-8 import xml.etree.ElementTree as ET import mysql.connector as cnn import os cnx = cnn.connect(host=‘192.168.2.180‘,user=‘root‘,password=‘123456‘,database=‘jdbc‘) file_list = os.listdir(‘C:\Users\Administrator\Desktop\jdbc‘) for jdbc_file in file_list: file_abs = os.path.join(‘C:\Users\Administrator\Desktop\jdbc‘,jdbc_file) print "开始处理:",file_abs tree = ET.parse(file_abs) #根节点jdbc-data-source root = tree.getroot() #name 查找元素 jdbc_name_tag = root.find(‘name‘) jdbc_name = jdbc_name_tag.text #print jdbc_name_tag.tag,jdbc_name #jdbc-data-source-params jdbc_data_source = root.find(‘jdbc-data-source-params‘) jndi_name_tag = jdbc_data_source.find(‘jndi-name‘) jndi_name = jndi_name_tag.text #print jndi_name_tag.tag,jndi_name #jdbc-driver-params jdbc_driver_tag = root.find(‘jdbc-driver-params‘) #db,user property_tag = jdbc_driver_tag.find(‘properties‘).findall(‘property‘) i = 1 for property in property_tag: if property.find(‘name‘).text == ‘user‘: dbuser = property.find(‘value‘).text elif property.find(‘name‘).text == ‘databaseName‘: dbname = property.find(‘value‘).text elif property.find(‘name‘).text == ‘driverType‘: dbtype = int(property.find(‘value‘).text) #print property.find(‘name‘).text,property.find(‘value‘).text #url url_tag = jdbc_driver_tag.find(‘url‘) url = url_tag.text #print url_tag.tag,url #max-capacity max_capacity_tag = root.find(‘jdbc-connection-pool-params‘).find(‘max-capacity‘) try: max_capacity = max_capacity_tag.text #print max_capacity_tag.tag,max_capacity except AttributeError: print "NO tag max_capacity" max_capacity = ‘‘ #cnx = cnn.connect(host=‘10.10.112.168‘,user=‘root‘,password=‘123456‘,database=‘jdbc‘) cursor = cnx.cursor() state = ("insert into weblogic_jdbc_999 (WEBLOGIC_HOST,JDBC_NAME,JNDI_NAME,DB_ALIAS,DB_USER,URL,CAPACITY) values (%s,%s,%s,%s,%s,%s,%s)") jdbc_data = (‘10.10.112.168‘,jdbc_name,jndi_name,dbname,dbuser,url,max_capacity) cursor.execute(state,jdbc_data) cursor.close() cnx.commit() cnx.close()
数据源所属weblogic服务器实例在另外一个xml(config.xml)中,也需要解析,然后更新数据。
import xml.etree.ElementTree as ET import mysql.connector as cnn tree = ET.parse(‘/root/Python_shell/config68.xml‘) root = tree.getroot() list_node = root.findall(‘jdbc-system-resource‘) cnx = cnn.connect(host=‘127.0.0.1‘,user=‘root‘,password=‘123456‘,database=‘jdbc‘) cursor = cnx.cursor() state = ("update weblogic_jdbc_999 set TARGET=%s where JDBC_NAME=%s") for i in list_node: jdbc_name = i.find(‘name‘).text server_target = i.find(‘target‘).text updata = (server_target,jdbc_name) cursor.execute(state,updata) cursor.close() cnx.commit() cnx.close()
但是出现一个问题:db2连接,一种是直接url连接,一种java利用db2客户端编目后连接:
需要把db2 node和db编目导入数据库:
node取4个值,然后转一行,手动导入数据库:
db2 list node directory | awk NF | egrep -i "Node|Protocol|Hostname|Service" | awk ‘!/entry|Directory/ {print $NF}‘ | awk ‘{printf $0","} NR%4==0 {print "10.10.112.168"}‘
WEBLOGIC_HOST
NODE_NAME
COMMENT
DIRECTORY
PROTOCOL
DB_HOST
INST_PORT
db取3个值,然后转一行,手动导入数据库:
db2 list db directory | awk NF | egrep -i ‘alias|Database name|Node‘ | awk ‘{print $NF}‘ | awk ‘{printf $0","} NR%3==0 {print "10.10.112.168"}‘
WEBLOGIC_HOST
DB_ALIAS
DB_NAME
NODE_NAME
这样一共三张表:可以按需筛选所需信息。
SELECT a.WEBLOGIC_HOST, a.JDBC_NAME, a.JNDI_NAME, a.DB_ALIAS, db.DB_NAME, nd.DB_HOST, nd.INST_PORT, a.DB_USER, a.TARGET, a.URL FROM weblogic_jdbc a LEFT JOIN db2_catalog_db db ON ( a.WEBLOGIC_HOST = db.WEBLOGIC_HOST AND a.DB_ALIAS = db.DB_ALIAS ) LEFT JOIN db2_catalog_node nd ON ( db.WEBLOGIC_HOST = nd.WEBLOGIC_HOST AND db.NODE_NAME = nd.NODE_NAME )
更新表信息:
UPDATE weblogic_jdbc, db2_catalog_db SET weblogic_jdbc.DB_NAME = db2_catalog_db.DB_NAME WHERE weblogic_jdbc.WEBLOGIC_HOST = db2_catalog_db.WEBLOGIC_HOST AND weblogic_jdbc.DB_ALIAS = db2_catalog_db.DB_ALIAS AND weblogic_jdbc.DB_NAME IS NULL; UPDATE weblogic_jdbc, db2_catalog_db, db2_catalog_node SET weblogic_jdbc.DB_HOST = db2_catalog_node.DB_HOST, weblogic_jdbc.INST_PORT = db2_catalog_node.INST_PORT WHERE weblogic_jdbc.WEBLOGIC_HOST = db2_catalog_db.WEBLOGIC_HOST AND weblogic_jdbc.DB_ALIAS = db2_catalog_db.DB_ALIAS AND db2_catalog_db.NODE_NAME = db2_catalog_node.NODE_NAME AND db2_catalog_db.WEBLOGIC_HOST = db2_catalog_node.WEBLOGIC_HOST AND weblogic_jdbc.DB_HOST IS NULL AND weblogic_jdbc.INST_PORT IS NULL;
自己写完代码后感觉太烂,一行一行命令堆积起来的,哈哈,先解决问题再说。
标签:ibm node 数据源 system tran connect color test 解析
原文地址:http://www.cnblogs.com/jjzd/p/7252664.html