#!/usr/bin/python import MySQLdb,datetime,string,time,socket import config from optparse import OptionParser parser = OptionParser() parser.add_option("-t", "--time", type="string", dest="time", default="",help="start time HH:MM:SS.sss") parser.add_option("-d", "--date", type="string", dest="date", default="",help="start date YYYY-MM-DD") parser.add_option("-a", "--accelerate", type="float", dest="acc", default=1.0,help="playback acceleration factor") (options, args) = parser.parse_args() def sqldatetime(t): return string.replace(t.isoformat(),"T"," ") outsock = socket.socket( socket.AF_INET,socket.SOCK_DGRAM ) devices={} def sendevent(e): print str(e[3]),str(e[1]),devices[e[3]]['IMSI'] outsock.sendto( devices[e[3]]['IMSI']+","+str(e[1]), (config.viz_ip, config.viz_port) ) def main(): startTime=None date=options.date logtime=options.time now=datetime.datetime.now() if logtime=="": if date=="": logtime=str(now.hour)+":"+str(now.minute)+":"+str(now.second)+"."+str(now.microsecond) else: logtime="0:00:00.0" if date=="": date=str(now.year)+"-"+str(now.month)+"-"+str(now.day) startTime=datetime.datetime.strptime(date+" "+logtime,"%Y-%m-%d %H:%M:%S.%f") db = MySQLdb.connect(config.mysql_ip,config.mysql_user,config.mysql_pword,config.mysql_db) cursor = db.cursor() sql = "SELECT * FROM IMSIs" results=None try: cursor.execute(sql) results = cursor.fetchall() for row in results: d={} d['IMSI']=row[1] d['number']=row[2] d['ts']=row[3] devices[row[0]]=d except: print "Error: unable to fetch handset list" #get 1st event sql = "SELECT * FROM events Where ts Between '"+sqldatetime(startTime)+"' And '"+sqldatetime(now)+"' LIMIT 1" try: cursor.execute(sql) results = cursor.fetchall() except: outsock.sendto( "no event data", (config.viz_ip, config.viz_port) ) print "Error: no event data" if len(results) > 0: #playback mode id=long(results[0][0])+1 device_id=results[0][3] now=results[0][2] print "playback, next id: ",id outsock.sendto( "openBTSviz: playback mode: starting at: "+sqldatetime(startTime), (config.viz_ip, config.viz_port) ) sendevent(results[0]) while True: sql = "SELECT * FROM events Where id = "+str(id) try: cursor.execute(sql) results = cursor.fetchall() print "waiting ",(((results[0][2]-now).total_seconds()+(results[0][2]-now).microseconds))*options.acc time.sleep((((results[0][2]-now).total_seconds()+(results[0][2]-now).microseconds))*options.acc) now=results[0][2] sendevent(results[0]) id+=1 except: outsock.sendto( "end of data", (config.viz_ip, config.viz_port) ) print "end of data" exit(0) else: #realtime mode: get id of last event sql = "SELECT * FROM events ORDER BY id DESC LIMIT 1" try: cursor.execute(sql) results = cursor.fetchall() id=long(results[0][0])+1 print "realtime, next id: ",id outsock.sendto( "openBTSviz: realtime mode: next event id: "+str(id), (config.viz_ip, config.viz_port) ) except: #empty table id=1 while True: #have to refresh db connection each time db.close() db = MySQLdb.connect(config.mysql_ip,config.mysql_user,config.mysql_pword,config.mysql_db) cursor = db.cursor() sql = "SELECT * FROM events Where id = "+str(id) cursor.execute(sql) results = cursor.fetchall() if len(results) > 0: #check if IMSI is new if not devices.has_key(results[0][3]): sql = "SELECT * FROM IMSIs Where id = "+str(results[0][3]) try: cursor.execute(sql) imresults = cursor.fetchall() for row in imresults: d={} d['IMSI']=row[1] d['number']=row[2] d['ts']=row[3] devices[row[0]]=d print "new IMSI: ",d['IMSI'] except: print "Error: unable to fetch event handset" exit() sendevent(results[0]) id+=1 else: #event doesn't exist: wait and retry time.sleep(0.05) db.close() if __name__ == '__main__': main()