#!/usr/bin/env python
##!/bin/env python
import re
import os
#Get the deploy temporary directory path
#deploy_conf="/tmp/deploy.conf"
#f=open(deploy_conf,'r')
#tmp_dir=f.readline().strip()
#print tmp_dir
#f.close()
db2_instance_name=''
#convert any validate type of capacity string to the unit of bytes
def convertCapToBytes(capacity):
cap_src=capacity.upper()
cap_dest=0
tmp=0
if re.match(r'^([1-9]\d*\.\d*|0\.\d*[1-9]\d*|\d+)[G][B]?$',cap_src):
tmp=float(re.sub('[G][B]?','',cap_src))
cap_dest=tmp*1024*1024*1024
elif re.match(r'^([1-9]\d*\.\d*|0\.\d*[1-9]\d*|\d+)[M][B]?$',cap_src):
tmp=float(re.sub('[M][B]?','',cap_src))
cap_dest=tmp*1024*1024
elif re.match(r'^([1-9]\d*\.\d*|0\.\d*[1-9]\d*|\d+)[K][B]?$',cap_src):
tmp=float(re.sub('[K][B]?','',cap_src))
cap_dest=tmp*1024
elif re.match(r'^([1-9]\d*\.\d*|0\.\d*[1-9]\d*|\d+)[B]?$',cap_src):
tmp=float(re.sub('[B]?','',cap_src))
cap_dest=tmp
else:
#illegal capacity string
pass
return int(cap_dest)
#print convertCapToBytes('1g')
#print convertCapToBytes('1.5g')
#print convertCapToBytes('1.5m')
#print convertCapToBytes('1k')
#print convertCapToBytes('10002123')
#calculate the pagecount accord to the pagesize and capacity
def calcPageCount(capacity,pagesize):
#convert the capacity to the unit of bytes
tmp_capacity=convertCapToBytes(capacity)
#convert the pagesize to the unit of bytes
tmp_pagesize=convertCapToBytes(pagesize)
#pagecount=capacity/pagesize, assume that capacity and pagesize has the same unit
return tmp_capacity/tmp_pagesize
#print calcPageCount('1g','16k')
#print calcPageCount('1.5g','16k')
#check whether a string match a re pattern
def checkStrValidation(str,pattern):
if re.match(str,pattern):
return 0
else:
return 1
#check pagesize string for validation
def checkPGValidation(str):
if re.match(r'^(4096|8192|16384|32768|(4|8|16|32)[kK][bB]?)$',str):
return 0
else:
return 1
#check Capacity string for validation
def checkCapValidation(str):
if re.match(r'^([1-9]\d*\.\d*|0\.\d*[1-9]\d*|\d+)[kKmMgG]?[Bb]?$',str):
return 0
else:
return 1
#check TS_TYPE string for validation
def checkTSTYPEValidation(str):
if re.match(r'^(re|RE|st|ST|ut|UT)$',str):
return 0
else:
return 1
#check S_TYPE string for validation
def checkSTYPEValidation(str):
if re.match(r'^(sms|SMS|dms|DMS)$',str):
return 0
else:
return 1
#Check each database configuration file validation
def checkConfigValidation(db_properties_list,bp_list,ts_list):
#Return: 0 -- pass 1 -- failed
#Each db config must have 3 sections: DB_PROPERTIES,BP_DEFINITIONS,TS_DEFINITIONS
#echo db config child section must satisfy corresponding conditions
#DB_PROPERTIES section:
# format: key=value
# one property per line
# mandatory: dbname,codeset,territory,pagesize,db_app_username,db2_instance_name
# dbname:validate name and length <= 8 , '^[_a-zA-Z][_a-zA-Z0-9]{1,7}$'
# codeset:UTF-8,GBK,...
# territory:CN
# pagesize:match '^(4096|8192|16384|32768|(4|8|16|32)[kK][bB]?)$'
# db_app_username: the app database user
# db2_instance_name: db2 instance user
#
#BP_DEFINITIONS section:
# format: BP_NAME ID TS_LIST BP_PAGESIZE CAPACITY
# mandatory: the five fields are all mandatory properties
# BP_NAME: can not be null
# ID: can not be null even not the right id
# TS_LIST: can not be null, tablespace name are concated by ',', eg:ts1,ts2,ts3
# BP_PAGESIZE: match '^(4096|8192|16384|32768|(4|8|16|32)[kK][bB]?)$'
# CAPACITY: match '^([1-9]\d*\.\d*|0\.\d*[1-9]\d*|\d+)[kKmMgG][Bb]?$'
#
#TS_DEFINITIONS section:
# format: TS_NAME ID FILE_PATH TS_PAGESIZE CAP TS_TYPE S_TYPE
# mandatory: the seven fields are all mandatory properties
# TS_NAME: can not be null
# ID: can not be null even not the right id
# FILE_PATH: validate file path, match '^(/\w+)+$'
# TS_PAGESIZE: match '^(4096|8192|16384|32768|(4|8|16|32)[kK][bB]?)$'
# CAP: match '^([1-9]\d*\.\d*|0\.\d*[1-9]\d*|\d+)[kKmMgG][Bb]?$'
# TS_TYPE: match '^(re|RE|st|ST|ut|UT)$'
# S_TYPE: match '^(sms|SMS|dms|DMS)$'
#
db_properties_dict={}
bp_list_array=[]
ts_list_array=[]
#errflag -- 0 : no errors, 1 : has errors
errflag_dbp = 0
errflag_bp = 0
errflag_ts = 0
errcount_dbp = 0
errcount_bp = 0
errcount_ts = 0
#Get DB properties Configuration into a dict
for line in db_properties_list:
l=line.split('=')
db_properties_dict[l[0]]=l[1]
#print db_properties_list
#convert bufferpool list table to array, for easy handle
bp_list_array=[map(str,bl.split()) for bl in bp_list]
#conver tablespace list table to array, for esay handle
ts_list_array=[map(str,tl.split()) for tl in ts_list]
#Reformat the tablespace creation configuration, add the bufferpool to the ts_list_array
for i in range(len(ts_list_array)):
for j in range(len(bp_list_array)):
if ts_list_array[i][0] in bp_list_array[j][2].split(','):
ts_list_array[i].append(bp_list_array[j][0])
#Check DB_PROPERTIES Section
dpd_list = db_properties_dict.keys()
#print dpd_list
if 'dbname' not in dpd_list:
print "Find errors in DB_PROPERTIES_SECTION: dbname property not found."
errflag_dbp = 1
errcount_dbp+=1
else:
#if re.match(r'^[_a-zA-Z][_a-zA-Z0-9]{1,7}$',db_properties_dict['dbname']):
if len(db_properties_dict['dbname'])>8:
#print "Find errors in DB_PROPERTIES_SECTION: dbname value illegal"
#print "validate pattern: '^[_a-zA-Z][_a-zA-Z0-9]{1,7}$'"
print "Find errors in DB_PROPERTIES_SECTION: dbname length should be no greater then 8"
errflag_dbp = 1
errcount_dbp+=1
pass
if 'codeset' not in dpd_list:
print "Find errors in DB_PROPERTIES_SECTION: codeset property not found."
errflag_dbp = 1
errcount_dbp+=1
else:
pass
if 'territory' not in dpd_list:
print "Find errors in DB_PROPERTIES_SECTION: territory property not found."
errflag_dbp = 1
errcount_dbp+=1
else:
pass
if 'pagesize' not in dpd_list:
print "Find errors in DB_PROPERTIES_SECTION: pagesize property not found."
errflag_dbp = 1
errcount_dbp+=1
else:
if checkPGValidation(db_properties_dict['pagesize']) == 1:
print "Find errors in DB_PROPERTIES_SECTION: pagesize value illegal"
print "pagesize validate pattern: '^(4096|8192|16384|32768|(4|8|16|32)[kK][bB]?)$'"
errflag_dbp = 1
errcount_dbp+=1
else:
pass
pass
if 'db_app_username' not in dpd_list:
print "Find errors in DB_PROPERTIES_SECTION: db_app_username property not found."
errflag_dbp = 1
errcount_dbp+=1
else:
pass
if 'db2_instance_name' not in dpd_list:
print "Find errors in DB_PROPERTIES_SECTION: db2_instance_name property not found."
errflag_dbp = 1
errcount_dbp+=1
else:
pass
if errflag_dbp == 0:
print "DB_PROPERTIES_SECTION check pass."
else:
print "DB_PROPERTIES_SECTION find %d errors"%errcount_dbp
#Generate ts_name_list from TS_DEFINTION_SECTION
ts_name_list=[]
for ts in ts_list_array:
ts_name_list.append(ts[0])
#print ts_name_list
#Check BP_DEFINITIONS section
has_default_bp=0
for bp in bp_list_array:
if len(bp) != 5:
errflag_bp = 1
errcount_bp+=1
print "BP_DEFINITIONS_SECTION find errors"
print 'The list line just below does not
评论1
最新资源