alert_slack.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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": "context",
  27. "elements": [
  28. {
  29. "type": "image",
  30. "image_url": "${alerticon}",
  31. "alt_text": "${alerticonalt}"
  32. },
  33. {
  34. "type": "mrkdwn",
  35. "text": "${gamename}"
  36. }
  37. ]
  38. },
  39. {
  40. "type": "header",
  41. "text": {
  42. "type": "plain_text",
  43. "text": "${servername}",
  44. "emoji": true
  45. }
  46. },
  47. {
  48. "type": "section",
  49. "text": {
  50. "type": "mrkdwn",
  51. "text": "*${alertemoji} ${alerttitle} ${alertemoji}*"
  52. },
  53. "accessory": {
  54. "type": "image",
  55. "image_url": "${alertimage}",
  56. "alt_text": "${alertimagealt}"
  57. }
  58. },
  59. {
  60. "type": "section",
  61. "text": {
  62. "type": "mrkdwn",
  63. "text": "*Game*\n${gamename}"
  64. }
  65. },
  66. {
  67. "type": "section",
  68. "fields": [
  69. {
  70. "type": "mrkdwn",
  71. "text": "*Map*"
  72. },
  73. {
  74. "type": "mrkdwn",
  75. "text": "*${alertplayerstitle}*"
  76. },
  77. {
  78. "type": "mrkdwn",
  79. "text": "${alertmap}"
  80. },
  81. {
  82. "type": "mrkdwn",
  83. "text": "${alertplayers}"
  84. }
  85. ]
  86. },
  87. {
  88. "type": "section",
  89. "fields": [
  90. {
  91. "type": "mrkdwn",
  92. "text": "*Version*"
  93. },
  94. {
  95. "type": "mrkdwn",
  96. "text": "*Country*"
  97. },
  98. {
  99. "type": "mrkdwn",
  100. "text": "${alertversion}"
  101. },
  102. {
  103. "type": "mrkdwn",
  104. "text": "${country}"
  105. }
  106. ]
  107. },
  108. {
  109. "type": "section",
  110. "fields": [
  111. {
  112. "type": "mrkdwn",
  113. "text": "*Server IP*"
  114. },
  115. {
  116. "type": "mrkdwn",
  117. "text": "*Hostname*"
  118. },
  119. {
  120. "type": "mrkdwn",
  121. "text": "${alertip}:${port}"
  122. },
  123. {
  124. "type": "mrkdwn",
  125. "text": "${HOSTNAME}"
  126. }
  127. ]
  128. },
  129. {
  130. "type": "section",
  131. "text": {
  132. "type": "mrkdwn",
  133. "text": "*Trigger Message*\n${alerttriggermessage}"
  134. }
  135. },
  136. {
  137. "type": "section",
  138. "text": {
  139. "type": "mrkdwn",
  140. "text": "*More info*\n${alertmoreinfourl}"
  141. }
  142. },
  143. {
  144. "type": "divider"
  145. },
  146. {
  147. "type": "context",
  148. "elements": [
  149. {
  150. "type": "image",
  151. "image_url": "https://raw.githubusercontent.com/GameServerManagers/LinuxGSM/master/lgsm/data/alert_discord_logo.jpg",
  152. "alt_text": "LinuxGSM Logo"
  153. },
  154. {
  155. "type": "plain_text",
  156. "text": "Powered by LinuxGSM ${version}",
  157. "emoji": true
  158. }
  159. ]
  160. },
  161. {
  162. "type": "divider"
  163. }
  164. ]
  165. }
  166. EOF
  167. )
  168. fn_print_dots "Sending Slack alert"
  169. slacksend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${slackwebhook}")
  170. if [ "${slacksend}" == "ok" ]; then
  171. fn_print_ok_nl "Sending Slack alert"
  172. fn_script_log_pass "Sending Slack alert"
  173. else
  174. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  175. fn_script_log_fatal "Sending Slack alert: ${slacksend}"
  176. fi