blob: a4d6f294a0e6499e141fd89ce6a369cae57c7aaa [file] [log] [blame]
Sapan Bhatia01ddea62017-02-10 11:28:48 -08001#!/usr/bin/python
2
3import pdb
4import argparse
5import sys
6from pyparsing import *
7import json
8import os
9import patterns
10import json
11import logging
12import logstash
13import time
14
15def follow(thefile):
16 while True:
17 line = thefile.readline()
18 if not line:
19 time.sleep(0.1)
20 continue
21 yield line
22
23
24parse = argparse.ArgumentParser(description='Convert log file to json.')
25parse.add_argument('--format', dest='format', action='store',default=None, help='Format e.g. Ansible')
26parse.add_argument('--hostport', dest='hostport', action='store',default=None, help='Logstash UDP host:port')
27parse.add_argument('--file', dest='filename', action='store',default=None, help='Filename to follow')
28
29
30args = parse.parse_args()
31
32host,port = args.hostport.split(':')
33
34format = args.format
35
36logger = logging.getLogger('python-logstash-logger')
37logger.setLevel(logging.INFO)
38logger.addHandler(logstash.LogstashHandler(host, int(port), version=1))
39
40def send_log(log):
41
42 try:
43 msg = log['desc']
44 except KeyError:
45 msg = 'Metadata'
46
47 time.sleep(0.1)
48 try:
49 logger.info(msg, extra=log)
50 except Exception,e:
51 logger.info('Logstash log failed', extra={'xos_fatal':1,'exception':str(e)})
52 pass
53
54### Read parser from patterns.py
55
56def extract_global(l):
57 g = None
58
59 try:
60 global_tag = "==>" + Word(alphanums) + ":" + SkipTo(LineEnd(),include=True)
61 s = global_tag.parseString(l)
62 l = s[3]+'\n'
63 g = s[1]
64 except:
65 pass
66
67 return g,l
68
69def serialize_raw(s):
70 for l in s.splitlines():
71 g,l = extract_global(l)
72 report(l, g)
73
74def report(payload,g):
75 if (not payload):
76 return
77
78 if (type(payload)!=dict):
79 payload = {'desc':payload, 'global_tag': g}
80
81 send_log(payload)
Sapan Bhatia1064f7e2017-02-15 17:51:30 -080082 #print payload
Sapan Bhatia01ddea62017-02-10 11:28:48 -080083
84def run_logger():
85 lst = []
86 for n in dir(patterns.Parser):
87 sym = getattr(patterns.Parser, n)
88 if isinstance(sym,ParserElement):
89 lst.append(sym)
90
91 default = SkipTo(LineEnd(),include=True)
92
93 xos_logger = Or(lst)
94
95 inp = ''
96 lc = 0
97
98
99
100 if (args.filename):
101 source = follow(open(args.filename))
102
103 while True:
104 if (not args.filename):
105 l = sys.stdin.readline()
106 else:
107 l = source.next()
108
109 if (l==""):
110 break
111
112 inp += l
Sapan Bhatia1064f7e2017-02-15 17:51:30 -0800113 lc = len(inp.split('\n'))
Sapan Bhatia01ddea62017-02-10 11:28:48 -0800114
115 if (lc>4):
116 top = inp.splitlines()[0]
Sapan Bhatia1064f7e2017-02-15 17:51:30 -0800117 try:
118 line_idx = inp.index('\n')
119 inp=inp[line_idx+1:]
Sapan Bhatia01ddea62017-02-10 11:28:48 -0800120
Sapan Bhatia1064f7e2017-02-15 17:51:30 -0800121 g,top = extract_global(top)
122 report(top,g)
123 lc = len(inp.split('\n'))
124 except ValueError:
125 pass
Sapan Bhatia01ddea62017-02-10 11:28:48 -0800126
127 try:
128 s = xos_logger.scanString(inp)
129
130 last = None
131 first = True
132 for i in s:
133
134 if (first):
135 pending = inp[:i[1]]
136 serialize_raw(pending)
137
138 first = False
139
140 try:
141 payload = i[0][0]
142 g,_ = extract_global(inp)
143 report(payload,g)
144 except IndexError:
145 pass
146
147 last = i
148
149 if (last):
150 inp = inp[last[2]:]
151 lines = inp.splitlines()
152 lc=len(lines)
153
154 except Exception,e:
155 # We don't want logging to hold up execution
156 #print str(e)
157 pass
158
159def main():
160 run_logger()
161
162
163if __name__ == "__main__":
164 main()