alert_slack.sh 1.8 KB

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