alert.sh 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 module for managing alerts.
  7. moduleselfname="$(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: ${remotebuildversion}"
  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: ${remotebuildversion}"
  65. }
  66. fn_alert_update_restart() {
  67. fn_script_log_info "Sending alert: Restarted"
  68. alertsubject="Alert - ${selfname} - Restarted"
  69. alertemoji="🎮"
  70. alertsound="1"
  71. alerturl="not enabled"
  72. alertbody="${gamename} received update: ${remotebuildversion} and required restarting"
  73. }
  74. fn_alert_permissions() {
  75. fn_script_log_info "Sending alert: Permissions error"
  76. alertsubject="Alert - ${selfname}: Permissions error"
  77. alertemoji="❗"
  78. alertsound="2"
  79. alerturl="not enabled"
  80. alertbody="${selfname} has permissions issues"
  81. }
  82. fn_alert_config() {
  83. fn_script_log_info "Sending alert: New _default.cfg"
  84. alertsubject="Alert - ${selfname} - New _default.cfg"
  85. alertemoji="🎮"
  86. alertsound="1"
  87. alerturl="not enabled"
  88. alertbody="${selfname} has received a new _default.cfg. Check file for changes."
  89. }
  90. if [ "${alert}" == "permissions" ]; then
  91. fn_alert_permissions
  92. elif [ "${alert}" == "restart" ]; then
  93. fn_alert_restart
  94. elif [ "${alert}" == "restartquery" ]; then
  95. fn_alert_restart_query
  96. elif [ "${alert}" == "test" ]; then
  97. fn_alert_test
  98. elif [ "${alert}" == "update" ]; then
  99. fn_alert_update
  100. elif [ "${alert}" == "check-update" ]; then
  101. fn_alert_check_update
  102. elif [ "${alert}" == "update-restart" ]; then
  103. fn_alert_update_restart
  104. elif [ "${alert}" == "config" ]; then
  105. fn_alert_config
  106. fi
  107. # Generate alert log.
  108. fn_alert_log
  109. # Generates the more info link.
  110. if [ "${postalert}" == "on" ] && [ -n "${postalert}" ]; then
  111. exitbypass=1
  112. command_postdetails.sh
  113. fn_firstcommand_reset
  114. unset exitbypass
  115. elif [ "${postalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  116. fn_print_warn_nl "More Info not enabled"
  117. fn_script_log_warn "More Info alerts not enabled"
  118. fi
  119. if [ "${discordalert}" == "on" ] && [ -n "${discordalert}" ]; then
  120. alert_discord.sh
  121. elif [ "${discordalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  122. fn_print_warn_nl "Discord alerts not enabled"
  123. fn_script_log_warn "Discord alerts not enabled"
  124. elif [ -z "${discordtoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  125. fn_print_error_nl "Discord token not set"
  126. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  127. fn_script_error "Discord token not set"
  128. fi
  129. if [ "${emailalert}" == "on" ] && [ -n "${email}" ]; then
  130. alert_email.sh
  131. elif [ "${emailalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  132. fn_print_warn_nl "Email alerts not enabled"
  133. fn_script_log_warn "Email alerts not enabled"
  134. elif [ -z "${email}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  135. fn_print_error_nl "Email not set"
  136. fn_script_log_error "Email not set"
  137. fi
  138. if [ "${gotifyalert}" == "on" ] && [ -n "${gotifyalert}" ]; then
  139. alert_gotify.sh
  140. elif [ "${gotifyalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  141. fn_print_warn_nl "Gotify alerts not enabled"
  142. fn_script_log_warn "Gotify alerts not enabled"
  143. elif [ -z "${gotifytoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  144. fn_print_error_nl "Gotify token not set"
  145. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  146. fn_script_error "Gotify token not set"
  147. elif [ -z "${gotifywebhook}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  148. fn_print_error_nl "Gotify webhook not set"
  149. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  150. fn_script_error "Gotify webhook not set"
  151. fi
  152. if [ "${iftttalert}" == "on" ] && [ -n "${iftttalert}" ]; then
  153. alert_ifttt.sh
  154. elif [ "${iftttalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  155. fn_print_warn_nl "IFTTT alerts not enabled"
  156. fn_script_log_warn "IFTTT alerts not enabled"
  157. elif [ -z "${ifttttoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  158. fn_print_error_nl "IFTTT token not set"
  159. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  160. fn_script_error "IFTTT token not set"
  161. fi
  162. if [ "${mailgunalert}" == "on" ] && [ -n "${mailgunalert}" ]; then
  163. alert_mailgun.sh
  164. elif [ "${mailgunalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  165. fn_print_warn_nl "Mailgun alerts not enabled"
  166. fn_script_log_warn "Mailgun alerts not enabled"
  167. elif [ -z "${mailguntoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  168. fn_print_error_nl "Mailgun token not set"
  169. echo -e "* https://docs.linuxgsm.com/alerts/mailgun"
  170. fn_script_error "Mailgun token not set"
  171. fi
  172. if [ "${pushbulletalert}" == "on" ] && [ -n "${pushbullettoken}" ]; then
  173. alert_pushbullet.sh
  174. elif [ "${pushbulletalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  175. fn_print_warn_nl "Pushbullet alerts not enabled"
  176. fn_script_log_warn "Pushbullet alerts not enabled"
  177. elif [ -z "${pushbullettoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  178. fn_print_error_nl "Pushbullet token not set"
  179. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  180. fn_script_error "Pushbullet token not set"
  181. fi
  182. if [ "${pushoveralert}" == "on" ] && [ -n "${pushoveralert}" ]; then
  183. alert_pushover.sh
  184. elif [ "${pushoveralert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  185. fn_print_warn_nl "Pushover alerts not enabled"
  186. fn_script_log_warn "Pushover alerts not enabled"
  187. elif [ -z "${pushovertoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  188. fn_print_error_nl "Pushover token not set"
  189. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  190. fn_script_error "Pushover token not set"
  191. fi
  192. if [ "${telegramalert}" == "on" ] && [ -n "${telegramtoken}" ]; then
  193. alert_telegram.sh
  194. elif [ "${telegramalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  195. fn_print_warn_nl "Telegram Messages not enabled"
  196. fn_script_log_warn "Telegram Messages not enabled"
  197. elif [ -z "${telegramtoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  198. fn_print_error_nl "Telegram token not set."
  199. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  200. fn_script_error "Telegram token not set."
  201. elif [ -z "${telegramchatid}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  202. fn_print_error_nl "Telegram chat id not set."
  203. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  204. fn_script_error "Telegram chat id not set."
  205. fi
  206. if [ "${rocketchatalert}" == "on" ] && [ -n "${rocketchatalert}" ]; then
  207. alert_rocketchat.sh
  208. elif [ "${rocketchatalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  209. fn_print_warn_nl "Rocketchat alerts not enabled"
  210. fn_script_log_warn "Rocketchat alerts not enabled"
  211. elif [ -z "${rocketchattoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  212. fn_print_error_nl "Rocketchat token not set"
  213. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  214. fn_script_error "Rocketchat token not set"
  215. fi
  216. if [ "${slackalert}" == "on" ] && [ -n "${slackalert}" ]; then
  217. alert_slack.sh
  218. elif [ "${slackalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  219. fn_print_warn_nl "Slack alerts not enabled"
  220. fn_script_log_warn "Slack alerts not enabled"
  221. elif [ -z "${slacktoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  222. fn_print_error_nl "Slack token not set"
  223. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  224. fn_script_error "Slack token not set"
  225. fi