alert.sh 8.2 KB

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