1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#!/usr/bin/python
#
#Translates openBTS logs into UDP event packets.
#
#http://openhere.data.ie/
#
#TJR June 2012
#
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):
#send all data in each event to avoid translation script having a state
print str(e[3]),devices[e[3]]['IMSI'],devices[e[3]]['IMEI'],devices[e[3]]['number'],str(e[1])
outsock.sendto( devices[e[3]]['IMSI']+","+devices[e[3]]['IMEI']+","+devices[e[3]]['number']+","+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['IMEI']=row[2]
d['number']=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()
|