DB系统预警联系人API(二)

2015-02-03 10:03:10 · 作者: · 浏览: 56
swd,port,dbname,interval=86400): tfile='/tmp/contact_dbinfo' list='' if os.path.isfile(tfile): a1=datetime.datetime.fromtimestamp(os.path.getctime(tfile)) a2=datetime.datetime.now() diffsec = (a2 - a1).seconds if diffsec > interval: os.remove(tfile) f=open(tfile,'a') db=database() db.conn(host,user,passwd,port,dbname) sql="select t.contact,t.username,t.group,t.`type`,l.levelname from db_alertcontact t , db_alertlevel l where t.levelid=l.id and l.levelname=%s and t.group=%s and t.`type`=%s" param=(level,group,type) cur=db.execute(sql,param) results=cur.fetchall() for row in results: if row[3] =='phone': #for r in row: list = list + row[0] + ',' elif row[3] == 'email': #for r in row: list = list + row[0] + ',' if type =='phone': f.write('phonelist='+ group + ':' + list + '\n') f.close() elif type == 'email': f.write('emaillist='+ group + ':' +list + '\n') f.close() else: strsearch = type + 'list='+ group istype = os.popen('cat '+ tfile +' | grep ' + strsearch + ' | wc -l').readline().strip() if int(istype) > 0: line = os.popen('cat '+ tfile +' | grep ' + strsearch).readline().strip() b=line.split('=') a=b[1].split(":") if b[0]=='phonelist': list=a[1] elif b[0]=='emaillist': list=a[1] elif int(istype) < 1: f=open(tfile,'a') db=database() db.conn(host,user,passwd,port,dbname) sql="select t.contact,t.username,t.group,t.`type`,l.levelname from db_alertcontact t , db_alertlevel l where t.levelid=l.id and l.levelname=%s and t.group=%s and t.`type`=%s" param=(level,group,type) cur=db.execute(sql,param) results=cur.fetchall() #list='' for row in results: if row[3] =='phone': list = list + row[0] + ',' elif row[3] == 'email': list = list + row[0] + ',' if type =='phone': f.write('phonelist='+ group + ':' + list + '\n') f.close() elif type == 'email': f.write('emaillist='+ group + ':' + list + '\n') f.close() else: f=open(tfile,'a') db=database() db.conn(host,user,passwd,port,dbname) sql="select t.contact,t.username,t.group,t.`type`,l.levelname from db_alertcontact t , db_alertlevel l where t.levelid=l.id and l.levelname=%s and t.group=%s and t.`type`=%s" param=(level,group,type) cur=db.execute(sql,param) results=cur.fetchall() for row in results: if row[3] =='phone': #for r in row: list = list + row[0] + ',' elif row[3] == 'email': #for r in row: list = list + row[0] + ',' if type =='phone': f.write('phonelist='+ group + ':' + list + '\n') f.close() elif type == 'email': f.write('emaillist='+ group + ':' + list + '\n') f.close() return list if __name__ == "__main__": parser = argparse.ArgumentParser("Contanct API v0.1 ,(C) Copyright Skate 2014") parser.add_argument('--group', action='store', dest='group',required=True, help=" = The contact group") parser.add_argument('--type', action='store', dest='type',required=True, help=" = The mode of contact") parser.add_argument('--level', action='store', dest='level',required=True, help=" = alarm level,info/warn/err") parser.add_argument('--interval', action='store', dest='interval',type=int,default=86400, help=" = Database query interval") parser.add_argument('--load', action='store', dest='load',default='', help=" = The configure center database,eg: \n load=user/pass@ip:port:dbname") results = parser.parse_args() load = results.load group = results.group type = results.type level = results.level interval = results.interval if (load !=''): user_info,url = load.split("@") host,port,db = url.split(":") port=int(port) user,passwd = user_info.split("/",1) str = contactlist(group,type,level,host,user,passwd,port,db,interval) print str


?

?

大家有好的意见,欢迎提出


------end-------

?

?