Created
December 12, 2018 09:32
-
-
Save brmzkw/4e927d1274d646cb3f1dd8417bd3d5f9 to your computer and use it in GitHub Desktop.
View long salt commands
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
#!/usr/bin/env python | |
import argparse | |
import datetime | |
import re | |
import sys | |
SALT_LOGLINE_DATE_REGEXP = r'^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3})' | |
SALT_LOGLINE_DATE_FORMAT = '%Y-%m-%d %H:%M:%S,%f' | |
SALT_LOGLINE_REGEXP = SALT_LOGLINE_DATE_REGEXP + r' \[salt' | |
def view_long_commands(logfile, maxtime): | |
prev = None | |
prev_line = None | |
for current_line in logfile: | |
if not re.match(SALT_LOGLINE_REGEXP, current_line): | |
continue | |
date = re.search(SALT_LOGLINE_DATE_REGEXP, current_line) | |
if not date: | |
sys.stderr.write( | |
'Bad date format on logline:\n\t%s' % current_line | |
) | |
continue | |
current = datetime.datetime.strptime( | |
date.group(1), SALT_LOGLINE_DATE_FORMAT | |
) | |
if prev and current - prev > datetime.timedelta(milliseconds=maxtime): | |
print ('%s between \n\t%s\nand\n\t%s\n==\n' % ( | |
(current - prev), | |
prev_line, | |
current_line | |
)) | |
prev = current | |
prev_line = current_line | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='View which salt commands are long. Takes the output ' | |
'of `salt-call -l debug state.apply [state]`' | |
) | |
parser.add_argument( | |
'-i', '--input', | |
help='Input file (default=/var/log/salt/minion)', | |
type=file, default='/var/log/salt/minion' | |
) | |
parser.add_argument( | |
'-m', '--max', | |
help='Display commands taking more than --max ms to execute', | |
type=int, default=300 | |
) | |
args = parser.parse_args() | |
view_long_commands(args.input, args.max) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment