Advertisement
Expurple

report-state.sh

Jun 21st, 2023 (edited)
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.37 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. set -o pipefail
  4.  
  5. archives_dir='archives'
  6. default_server='unknown-host'
  7. services_url='https://raw.githubusercontent.com/GreatMedivack/files/master/list.out'
  8.  
  9. log() {
  10.     echo 1>&2 "$script_name: $*"
  11. }
  12.  
  13. abort() {
  14.     log 'ERROR: aborting due to failed command.'
  15.     exit 1
  16. }
  17.  
  18. # Select rows from $services_file, using the argument as an awk(1) condition.
  19. # Then print the names of matching services,
  20. # with trailing ids (-??????????-?????) removed.
  21. select_services() {
  22.     local condition=$1
  23.     awk "$condition {print \$1}" "$services_file" | sed -E 's/-[^-]{10}-[^-]{5}$//'
  24. }
  25.  
  26. # main():
  27.  
  28. # To access the script's $0 from log().
  29. script_name=$0
  30.  
  31. if [[ $# -eq 0 ]]; then
  32.     log "WARN: server name not provided, using '$default_server' as the default."
  33.     server=$default_server
  34. elif [[ $# -eq 1 ]]; then
  35.     server=$1
  36. else
  37.     log "ERROR: too many arguments (expected 0 or 1, but got $#)."
  38.     exit 1
  39. fi
  40.  
  41. log "INFO: getting the current date..."
  42. current_date=$(date '+%d_%m_%Y') || abort
  43.  
  44. services_file="${server}_${current_date}_list.out"
  45. log "INFO: downloading the list of services into '$services_file' ..."
  46. wget "$services_url" -O "$services_file" || abort
  47.  
  48. failed_services_file="${server}_${current_date}_failed.out"
  49. log "INFO: saving failed services to '$failed_services_file' ..."
  50. # shellcheck disable=SC2016 # $3 is an awk variable, not a bash variable.
  51. select_services '($3 == "Error" || $3 == "CrashLoopBackOff")' > "$failed_services_file" || abort
  52.  
  53. running_services_file="${server}_${current_date}_running.out"
  54. log "INFO: saving running services to '$running_services_file' ..."
  55. # shellcheck disable=SC2016 # $3 is an awk variable, not a bash variable.
  56. select_services '$3 == "Running"' > "$running_services_file" || abort
  57.  
  58. report_file="${server}_${current_date}_report.out"
  59. log "INFO: generating the report into '$report_file' ..."
  60. # Need to pre-calculate the values outside of the heredoc,
  61. # in order for abort() to work properly.
  62. n_running=$(wc -l < "$running_services_file") || abort
  63. n_failed=$(wc -l < "$failed_services_file") || abort
  64. username=$(id -un) || abort
  65. cat << EOF > "$report_file" || abort
  66. - Количество работающих сервисов: $n_running
  67. - Количество сервисов с ошибками: $n_failed
  68. - Имя системного пользователя: $username
  69. - Дата: $current_date
  70. EOF
  71. chmod a+r "$report_file" || abort
  72.  
  73. # It's not clear whether $services_file should be there, added it just in case.
  74. archived_files=("$services_file" "$failed_services_file" "$running_services_file" "$report_file")
  75. if [[ ! -e "$archives_dir" ]]; then
  76.     log "WARN: archives directory '$archives_dir' doesn't exist, creating it..."
  77.     mkdir "$archives_dir" || abort
  78. fi
  79. archive_file="$archives_dir/${server}_$current_date"
  80. log "INFO: archiving gathered files into '$archive_file' ..."
  81. # IMO, this should be checked at the very beginning of the script to avoid
  82. # needless work, but I've implemented the task verbatim just in case.
  83. if [[ -e "$archive_file" ]]; then
  84.     log "WARN: file already exists, skipping this step."
  85. else
  86.     tar -czf "$archive_file" "${archived_files[@]}" || abort
  87. fi
  88.  
  89. log "INFO: deleting gathered files..."
  90. rm "${archived_files[@]}" || abort
  91.  
  92. log "INFO: checking archive integrity..."
  93. tar -tzf "$archive_file" > /dev/null || abort
  94.  
  95. log "INFO: done, exiting."
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement