4
0

alert_ntfy.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 (keep formatting consistent with other plain-text alerts)
  33. message="Server Name
  34. ${servername}
  35. Information
  36. ${alertmessage}
  37. Game
  38. ${gamename}
  39. Server IP
  40. ${alertip}:${port}
  41. Server Time
  42. $(date)"
  43. # Add optional links if available
  44. if [ -n "${querytype}" ]; then
  45. message+="\n\nIs my Game Server Online?\nhttps://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}"
  46. fi
  47. # Use alerturl for the click action if available
  48. clickurl=""
  49. if [ -n "${alerturl}" ]; then
  50. message+="\n\nMore info\n${alerturl}"
  51. clickurl="${alerturl}"
  52. fi
  53. # Prepare curl command
  54. # Start with base command and add headers
  55. cmd=(curl --connect-timeout 10 -sS -X POST)
  56. cmd+=(-H "Title: ${alerttitle}")
  57. cmd+=(-H "Priority: ${priority}")
  58. cmd+=(-H "Tags: ${tags}")
  59. # Add icon if available
  60. if [ -n "${alerticon}" ]; then
  61. cmd+=(-H "Icon: ${alerticon}")
  62. fi
  63. # Add click URL if available
  64. if [ -n "${clickurl}" ]; then
  65. cmd+=(-H "Click: ${clickurl}")
  66. fi
  67. # Add authentication header if token is provided
  68. if [ -n "${ntfytoken}" ]; then
  69. cmd+=(-H "Authorization: Bearer ${ntfytoken}")
  70. # Add basic auth if username/password provided (and token isn't)
  71. elif [ -n "${ntfyusername}" ] && [ -n "${ntfypassword}" ]; then
  72. cmd+=(-u "${ntfyusername}:${ntfypassword}")
  73. fi
  74. # Add message body and target URL
  75. cmd+=(-d "${message}" "${ntfyurl}")
  76. fn_print_dots "Sending ntfy alert to ${ntfyurl}"
  77. # Execute the command
  78. ntfysend=$("${cmd[@]}")
  79. exitcode=$?
  80. # Check exit code and response
  81. if [ "${exitcode}" -eq 0 ] && [[ "${ntfysend}" != *"\"code\":"* ]] && [[ "${ntfysend}" != *"401"* ]] && [[ "${ntfysend}" != *"404"* ]]; then
  82. fn_print_ok_nl "Sending ntfy alert to ${ntfyurl}"
  83. fn_script_log_pass "Sending ntfy alert to ${ntfyurl}"
  84. else
  85. fn_print_fail_nl "Sending ntfy alert to ${ntfyurl}: ${ntfysend} (Exit code: ${exitcode})"
  86. fn_script_log_fail "Sending ntfy alert to ${ntfyurl}: ${ntfysend} (Exit code: ${exitcode})"
  87. fi