-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
69 lines (49 loc) · 1.54 KB
/
query.py
File metadata and controls
69 lines (49 loc) · 1.54 KB
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
import requests
import json
import ConfigParser
import time
config = ConfigParser.ConfigParser()
params = {}
def continue_request(req):
if 'links' in req.json():
continue_url = req.json()['links'][0]['href']
new_response = make_request(continue_url)
handle_response(new_response)
def handle_response(resp):
response = resp
time.sleep(5)
if response.status_code == 200:
print json.dumps(resp.json(), indent=4, separators={':', ';'})
return
if response.status_code == 202:
continue_request(resp)
return
if response.status_code > 202:
print 'Error status code ' + str(response.status_code)
return
def make_request(provided_url=None):
headers = {'x-api-key': params['api-key']}
url = "https://rest.logentries.com/query/logs/"+ params['log-key'] + "/?query=" \
+ params['query'] + "&from=" + params['from'] + '&to=' + params['to']
if provided_url:
url = provided_url
req = requests.get(url, headers=headers)
return req
def print_stats():
req = make_request()
handle_response(req)
def load_config():
config.read('query.ini')
def get_params():
params['api-key'] = config.get('Auth', 'api-key')
params['log-key'] = config.get('Auth', 'log-key')
params['query'] = config.get('Search', 'query')
params['from'] = config.get('Search', 'from')
params['to'] = config.get('Search', 'to')
return params
def start():
load_config()
get_params()
print_stats()
if __name__ == '__main__':
start()