Skip to content

Instantly share code, notes, and snippets.

@taoky
Last active February 27, 2021 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taoky/91c12185c2cd38f264fe2863a6b13c27 to your computer and use it in GitHub Desktop.
Save taoky/91c12185c2cd38f264fe2863a6b13c27 to your computer and use it in GitHub Desktop.
Analyze rsyncd log with state-machine style python script
from sys import stdin
from collections import defaultdict
def analyze(line):
state = 0
for idx, c in enumerate(line):
if state == 0:
if c == ']':
if line[idx + 2:idx + 6] == "send":
state = 1
else:
return None
elif state == 1:
if c == ']':
if line[idx + 2:idx + 6] == "repo":
state = 2
else:
pos = line[idx:].find("(")
return line[idx + 2:idx + pos - 1]
elif state == 2:
if c == ')':
pos = line[idx:].find("/")
if pos < 0:
raise ValueError
return line[idx + 2:idx + pos]
def main():
counters = defaultdict(int)
for line in stdin:
line = line.strip()
result = analyze(line)
if result is None:
continue
counters[result] += 1
print(counters)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment