Skip to content

Instantly share code, notes, and snippets.

@bdargan
Created January 25, 2013 01:25
Show Gist options
  • Save bdargan/4630755 to your computer and use it in GitHub Desktop.
Save bdargan/4630755 to your computer and use it in GitHub Desktop.
python log parser, to pull out device type and versions
#!/usr/bin/env python
import sys,re
from collections import defaultdict, namedtuple
# cat wip/mobile_log |./apl | sed -e's/_/\./'
mobile_pat = re.compile(
r'^(?P<app>\S*?)\s"'
r'(?P<host>[\d\.]+?)"\s'
r'(?P<user>\S*?)\s'
r'"(?P<datetime>.*?)"\s'
r'"(?P<method>[A-Z]*?)\s'
r'(?P<uri>.*?)"\s'
r'(?P<resp>\d{3})\s'
r'(?P<elapsed>\d+)\s'
r'"(?P<unknown>\d*?)"\s'
r'"(?P<unknown2>.*?)"\s'
r'"(?P<ua>.*?)"\s'
r'"(?P<referer>.*?)"\s'
r'"(?P<cookie>.*)"')
ios_pat = re.compile(
r'(iP(?:ad|hone)).*?\s'
r'(?P<version>\d_\d)'
)
android_pat = re.compile(
r'\(.*?(?P<device>Android) '
r'(?P<version>\d\.\d)'
)
def process(input):
for line in input:
if re.search(mobile_pat, line):
d = re.search(mobile_pat, line)
dg = d.groupdict()
ios = re.search(ios_pat, dg['ua'])
dev_info = "device?"
if ios:
dev_info = ios.group(1) + " " + ios.group(2)
android = re.search(android_pat, dg['ua'])
if android:
dev_info = android.group(1) + " " + android.group(2)
if dg['ua'] != "NULL-USER-AGENT":
print dev_info
if __name__ == '__main__':
process(sys.stdin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment