alert_slack.sh 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "color": "#36a64f",
  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}"
  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. "type": "mrkdwn",
  54. "text": "*Server Name:* \n ${servername}"
  55. }
  56. ]
  57. },
  58. {
  59. "type": "section",
  60. "text": {
  61. "type": "mrkdwn",
  62. "text": "Hostname: ${HOSTNAME} / More info: ${alerturl}"
  63. }
  64. }
  65. ]
  66. }
  67. ]
  68. }
  69. EOF
  70. )
  71. fn_print_dots "Sending Slack alert"
  72. slacksend=$(curl -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "$json" | jq -c .)" "${slackwebhook}")
  73. if [ "${slacksend}" == "ok" ]; then
  74. fn_print_ok_nl "Sending Slack alert"
  75. fn_script_log_pass "Sending Slack alert"
  76. else
  77. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  78. fn_script_log_fatal "Sending Slack alert: ${slacksend}"
  79. fi