Sapan Bhatia | 01ddea6 | 2017-02-10 11:28:48 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import pdb |
| 4 | import argparse |
| 5 | import sys |
| 6 | from pyparsing import * |
| 7 | import json |
| 8 | import os |
| 9 | import patterns |
| 10 | import json |
| 11 | import logging |
| 12 | import logstash |
| 13 | import time |
| 14 | |
| 15 | def 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 | |
| 24 | parse = argparse.ArgumentParser(description='Convert log file to json.') |
| 25 | parse.add_argument('--format', dest='format', action='store',default=None, help='Format e.g. Ansible') |
| 26 | parse.add_argument('--hostport', dest='hostport', action='store',default=None, help='Logstash UDP host:port') |
| 27 | parse.add_argument('--file', dest='filename', action='store',default=None, help='Filename to follow') |
| 28 | |
| 29 | |
| 30 | args = parse.parse_args() |
| 31 | |
| 32 | host,port = args.hostport.split(':') |
| 33 | |
| 34 | format = args.format |
| 35 | |
| 36 | logger = logging.getLogger('python-logstash-logger') |
| 37 | logger.setLevel(logging.INFO) |
| 38 | logger.addHandler(logstash.LogstashHandler(host, int(port), version=1)) |
| 39 | |
| 40 | def 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 | |
| 56 | def 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 | |
| 69 | def serialize_raw(s): |
| 70 | for l in s.splitlines(): |
| 71 | g,l = extract_global(l) |
| 72 | report(l, g) |
| 73 | |
| 74 | def 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 Bhatia | 1064f7e | 2017-02-15 17:51:30 -0800 | [diff] [blame] | 82 | #print payload |
Sapan Bhatia | 01ddea6 | 2017-02-10 11:28:48 -0800 | [diff] [blame] | 83 | |
| 84 | def 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 Bhatia | 1064f7e | 2017-02-15 17:51:30 -0800 | [diff] [blame] | 113 | lc = len(inp.split('\n')) |
Sapan Bhatia | 01ddea6 | 2017-02-10 11:28:48 -0800 | [diff] [blame] | 114 | |
| 115 | if (lc>4): |
| 116 | top = inp.splitlines()[0] |
Sapan Bhatia | 1064f7e | 2017-02-15 17:51:30 -0800 | [diff] [blame] | 117 | try: |
| 118 | line_idx = inp.index('\n') |
| 119 | inp=inp[line_idx+1:] |
Sapan Bhatia | 01ddea6 | 2017-02-10 11:28:48 -0800 | [diff] [blame] | 120 | |
Sapan Bhatia | 1064f7e | 2017-02-15 17:51:30 -0800 | [diff] [blame] | 121 | g,top = extract_global(top) |
| 122 | report(top,g) |
| 123 | lc = len(inp.split('\n')) |
| 124 | except ValueError: |
| 125 | pass |
Sapan Bhatia | 01ddea6 | 2017-02-10 11:28:48 -0800 | [diff] [blame] | 126 | |
| 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 | |
| 159 | def main(): |
| 160 | run_logger() |
| 161 | |
| 162 | |
| 163 | if __name__ == "__main__": |
| 164 | main() |