Extract.py

From Federal Burro of Information
Jump to navigationJump to search
#!/usr/local/bin/python

import json
import sys

'''

{
    "remote-addr": "209.171.88.136",
    "hostname": "node-app-custom12",
    "pid": 30239,
    "protocol": "https",
    "http-version": "1.1",
    "method": "GET",
    "url": "/v1/p2p",
    "user-agent": "App/1.8.0 (iPhone; iOS 11.4.1; Scale/2.00)",
    "request-date": "2018-08-09 00:00:10 UTC",
    "user-id": 470005,
    "time-taken": 13,
    "content-length": "246",
    "status": 200,
    "status-message": "OK"
}
'''

count = dict()
sum = dict()

fields = [ "url" , "time-taken" ]
field2 = ""


f=open('log','r')
#f=open('head','r')

while True:
    x = f.readline()
    if not x: break
    try:
        j = json.loads(x)
    except ValueError as err:
        print(err)
    # j = json.loads(x)
    # sys.stdout.write('.')
    print "%s %s" % ( j[fields[0]], j[fields[1]] )
 
    # print "%s %s %s %s %s %s %s \"%s\" %s %s" % ( j["remote-addr"] , j["hostname"], j["pid"] , j["protocol"], j["http-version"], j["method"], j["url"], j["user-agent"], j["request-date"], j["time-taken"] )
    # print j

#for key in sorted(rollup.iterkeys()):
#    print "%s: %s" % (key, rollup[key])

#for key, value in sorted(rollup.iteritems(), key=lambda (k,v): (v,k)):
#    print "%s: %s" % (key, value)