83 lines
1.7 KiB
Bash
Executable file
83 lines
1.7 KiB
Bash
Executable file
#!/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
|
|
|
|
|