alert.sh 7.9 KB

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