alert_slack.sh 2.2 KB

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