- add show_changed - python and much faster than the shell

- remove show-changed
This commit is contained in:
Seth Vidal 2013-07-04 03:03:58 +00:00
parent cfaddae895
commit 8dc05c6294
2 changed files with 75 additions and 83 deletions

View file

@ -1,83 +0,0 @@
#!/bin/bash
# view the ansible logs
# takes all the options of grep and passes them straight through - then parses the output so it looks better and readable
# should only be used on the .log files not the .info files. info files are flat readable
# example:
# show-changed -v today mirrorlist
logpath='/var/log/ansible'
search_terms="CHANGED|FAILED"
function search_logs ()
{
IFS='
'
for line in `grep -H -E $search_terms $@`
do
logpath=`echo $line| cut -d: -f1`
hostname=`basename $logpath`
dir=`dirname $logpath`
runtime=`basename $dir`
echo -n "$runtime - $hostname "
pre=`echo $line | cut -d: -f2-| cut -f3-4`
json=`echo $line | cut -d: -f2- |cut -f5-`
echo $json| python -m json.tool 2>/dev/null >&2
if [ $? != 0 ]; then
echo "Error parsing json"
else
if [ "$verbose" == 'yes' ]; then
echo $pre
echo $json| python -m json.tool
else
echo -n $pre
for term in task_userid cmd task_args task_module; do
res=`echo $json| /srv/web/infra/ansible/scripts/keyreturn $term 2>/dev/null`
if [ $? == 0 ]; then
echo -n " $res"
#$json| /srv/web/infra/ansible/scripts/keyreturn $term
fi
done
echo ''
fi
fi
done
}
while getopts ":v" opt; do
case $opt in
v)
export verbose='yes'
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $(( OPTIND - 1 ));
when='yesterday'
if [ -n "$1" ]; then
when=$1
fi
ts=`date -d "$when" +%Y/%m/%d`
if [ -z "$2" ]; then
where='*'
else
where=$2
fi
for pb in $logpath/$where/; do
if [ -d $pb/$ts ]; then
echo `basename $pb`
search_logs $pb/$ts/*/*.log
fi
done

75
scripts/show_changed Executable file
View file

@ -0,0 +1,75 @@
#!/usr/bin/python -tt
import sys
import json
import time
from optparse import OptionParser
import subprocess
import os
import glob
logpath='/var/log/ansible'
search_terms=['CHANGED','FAILED']
def date_cheat(datestr):
dc = subprocess.Popen(['date', '-d', datestr, '+%Y/%m/%d'], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
dc = dc.strip()
return dc
def parse_args(args):
usage = """
show_changed [options] [-d datestr] [-p playbook]
"""
parser = OptionParser(usage=usage)
parser.add_option("-d", default='today', dest='datestr', help="time string of when you want logs")
parser.add_option("-p", default='*', dest='playbook', help="the playbook you want to look for")
parser.add_option("-v", default=False, dest='verbose', action='store_true', help='Verbose')
parser.add_option("-s", default=[], dest='search_terms', action='append', help="status to search for")
(opts, args) = parser.parse_args(args)
opts.datestr = date_cheat(opts.datestr)
if not opts.search_terms:
opts.search_terms = search_terms
return opts, args
def search_logs(opts, logfiles):
msg = ''
for fn in logfiles:
hostname=os.path.basename(fn).replace('.log', '')
timestamp = os.path.basename(os.path.dirname(fn))
for line in open(fn):
things = line.split('\t')
if things[2] in opts.search_terms:
slurp = json.loads(things[4])
msg += '%s\t%s\t%s\t%s\t%s\t%s' % (timestamp, hostname, things[0], things[1], things[2], things[3])
if not opts.verbose:
if type(slurp) == dict:
for term in ['task_userid', 'cmd']:
if term in slurp:
msg += '\t%s:%s' % (term, slurp.get(term, None))
msg += '\n'
else:
msg += '\n'
msg += json.dumps(slurp, indent=4)
msg += '\n'
return msg
def main(args):
opts,args = parse_args(args)
for pb in glob.glob(logpath + '/' + opts.playbook):
pb_name = os.path.basename(pb)
for pb_logdir in glob.glob(pb + '/' + opts.datestr):
logfiles = glob.glob(pb_logdir + '/*/*.log')
msg = search_logs(opts, logfiles)
if msg:
print pb_name
print msg
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))