Skip to content

Instantly share code, notes, and snippets.

@Skarlso
Last active August 3, 2017 13:35
Show Gist options
  • Select an option

  • Save Skarlso/3d39a3669b0e9dd8ccb5ae17153e9bde to your computer and use it in GitHub Desktop.

Select an option

Save Skarlso/3d39a3669b0e9dd8ccb5ae17153e9bde to your computer and use it in GitHub Desktop.
Filter out Jenkins logs.
#!/usr/bin/env zsh
help() {
echo '
Define somewhere in your secrets:
export JENKINS_SERVER=''
export JENKINS_USERNAME=''
export JENKINS_PASSWORD='' -- token located under your users settings
Usage:
jenkinslog [--gff|--stat] <jobname> <buildid>
Example:
jenkinslog build-name 900 -> get logs for build-name/900 Jenkins build
jenkinslog --stat build-name 97 -> display time stats and errors only
for build-name/97 Jenkins build
jenkinslog --gff build-name 97 -> Get test Filter for Failed tests
(FILTER_REGEXP value)
'
exit 1
}
colorize_line() {
grep -e "status 0" <<< "${@}" > /dev/null
if [[ $? -lt 1 ]];
then echo -en "${fg[green]}";
else
grep -e "status 66" <<< "${@}" > /dev/null
if [[ $? -lt 1 ]]; then echo -en "${fg[yellow]}";
else echo -en "${fg[red]}"; fi
fi
echo -en "${@}"
echo -e "${reset_color}"
}
if [ "$1" = "--display" ]; then
autoload colors && colors
shift
colorize_line "${@}"
exit $?
fi
if [ "$1" = "--gff" ]; then
echo $(${SHELL} $0 $2 $3 | grep "finished" | \
grep -v "create_hosting_api" | \
grep -v "status 0" | \
# grep -v "status 66" | \
sed -e 's/^.* \([0-9]\{4\}\)_.*$/\1/g' | sort -n | uniq) | tr ' ' '|'
exit
fi
if [ "$1" = "--stat" ]; then
autoload colors && colors
read -r -d '' AWKSCRIPT << EOS
{
sections[\$4+0] += \$11;
if (\$9 > 0) {
errors[\$4+0] += 1;
success[\$4+0] += 0;
} else {
errors[\$4+0] += 0;
success[\$4+0] += 1;
}
} END {
total_errors = 0;
total_tests = 0;
for (i in sections) {
total_errors += errors[i]
total_tests += errors[i] + success[i]
hours = int(sections[i]/60/60);
minutes = int((sections[i] % (60*60)) / 60);
seconds = sections[i] % 60;
print "${fg[blue]}Worker#" i "${reset_color} ran a total of" \
" ${fg[red]}" hours "${reset_color} hours" \
" ${fg[yellow]}" minutes "${reset_color} minutes" \
" ${fg[green]}" seconds "${reset_color} seconds with" \
" " errors[i] " errors on " (success[i] + errors[i]) " tests.";
}
print "Total: " total_errors " out of " total_tests " tests"
}
EOS
# Call this script recursively with the output provided below.
"${SHELL}" "${0}" "${2}" "${3}" | awk "${AWKSCRIPT}" | sort
echo "Errors: "
# Twice
"${SHELL}" "${0}" "${2}" "${3}" | \
grep "finished" | \
grep -v "status 0 in" | \
# grep -v "status 66 in" | \
grep -o "[0-9]*_.*.rb" | \
sort -n | \
sed -e 's/\.rb//g' -e 's/_/: /1' -e 's/^/ /g'
exit
fi
CI_AQ_JOBNAME="${1}"
CI_AQ_BUILDID="${2}"
if [[ "x${1}" = "x" ]]; then help; fi
if [[ "x${2}" = "x" ]]; then help; fi
TEMPFILE=$(mktemp)
jenkins2api build logs "${CI_AQ_JOBNAME}" "${CI_AQ_BUILDID}" >> $TEMPFILE
grep -e "Worker #[0-9]*\[.*\]: finished" "${TEMPFILE}" | \
# Remove the '#[0-9]' from the Worker
sed -e 's/<[^>]*>//g;s/Worker #\([0-9]*\)\[.*\]/Worker \1/g' | \
xargs -L 1 $SHELL "$0" --display
grep -e "^Finished: " "${TEMPFILE}"
if [[ $? -gt 0 ]]; then
grep -e 'items in queue' "${TEMPFILE}" | tail -n 1
grep -e 'Waiting for [0-9]* workers.' "${TEMPFILE}" | tail -n 1
fi
rm -rf ${TEMPFILE}
@Skarlso
Copy link
Author

Skarlso commented Aug 3, 2017

With most of work done by @yitsushi. Using Jenkins2-api gem by Yitsushi. Jenkins2-Api. ( I just adjusted it to the new log format. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment