alert_slack.sh 1.9 KB

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