37,719
社区成员
发帖
与我相关
我的任务
分享
# -*- coding:utf-8 -*-
# -*- author:chenyingzi -*-
# -*- descrpiton -*-
import configparser
import time, sched, os
import threading
import random
import cx_Oracle
class HostInfo(object):
'''
读取配置文件db.conf信息,根据传入的section,返回section中各参数的参数值
a=hostinfo('dbinfo1')
a.hostip则返回IP地址192.168.1.200
'''
def __init__(self, section,cf):
self.section = section
if cf.has_option(section, 'hostip'):
self.hostip = cf.get(section, 'hostip')
else:
self.hostip = ''
if cf.has_option(section, 'dbtype'):
self.dbtype = cf.get(section, 'dbtype')
else:
self.dbtype = 'Oracle'
if cf.has_option(section, 'port'):
self.port = int(cf.get(section, 'port'))
else:
self.port = 1521
if cf.has_option(section, 'version'):
self.version = cf.get(section, 'version')
else:
self.version = ''
if cf.has_option(section, 'rac'):
self.rac = cf.get(section, 'rac')
else:
self.rac = False
if cf.has_option(section, 'service_name'):
self.service_name = cf.get(section, 'service_name')
else:
self.service_name = ''
if cf.has_option(section, 'querylistfile'):
self.querylistfile = cf.get(section, 'querylistfile')
else:
self.querylistfile = ''
if cf.has_option(section, 'user'):
self.user = cf.get(section, 'user')
else:
self.user = ''
if cf.has_option(section, 'password'):
self.password = cf.get(section, 'password')
else:
self.password = ''
if cf.has_option(section, 'tnsname'):
self.tnsname = cf.get(section, 'tnsname')
else:
self.tnsname = ''
if cf.has_option(section, 'maxactive'):
self.maxactive = int(cf.get(section, 'maxactive'))
else:
self.maxactive = 5
if cf.has_option(section, 'maxwait'):
self.maxwait = int(cf.get(section, 'maxwait'))
else:
self.maxwait = 100
if cf.has_option(section, 'maxidle'):
self.maxidle = int(cf.get(section, 'maxidle'))
else:
self.maxidle = 1
def hostdic(self):
self.hostdic = {}
for option in cf.options(section):
self.hostdic[option] = cf[section][option]
return self.hostdic
def __str__(self):
return 'The DB detail as followed:' + \
'\nSection : ' + self.section +\
'\nHostIP : ' + self.hostip + \
'\nDBType : ' + self.dbtype + \
'\nRAC : ' + self.rac + \
'\nPort : ' + str(self.port) + \
'\nVersion : ' + self.version + \
'\nSrv_Name : ' + self.service_name + \
'\nUser : ' + self.user + \
'\nPassword : ' + self.password + \
'\nQueryFile: ' + self.querylistfile + \
'\nMaxActive: ' + str(self.maxactive) + \
'\nMaxWait : ' + str(self.maxwait) + \
'\nMaxIdle : ' + str(self.maxidle)
class QueryInfo(object):
def __init__(self, section,qf):
self.section = section
if qf.has_option(section, 'sqlstr'):
self.sqlstr = qf.get(section, 'sqlstr')
else:
print('No sql defined for section: '%section)
if qf.has_option(section, 'period'):
self.period = int(qf.get(section, 'period'))
else:
self.period = 300
if qf.has_option(section, 'priority'):
self.priority = int(qf.get(section, 'priority'))
else:
self.priority = random.randint(999,99999)
if qf.has_option(section, 'nodatafound'):
self.nodatafound = qf.get(section, 'nodatafound')
else:
self.nodatafound =''
def querydic(self):
self.dic = {}
for option in cf.options(section):
self.querydic[option] = qf[section][option].upper()
return self.querydic
def __str__(self):
return 'The Query detail as followed:' + \
'\nSqlstr : ' + self.sqlstr + \
'\nPeriod : ' + str(self.period) + \
'\nNoDataFound : ' + str(self.nodatafound)
def perform(hostip,sql,s):
print('IP: %s\nsqlname: %s\n sqlstr: %s\n period: %d \n priority: %d \ntime: %d\n' % (hostip, sql.section, sql.sqlstr, sql.period, sql.priority,time.time()))
s.enter(sql.period, sql.priority, perform, (hostip,sql,s,))
def hello3(hostip,querycfg):
print(threading.current_thread().name)
s = sched.scheduler(time.time, time.sleep)
for sqlsec in querycfg.sections():
sql = QueryInfo(sqlsec,querycfg)
s.enter(0, sql.period, perform, (hostip,sql,s,))
s.run()
#初始化ConfigParser类
hostcfg = configparser.ConfigParser()
#读取被控服务器列表配置文件
hostcfg.read('db.conf')
# #循环配置文件section
# 初始化ConfigParser类
querycfg = configparser.ConfigParser()
for section in hostcfg.sections():
host = HostInfo(section,hostcfg)
# 读取sql脚本文件
querycfg.read(host.querylistfile)
# print(querycfg)
#生成线程,处理sql语句
t1 = threading.Thread(target=hello3,args=(host.section,querycfg,),name='Thread for '+host.section)
t1.start()
t1.join()