alert_ntfy.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. # LinuxGSM alert_ntfy.sh module
  3. # Author: Daniel Gibbs (Original Structure), rconjoe (ntfy Adaptation)
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Sends ntfy alert.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. # Default ntfy server if not set in config
  9. if [ -z "${ntfyserver}" ]; then
  10. ntfyserver="https://ntfy.sh"
  11. fi
  12. # Construct the target URL
  13. ntfyurl="${ntfyserver}/${ntfytopic}"
  14. # Determine priority based on alertsound or config override
  15. # ntfy priorities: 1 (min), 2 (low), 3 (default), 4 (high), 5 (max)
  16. if [ -n "${ntfypriority}" ]; then
  17. priority="${ntfypriority}"
  18. elif [ "${alertsound}" == "2" ]; then
  19. # High priority for monitor failures/permissions issues
  20. priority="4"
  21. else
  22. # Default priority for standard notifications
  23. priority="3"
  24. fi
  25. # Determine tags based on alertemoji or config override
  26. if [ -n "${ntfytags}" ]; then
  27. tags="${ntfytags}"
  28. else
  29. # Use the alert emoji as a tag
  30. tags="${alertemoji}"
  31. fi
  32. # Construct the message body
  33. message="Server Name: ${servername}
  34. Information: ${alertmessage}
  35. Game: ${gamename}
  36. Server IP: ${alertip}:${port}
  37. Hostname: ${HOSTNAME}
  38. Server Time: $(date)"
  39. # Add optional links if available
  40. if [ -n "${querytype}" ]; then
  41. message+="\nIs my Game Server Online?: https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}"
  42. fi
  43. # Use alerturl for the click action if available
  44. clickurl=""
  45. if [ -n "${alerturl}" ]; then
  46. message+="\nMore info: ${alerturl}"
  47. clickurl="${alerturl}"
  48. fi
  49. # Prepare curl command
  50. # Start with base command and add headers
  51. cmd=(curl --connect-timeout 10 -sS -X POST)
  52. cmd+=(-H "Title: ${alerttitle}")
  53. cmd+=(-H "Priority: ${priority}")
  54. cmd+=(-H "Tags: ${tags}")
  55. # Add icon if available
  56. if [ -n "${alerticon}" ]; then
  57. cmd+=(-H "Icon: ${alerticon}")
  58. fi
  59. # Add click URL if available
  60. if [ -n "${clickurl}" ]; then
  61. cmd+=(-H "Click: ${clickurl}")
  62. fi
  63. # Add authentication header if token is provided
  64. if [ -n "${ntfytoken}" ]; then
  65. cmd+=(-H "Authorization: Bearer ${ntfytoken}")
  66. # Add basic auth if username/password provided (and token isn't)
  67. elif [ -n "${ntfyusername}" ] && [ -n "${ntfypassword}" ]; then
  68. cmd+=(-u "${ntfyusername}:${ntfypassword}")
  69. fi
  70. # Add message body and target URL
  71. cmd+=(-d "${message}" "${ntfyurl}")
  72. fn_print_dots "Sending ntfy alert to ${ntfyurl}"
  73. # Execute the command
  74. ntfysend=$("${cmd[@]}")
  75. exitcode=$?
  76. # Check exit code and response
  77. if [ "${exitcode}" -eq 0 ] && [[ "${ntfysend}" != *"\"code\":"* ]] && [[ "${ntfysend}" != *"401"* ]] && [[ "${ntfysend}" != *"404"* ]]; then
  78. fn_print_ok_nl "Sending ntfy alert to ${ntfyurl}"
  79. fn_script_log_pass "Sending ntfy alert to ${ntfyurl}"
  80. else
  81. fn_print_fail_nl "Sending ntfy alert to ${ntfyurl}: ${ntfysend} (Exit code: ${exitcode})"
  82. fn_script_log_fail "Sending ntfy alert to ${ntfyurl}: ${ntfysend} (Exit code: ${exitcode})"
  83. fi