-
Notifications
You must be signed in to change notification settings - Fork 2
/
sumo-search.py
116 lines (97 loc) · 3.92 KB
/
sumo-search.py
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
#!/usr/bin/python -tt
__author__ = 'Sijis Aviles'
import sys
from optparse import OptionParser
import datetime
import pprint
import sumologic.client
import sumologic.search
def parse_results(results, data):
pprint.pprint(results[-data['limits']:])
print 'Records found: %d' % len(results)
print 'Only showing %d records.' % data['limits']
def main():
time_now = datetime.datetime.now().replace(second=0, microsecond=0)
right_now = time_now.isoformat()
minutes_ago = (time_now - datetime.timedelta(minutes=5)).isoformat()
parser = OptionParser(version='%prog 0.2',
description='Sumologic cli to query data through API')
parser.add_option('-u', '--username',
dest='username', metavar='USERNAME',
default=None,
help='Username of API login')
parser.add_option('-p', '--password',
dest='password', metavar='PASSWORD',
default=None,
help='Password of API login')
parser.add_option('-s', '--search',
dest='search', metavar='TEXT',
default=None,
help='Search text against log')
parser.add_option('-f', '--format',
dest='format', metavar='FORMAT',
default='json',
help='Search results output format')
parser.add_option('-m', '--timezone',
dest='timezone', metavar='TIMEZONE',
default='UTC',
help='Timezone used in results')
parser.add_option('-o', '--time-to',
dest='timeto', metavar='TIME',
default='%s' % right_now,
help='End time for search')
parser.add_option('-i', '--time-from',
dest='timefrom', metavar='TIME',
default='%s' % minutes_ago,
help='Start time for search')
parser.add_option('-n', '--nodrop',
dest='nodrop',
action='store_true',
default=False,
help='Enabling will return all results - DISABLED')
parser.add_option('-l', '--limits',
dest='limits', metavar='NUMBER',
default=1000,
help='Number of results to return')
parser.add_option('--debug',
dest='debug',
default=False, action='store_true',
help='Enable debugging mode')
(options, args) = parser.parse_args()
data = vars(options)
data['valid_formats'] = ['json', 'text']
if data['username'] is None or \
data['password'] is None:
print 'Username and Password are required.'
print 'Exiting....'
sys.exit(1)
if data['search'] is None:
print 'A search criteria is required.'
sys.exit(1)
t_options = {}
if data['format'] not in data['valid_formats']:
print 'Invalid and unsupported format specified'
print 'The valid formats are: %s' % ', '.join(data['valid_formats'])
sys.exit(3)
else:
t_options['format'] = data['format']
data['limits'] = int(data['limits'])
t_options['tz'] = data['timezone']
t_options['from'] = data['timefrom']
t_options['to'] = data['timeto']
client = sumologic.client.Client(auth=(data['username'], data['password']), debug=data['debug'])
search = sumologic.search.Search(client)
results = search.query(data['search'], **t_options)
client.debug()
try:
results['data'][0]
except KeyError:
print 'An issue was encountered.'
print '%s: %s' % (results['response'], results['reason'])
sys.exit(10)
except IndexError:
print 'No records found.'
sys.exit(3)
parse_results(results['data'], data)
if __name__ == "__main__":
main()