alert_slack.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. jsonnoinfo=$(
  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. jsoninfo=$(
  88. cat << EOF
  89. {
  90. "attachments": [
  91. {
  92. "color": "${alertcolourhex}",
  93. "blocks": [
  94. {
  95. "type": "header",
  96. "text": {
  97. "type": "mrkdwn",
  98. "text": "${alerttitle}",
  99. "emoji": true
  100. }
  101. },
  102. {
  103. "type": "divider"
  104. },
  105. {
  106. "type": "section",
  107. "text": {
  108. "type": "mrkdwn",
  109. "text": "*Server Name*\n${servername}"
  110. }
  111. },
  112. {
  113. "type": "section",
  114. "text": {
  115. "type": "mrkdwn",
  116. "text": "*Information*\n${alertmessage}"
  117. }
  118. },
  119. {
  120. "type": "section",
  121. "fields": [
  122. {
  123. "type": "mrkdwn",
  124. "text": "*Game*\n${gamename}"
  125. },
  126. {
  127. "type": "mrkdwn",
  128. "text": "*Server IP*\n\`${alertip}:${port}\`"
  129. },
  130. {
  131. "type": "mrkdwn",
  132. "text": "*Hostname*\n${HOSTNAME}"
  133. },
  134. {
  135. "type": "mrkdwn",
  136. "text": "*Server Time*\n$(date)"
  137. }
  138. ],
  139. "accessory": {
  140. "type": "image",
  141. "image_url": "${alerticon}",
  142. "alt_text": "cute cat"
  143. }
  144. },
  145. {
  146. "type": "section",
  147. "text": {
  148. "type": "mrkdwn",
  149. "text": "*Server Time*\n${alertmessage}"
  150. }
  151. },
  152. {
  153. "type": "context",
  154. "elements": [
  155. {
  156. "type": "image",
  157. "image_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  158. "alt_text": "LinuxGSM icon"
  159. },
  160. {
  161. "type": "plain_text",
  162. "text": "Sent by LinuxGSM ${version}",
  163. "emoji": true
  164. }
  165. ]
  166. }
  167. ]
  168. }
  169. ]
  170. }
  171. EOF
  172. )
  173. if [ -z "${alerturl}" ]; then
  174. json="${jsonnoinfo}"
  175. else
  176. json="${jsoninfo}"
  177. fi
  178. fn_print_dots "Sending Slack alert"
  179. slacksend=$(curl --connect-timeout 3 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${slackwebhook}")
  180. if [ "${slacksend}" == "ok" ]; then
  181. fn_print_ok_nl "Sending Slack alert"
  182. fn_script_log_pass "Sending Slack alert"
  183. else
  184. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  185. fn_script_log_fail "Sending Slack alert: ${slacksend}"
  186. fi