alert_slack.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. json=$(
  9. cat << EOF
  10. {
  11. "attachments": [
  12. {
  13. "color": "#36a64f",
  14. "blocks": [
  15. {
  16. "type": "section",
  17. "text": {
  18. "type": "mrkdwn",
  19. "text": "*LinuxGSM Alert*"
  20. }
  21. },
  22. {
  23. "type": "section",
  24. "text": {
  25. "type": "mrkdwn",
  26. "text": "*${alertemoji} ${alertsubject}* \n ${alertbody}"
  27. }
  28. },
  29. {
  30. "type": "divider"
  31. },
  32. {
  33. "type": "section",
  34. "fields": [
  35. {
  36. "type": "mrkdwn",
  37. "text": "*Game:* \n ${gamename}"
  38. },
  39. {
  40. "type": "mrkdwn",
  41. "text": "*Server IP:* \n ${alertip}:${port}"
  42. },
  43. {
  44. "type": "mrkdwn",
  45. "text": "*Server Name:* \n ${servername}"
  46. }
  47. ]
  48. },
  49. {
  50. "type": "section",
  51. "text": {
  52. "type": "mrkdwn",
  53. "text": "Hostname: ${HOSTNAME} / More info: ${alerturl}"
  54. }
  55. }
  56. ]
  57. }
  58. ]
  59. }
  60. EOF
  61. )
  62. fn_print_dots "Sending Slack alert"
  63. slacksend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "${json}" | jq -c .)" "${slackwebhook}")
  64. if [ "${slacksend}" == "ok" ]; then
  65. fn_print_ok_nl "Sending Slack alert"
  66. fn_script_log_pass "Sending Slack alert"
  67. else
  68. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  69. fn_script_log_fatal "Sending Slack alert: ${slacksend}"
  70. fi