-
Notifications
You must be signed in to change notification settings - Fork 0
/
swatch.py
executable file
·175 lines (127 loc) · 4.62 KB
/
swatch.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/opt/anaconda/bin/python
import re
import sys
import logging
import datetime
logger = logging.getLogger("")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s:%(levelname)9s:%(name)20s: %(message)s")
handler.setFormatter(formatter)
#logger.addHandler(handler)
examples = """
DEBUG 2013-09-29 13:36:49,634 common 10529 140713088272128 command:ssh -t -t [email protected] "sudo cp /user_home/private/w_jswfnal/ssh_config /vz/root/522/etc/ssh/ssh_config"
DEBUG 2013-09-29 13:36:49,899 common 10529 140713088272128 stdout:
DEBUG 2013-09-29 13:36:49,900 common 10529 140713088272128 stderr:tcgetattr: Invalid argument
"""
def is_valid(line):
return line
def process(line):
"""Return False for any failures"""
output = {}
fields = line.split(" ")
if len(fields) < 6:
return False
output["log_level"] = fields[0]
# Convert date/time to object or reject line
try:
output["datetime"] = datetime.datetime.strptime(
"%s %s" % (fields[1], fields[2]),
"%Y-%m-%d %H:%M:%S,%f"
)
except ValueError:
return False
output["logger"] = fields[3]
output["thread"] = fields[4]
output["process"] = fields[5]
output["message"] = " ".join(fields[6:])
return output
def gen_parse_log(lines):
current = {}
try:
for line in lines:
line = line.strip()
if line == "":
logger.debug("Skipping empty line")
continue
logger.debug("checking line: %s" % (line,))
# Fresh state
if current == {}:
# We are valid -- look to see if next line is valid
output = process(line)
if is_valid(output):
current = output
logger.debug("Looking at next line")
continue
else:
logger.debug("Bad line: %s" % (line,))
continue
else:
# Already parsing lines
output = process(line)
if is_valid(output):
logger.debug("Line is valid so yielding current")
yield current
current = output
else:
# keep collapsing invalid lines into message
logger.debug("invalid: %s, so collapsing" % (line,))
current["message"] = "%s\n%s" % (current["message"], line)
continue
# Don't yield on other exceptions
except StopIteration:
yield current
else:
yield current
def gen_filter_standard(messages):
standard_ignores = [
re.compile(r"^success.*retries"),
re.compile(r"^checking.*retries"),
re.compile(r"^stderr:tcgetattr: Invalid argument"),
re.compile(r"^stdout:$"),
re.compile(r"^Public Data list length"),
re.compile(r"^INFO: import publicdata succeeded.$"),
re.compile(r"^Log Debug information can pass in variables"),
re.compile(r"^Log info$")
]
for message in messages:
# Most likely garbage in
if "message" not in message:
continue
reject = False
for filter_ in standard_ignores:
m = filter_.match(message["message"])
if m:
reject = True
break
if reject:
continue
yield message
def gen_filter_stopidle(messages):
# 530 000d00h:07m:14s 4 260 6 6 23817128 w_avrba782
ignores = [
re.compile(r"sudo /srv/deploy/wakari/scripts/container_status.sh"),
re.compile(r"\d\d\d\s+\d\d\dd\d\dh:\d\dm:\d\ds"),
]
for message in messages:
# Most likely garbage in
if "message" not in message:
continue
reject = False
for filter_ in ignores:
m = filter_.search(message["message"])
if m:
reject = True
break
if reject:
continue
yield message
def main():
import sys
parsed_input = gen_parse_log(sys.stdin)
stop_idle = gen_filter_stopidle(parsed_input)
filtered = gen_filter_standard(stop_idle)
for line in filtered:
print "%(log_level)s %(datetime)s %(logger)s %(thread)s %(message)s" % line
if __name__ == '__main__':
main()