Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- set -o pipefail
- archives_dir='archives'
- default_server='unknown-host'
- services_url='https://raw.githubusercontent.com/GreatMedivack/files/master/list.out'
- log() {
- echo 1>&2 "$script_name: $*"
- }
- abort() {
- log 'ERROR: aborting due to failed command.'
- exit 1
- }
- # Select rows from $services_file, using the argument as an awk(1) condition.
- # Then print the names of matching services,
- # with trailing ids (-??????????-?????) removed.
- select_services() {
- local condition=$1
- awk "$condition {print \$1}" "$services_file" | sed -E 's/-[^-]{10}-[^-]{5}$//'
- }
- # main():
- # To access the script's $0 from log().
- script_name=$0
- if [[ $# -eq 0 ]]; then
- log "WARN: server name not provided, using '$default_server' as the default."
- server=$default_server
- elif [[ $# -eq 1 ]]; then
- server=$1
- else
- log "ERROR: too many arguments (expected 0 or 1, but got $#)."
- exit 1
- fi
- log "INFO: getting the current date..."
- current_date=$(date '+%d_%m_%Y') || abort
- services_file="${server}_${current_date}_list.out"
- log "INFO: downloading the list of services into '$services_file' ..."
- wget "$services_url" -O "$services_file" || abort
- failed_services_file="${server}_${current_date}_failed.out"
- log "INFO: saving failed services to '$failed_services_file' ..."
- # shellcheck disable=SC2016 # $3 is an awk variable, not a bash variable.
- select_services '($3 == "Error" || $3 == "CrashLoopBackOff")' > "$failed_services_file" || abort
- running_services_file="${server}_${current_date}_running.out"
- log "INFO: saving running services to '$running_services_file' ..."
- # shellcheck disable=SC2016 # $3 is an awk variable, not a bash variable.
- select_services '$3 == "Running"' > "$running_services_file" || abort
- report_file="${server}_${current_date}_report.out"
- log "INFO: generating the report into '$report_file' ..."
- # Need to pre-calculate the values outside of the heredoc,
- # in order for abort() to work properly.
- n_running=$(wc -l < "$running_services_file") || abort
- n_failed=$(wc -l < "$failed_services_file") || abort
- username=$(id -un) || abort
- cat << EOF > "$report_file" || abort
- - Количество работающих сервисов: $n_running
- - Количество сервисов с ошибками: $n_failed
- - Имя системного пользователя: $username
- - Дата: $current_date
- EOF
- chmod a+r "$report_file" || abort
- # It's not clear whether $services_file should be there, added it just in case.
- archived_files=("$services_file" "$failed_services_file" "$running_services_file" "$report_file")
- if [[ ! -e "$archives_dir" ]]; then
- log "WARN: archives directory '$archives_dir' doesn't exist, creating it..."
- mkdir "$archives_dir" || abort
- fi
- archive_file="$archives_dir/${server}_$current_date"
- log "INFO: archiving gathered files into '$archive_file' ..."
- # IMO, this should be checked at the very beginning of the script to avoid
- # needless work, but I've implemented the task verbatim just in case.
- if [[ -e "$archive_file" ]]; then
- log "WARN: file already exists, skipping this step."
- else
- tar -czf "$archive_file" "${archived_files[@]}" || abort
- fi
- log "INFO: deleting gathered files..."
- rm "${archived_files[@]}" || abort
- log "INFO: checking archive integrity..."
- tar -tzf "$archive_file" > /dev/null || abort
- log "INFO: done, exiting."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement