alert_slack.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/bash
  2. # LinuxGSM alert_slack.sh function
  3. # Author: Kenneth Lindeof
  4. # Website: https://linuxgsm.com
  5. # Description: Sends Slack alert.
  6. local modulename="ALERT"
  7. local commandaction="Alert"
  8. local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  9. if ! command -v jq > /dev/null; then
  10. fn_print_fail_nl "Sending Slack alert: jq is missing."
  11. fn_script_log_fatal "Sending Slack alert: jq is missing."
  12. fi
  13. json=$(cat <<EOF
  14. {
  15. "attachments": [
  16. {
  17. "color": "#36a64f",
  18. "blocks": [
  19. {
  20. "type": "section",
  21. "text": {
  22. "type": "mrkdwn",
  23. "text": "*LinuxGSM Alert*"
  24. }
  25. },
  26. {
  27. "type": "section",
  28. "text": {
  29. "type": "mrkdwn",
  30. "text": "*${alertemoji} ${alertsubject}* \n ${alertbody}"
  31. }
  32. },
  33. {
  34. "type": "divider"
  35. },
  36. {
  37. "type": "section",
  38. "fields": [
  39. {
  40. "type": "mrkdwn",
  41. "text": "*Game:* \n ${gamename}"
  42. },
  43. {
  44. "type": "mrkdwn",
  45. "text": "*Server IP:* \n ${alertip}:${port}"
  46. },
  47. {
  48. "type": "mrkdwn",
  49. "text": "*Server Name:* \n ${servername}"
  50. }
  51. ]
  52. },
  53. {
  54. "type": "section",
  55. "text": {
  56. "type": "mrkdwn",
  57. "text": "Hostname: ${HOSTNAME} / More info: ${alerturl}"
  58. }
  59. }
  60. ]
  61. }
  62. ]
  63. }
  64. EOF
  65. )
  66. fn_print_dots "Sending Slack alert"
  67. slacksend=$(curl -sSL -H "Content-Type: application/json" -X POST -d "$(echo -n "$json" | jq -c .)" "${slackwebhook}")
  68. if [ "${slacksend}" == "ok" ]; then
  69. fn_print_ok_nl "Sending Slack alert"
  70. fn_script_log_pass "Sending Slack alert"
  71. else
  72. fn_print_fail_nl "Sending Slack alert: ${slacksend}"
  73. fn_script_log_fatal "Sending Slack alert: ${slacksend}"
  74. fi