Skip to content

Instantly share code, notes, and snippets.

@waprin
Last active June 21, 2018 15:29
Show Gist options
  • Save waprin/7aee67aefd7530d3587a to your computer and use it in GitHub Desktop.
Save waprin/7aee67aefd7530d3587a to your computer and use it in GitHub Desktop.
Verify Splunk Regex Matches Against Your Entire Log File
#!/usr/bin/env python
# usage ./verify_splunk_coverage.py <log_file>
# set these from splunk-apps/apps/<service-name>/default/props.conf
BREAK_ONLY_BEFORE_REGEX=r'^\d\d:\d\d:\d\d\s\[[^\]]+\]'
EXTRACT_REGEX=r'^\d?\d:\d\d:\d\d\s\[(?P<thread>[^\]]+)\]\s\[(?P<loglevel>\w+)\]\s\[(?P<class>[^\]]+)\]\s\[(?P<username>[^\]]*)\](?P<message>.*)$'
import re,sys
f = open(sys.argv[1])
s = f.readline()
while True:
n = f.readline()
if not n:
if not re.match(EXTRACT_REGEX, s):
print "Failed to match line " + s
sys.exit(0)
if not re.match(BREAK_ONLY_BEFORE_REGEX, n):
s = s + n
else:
if not re.match(EXTRACT_REGEX, s):
print "Failed to match line:\n", s
s = n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment