alert_slack.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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": "*Hostname*\n${HOSTNAME}"
  54. },
  55. {
  56. "type": "mrkdwn",
  57. "text": "*Server Time*\n$(date)"
  58. }
  59. ],
  60. "accessory": {
  61. "type": "image",
  62. "image_url": "${alerticon}",
  63. "alt_text": "cute cat"
  64. }
  65. },
  66. {
  67. "type": "context",
  68. "elements": [
  69. {
  70. "type": "image",
  71. "image_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  72. "alt_text": "LinuxGSM icon"
  73. },
  74. {
  75. "type": "plain_text",
  76. "text": "Sent by LinuxGSM ${version}",
  77. "emoji": true
  78. }
  79. ]
  80. }
  81. ]
  82. }
  83. ]
  84. }
  85. EOF
  86. )
  87. if [ -n "${querytype}" ]; then
  88. json+=$(
  89. cat << EOF
  90. {
  91. "type": "section",
  92. "text": {
  93. "type": "mrkdwn",
  94. "text": "*Is my Game Server Online?*\n<https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}|Check here>"
  95. }
  96. },
  97. EOF
  98. )
  99. fi
  100. if [ -n "${alerturl}" ]; then
  101. json+=$(
  102. cat << EOF
  103. {
  104. "type": "section",
  105. "text": {
  106. "type": "mrkdwn",
  107. "text": "*More info*\n<${alerturl}|${alerturl}>"
  108. }
  109. },
  110. EOF
  111. )
  112. fi
  113. json+=$(
  114. cat << EOF
  115. {
  116. "attachments": [
  117. {
  118. "color": "${alertcolourhex}",
  119. "blocks": [
  120. {
  121. "type": "header",
  122. "text": {
  123. "type": "mrkdwn",
  124. "text": "${alerttitle}",
  125. "emoji": true
  126. }
  127. },
  128. {
  129. "type": "divider"
  130. },
  131. {
  132. "type": "section",
  133. "text": {
  134. "type": "mrkdwn",
  135. "text": "*Server Name*\n${servername}"
  136. }
  137. },
  138. {
  139. "type": "section",
  140. "text": {
  141. "type": "mrkdwn",
  142. "text": "*Information*\n${alertmessage}"
  143. }
  144. },
  145. {
  146. "type": "section",
  147. "fields": [
  148. {
  149. "type": "mrkdwn",
  150. "text": "*Game*\n${gamename}"
  151. },
  152. {
  153. "type": "mrkdwn",
  154. "text": "*Server IP*\n\`${alertip}:${port}\`"
  155. },
  156. {
  157. "type": "mrkdwn",
  158. "text": "*Hostname*\n${HOSTNAME}"
  159. },
  160. {
  161. "type": "mrkdwn",
  162. "text": "*Server Time*\n$(date)"
  163. }
  164. ],
  165. "accessory": {
  166. "type": "image",
  167. "image_url": "${alerticon}",
  168. "alt_text": "cute cat"
  169. }
  170. },
  171. {
  172. "type": "section",
  173. "text": {
  174. "type": "mrkdwn",
  175. "text": "*Server Time*\n${alertmessage}"
  176. }
  177. },
  178. {
  179. "type": "context",
  180. "elements": [
  181. {
  182. "type": "image",
  183. "image_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  184. "alt_text": "LinuxGSM icon"
  185. },
  186. {
  187. "type": "plain_text",
  188. "text": "Sent by LinuxGSM ${version}",
  189. "emoji": true
  190. }
  191. ]
  192. }
  193. ]
  194. }
  195. ]
  196. }
  197. EOF
  198. )
  199. fn_print_dots "Sending Slack alert"
  200. slacksend=$(curl --connect-timeout 3 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${slackwebhook}")
  201. if [ "${slacksend}" == "ok" ]; then
  202. fn_print_ok_nl "Sending Slack alert"
  203. fn_script_log_pass "Sending Slack alert"
  204. else
  205. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  206. fn_script_log_fail "Sending Slack alert: ${slacksend}"
  207. fi