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) |
| 82 | |
| 83 | def 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 | |
| 156 | def main(): |
| 157 | run_logger() |
| 158 | |
| 159 | |
| 160 | if __name__ == "__main__": |
| 161 | main() |