alert_slack.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. # LinuxGSM alert_slack.sh function
  3. # Author: Kenneth Lindeof
  4. # Website: https://linuxgsm.com
  5. # Description: Sends Slack alert.
  6. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. if ! command -v jq > /dev/null; then
  8. fn_print_fail_nl "Sending Slack alert: jq is missing."
  9. fn_script_log_fatal "Sending Slack alert: jq is missing."
  10. fi
  11. json=$(cat <<EOF
  12. {
  13. "attachments": [
  14. {
  15. "blocks": [
  16. {
  17. "type": "section",
  18. "text": {
  19. "type": "mrkdwn",
  20. "text": "*${alertemoji} ${alertsubject} ${alertemoji}*"
  21. }
  22. },
  23. {
  24. "type": "section",
  25. "text": {
  26. "type": "mrkdwn",
  27. "text": "*${servername}*"
  28. }
  29. },
  30. {
  31. "type": "section",
  32. "text": {
  33. "type": "mrkdwn",
  34. "text": "${alertbody} \n More info: ${alerturl}"
  35. }
  36. },
  37. {
  38. "type": "divider"
  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. },
  53. {
  54. "type": "divider"
  55. },
  56. {
  57. "type": "section",
  58. "text": {
  59. "type": "mrkdwn",
  60. "text": "*Hostname:* ${HOSTNAME}"
  61. }
  62. }
  63. ]
  64. }
  65. ]
  66. }
  67. EOF
  68. )
  69. fn_print_dots "Sending Slack alert"
  70. slacksend=$(curl --connect-timeout 10 -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "$json" | jq -c .)" "${slackwebhook}")
  71. if [ "${slacksend}" == "ok" ]; then
  72. fn_print_ok_nl "Sending Slack alert"
  73. fn_script_log_pass "Sending Slack alert"
  74. else
  75. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  76. fn_script_log_fatal "Sending Slack alert: ${slacksend}"
  77. fi