alert.sh 7.6 KB

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