alert.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/bin/bash
  2. # LinuxGSM alert.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://linuxgsm.com
  5. # Description: Overall function for managing alerts.
  6. functionselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  7. fn_alert_log(){
  8. if [ -f "${alertlog}" ]; then
  9. rm -f "${alertlog:?}"
  10. fi
  11. {
  12. fn_info_message_head
  13. fn_info_message_distro
  14. fn_info_message_server_resource
  15. fn_info_message_gameserver_resource
  16. fn_info_message_gameserver
  17. fn_info_logs
  18. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${alertlog}" > /dev/null 2>&1
  19. }
  20. fn_alert_test(){
  21. fn_script_log_info "Sending test alert"
  22. alertsubject="Alert - ${selfname} - Test"
  23. alertemoji="🚧"
  24. alertsound="1"
  25. alerturl="not enabled"
  26. alertbody="Testing LinuxGSM Alert. No action to be taken."
  27. }
  28. fn_alert_restart(){
  29. fn_script_log_info "Sending alert: Restarted: ${executable} not running"
  30. alertsubject="Alert - ${selfname} - Restarted"
  31. alertemoji="🚨"
  32. alertsound="2"
  33. alerturl="not enabled"
  34. alertbody="${selfname} ${executable} not running"
  35. }
  36. fn_alert_restart_query(){
  37. fn_script_log_info "Sending alert: Restarted: ${selfname}"
  38. alertsubject="Alert - ${selfname} - Restarted"
  39. alertemoji="🚨"
  40. alertsound="2"
  41. alerturl="not enabled"
  42. alertbody="Unable to query: ${selfname}"
  43. }
  44. fn_alert_update(){
  45. fn_script_log_info "Sending alert: Updated"
  46. alertsubject="Alert - ${selfname} - Updated"
  47. alertemoji="🎮"
  48. alertsound="1"
  49. alerturl="not enabled"
  50. alertbody="${gamename} received update"
  51. }
  52. fn_alert_permissions(){
  53. fn_script_log_info "Sending alert: Permissions error"
  54. alertsubject="Alert - ${selfname}: Permissions error"
  55. alertemoji="❗"
  56. alertsound="2"
  57. alerturl="not enabled"
  58. alertbody="${selfname} has permissions issues"
  59. }
  60. fn_alert_config(){
  61. fn_script_log_info "Sending alert: New _default.cfg"
  62. alertsubject="Alert - ${selfname} - New _default.cfg"
  63. alertemoji="🎮"
  64. alertsound="1"
  65. alerturl="not enabled"
  66. alertbody="${selfname} has received a new _default.cfg. Check file for changes."
  67. }
  68. info_distro.sh
  69. info_config.sh
  70. info_messages.sh
  71. # JSON-safe escaping string. - use this is json based alerts
  72. escaped_servername=$(echo -n "${servername}" | jq -sRr "@json")
  73. escaped_alertbody=$(echo -n "${alertbody}" | jq -sRr "@json")
  74. if [ "${alert}" == "permissions" ]; then
  75. fn_alert_permissions
  76. elif [ "${alert}" == "restart" ]; then
  77. fn_alert_restart
  78. elif [ "${alert}" == "restartquery" ]; then
  79. fn_alert_restart_query
  80. elif [ "${alert}" == "test" ]; then
  81. fn_alert_test
  82. elif [ "${alert}" == "update" ]; then
  83. fn_alert_update
  84. elif [ "${alert}" == "config" ]; then
  85. fn_alert_config
  86. fi
  87. # Generate alert log.
  88. fn_alert_log
  89. # Generates the more info link.
  90. if [ "${postalert}" == "on" ]&&[ -n "${postalert}" ]; then
  91. exitbypass=1
  92. command_postdetails.sh
  93. fn_firstcommand_reset
  94. elif [ "${postalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  95. fn_print_warn_nl "More Info not enabled"
  96. fn_script_log_warn "More Info alerts not enabled"
  97. elif [ -z "${posttarget}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  98. fn_print_error_nl "posttarget not set"
  99. fn_script_error "posttarget not set"
  100. elif [ -z "${postdays}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  101. fn_print_error_nl "postdays not set"
  102. fn_script_error "postdays not set"
  103. fi
  104. if [ "${discordalert}" == "on" ]&&[ -n "${discordalert}" ]; then
  105. alert_discord.sh
  106. elif [ "${discordalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  107. fn_print_warn_nl "Discord alerts not enabled"
  108. fn_script_log_warn "Discord alerts not enabled"
  109. elif [ -z "${discordtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  110. fn_print_error_nl "Discord token not set"
  111. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  112. fn_script_error "Discord token not set"
  113. fi
  114. if [ "${emailalert}" == "on" ]&&[ -n "${email}" ]; then
  115. alert_email.sh
  116. elif [ "${emailalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  117. fn_print_warn_nl "Email alerts not enabled"
  118. fn_script_log_warn "Email alerts not enabled"
  119. elif [ -z "${email}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  120. fn_print_error_nl "Email not set"
  121. fn_script_log_error "Email not set"
  122. fi
  123. if [ "${iftttalert}" == "on" ]&&[ -n "${iftttalert}" ]; then
  124. alert_ifttt.sh
  125. elif [ "${iftttalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  126. fn_print_warn_nl "IFTTT alerts not enabled"
  127. fn_script_log_warn "IFTTT alerts not enabled"
  128. elif [ -z "${ifttttoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  129. fn_print_error_nl "IFTTT token not set"
  130. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  131. fn_script_error "IFTTT token not set"
  132. fi
  133. if [ "${mailgunalert}" == "on" ]&&[ -n "${mailgunalert}" ]; then
  134. alert_mailgun.sh
  135. elif [ "${mailgunalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  136. fn_print_warn_nl "Mailgun alerts not enabled"
  137. fn_script_log_warn "Mailgun alerts not enabled"
  138. elif [ -z "${mailguntoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  139. fn_print_error_nl "Mailgun token not set"
  140. echo -e "* https://docs.linuxgsm.com/alerts/mailgun"
  141. fn_script_error "Mailgun token not set"
  142. fi
  143. if [ "${pushbulletalert}" == "on" ]&&[ -n "${pushbullettoken}" ]; then
  144. alert_pushbullet.sh
  145. elif [ "${pushbulletalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  146. fn_print_warn_nl "Pushbullet alerts not enabled"
  147. fn_script_log_warn "Pushbullet alerts not enabled"
  148. elif [ -z "${pushbullettoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  149. fn_print_error_nl "Pushbullet token not set"
  150. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  151. fn_script_error "Pushbullet token not set"
  152. fi
  153. if [ "${pushoveralert}" == "on" ]&&[ -n "${pushoveralert}" ]; then
  154. alert_pushover.sh
  155. elif [ "${pushoveralert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  156. fn_print_warn_nl "Pushover alerts not enabled"
  157. fn_script_log_warn "Pushover alerts not enabled"
  158. elif [ -z "${pushovertoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  159. fn_print_error_nl "Pushover token not set"
  160. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  161. fn_script_error "Pushover token not set"
  162. fi
  163. if [ "${telegramalert}" == "on" ]&&[ -n "${telegramtoken}" ]; then
  164. alert_telegram.sh
  165. elif [ "${telegramalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  166. fn_print_warn_nl "Telegram Messages not enabled"
  167. fn_script_log_warn "Telegram Messages not enabled"
  168. elif [ -z "${telegramtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  169. fn_print_error_nl "Telegram token not set."
  170. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  171. fn_script_error "Telegram token not set."
  172. elif [ -z "${telegramchatid}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  173. fn_print_error_nl "Telegram chat id not set."
  174. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  175. fn_script_error "Telegram chat id not set."
  176. fi
  177. if [ "${rocketchatalert}" == "on" ]&&[ -n "${rocketchatalert}" ]; then
  178. alert_rocketchat.sh
  179. elif [ "${rocketchatalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  180. fn_print_warn_nl "Rocketchat alerts not enabled"
  181. fn_script_log_warn "Rocketchat alerts not enabled"
  182. elif [ -z "${rocketchattoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  183. fn_print_error_nl "Rocketchat token not set"
  184. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  185. fn_script_error "Rocketchat token not set"
  186. fi
  187. if [ "${slackalert}" == "on" ]&&[ -n "${slackalert}" ]; then
  188. alert_slack.sh
  189. elif [ "${slackalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  190. fn_print_warn_nl "Slack alerts not enabled"
  191. fn_script_log_warn "Slack alerts not enabled"
  192. elif [ -z "${slacktoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  193. fn_print_error_nl "Slack token not set"
  194. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  195. fn_script_error "Slack token not set"
  196. fi