alert.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_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. info_distro.sh
  81. info_game.sh
  82. query_gamedig.sh
  83. # Allow Alert to display gamedig info if available.
  84. if [ "${querystatus}" != "0" ]; then
  85. if [ -n "${maxplayers}" ]; then
  86. alertplayerstitle="Maxplayers"
  87. alertplayers="-${maxplayers}"
  88. fi
  89. else
  90. if [ -n "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  91. alertplayerstitle="Current Players"
  92. alertplayers="${gdplayers}/${gdmaxplayers}"
  93. elif [ -n "${gdplayers}" ]&&[ -n "${maxplayers}" ]; then
  94. alertplayerstitle="Current Players"
  95. alertplayers="${gdplayers}/${maxplayers}"
  96. elif [ -z "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  97. alertplayerstitle="Current Players"
  98. alertplayers="-1/${gdmaxplayers}"
  99. elif [ -n "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]; then
  100. alertplayerstitle="Current Players"
  101. alertplayers="${gdplayers}/∞"
  102. elif [ -z "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]&&[ -n "${maxplayers}" ]; then
  103. alertplayerstitle="Maxplayers"
  104. alertplayers="${maxplayers}"
  105. fi
  106. fi
  107. if [ -n "${gdmap}" ]; then
  108. alertmap="${gdmap}"
  109. else
  110. alertmap="Unknown"
  111. fi
  112. if [ "${alert}" == "permissions" ]; then
  113. fn_alert_permissions
  114. elif [ "${alert}" == "restart" ]; then
  115. fn_alert_restart
  116. elif [ "${alert}" == "restartquery" ]; then
  117. fn_alert_restart_query
  118. elif [ "${alert}" == "test" ]; then
  119. fn_alert_test
  120. elif [ "${alert}" == "update" ]; then
  121. fn_alert_update
  122. elif [ "${alert}" == "check-update" ]; then
  123. fn_alert_check_update
  124. elif [ "${alert}" == "config" ]; then
  125. fn_alert_config
  126. fi
  127. # Generate alert log.
  128. fn_alert_log
  129. # Generates the more info link.
  130. if [ "${postalert}" == "on" ]&&[ -n "${postalert}" ]; then
  131. exitbypass=1
  132. command_postdetails.sh
  133. fn_firstcommand_reset
  134. unset exitbypass
  135. elif [ "${postalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  136. fn_print_warn_nl "More Info not enabled"
  137. fn_script_log_warn "More Info alerts not enabled"
  138. fi
  139. if [ "${discordalert}" == "on" ]&&[ -n "${discordalert}" ]; then
  140. alert_discord.sh
  141. elif [ "${discordalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  142. fn_print_warn_nl "Discord alerts not enabled"
  143. fn_script_log_warn "Discord alerts not enabled"
  144. elif [ -z "${discordtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  145. fn_print_error_nl "Discord token not set"
  146. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  147. fn_script_error "Discord token not set"
  148. fi
  149. if [ "${emailalert}" == "on" ]&&[ -n "${email}" ]; then
  150. alert_email.sh
  151. elif [ "${emailalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  152. fn_print_warn_nl "Email alerts not enabled"
  153. fn_script_log_warn "Email alerts not enabled"
  154. elif [ -z "${email}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  155. fn_print_error_nl "Email not set"
  156. fn_script_log_error "Email not set"
  157. fi
  158. if [ "${gotifyalert}" == "on" ]&&[ -n "${gotifyalert}" ]; then
  159. alert_gotify.sh
  160. elif [ "${gotifyalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  161. fn_print_warn_nl "Gotify alerts not enabled"
  162. fn_script_log_warn "Gotify alerts not enabled"
  163. elif [ -z "${gotifytoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  164. fn_print_error_nl "Gotify token not set"
  165. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  166. fn_script_error "Gotify token not set"
  167. elif [ -z "${gotifywebhook}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  168. fn_print_error_nl "Gotify webhook not set"
  169. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  170. fn_script_error "Gotify webhook not set"
  171. fi
  172. if [ "${iftttalert}" == "on" ]&&[ -n "${iftttalert}" ]; then
  173. alert_ifttt.sh
  174. elif [ "${iftttalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  175. fn_print_warn_nl "IFTTT alerts not enabled"
  176. fn_script_log_warn "IFTTT alerts not enabled"
  177. elif [ -z "${ifttttoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  178. fn_print_error_nl "IFTTT token not set"
  179. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  180. fn_script_error "IFTTT token not set"
  181. fi
  182. if [ "${mailgunalert}" == "on" ]&&[ -n "${mailgunalert}" ]; then
  183. alert_mailgun.sh
  184. elif [ "${mailgunalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  185. fn_print_warn_nl "Mailgun alerts not enabled"
  186. fn_script_log_warn "Mailgun alerts not enabled"
  187. elif [ -z "${mailguntoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  188. fn_print_error_nl "Mailgun token not set"
  189. echo -e "* https://docs.linuxgsm.com/alerts/mailgun"
  190. fn_script_error "Mailgun token not set"
  191. fi
  192. if [ "${pushbulletalert}" == "on" ]&&[ -n "${pushbullettoken}" ]; then
  193. alert_pushbullet.sh
  194. elif [ "${pushbulletalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  195. fn_print_warn_nl "Pushbullet alerts not enabled"
  196. fn_script_log_warn "Pushbullet alerts not enabled"
  197. elif [ -z "${pushbullettoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  198. fn_print_error_nl "Pushbullet token not set"
  199. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  200. fn_script_error "Pushbullet token not set"
  201. fi
  202. if [ "${pushoveralert}" == "on" ]&&[ -n "${pushoveralert}" ]; then
  203. alert_pushover.sh
  204. elif [ "${pushoveralert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  205. fn_print_warn_nl "Pushover alerts not enabled"
  206. fn_script_log_warn "Pushover alerts not enabled"
  207. elif [ -z "${pushovertoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  208. fn_print_error_nl "Pushover token not set"
  209. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  210. fn_script_error "Pushover token not set"
  211. fi
  212. if [ "${telegramalert}" == "on" ]&&[ -n "${telegramtoken}" ]; then
  213. alert_telegram.sh
  214. elif [ "${telegramalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  215. fn_print_warn_nl "Telegram Messages not enabled"
  216. fn_script_log_warn "Telegram Messages not enabled"
  217. elif [ -z "${telegramtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  218. fn_print_error_nl "Telegram token not set."
  219. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  220. fn_script_error "Telegram token not set."
  221. elif [ -z "${telegramchatid}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  222. fn_print_error_nl "Telegram chat id not set."
  223. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  224. fn_script_error "Telegram chat id not set."
  225. fi
  226. if [ "${rocketchatalert}" == "on" ]&&[ -n "${rocketchatalert}" ]; then
  227. alert_rocketchat.sh
  228. elif [ "${rocketchatalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  229. fn_print_warn_nl "Rocketchat alerts not enabled"
  230. fn_script_log_warn "Rocketchat alerts not enabled"
  231. elif [ -z "${rocketchattoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  232. fn_print_error_nl "Rocketchat token not set"
  233. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  234. fn_script_error "Rocketchat token not set"
  235. fi
  236. if [ "${slackalert}" == "on" ]&&[ -n "${slackalert}" ]; then
  237. alert_slack.sh
  238. elif [ "${slackalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  239. fn_print_warn_nl "Slack alerts not enabled"
  240. fn_script_log_warn "Slack alerts not enabled"
  241. elif [ -z "${slacktoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  242. fn_print_error_nl "Slack token not set"
  243. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  244. fn_script_error "Slack token not set"
  245. fi