alert_slack.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. EOF
  62. )
  63. if [ -n "${querytype}" ]; then
  64. json+=$(
  65. cat << EOF
  66. ,
  67. {
  68. "type": "section",
  69. "text": {
  70. "type": "mrkdwn",
  71. "text": "*Is my Game Server Online?*\n<https://ismygameserver.online/${imgsoquerytype}/${alertip}:${queryport}|Check here>"
  72. }
  73. }
  74. EOF
  75. )
  76. fi
  77. if [ -n "${alerturl}" ]; then
  78. json+=$(
  79. cat << EOF
  80. ,
  81. {
  82. "type": "section",
  83. "text": {
  84. "type": "mrkdwn",
  85. "text": "*More info*\n<${alerturl}|${alerturl}>"
  86. }
  87. }
  88. EOF
  89. )
  90. fi
  91. json+=$(
  92. cat << EOF
  93. ,
  94. {
  95. "type": "context",
  96. "elements": [
  97. {
  98. "type": "image",
  99. "image_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg",
  100. "alt_text": "LinuxGSM icon"
  101. },
  102. {
  103. "type": "plain_text",
  104. "text": "Sent by LinuxGSM ${version}",
  105. "emoji": true
  106. }
  107. ]
  108. }
  109. ]
  110. }
  111. ]
  112. }
  113. EOF
  114. )
  115. fn_print_dots "Sending Slack alert"
  116. slacksend=$(curl --connect-timeout 3 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${slackwebhook}")
  117. if [ "${slacksend}" == "ok" ]; then
  118. fn_print_ok_nl "Sending Slack alert"
  119. fn_script_log_pass "Sending Slack alert"
  120. else
  121. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  122. fn_script_log_fail "Sending Slack alert: ${slacksend}"
  123. fi