alert_slack.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/bin/bash
  2. # LinuxGSM alert_slack.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Sends Slack alert.
  7. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. json=$(cat <<EOF
  9. {
  10. "blocks": [
  11. {
  12. "type": "context",
  13. "elements": [
  14. {
  15. "type": "image",
  16. "image_url": "https://raw.githubusercontent.com/GameServerManagers/LinuxGSM/master/lgsm/data/alert_discord_logo.jpg",
  17. "alt_text": "LinuxGSM"
  18. },
  19. {
  20. "type": "mrkdwn",
  21. "text": "*LinuxGSM Alert*"
  22. }
  23. ]
  24. },
  25. {
  26. "type": "header",
  27. "text": {
  28. "type": "plain_text",
  29. "text": "${servername}",
  30. "emoji": true
  31. }
  32. },
  33. {
  34. "type": "section",
  35. "text": {
  36. "type": "mrkdwn",
  37. "text": "*${alertemoji} ${alerttitle} ${alertemoji}*"
  38. },
  39. "accessory": {
  40. "type": "image",
  41. "image_url": "${alertimage}",
  42. "alt_text": "${alertimagealt}"
  43. }
  44. },
  45. {
  46. "type": "context",
  47. "elements": [
  48. {
  49. "type": "image",
  50. "image_url": "${alerticon}",
  51. "alt_text": "${alerticonalt}"
  52. },
  53. {
  54. "type": "mrkdwn",
  55. "text": "*${gamename}*"
  56. }
  57. ]
  58. },
  59. {
  60. "type": "section",
  61. "fields": [
  62. {
  63. "type": "mrkdwn",
  64. "text": "*Game*"
  65. },
  66. {
  67. "type": "mrkdwn",
  68. "text": "*${alertplayerstitle}*"
  69. },
  70. {
  71. "type": "mrkdwn",
  72. "text": "${gamename}"
  73. },
  74. {
  75. "type": "mrkdwn",
  76. "text": "${alertplayers}"
  77. }
  78. ]
  79. },
  80. {
  81. "type": "section",
  82. "fields": [
  83. {
  84. "type": "mrkdwn",
  85. "text": "*Map*"
  86. },
  87. {
  88. "type": "mrkdwn",
  89. "text": "*Server IP*"
  90. },
  91. {
  92. "type": "mrkdwn",
  93. "text": "${alertmap}"
  94. },
  95. {
  96. "type": "mrkdwn",
  97. "text": "${alertip}:${port}"
  98. }
  99. ]
  100. },
  101. {
  102. "type": "section",
  103. "fields": [
  104. {
  105. "type": "mrkdwn",
  106. "text": "*Hostname*"
  107. },
  108. {
  109. "type": "mrkdwn",
  110. "text": "*Version*"
  111. },
  112. {
  113. "type": "mrkdwn",
  114. "text": "${HOSTNAME}"
  115. },
  116. {
  117. "type": "mrkdwn",
  118. "text": "${alertversion}"
  119. }
  120. ]
  121. },
  122. {
  123. "type": "section",
  124. "text": {
  125. "type": "mrkdwn",
  126. "text": "*Trigger Message*\n${alerttriggermessage}"
  127. }
  128. },
  129. {
  130. "type": "section",
  131. "text": {
  132. "type": "mrkdwn",
  133. "text": "*More info*\n${alertmoreinfourl}"
  134. }
  135. },
  136. {
  137. "type": "divider"
  138. },
  139. {
  140. "type": "context",
  141. "elements": [
  142. {
  143. "type": "image",
  144. "image_url": "https://raw.githubusercontent.com/GameServerManagers/LinuxGSM/master/lgsm/data/alert_discord_logo.jpg",
  145. "alt_text": "LinuxGSM Logo"
  146. },
  147. {
  148. "type": "plain_text",
  149. "text": "Powered by LinuxGSM ${version}",
  150. "emoji": true
  151. }
  152. ]
  153. },
  154. {
  155. "type": "divider"
  156. }
  157. ]
  158. }
  159. EOF
  160. )
  161. fn_print_dots "Sending Slack alert"
  162. slacksend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${slackwebhook}")
  163. if [ "${slacksend}" == "ok" ]; then
  164. fn_print_ok_nl "Sending Slack alert"
  165. fn_script_log_pass "Sending Slack alert"
  166. else
  167. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  168. fn_script_log_fatal "Sending Slack alert: ${slacksend}"
  169. fi