def tick_to_check_cert_online(): print('tick_to_check_cert_online now active' + str(datetime.datetime.now())) cert_collect = [] dic = {} if record_mode == "0": tb_vip_certification_list = db_test.read_information_from_temp(2) elif record_mode == "1": tb_vip_certification_list = db_test.DB_select_connect( "select * from dfh.tb_vip_certification") for r in tb_vip_certification_list: if int(r[4]) >= 2: cert_collect.append(r[3]) elif int(r[4]) < 2: if r[1] == 'slogin': logout(r[3]) else: if (len(find_cert(r[1], r[2])) > 0): #logout(find_cert(r[1],r[2])) logout(r[3]) DB_log("DEBUG", "tick_to_check_cert_online force logout", "id:" + r[1] + ",pwd:" + r[2] + ",cert:" + r[3]) if len(cert_collect) > 0: for item in cert_collect: dic.update({'login_cert': item}) #update cert to 1 db_test.write_temp(dic, 2, 'set')
def del_vip(dic_, write_file_TF): if write_file_TF: txb_sn = db_test.key_exist_in_object('txb_sn', dic_) txb_ip = db_test.key_exist_in_object('txb_ip', dic_) txb_ver = db_test.key_exist_in_object('txb_ver', dic_) txb_area = db_test.key_exist_in_object('txb_area', dic_) curr_link = db_test.key_exist_in_object('curr_link', dic_) txb_max_link = db_test.key_exist_in_object('txb_max_link', dic_) txb_pro = db_test.key_exist_in_object('txb_pro', dic_) txb_act = db_test.key_exist_in_object('txb_act', dic_) og_txb_ip = db_test.key_exist_in_object('og_txb_ip', dic_) og_txb_ver = db_test.key_exist_in_object('og_txb_ver', dic_) og_txb_area = db_test.key_exist_in_object('og_txb_area', dic_) if record_mode == "0": # update file db_test.write_temp(dic_, 0, "del") elif record_mode == "1": if len(og_txb_ip) > 0: sqlcmd = "delete from dfh.tb_vip_relation where ip = \'" + txb_ip + "\'" db_test.DB_update_connect(sqlcmd) if len(og_txb_ver) > 0: sqlcmd = "update dfh.tb_vip_relation set version = \'\',last_update = timezone(\'CCT\'::text, now()) where version = \'" + txb_ver + "\'" db_test.DB_update_connect(sqlcmd) if len(og_txb_area) > 0: sqlcmd = "update dfh.tb_vip_relation set area = \'\',last_update = timezone(\'CCT\'::text, now()) where area = \'" + txb_area + "\'" db_test.DB_update_connect(sqlcmd) db_test.write_config(dic_) DB_log('DEBUG', 'del_vip', "==== del_vip complete ====")
def vip_distribution(ver_, area_): group_vip_list = [] lowest = 999 vip_lowest = "" if record_mode == "0": tb_vip_status_list = db_test.read_information_from_temp(0) tb_vip_relation_list = db_test.read_information_from_temp(1) elif record_mode == "1": tb_vip_status_list = DB_select_connect( "select * from dfh.tb_vip_status") tb_vip_relation_list = DB_select_connect( "select * from dfh.tb_vip_relation") for r in tb_vip_relation_list: if r[2] == ver_ and r[3] == area_ and r[6] == 'T': group_vip_list.append(r[1]) #find lowest dic_ = {} for r in group_vip_list: for rr in tb_vip_status_list: if r == rr[0]: if int(rr[1]) < int(lowest): lowest = int(rr[1]) vip_lowest = rr[0] #curr_link = lowest+1#new login vip distribution, so count +1 curr_link = lowest #20171108 count +0 dic_.update({'txb_ip': vip_lowest}) dic_.update({'curr_link': str(curr_link)}) if vip_distribution_lock.acquire(1): db_test.write_temp(dic_, 0, 'update_relation') vip_distribution_lock.release() #load>80%, 20151224 # curr_link/nax_link return vip_lowest
def set_config(dic_, write_file_TF): if write_file_TF: txb_sn = db_test.key_exist_in_object('txb_sn', dic_) txb_ip = db_test.key_exist_in_object('txb_ip', dic_) txb_ver = db_test.key_exist_in_object('txb_ver', dic_) txb_area = db_test.key_exist_in_object('txb_area', dic_) curr_link = db_test.key_exist_in_object('curr_link', dic_) txb_max_link = db_test.key_exist_in_object('txb_max_link', dic_) txb_pro = db_test.key_exist_in_object('txb_pro', dic_) txb_act = db_test.key_exist_in_object('txb_act', dic_) og_txb_ip = db_test.key_exist_in_object('og_txb_ip', dic_) og_txb_ver = db_test.key_exist_in_object('og_txb_ver', dic_) og_txb_area = db_test.key_exist_in_object('og_txb_area', dic_) if record_mode == "0": # update file db_test.write_temp(dic_, 0, 'set') # 0 : Overwrite status db_test.write_temp(dic_, 1, 'set') # 1 : Overwrite relation elif record_mode == "1": # update postgraSQL if len(og_txb_ip) > 0: sqlcmd = "update dfh.tb_vip_relation set ip = \'" + txb_ip + "\',last_update = timezone(\'CCT\'::text, now()) where ip = \'" + og_txb_ip + "\'" db_test.DB_update_connect(sqlcmd) if len(og_txb_ver) > 0: sqlcmd = "update dfh.tb_vip_relation set version = \'" + txb_ver + "\',last_update = timezone(\'CCT\'::text, now()) where version = \'" + og_txb_ver + "\'" db_test.DB_update_connect(sqlcmd) if len(og_txb_area) > 0: sqlcmd = "update dfh.tb_vip_relation set area = \'" + txb_area + "\',last_update = timezone(\'CCT\'::text, now()) where area = \'" + og_txb_area + "\'" db_test.DB_update_connect(sqlcmd) db_test.write_config(dic_) DB_log('DEBUG', 'broker_set_config', '==== set_config complete ====')
def count_vip_socket(vip, og_curr_link, int_): curr_link_result = 0 viplist = vip.split(':') try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((viplist[0], 4300)) #60.250.174.141:4300 sock.settimeout(10) #sock.connect(('192.168.12.112', 4300)) if int_ == 73: sock.send(chr(0x73).encode('ascii')) data = sock.recv(32) curr_link = int.from_bytes(data[2:3], byteorder='little') elif int_ == 75: #version sock.send(chr(0x75).encode('ascii')) data = sock.recv(32) ver1 = int.from_bytes(data[2:3], byteorder='little') ver2 = int.from_bytes(data[3:4], byteorder='little') ver3 = int.from_bytes(data[4:5], byteorder='little') ver4 = int.from_bytes(data[5:6], byteorder='little') elif int_ == 98: #sock.connect(('192.168.12.122', 4300)) sock.send(chr(98).encode('ascii')) data = sock.recv(32) curr_link = int.from_bytes(data[8:9], byteorder='little') #print(curr_link) DB_log('DEBUG', 'count_vip_socket sock.recv', str(data)) sock.close() except: curr_link = 0 DB_log('ERROR', 'count_vip_socket', viplist[0] + " socket fail") #if int(og_curr_link) >= curr_link: #curr_link_result = int(og_curr_link) #else:#curr_link > og_curr_link curr_link_result = curr_link if record_mode == "0": # update file dic = {} dic.update({'txb_ip': vip}) dic.update({'curr_link': curr_link_result}) if count_vip_socket_lock.acquire(1): db_test.write_temp(dic, 0, 'update_relation') # 0 = status count_vip_socket_lock.release() elif record_mode == "1": # update DB sqlcmd = "update dfh.tb_vip_status set curr_link = \'" + str( curr_link_result ) + "\',last_update = timezone(\'CCT\'::text, now()) where ip = \'" + str( vip) + "\'" DB_update_connect(sqlcmd) DB_log( "DEBUG", "tick_to_update_curr_link", "ip:" + vip + ",og_curr_link:" + str(og_curr_link) + ",curr_link:" + str(curr_link))
def mod_list(dic_, write_file_TF): if write_file_TF: txb_sn = db_test.key_exist_in_object('txb_sn', dic_) txb_ip = db_test.key_exist_in_object('txb_ip', dic_) txb_ver = db_test.key_exist_in_object('txb_ver', dic_) txb_area = db_test.key_exist_in_object('txb_area', dic_) curr_link = db_test.key_exist_in_object('curr_link', dic_) txb_max_link = db_test.key_exist_in_object('txb_max_link', dic_) if txb_ver.split(',')[0] == 'Thin': txb_pro = 'D' else: txb_pro = 'S' dic_.update({'txb_pro': txb_pro}) #txb_pro = db_test.key_exist_in_object('txb_pro',dic_) txb_act = db_test.key_exist_in_object('txb_act', dic_) if record_mode == "0": db_test.write_temp(dic_, 0, 'update_relation') # 0 : Overwrite status db_test.write_temp(dic_, 1, 'update_relation') # 1 : Overwrite relation elif record_mode == "1": sqlcmd = 'update dfh.tb_vip_relation set ' if len(txb_ver) > 0: sqlcmd = sqlcmd + 'version = \'' + txb_ver + '\',' if len(txb_area) > 0: sqlcmd = sqlcmd + 'area = \'' + txb_area + '\',' if txb_max_link != "--" and len(txb_max_link) > 0: sqlcmd = sqlcmd + 'max_link = \'' + txb_max_link + '\',' if txb_pro != "--" and len(txb_pro) > 0: sqlcmd = sqlcmd + 'pro = \'' + txb_pro + '\',' if txb_act != "--" and len(txb_act) > 0: sqlcmd = sqlcmd + 'act = \'' + txb_act + '\',' sqlcmd = sqlcmd + 'last_update = timezone(\'CCT\'::text, now()) where sn =\'' + txb_sn + '\';' db_test.DB_update_connect(sqlcmd) DB_log('DEBUG', 'mod_list', '==== mod_list complete ====') if record_mode == "0": results = db_test.read_information_from_temp( 1) #0: tb_vip_status, 1: tb_vip_relation elif record_mode == "1": sqlcmd = 'select sn,ip,version,area,max_link,pro,act,last_update from dfh.tb_vip_relation ORDER BY sn;' results = db_test.DB_select_connect(sqlcmd) return results
def add_vip(dic_, write_file_TF): if write_file_TF: txb_sn = db_test.key_exist_in_object('txb_sn', dic_) txb_ip = db_test.key_exist_in_object('txb_ip', dic_) txb_ver = db_test.key_exist_in_object('txb_ver', dic_) txb_area = db_test.key_exist_in_object('txb_area', dic_) curr_link = db_test.key_exist_in_object('curr_link', dic_) txb_max_link = db_test.key_exist_in_object('txb_max_link', dic_) txb_pro = db_test.key_exist_in_object('txb_pro', dic_) txb_act = db_test.key_exist_in_object('txb_act', dic_) og_txb_ip = db_test.key_exist_in_object('og_txb_ip', dic_) og_txb_ver = db_test.key_exist_in_object('og_txb_ver', dic_) og_txb_area = db_test.key_exist_in_object('og_txb_area', dic_) if record_mode == "0": # update file db_test.write_temp( dic_, 0, "add") # add only call once, Overwrite status & relation elif record_mode == "1": sqlcmd = "insert into dfh.tb_vip_relation (ip,version,area,max_link,pro,act)values(\'" + txb_ip + "\',\'\',\'\',\'100\',\'S\',\'F\');" db_test.DB_update_connect(sqlcmd) db_test.write_config(dic_) DB_log('DEBUG', 'add_vip', "==== add_vip complete ====")
def logout(cert_): have_cert = "" have_id = "" if record_mode == "0": tb_vip_status_list = db_test.read_information_from_temp(0) tb_vip_certification_list = db_test.read_information_from_temp( 2) #tb_vip_certification elif record_mode == "1": # select where have some problem WTF??? 20151229 tb_vip_status_list = db_test.DB_select_connect( "select * from dfh.tb_vip_status") tb_vip_certification_list = db_test.DB_select_connect( "select ip,account,pwd,cert from dfh.tb_vip_certification") for r in tb_vip_certification_list: if cert_ == r[3]: vip_ip = r[0] have_id = r[1] have_cert = r[3] if len(have_cert) > 0: if record_mode == "0": dic = {} dic.update({'login_cert': have_cert}) #db_test.write_temp(dic,0,'del_cert') # curr_link -1 for s in tb_vip_status_list: if vip_ip == s[0]: if int(s[1]) > 0: curr_link = int(s[1]) - 1 dic.update({'txb_ip': vip_ip}) dic.update({'curr_link': str(curr_link)}) db_test.write_temp(dic, 0, 'update_relation') elif record_mode == "1": db_test.DB_update_connect( "delete from dfh.tb_vip_certification where cert = \'" + have_cert + "\'") DB_log("DEBUG", "logout", "id = " + have_id + ", cert = " + cert_)