alert.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. alertdescription="Testing LinuxGSM Alert. No action to be taken."
  31. # Green
  32. alertcolourhex="#cdcd00"
  33. alertcolourdec="13487360"
  34. }
  35. fn_alert_restart(){
  36. fn_script_log_info "Sending alert: Restarted: ${selfname}, ${executable} is not running"
  37. alertsubject="Alert - ${selfname} - Restarted"
  38. alertemoji="🚨"
  39. alertsound="2"
  40. alerturl="not enabled"
  41. alertdescription="${selfname} is not running. Game Server has been restarted."
  42. # Red
  43. alertcolourhex="#cd0000"
  44. alertcolourdec="13434880"
  45. }
  46. fn_alert_restart_query(){
  47. fn_script_log_info "Sending alert: Restarted: ${selfname}"
  48. alertsubject="Alert - ${selfname} - Restarted"
  49. alertemoji="🚨"
  50. alertsound="2"
  51. alerturl="not enabled"
  52. alertdescription="Unable to query ${selfname}. Game server has been restarted"
  53. # Red
  54. alertcolourhex="#cd0000"
  55. alertcolourdec="13434880"
  56. }
  57. fn_alert_update(){
  58. fn_script_log_info "Sending alert: Updated: ${selfname}"
  59. alertsubject="Alert - ${selfname} - Updated"
  60. alertemoji="🎮"
  61. alertsound="1"
  62. alerturl="not enabled"
  63. alertdescription="${selfname} has received an update"
  64. # Green
  65. alertcolourhex="#00cd00"
  66. alertcolourdec="52480"
  67. }
  68. fn_alert_check_update(){
  69. fn_script_log_info "Sending alert: Update available"
  70. alertsubject="Alert - ${selfname} - Update available"
  71. alertemoji="🎮"
  72. alertsound="1"
  73. alerturl="not enabled"
  74. alertdescription="Update availablefor ${selfname}"
  75. # Blue
  76. alertcolourhex="#1e90ff"
  77. alertcolourdec="2003199"
  78. }
  79. fn_alert_permissions(){
  80. fn_script_log_info "Sending alert: Permissions error"
  81. alertsubject="Alert - ${selfname}: Permissions error"
  82. alertemoji="❗"
  83. alertsound="2"
  84. alerturl="not enabled"
  85. alertdescription="${selfname} has permissions issues"
  86. # Red
  87. alertcolourhex="#cd0000"
  88. alertcolourdec="13434880"
  89. }
  90. fn_alert_config(){
  91. fn_script_log_info "Sending alert: New _default.cfg"
  92. alertsubject="Alert - ${selfname} - New _default.cfg"
  93. alertemoji="📄"
  94. alertsound="1"
  95. alerturl="not enabled"
  96. alertdescription="${selfname} has received a new _default.cfg. Check file for changes."
  97. # Blue
  98. alertcolourhex="#1e90ff"
  99. alertcolourdec="2003199"
  100. }
  101. fn_alert_wipe(){
  102. fn_script_log_info "Sending alert: Wiped: ${selfname} wiped"
  103. alertsubject="Alert - ${selfname} - Wiped"
  104. alertemoji="💿"
  105. alertsound="1"
  106. alerturl="not enabled"
  107. alertdescription="${selfname} as been wiped."
  108. # Green
  109. alertcolourhex="#00cd00"
  110. alertcolourdec="52480"
  111. }
  112. # Gather info required for alert.
  113. info_distro.sh
  114. info_game.sh
  115. query_gamedig.sh
  116. # Images
  117. if [ -n "${appid}" ]; then
  118. alertimage="https://steamcdn-a.akamaihd.net/steam/apps/${appid}/header.jpg"
  119. else
  120. alertimage="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameheaders/${shortname}-header.jpg"
  121. fi
  122. alerticon="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameicons/${shortname}-icon.png"
  123. # Allow Alert to display gamedig info if available.
  124. if [ "${querystatus}" != "0" ]; then
  125. if [ -n "${maxplayers}" ]; then
  126. alertplayerstitle="Maxplayers"
  127. alertplayers="${maxplayers}"
  128. fi
  129. else
  130. if [ -n "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  131. alertplayerstitle="Current Players"
  132. alertplayers="${gdplayers}/${gdmaxplayers}"
  133. elif [ -n "${gdplayers}" ]&&[ -n "${maxplayers}" ]; then
  134. alertplayerstitle="Current Players"
  135. alertplayers="${gdplayers}/${maxplayers}"
  136. elif [ -z "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  137. alertplayerstitle="Current Players"
  138. alertplayers="-1/${gdmaxplayers}"
  139. elif [ -n "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]; then
  140. alertplayerstitle="Current Players"
  141. alertplayers="${gdplayers}/∞"
  142. elif [ -z "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]&&[ -n "${maxplayers}" ]; then
  143. alertplayerstitle="Maxplayers"
  144. alertplayers="${maxplayers}"
  145. fi
  146. fi
  147. if [ -z "${alertplayers}" ]; then
  148. alertplayerstitle="Current Players"
  149. alertplayers="Unknown"
  150. fi
  151. if [ -n "${gdmap}" ]; then
  152. alertmap="${gdmap}"
  153. else
  154. alertmap="Unknown"
  155. fi
  156. if [ "${alert}" == "permissions" ]; then
  157. fn_alert_permissions
  158. elif [ "${alert}" == "restart" ]; then
  159. fn_alert_restart
  160. elif [ "${alert}" == "restartquery" ]; then
  161. fn_alert_restart_query
  162. elif [ "${alert}" == "test" ]; then
  163. fn_alert_test
  164. elif [ "${alert}" == "update" ]; then
  165. fn_alert_update
  166. elif [ "${alert}" == "check-update" ]; then
  167. fn_alert_check_update
  168. elif [ "${alert}" == "config" ]; then
  169. fn_alert_config
  170. elif [ "${alert}" == "wipe" ]; then
  171. fn_alert_wipe
  172. fi
  173. # Generate alert log.
  174. fn_alert_log
  175. # Generates the more info link.
  176. if [ "${postalert}" == "on" ]&&[ -n "${postalert}" ]; then
  177. exitbypass=1
  178. command_postdetails.sh
  179. fn_firstcommand_reset
  180. unset exitbypass
  181. elif [ "${postalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  182. fn_print_warn_nl "More Info not enabled"
  183. fn_script_log_warn "More Info alerts not enabled"
  184. fi
  185. if [ "${discordalert}" == "on" ]&&[ -n "${discordalert}" ]; then
  186. alert_discord.sh
  187. elif [ "${discordalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  188. fn_print_warn_nl "Discord alerts not enabled"
  189. fn_script_log_warn "Discord alerts not enabled"
  190. elif [ -z "${discordtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  191. fn_print_error_nl "Discord token not set"
  192. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  193. fn_script_error "Discord token not set"
  194. fi
  195. if [ "${emailalert}" == "on" ]&&[ -n "${email}" ]; then
  196. alert_email.sh
  197. elif [ "${emailalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  198. fn_print_warn_nl "Email alerts not enabled"
  199. fn_script_log_warn "Email alerts not enabled"
  200. elif [ -z "${email}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  201. fn_print_error_nl "Email not set"
  202. fn_script_log_error "Email not set"
  203. fi
  204. if [ "${gotifyalert}" == "on" ]&&[ -n "${gotifyalert}" ]; then
  205. alert_gotify.sh
  206. elif [ "${gotifyalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  207. fn_print_warn_nl "Gotify alerts not enabled"
  208. fn_script_log_warn "Gotify alerts not enabled"
  209. elif [ -z "${gotifytoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  210. fn_print_error_nl "Gotify token not set"
  211. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  212. fn_script_error "Gotify token not set"
  213. elif [ -z "${gotifywebhook}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  214. fn_print_error_nl "Gotify webhook not set"
  215. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  216. fn_script_error "Gotify webhook not set"
  217. fi
  218. if [ "${iftttalert}" == "on" ]&&[ -n "${iftttalert}" ]; then
  219. alert_ifttt.sh
  220. elif [ "${iftttalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  221. fn_print_warn_nl "IFTTT alerts not enabled"
  222. fn_script_log_warn "IFTTT alerts not enabled"
  223. elif [ -z "${ifttttoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  224. fn_print_error_nl "IFTTT token not set"
  225. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  226. fn_script_error "IFTTT token not set"
  227. fi
  228. if [ "${mailgunalert}" == "on" ]&&[ -n "${mailgunalert}" ]; then
  229. alert_mailgun.sh
  230. elif [ "${mailgunalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  231. fn_print_warn_nl "Mailgun alerts not enabled"
  232. fn_script_log_warn "Mailgun alerts not enabled"
  233. elif [ -z "${mailguntoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  234. fn_print_error_nl "Mailgun token not set"
  235. echo -e "* https://docs.linuxgsm.com/alerts/mailgun"
  236. fn_script_error "Mailgun token not set"
  237. fi
  238. if [ "${pushbulletalert}" == "on" ]&&[ -n "${pushbullettoken}" ]; then
  239. alert_pushbullet.sh
  240. elif [ "${pushbulletalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  241. fn_print_warn_nl "Pushbullet alerts not enabled"
  242. fn_script_log_warn "Pushbullet alerts not enabled"
  243. elif [ -z "${pushbullettoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  244. fn_print_error_nl "Pushbullet token not set"
  245. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  246. fn_script_error "Pushbullet token not set"
  247. fi
  248. if [ "${pushoveralert}" == "on" ]&&[ -n "${pushoveralert}" ]; then
  249. alert_pushover.sh
  250. elif [ "${pushoveralert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  251. fn_print_warn_nl "Pushover alerts not enabled"
  252. fn_script_log_warn "Pushover alerts not enabled"
  253. elif [ -z "${pushovertoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  254. fn_print_error_nl "Pushover token not set"
  255. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  256. fn_script_error "Pushover token not set"
  257. fi
  258. if [ "${telegramalert}" == "on" ]&&[ -n "${telegramtoken}" ]; then
  259. alert_telegram.sh
  260. elif [ "${telegramalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  261. fn_print_warn_nl "Telegram Messages not enabled"
  262. fn_script_log_warn "Telegram Messages not enabled"
  263. elif [ -z "${telegramtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  264. fn_print_error_nl "Telegram token not set."
  265. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  266. fn_script_error "Telegram token not set."
  267. elif [ -z "${telegramchatid}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  268. fn_print_error_nl "Telegram chat id not set."
  269. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  270. fn_script_error "Telegram chat id not set."
  271. fi
  272. if [ "${rocketchatalert}" == "on" ]&&[ -n "${rocketchatalert}" ]; then
  273. alert_rocketchat.sh
  274. elif [ "${rocketchatalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  275. fn_print_warn_nl "Rocketchat alerts not enabled"
  276. fn_script_log_warn "Rocketchat alerts not enabled"
  277. elif [ -z "${rocketchattoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  278. fn_print_error_nl "Rocketchat token not set"
  279. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  280. fn_script_error "Rocketchat token not set"
  281. fi
  282. if [ "${slackalert}" == "on" ]&&[ -n "${slackalert}" ]; then
  283. alert_slack.sh
  284. elif [ "${slackalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  285. fn_print_warn_nl "Slack alerts not enabled"
  286. fn_script_log_warn "Slack alerts not enabled"
  287. elif [ -z "${slacktoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  288. fn_print_error_nl "Slack token not set"
  289. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  290. fn_script_error "Slack token not set"
  291. fi