alert_slack.sh 1.7 KB

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