blob: 3543011fa23cbda0ca181affec7dc5e1cea7d2ca [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)
82
83def run_logger():
84 lst = []
85 for n in dir(patterns.Parser):
86 sym = getattr(patterns.Parser, n)
87 if isinstance(sym,ParserElement):
88 lst.append(sym)
89
90 default = SkipTo(LineEnd(),include=True)
91
92 xos_logger = Or(lst)
93
94 inp = ''
95 lc = 0
96
97
98
99 if (args.filename):
100 source = follow(open(args.filename))
101
102 while True:
103 if (not args.filename):
104 l = sys.stdin.readline()
105 else:
106 l = source.next()
107
108 if (l==""):
109 break
110
111 inp += l
112 lc += 1
113
114 if (lc>4):
115 top = inp.splitlines()[0]
116 line_idx = inp.index('\n')
117 inp=inp[line_idx+1:]
118
119 g,top = extract_global(top)
120 report(top,g)
121
122 lc-=1
123
124 try:
125 s = xos_logger.scanString(inp)
126
127 last = None
128 first = True
129 for i in s:
130
131 if (first):
132 pending = inp[:i[1]]
133 serialize_raw(pending)
134
135 first = False
136
137 try:
138 payload = i[0][0]
139 g,_ = extract_global(inp)
140 report(payload,g)
141 except IndexError:
142 pass
143
144 last = i
145
146 if (last):
147 inp = inp[last[2]:]
148 lines = inp.splitlines()
149 lc=len(lines)
150
151 except Exception,e:
152 # We don't want logging to hold up execution
153 #print str(e)
154 pass
155
156def main():
157 run_logger()
158
159
160if __name__ == "__main__":
161 main()