#!/usr/bin/python import MySQLdb,datetime,string 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") (options, args) = parser.parse_args() def sqldatetime(t): return string.replace(t.isoformat(),"T"," ") def main(): startTime=None date=options.date time=options.time now=datetime.datetime.now() if date=="": date=str(now.year)+"-"+str(now.month)+"-"+str(now.day) if time=="": time="0:00:00.0" startTime=datetime.datetime.strptime(date+" "+time,"%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" devices={} 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 data" #get 1st event sql = "SELECT * FROM events Where ts Between '"+sqldatetime(startTime)+"' And '"+sqldatetime(now)+"' LIMIT 1" print sql try: cursor.execute(sql) results = cursor.fetchall() id=long(results[0][0]) device_id=results[0][3] print "id:",id,results[0][1],devices[device_id]['IMSI'] sql = "SELECT * FROM events Where id = "+str(id+1) cursor.execute(sql) results = cursor.fetchall() print "next:",results[0][1] except: print "Error: unable to fetch data" #2 modes of operation depending whether realtime or historical db.close() if __name__ == '__main__': main()