diff --git a/script/foreman-debug b/script/foreman-debug index 49b75d1eb5e..77fc4340089 100755 --- a/script/foreman-debug +++ b/script/foreman-debug @@ -22,10 +22,11 @@ OPTIONS: -d DIR Directory to place the tarball in (default /tmp/foreman-XYZ) -g Skip generic info (CPU, memory, firewall etc.) -a Do not generate a tarball from the resulting directory - -m NUM Maximum lines to keep for each file (default 5000), zero means no limit + -m NUM Maximum lines to keep for each file, deprecated, use -s -j PRG Filter with provided program when creating a tarball -p Additionally print password patterns being filtered out -q Quiet mode + -s SIZE Maximum log set size in MB (current+rotated files, defaults to 10 MB) -v Verbose mode -u Upload tarball$UPLOAD_INFO -h Shows this message @@ -79,41 +80,51 @@ add_cmd() { eval $CMD >> "$DIR/$OUT" 2>&1 } -# add and filter if it is a non zero, readable, regular file or symlink (skip otherwise) -add_files() { - if [ $MAXLINES -eq 0 ]; then - TAIL_OR_CAT="cat" - else - TAIL_OR_CAT="tail -n ${MAXLINES}" - fi - for FILE in $*; do - if [ \( -f "$FILE" -o -h "$FILE" \) -a \( -r "$FILE" -a -s "$FILE" \) ]; then - printv " - $FILE" - SUBDIR=$(dirname $FILE) - [ ! -d "$DIR$SUBDIR" ] && mkdir -p "$DIR$SUBDIR" - MIME=$(file -bi "$FILE" | cut -d\; -f1) - case $MIME in +# add and filter file of known MIME type, calculate size +add_file() { + FILE=$1 + SIZE=$(stat -c "%s" $FILE) + MIME=$(file -bi "$FILE" | cut -d\; -f1) + case $MIME in application/x-gzip) - zcat "$FILE" | $TAIL_OR_CAT | sed -r "$FILTER" > "$DIR$FILE.txt" + zcat "$FILE" | sed -r "$FILTER" > "$DIR$FILE.txt" ;; application/x-bzip2) - bzcat "$FILE" | $TAIL_OR_CAT | sed -r "$FILTER" > "$DIR$FILE.txt" + bzcat "$FILE" | sed -r "$FILTER" > "$DIR$FILE.txt" ;; application/x-xz) - xzcat "$FILE" | $TAIL_OR_CAT | sed -r "$FILTER" > "$DIR$FILE.txt" + xzcat "$FILE" | sed -r "$FILTER" > "$DIR$FILE.txt" ;; text/plain | application/xml) - $TAIL_OR_CAT "$FILE" | sed -r "$FILTER" > "$DIR$FILE" + sed -r "$FILTER" "$FILE" > "$DIR$FILE" [ $PRINTPASS -eq 1 ] && grep -H "\*\*\*\*\*" "$DIR$FILE" ;; - inode/symlink) - cp -a "$FILE" "$DIR$FILE" - add_files $(readlink -f $FILE) - ;; *) echo "Skipping file $FILE: unknown MIME type $MIME" >> "$DIR/skipped_files" + SIZE=0 # don't count the size to collected files ;; esac +} + +# add files (from newest to oldest) that are non zero, readable, regular file or symlink, until $MAXSIZE limit is reached +add_files() { + SUMSIZE=0 + # sort regular+symlink readable nonempty files per modification time, newest first - assuming no space in a filename + for FILE in $(find -L $* -type f -readable -size +0b -printf "%T@ %p\n" 2> /dev/null | sort -nr | cut -d' ' -f2 2> /dev/null); do + # if we are over size limit, skip rest older files + if [ \( $SUMSIZE -gt $MAXSIZE \) -a \( $MAXSIZE -gt 0 \) ]; then + printv " - $FILE (skipped due to size)" + else + printv " - $FILE" + SUBDIR=$(dirname $FILE) + [ ! -d "$DIR$SUBDIR" ] && mkdir -p "$DIR$SUBDIR" + # if the file is symlink, copy source and add target + if [ -h $FILE ]; then + cp -a "$FILE" "$DIR$FILE" + FILE=$(readlink -f $FILE) + fi + add_file $FILE + SUMSIZE=$(($((SUMSIZE))+$((SIZE)))) fi done } @@ -122,7 +133,7 @@ add_files() { DIR="" NOGENERIC=0 NOTAR=0 -MAXLINES=5000 +MAXSIZE=10485760 # 10 MB in bytes COMPRESS="" PRINTPASS=0 QUIET=0 @@ -149,7 +160,7 @@ fi CONF_FILE=/usr/share/foreman/config/foreman-debug.conf test -f $CONF_FILE && source $CONF_FILE -while getopts "d:gam:j:uqpvhx" opt; do +while getopts "d:gam:s:j:uqpvhx" opt; do case $opt in d) DIR="$OPTARG" @@ -170,12 +181,17 @@ while getopts "d:gam:j:uqpvhx" opt; do VERBOSE=1 ;; m) - MAXLINES="$OPTARG" + error "Warning: -m option is deprecated, use -s option" ;; j) COMPRESS="$OPTARG" EXTENSION=".$(echo "$OPTARG" | awk '{ print $1 }')" ;; + s) + #read the value and convert from MB to bytes + MAXSIZE="$OPTARG" + MAXSIZE=$((MAXSIZE*1024*1024)) + ;; u) UPLOAD=1 ;; @@ -259,7 +275,9 @@ if [ $NOGENERIC -eq 0 ]; then add_cmd "scl -l" "software_collections" add_cmd "ps auxwwwZ" "process_list" - add_files /var/log/messages /var/log/audit/audit.log /var/log/syslog + add_files /var/log/messages* + add_files /var/log/audit/audit.log* + add_files /var/log/syslog* add_cmd "ausearch -m AVC -m USER_AVC -m SELINUX_ERR | head -n 100" "selinux_first_denials.log" add_cmd "ausearch -m AVC -m USER_AVC -m SELINUX_ERR || grep AVC /var/log/audit/audit.log" "selinux_denials.log" if [ -f /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled; then @@ -287,8 +305,18 @@ add_cmd "gem list" "gem_list" add_cmd "scl enable $SCLNAME 'gem list'" "gem_list_scl" add_cmd "bundle --local --gemfile=/usr/share/foreman/Gemfile" "bundle_list" add_cmd "facter" "facts" -add_files /etc/foreman/* /var/log/foreman/*.log* -add_files /etc/foreman-installer/scenarios.d/{*,*/*,*/.*} /var/log/foreman-installer/*.log* + +add_files /var/log/foreman/apipie_cache*.log* +add_files /var/log/foreman/cron*.log* +add_files /var/log/foreman/db_migrate*.log* +add_files /var/log/foreman/db_seed*.log* +add_files /var/log/foreman/production-*.log* +add_files /var/log/foreman/production.log + +add_files /etc/foreman/* +add_files /etc/foreman-installer/scenarios.d/{*,*/*,*/.*} +add_files /var/log/foreman-installer/ + add_files /usr/share/foreman/Gemfile* add_cmd "virsh list" "virsh_list" add_files /etc/libvirt/* /etc/libvirt/storage/* /etc/libvirt/qemu/* /etc/libvirt/qemu/networks @@ -306,7 +334,7 @@ add_cmd "foreman-selinux-relabel -nv" "foreman_filecontexts" add_files /etc/{sysconfig,default}/foreman add_files /etc/{sysconfig,default}/libvirt* add_files /etc/sysconfig/pgsql -add_files "/var/lib/pgsql/data/pg_log/*" +add_files /var/lib/pgsql/data/pg_log/* add_cmd "foreman-rake plugin:list" "plugin_list" # Look for any debug extensions provided by plugins