alert_slack.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. # LinuxGSM alert_slack.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Sends Slack alert.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. json=$(
  9. cat << EOF
  10. {
  11. "attachments": [
  12. {
  13. "color": "${alertcolourhex}",
  14. "blocks": [
  15. {
  16. "type": "header",
  17. "text": {
  18. "type": "mrkdwn",
  19. "text": "${alerttitle}",
  20. "emoji": true
  21. }
  22. },
  23. {
  24. "type": "divider"
  25. },
  26. {
  27. "type": "section",
  28. "text": {
  29. "type": "mrkdwn",
  30. "text": "*Server Name*\n${servername}"
  31. }
  32. },
  33. {
  34. "type": "section",
  35. "text": {
  36. "type": "mrkdwn",
  37. "text": "*Information*\n${alertmessage}"
  38. }
  39. },
  40. {
  41. "type": "section",
  42. "fields": [
  43. {
  44. "type": "mrkdwn",
  45. "text": "*Game*\n${gamename}"
  46. },
  47. {
  48. "type": "mrkdwn",
  49. "text": "*Server IP*\n\`${alertip}:${port}\`"
  50. },
  51. {
  52. "type": "mrkdwn",
  53. "text": "*Server Time*\n$(date)"
  54. }
  55. ],
  56. "accessory": {
  57. "type": "image",
  58. "image_url": "${alerticon}",
  59. "alt_text": "LinuxGSM game icon"
  60. }
  61. }
  62. EOF
  63. )
  64. if [ -n "${querytype}" ]; then
  65. json+=$(
  66. cat << EOF
  67. ,
  68. {
  69. "type": "section",
  70. "text": {
  71. "type": "mrkdwn",
  72. "text": "*Is my Game Server Online?*\n<https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}|Check here>"
  73. }
  74. }
  75. EOF
  76. )
  77. fi
  78. if [ -n "${alerturl}" ]; then
  79. json+=$(
  80. cat << EOF
  81. ,
  82. {
  83. "type": "section",
  84. "text": {
  85. "type": "mrkdwn",
  86. "text": "*More info*\n<${alerturl}|${alerturl}>"
  87. }
  88. }
  89. EOF
  90. )
  91. fi
  92. json+=$(
  93. cat << EOF
  94. ,
  95. {
  96. "type": "context",
  97. "elements": [
  98. {
  99. "type": "image",
  100. "image_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  101. "alt_text": "LinuxGSM icon"
  102. },
  103. {
  104. "type": "plain_text",
  105. "text": "Sent by LinuxGSM ${version}",
  106. "emoji": true
  107. }
  108. ]
  109. }
  110. ]
  111. }
  112. ]
  113. }
  114. EOF
  115. )
  116. fn_print_dots "Sending Slack alert"
  117. slacksend=$(curl --connect-timeout 3 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${slackwebhook}")
  118. if [ "${slacksend}" == "ok" ]; then
  119. fn_print_ok_nl "Sending Slack alert"
  120. fn_script_log_pass "Sending Slack alert"
  121. else
  122. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  123. fn_script_log_fail "Sending Slack alert: ${slacksend}"
  124. fi