Last active
February 27, 2021 17:04
-
-
Save taoky/91c12185c2cd38f264fe2863a6b13c27 to your computer and use it in GitHub Desktop.
Analyze rsyncd log with state-machine style python script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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