189 lines
8.2 KiB
Python
Executable File
189 lines
8.2 KiB
Python
Executable File
#!/nmsmw/python/bin/python
|
|
|
|
import os
|
|
import sys
|
|
import re
|
|
import time
|
|
import datetime
|
|
import cx_Oracle
|
|
from multiprocessing import Pool
|
|
|
|
remote_ora = {}#<<FOLD<<
|
|
#remote_ora['gk'] = 'boya/boya@10.128.75.65:1521/orcl.ntars.com'
|
|
#remote_ora['gk'] = 'ibms/ibms@10.166.64.41:1521/orcl'
|
|
remote_ora['108'] = 'boya/boya@10.166.64.41:1521/orcl'
|
|
remote_ora['anhui'] = 'boya/boya@10.152.64.33:1521/orcl.idevelopment.info'
|
|
remote_ora['beijing'] = 'boya/boya@10.134.64.33:1521/orcl.ntars.com'
|
|
remote_ora['chongqing'] = 'boya/boya@10.137.64.33:1521/orcl'
|
|
remote_ora['fujian'] = 'boya/boya@10.162.64.33:1521/orcl.ntars.com'
|
|
remote_ora['gansu'] = 'boya/boya@10.142.64.33:1521/orcl'
|
|
remote_ora['guangdong'] = 'boya/boya@10.164.64.33:1521/orcl.ntars.com'
|
|
remote_ora['guangxi'] = 'boya/boya@10.161.64.33:1521/orcl'
|
|
remote_ora['guizhou'] = 'boya/boya@10.157.64.33:1521/orcl'
|
|
remote_ora['hainan'] = 'boya/boya@10.160.64.33:1521/orcl'
|
|
remote_ora['hebei'] = 'boya/boya@10.141.64.33:1521/orcl.ntars.com'
|
|
remote_ora['heilong'] = 'boya/boya@10.146.64.33:1521/orcl.ntars.com'
|
|
remote_ora['henan'] = 'boya/boya@10.151.64.33:1521/orcl.ntars.com'
|
|
remote_ora['hubei'] = 'boya/boya@10.155.64.33:1521/orcl.ntars.com'
|
|
remote_ora['hunan'] = 'boya/boya@10.158.64.33:1521/orcl.ntars.com'
|
|
remote_ora['jiangsu'] = 'boya/boya@10.150.64.33:1521/orclXDB.ntars.com'
|
|
remote_ora['jiangxi'] = 'boya/boya@10.159.64.33:1521/orcl.info'
|
|
remote_ora['jilin'] = 'boya/boya@10.147.64.33:1521/orcl'
|
|
remote_ora['liaoning'] = 'boya/boya@10.143.64.33:1521/orcl.ntars.info'
|
|
remote_ora['neimeng'] = 'boya/boya@10.135.64.33:1521/orcl'
|
|
remote_ora['ningxia'] = 'boya3w/boya3w@10.139.64.33:1521/orcl'
|
|
remote_ora['qinghai'] = 'boya/boya@10.144.64.33:1521/orcl.ntars.com'
|
|
remote_ora['shandong'] = 'boya/boya@10.154.64.33:1521/orcl.ntars.com'
|
|
remote_ora['shanghai'] = 'boya/boya@10.163.64.33:1521/orcl.ntars.com'
|
|
remote_ora['shanxi'] = 'boya/boya@10.140.64.33:1521/orcl.ntars.com'
|
|
remote_ora['sanxi'] = 'boya/boya@10.145.64.33:1521/orcl.ntars.com'
|
|
remote_ora['sichuan'] = 'boya/boya@10.138.64.33:1521/orcl.ntars.com'
|
|
remote_ora['tianjin'] = 'boya/boya@10.148.64.33:1521/orcl'
|
|
remote_ora['xinjiang'] = 'boya/boya@10.149.64.33:1521/orcl.ntars.com'
|
|
remote_ora['xizang'] = 'boya/boya@10.136.64.33:1521/orcl.ntars.com'
|
|
remote_ora['yunnan'] = 'boya/boya@10.156.64.33:1521/orcl'
|
|
remote_ora['zhejiang'] = 'boya/boya@10.153.64.33:1521/orcl.ntars.com'
|
|
|
|
remote_rac = {}
|
|
#remote_rac['gk'] = 'boya/boya@10.128.75.67:1521/orcl.ntars.com'
|
|
remote_rac['anhui'] = 'boya/boya@10.152.64.34:1521/orcl.idevelopment.info'
|
|
remote_rac['beijing'] = 'boya/boya@10.134.64.34:1521/orcl.ntars.com'
|
|
remote_rac['chongqing'] = 'boya/boya@10.137.64.34:1521/orcl'
|
|
remote_rac['fujian'] = 'boya/boya@10.162.64.34:1521/orcl.ntars.com'
|
|
remote_rac['gansu'] = 'boya/boya@10.142.64.34:1521/orcl'
|
|
remote_rac['guangdong'] = 'boya/boya@10.164.64.34:1521/orcl.ntars.com'
|
|
remote_rac['guangxi'] = 'boya/boya@10.161.64.34:1521/orcl'
|
|
remote_rac['guizhou'] = 'boya/boya@10.157.64.34:1521/orcl'
|
|
remote_rac['hainan'] = 'boya/boya@10.160.64.34:1521/orcl'
|
|
remote_rac['hebei'] = 'boya/boya@10.141.64.34:1521/orcl.ntars.com'
|
|
remote_rac['heilong'] = 'boya/boya@10.146.64.34:1521/orcl.ntars.com'
|
|
remote_rac['henan'] = 'boya/boya@10.151.64.34:1521/orcl.ntars.com'
|
|
remote_rac['hubei'] = 'boya/boya@10.155.64.34:1521/orcl.ntars.com'
|
|
remote_rac['hunan'] = 'boya/boya@10.158.64.34:1521/orcl.ntars.com'
|
|
remote_rac['jiangsu'] = 'boya/boya@10.150.64.34:1521/orclXDB.ntars.com'
|
|
remote_rac['jiangxi'] = 'boya/boya@10.159.64.34:1521/orcl.info'
|
|
remote_rac['jilin'] = 'boya/boya@10.147.64.34:1521/orcl'
|
|
remote_rac['liaoning'] = 'boya/boya@10.143.64.34:1521/orcl.ntars.info'
|
|
remote_rac['neimeng'] = 'boya/boya@10.135.64.34:1521/orcl'
|
|
remote_rac['ningxia'] = 'boya3w/boya3w@10.139.64.34:1521/orcl'
|
|
remote_rac['qinghai'] = 'boya/boya@10.144.64.34:1521/orcl.ntars.com'
|
|
remote_rac['shandong'] = 'boya/boya@10.154.64.34:1521/orclXDB.ntars.com'
|
|
remote_rac['shanghai'] = 'boya/boya@10.163.64.34:1521/orcl.ntars.com'
|
|
remote_rac['shanxi'] = 'boya/boya@10.140.64.34:1521/orcl.ntars.com'
|
|
remote_rac['sanxi'] = 'boya/boya@10.145.64.34:1521/orcl.ntars.com'
|
|
remote_rac['sichuan'] = 'boya/boya@10.138.64.34:1521/orcl.ntars.com'
|
|
remote_rac['tianjin'] = 'boya/boya@10.148.64.34:1521/orcl'
|
|
remote_rac['xinjiang'] = 'boya/boya@10.149.64.34:1521/orcl.ntars.com'
|
|
remote_rac['xizang'] = 'boya/boya@10.136.64.34:1521/orcl.ntars.com'
|
|
remote_rac['yunnan'] = 'boya/boya@10.156.64.34:1521/orcl'
|
|
remote_rac['zhejiang'] = 'boya/boya@10.153.64.34:1521/orcl.ntars.com'
|
|
#>>FOLD>>
|
|
|
|
vds_tables = {}#<<FOLD<<
|
|
vds_tables['VDSRPT_DOMAIN_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_PROGRAM_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_SIP_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_URL_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_VIRUSFALIMY_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_VIRUSTYPE_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_VIRUSTYPE_VISIT'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_VIRUS_DOWNLOAD'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_VIRUS_NEW'] = 'VDS_REPORT_SPACE'
|
|
vds_tables['VDSRPT_VIRUS_VISIT'] = 'VDS_REPORT_SPACE'
|
|
#>>FOLD>>
|
|
|
|
def StringToTime(strtime):#<<FOLD<<
|
|
t_tuple = time.strptime(str(strtime),"%Y%m%d")
|
|
return int(time.mktime(t_tuple))
|
|
#>>FOLD>>
|
|
|
|
def gen_sqls(operate, date):#<<FOLD<<
|
|
tables = vds_tables
|
|
sqls = []
|
|
if 'add' == operate:
|
|
date1 = StringToTime(date)
|
|
for table in tables:
|
|
sqls.append('alter table %s add partition PART_%s values less than(%s) tablespace %s'%(table, date/10000, date1, tables[table]))
|
|
elif 'drop' == operate:
|
|
for table in tables:
|
|
sqls.append('alter table %s drop partition PART_%s'%(table, date/10000))
|
|
else:
|
|
print 'Wrong operation on table partitions, now quit.'
|
|
return
|
|
return sqls
|
|
#>>FOLD>>
|
|
|
|
def ora_init(ora):#<<FOLD<<
|
|
if ora in remote_ora.keys():
|
|
#print ' ------>', ora, '<------'
|
|
try:
|
|
db = cx_Oracle.connect(remote_ora[ora])
|
|
db.autocommit = True
|
|
#print 'Connect to oracle server successfully ...'
|
|
except:
|
|
if ora in remote_rac.keys():
|
|
#print 'Time out, now try to connect the rac ...'
|
|
try:
|
|
db = cx_Oracle.connect(remote_rac[ora])
|
|
db.autocommit = True
|
|
#print 'Connect to oracle server successfully ...'
|
|
except:
|
|
#print 'Error, Failed to connect oracle', ora
|
|
return
|
|
else:
|
|
#print 'Error, Failed to connect oracle', ora
|
|
return
|
|
else:
|
|
#print 'Error, Wrong oracle', ora
|
|
return
|
|
return db
|
|
#>>FOLD>>
|
|
|
|
def operate_partitions(ora):#<<FOLD<<
|
|
db = ora_init(ora)
|
|
if not db:
|
|
print '\n\033[31m %s Connect failed.\033[0m'%ora
|
|
return (ora, 'Connect failed')
|
|
cur = db.cursor()
|
|
date = (int(sys.argv[2])*100+1)*100+1
|
|
sqls = gen_sqls(sys.argv[1], date)
|
|
for sql in sqls:
|
|
try:
|
|
cur.execute(sql)
|
|
sys.stdout.write('.')
|
|
sys.stdout.flush()
|
|
except Exception, e:
|
|
matched = 0
|
|
ignore_errors = ['ORA-00942', 'ORA-02149']
|
|
for error in ignore_errors:
|
|
if error in str(e).upper():
|
|
matched = 1
|
|
sys.stdout.write('.')
|
|
sys.stdout.flush()
|
|
break
|
|
if not matched:
|
|
print "\n\033[31mRUN %s ON %s failed:%s\033[0m"%(sql, ora, e)
|
|
cur.close()
|
|
db.close()
|
|
print '\n\033[32m %s Complete.\033[0m'%ora
|
|
return (ora, 'Complete')
|
|
#>>FOLD>>
|
|
|
|
def main():#<<FOLD<<
|
|
if 0 == len(sys.argv[3:]):
|
|
oras = remote_ora.keys()
|
|
else:
|
|
oras = sys.argv[3:]
|
|
process_pool = Pool(8)
|
|
result = process_pool.map_async(operate_partitions, oras)
|
|
for res in result.get():
|
|
print res
|
|
#>>FOLD>>
|
|
|
|
if __name__ == '__main__':
|
|
if 2 < len(sys.argv) and sys.argv[2].isdigit():
|
|
main()
|
|
else:
|
|
print 'Usage: %s {add|drop} 2017 [pc1, pc2 ...]'%sys.argv[0]
|
|
|