D4rkSl4ve

https://github.com/christronyxyocum/docker-scripts/blob/mast

Apr 7th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # Script to check container status and report to Discord if any of them are not running
  4. # Tronyx
  5.  
  6. # Define some variables
  7. tempDir='/tmp/'
  8. containerNamesFile="${tempDir}container_names.txt"
  9. # Your webhook URL for the Discord channel you want alerts sent to
  10. discordWebhookURL=''
  11. # Your Discord numeric user ID
  12. # To find your user ID just type \@<username> or \@<role>, like so \@username#1337
  13. # It will look something like <@123492578063015834> and you NEED the exclamation point like below
  14. discordUserID='<@!123492578063015834>'
  15.  
  16. # Function to create list of Docker containers
  17. create_containers_list() {
  18. docker ps --format '{{.Names}}' |sort > "${containerNamesFile}"
  19. }
  20.  
  21. # Function to check Docker containers
  22. check_containers() {
  23. while IFS= read -r container; do
  24. containerStatus=$(docker inspect "${container}" |jq .[].State.Status |tr -d '"')
  25. if [ "${containerStatus}" = 'running' ];then
  26. :
  27. elif [ "${containerStatus}" = 'exited' ];then
  28. curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"' The '"${container}"' container is currently stopped!"}' "${discordWebhookURL}"
  29. elif [ "${containerStatus}" = 'dead' ];then
  30. curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"'The '"${container}"' container is currently dead!"}' "${discordWebhookURL}"
  31. elif [ "${containerStatus}" = 'restarting' ];then
  32. curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"'The '"${container}"' container is currently restarting!"}' "${discordWebhookURL}"
  33. elif [ "${containerStatus}" = 'paused' ];then
  34. curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"'The '"${container}"' container is currently paused!"}' "${discordWebhookURL}"
  35. else
  36. curl -s -H "Content-Type: application/json" -X POST -d '{"content": "'"${discordUserID}"'The '"${container}"' container currently has an unknown status!"}' "${discordWebhookURL}"
  37. fi
  38. done < <(cat "${containerNamesFile}")
  39. }
  40.  
  41. # Main function to run all other functions
  42. main() {
  43. create_containers_list
  44. check_containers
  45. }
  46.  
  47. main
Add Comment
Please, Sign In to add comment