def main(): global f_client, g_bquit init() f_client = Fdfs_client('/etc/fdfs/client.conf') signal.signal(signal.SIGTERM, onsignal_term) signal.signal(signal.SIGINT, onsignal_term) logger.info("begin......") del_sql = "delete from PIC_TEMP where id=%d" while 1: if g_bquit: print "i'm quit" break sql = "select ID,PICURL from PIC_TEMP limit 1000" # logger.debug( "sql:%s"%(sql) ) rows = dbutil.select_sql(sql) if rows is None or len(rows) <= 0: logger.info("table is empty") continue for row in rows: if g_bquit: print "i'm quit" break # print row[1] pic_dict = json.loads(row[1]) # print pic_dict del_rankingpic(pic_dict) del_s = del_sql % (row[0]) print del_s dbutil.execute_sql(del_s) time.sleep(sleep_time) logger.info("end!!!!!!")
def get_proxy_server_list(): sql = "select id from vm_server_list where proxy_mode=1" res= dbutil.select_sql(sql) slist = [] for r in res: slist.append(r[0]) return slist
def handle(): sql = '''select task_id,order_id,start_time from vm_task_allot_impl a, vm_task b where time_to_sec(NOW()) BETWEEN time_to_sec(a.start_time) AND time_to_sec(a.end_time) and a.remedy=0 and a.task_id=b.id and b.status=1 order by a.task_id''' print sql res = dbutil.select_sql(sql) if not res or len(res) <= 0: return None for r in res: tid = r[0] oid = r[1] start_time = r[2] print "taskid", tid, "order id:", oid loid, remedy_num = get_last_resttimes(start_time, tid) if oid == loid: print 'oid==loid', oid, loid continue if loid is None: continue print "loid:", loid, "remedynum:", remedy_num sum_times = sum_impl_times(tid, loid) cur_times = current_times(tid) adjust_num = sum_times - cur_times print "taskid:", tid, "sum_impl:", sum_times, "curtimes:", cur_times print "taskid:", tid, "adjust time:", adjust_num if adjust_num <= 0: adjust_num = 0 else: adjust_rantimes(oid, tid, adjust_num) if remedy_num: remedy(oid, tid, remedy_num) record_remedy_status(oid)
def get_timeout_res(timeout): sql = "SELECT a.server_id,TIMESTAMPDIFF(minute, max(b.c_time), CURRENT_TIMESTAMP()) AS mins from vm_oprcode a,\ vm_task_log b where a.create_time>CURRENT_DATE and a.oprcode=b.oprcode group by a.server_id" res = dbutil.select_sql(sql) timeout_array = [] timeout_none = [] for r in res: print r server_id = r[0] mins = r[1] if mins >= timeout: timeout_array.append(server_id) elif mins is None: timeout_none.append(server_id) if not timeout_array and not timeout_none: return None s1 = json.dumps(timeout_array) s2 = json.dumps(timeout_none) print s1, s2 s1_msg = "超时:" + s1 s2_msg = "没有c_time:" + s2 msg = s1_msg + "\r\n" + s2_msg print msg return msg
def main(): global f_client,g_bquit init() f_client = Fdfs_client('/etc/fdfs/client.conf') signal.signal(signal.SIGTERM,onsignal_term) signal.signal(signal.SIGINT,onsignal_term) logger.info( "begin......" ) del_sql = "delete from PIC_TEMP where id=%d" while 1: if g_bquit: print "i'm quit" break sql = "select ID,PICURL from PIC_TEMP limit 1000" # logger.debug( "sql:%s"%(sql) ) rows = dbutil.select_sql(sql) if rows is None or len(rows)<=0: logger.info("table is empty") continue for row in rows: if g_bquit: print "i'm quit" break # print row[1] pic_dict = json.loads(row[1]) # print pic_dict del_rankingpic( pic_dict ) del_s = del_sql%(row[0]) print del_s dbutil.execute_sql(del_s) time.sleep( sleep_time ) logger.info( "end!!!!!!" )
def current_times(task_id): sql = '''select count(*) from vm_cur_task where start_time>current_date and cur_task_id={0} and succ_time is not null'''.format(task_id) res = dbutil.select_sql(sql) if not res or len(res) < 1: return 0 times = res[0][0] return times
def get_last_resttimes(start_time, task_id): sql = '''select order_id, allot_times-ran_times from vm_task_allot_impl where start_time<'{0}' and task_id={1} order by start_time desc limit 1'''.format( start_time, task_id) res = dbutil.select_sql(sql) if not res or len(res) <= 0: return None, None return res[0][0], res[0][1]
def ranout_task(): sql = '''select task_id,times,ran_times from vm_task_group a,vm_task b where a.task_id=b.id and b.status=1 and a.ran_times>=a.times and a.times>0''' res = dbutil.select_sql(sql) if not res or len(res)<1: return None tasks = {} for r in res: tasks.update({r[0]:(r[1],r[2])}) return tasks
def sum_impl_times(task_id, oid): sql = '''select sum(ran_times) from vm_task_allot_impl where task_id={0} and order_id<={1}'''.format(task_id, oid) print sql res = dbutil.select_sql(sql) if not res or len(res) < 1: return 0 times = res[0][0] if times is None: return 0 return times
def vpn_dial_times_warning(): sql = '''select area,count(*) from vpn_dial_log where UNIX_TIMESTAMP(change_1_time)>(UNIX_TIMESTAMP(now()) -3600) group by area HAVING count(*)<2''' res = dbutil.select_sql(sql) warn_areas = [] if not res or len(res)<=0: return None for r in res: warn_areas.append(r[0]) return set(warn_areas)
def get_time_range(task_id): sql = """ select start_time, end_time, now(), order_id,templ_id from vm_task_allot_impl where now() between start_time and end_time and task_id={0} """.format(task_id) res = dbutil.select_sql(sql) if not res or len(res) < 1: return None, None, None, None, None return res[0][0], res[0][1], res[0][2], res[0][3], res[0][4]
def vpn_nodialtimes_warning(): total_areas=range(31,101) sql = '''select area,count(*) from vpn_dial_log where UNIX_TIMESTAMP(change_1_time)>(UNIX_TIMESTAMP(now()) -3600) group by area''' res = dbutil.select_sql(sql) areas = [] if not res or len(res)<=0: return None for r in res: areas.append(r[0]) warn_areas = set(total_areas)-set(areas) return warn_areas
def get_succ_count(task_id, start_time, end_time): sql = """ select count(*) from vm_cur_task where (succ_time is not null or status in(-1,1,2,4)) and start_time between '{0}' and '{1}' and cur_task_id={2} """.format(start_time, end_time, task_id) logger.info(sql) res = dbutil.select_sql(sql) if not res or len(res) < 1: return None count = res[0][0] logger.info("id:", task_id, "is handling", " count:", count) return count
def get_day_succ_count(task_id): sql = """ select count(*) from vm_cur_task where (succ_time is not null or status in(-1,0,1,2,4)) and start_time>current_date and cur_task_id={0} """.format(task_id) logger.info(sql) res = dbutil.select_sql(sql) if not res or len(res) < 1: return None count = res[0][0] logger.info("id:", task_id, "is handling", " count:", count) return count
def get_timeout_res(timeout): sql = '''SELECT serverid from vpn_status where update_time>current_date and TIMESTAMPDIFF(minute, update_time, CURRENT_TIMESTAMP())>5''' res = dbutil.select_sql(sql) timeout_array = [] timeout_none = [] for r in res: print r server_id = r[0] timeout_array.append(server_id) s1 = json.dumps(timeout_array) s1_msg = "超时:"+s1 msg = s1_msg print msg return msg
def get_notdone_task_timerange(task_id, start_time): sql = """ select start_time, end_time, order_id from vm_task_allot_impl where is_done=0 and task_id={0} and allot_times>0 and start_time<'{1}' ;""".format(task_id, start_time) logger.info(sql) res = dbutil.select_sql(sql) if not res or len(res) < 1: return None return res
def is_task_done(task_id, start_time, end_time): sql = """ select count(*) from vm_cur_task where status in(-1,1,2) and start_time between '{0}' and '{1}' and cur_task_id={2} """.format(start_time, end_time, task_id) logger.info(sql) res = dbutil.select_sql(sql) if not res or len(res) < 1: return True count = res[0][0] if count == 0: return True return False
def get_running_task(): sql = """ select a.id from vm_task a, vm_task_group b where a.id=b.task_id and a.status=1 and b.times>0 """ logger.info(sql) res = dbutil.select_sql(sql) if not res or len(res) < 1: return [] ids = [] for r in res: ids.append(r[0]) return ids
def get_no_succ_vm(): no_succ_dict= {} server_list = get_server_list() for s in server_list: for i in range(1,7): sql = '''select 1 from vm_cur_task where UNIX_TIMESTAMP(update_time)>UNIX_TIMESTAMP(now())-3600 and succ_time is not null and server_id={0} and vm_id={1}'''.format(s,i) res = dbutil.select_sql(sql) if not res or len(res)<=0: if not no_succ_dict.has_key(s): no_succ_dict[s]=[i] else: no_succ_dict[s].append(i) if no_succ_dict: s1 = json.dumps(no_succ_dict) s1_msg = "1小时内没有成功的vm:"+s1 return s1_msg return None
def handle2(tid): sql = '''select task_id,order_id,start_time from vm_task_allot_impl where remedy=0 and task_id={0} and time_to_sec(end_time)<time_to_sec(NOW()) order by task_id,start_time'''.format( tid) res = dbutil.select_sql(sql) if not res or len(res) <= 0: return None for r in res: tid = r[0] oid = r[1] start_time = r[2] print "taskid", tid, "order id:", oid loid, remedy_num = get_last_resttimes(start_time, tid) if oid == loid: print 'oid==loid', oid, loid continue print "loid:", loid, "remedynum:", remedy_num if remedy_num: remedy(oid, tid, remedy_num) record_remedy_status(oid)
def get_timeout_res(timeout): sql = '''SELECT area from vpn_status where UNIX_TIMESTAMP(update_time)>UNIX_TIMESTAMP(current_date)-3600*24 and TIMESTAMPDIFF(minute, update_time, CURRENT_TIMESTAMP())>60''' res = dbutil.select_sql(sql) timeout_array = [] timeout_none = [] if not res or len(res)<=0: return None for r in res: print r server_id = r[0] timeout_array.append(server_id) if not timeout_array: return None s1 = json.dumps(timeout_array) s1_msg = "超时:"+s1 msg = s1_msg print msg return msg
def get_failed_vm(): sql = '''select server_id,vm_id,count(*) num from vm_cur_task where UNIX_TIMESTAMP(update_time)>UNIX_TIMESTAMP(now())-600 and status not in(-1,1,2,4,11,12,13) group by server_id,vm_id having num>=5''' res = dbutil.select_sql(sql) timeout_array = [] if not res or len(res)<=0: return None for r in res: print r server_id = r[0] vm_id = r[1] timeout_array.append( (server_id, vm_id) ) if not timeout_array: return None s1 = json.dumps(timeout_array) s1_msg = "10分钟连续失败的虚拟机:"+s1 msg = s1_msg print msg return msg
def set_task_done(task_id, order_id, ran_times): """TODO: Docstring for set_task_done. :returns: TODO """ sql = """ update vm_task_allot_impl set is_done=1,ran_times={1} where order_id={0} ;""".format(order_id, ran_times) logger.info(sql) ret = dbutil.execute_sql(sql) if ret < 0: raise Exception("update wrong:{0}".format(order_id)) sql = """ select allot_times-ran_times from vm_task_allot_impl where order_id={0}""".format( order_id) logger.info(sql) res = dbutil.select_sql(sql) if not res or len(res) < 1: return None count = res[0][0] logger.info("id:", task_id, "ran left", " count:", count) if count != 0: set_ran_times(task_id, count)