alert_slack.sh 1.7 KB

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