alert.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. alerttitle="Alert - ${selfname} - Test"
  27. alertemoji="🚧"
  28. alertsound="1"
  29. alerturl="not enabled"
  30. alertmessage="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. alerttitle="Alert - ${selfname} - Restarted"
  38. alertemoji="🚨"
  39. alertsound="2"
  40. alerturl="not enabled"
  41. alertmessage="${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. alerttitle="Alert - ${selfname} - Restarted"
  49. alertemoji="🚨"
  50. alertsound="2"
  51. alerturl="not enabled"
  52. alertmessage="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. alerttitle="Alert - ${selfname} - Updated"
  60. alertemoji="🎮"
  61. alertsound="1"
  62. alerturl="not enabled"
  63. alertmessage="${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. alerttitle="Alert - ${selfname} - Update available"
  71. alertemoji="🎮"
  72. alertsound="1"
  73. alerturl="not enabled"
  74. alertmessage="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. alerttitle="Alert - ${selfname}: Permissions error"
  82. alertemoji="❗"
  83. alertsound="2"
  84. alerturl="not enabled"
  85. alertmessage="${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. alerttitle="Alert - ${selfname} - New _default.cfg"
  93. alertemoji="📄"
  94. alertsound="1"
  95. alerturl="not enabled"
  96. alertmessage="${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. alerttitle="Alert - ${selfname} - Wiped"
  104. alertemoji="💿"
  105. alertsound="1"
  106. alerturl="not enabled"
  107. alertmessage="${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. # Allow Alert to display gamedig info if available.
  117. if [ "${querystatus}" != "0" ]; then
  118. if [ -n "${maxplayers}" ]; then
  119. alertplayerstitle="Maxplayers"
  120. alertplayers="${maxplayers}"
  121. fi
  122. else
  123. if [ -n "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  124. alertplayerstitle="Current Players"
  125. alertplayers="${gdplayers}/${gdmaxplayers}"
  126. elif [ -n "${gdplayers}" ]&&[ -n "${maxplayers}" ]; then
  127. alertplayerstitle="Current Players"
  128. alertplayers="${gdplayers}/${maxplayers}"
  129. elif [ -z "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  130. alertplayerstitle="Current Players"
  131. alertplayers="-1/${gdmaxplayers}"
  132. elif [ -n "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]; then
  133. alertplayerstitle="Current Players"
  134. alertplayers="${gdplayers}/∞"
  135. elif [ -z "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]&&[ -n "${maxplayers}" ]; then
  136. alertplayerstitle="Maxplayers"
  137. alertplayers="${maxplayers}"
  138. fi
  139. fi
  140. if [ -z "${alertplayers}" ]; then
  141. alertplayerstitle="Current Players"
  142. alertplayers="Unknown"
  143. fi
  144. if [ -n "${gdmap}" ]; then
  145. alertmap="${gdmap}"
  146. else
  147. alertmap="Unknown"
  148. fi
  149. if [ -n "${gdversion}" ]; then
  150. alertversion="${gdversion}"
  151. else
  152. alertversion="Unknown"
  153. fi
  154. # Images
  155. mapimagestatus="$(curl -o /dev/null -s -w "%{http_code}\n" https://raw.githubusercontent.com/${githubuser}/game-server-map-images/main/${shortname}/${alertmap}.jpg)"
  156. if [ -n "${gdmap}" ]&&[ "${mapimagestatus}" == "200" ]; then
  157. alertimage="https://raw.githubusercontent.com/${githubuser}/game-server-map-images/main/${shortname}/${gdmap}.jpg"
  158. elif [ -n "${appid}" ]; then
  159. alertimage="https://cdn.cloudflare.steamstatic.com/steam/apps/${gameappid}/header.jpg"
  160. else
  161. alertimage="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameheaders/${shortname}-header.jpg"
  162. fi
  163. alerticon="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameicons/${shortname}-icon.png"
  164. if [ "${alert}" == "permissions" ]; then
  165. fn_alert_permissions
  166. elif [ "${alert}" == "restart" ]; then
  167. fn_alert_restart
  168. elif [ "${alert}" == "restartquery" ]; then
  169. fn_alert_restart_query
  170. elif [ "${alert}" == "test" ]; then
  171. fn_alert_test
  172. elif [ "${alert}" == "update" ]; then
  173. fn_alert_update
  174. elif [ "${alert}" == "check-update" ]; then
  175. fn_alert_check_update
  176. elif [ "${alert}" == "config" ]; then
  177. fn_alert_config
  178. elif [ "${alert}" == "wipe" ]; then
  179. fn_alert_wipe
  180. fi
  181. # Generate alert log.
  182. fn_alert_log
  183. # Generates the more info link.
  184. if [ "${postalert}" == "on" ]&&[ -n "${postalert}" ]; then
  185. exitbypass=1
  186. command_postdetails.sh
  187. fn_firstcommand_reset
  188. unset exitbypass
  189. elif [ "${postalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  190. fn_print_warn_nl "More Info not enabled"
  191. fn_script_log_warn "More Info alerts not enabled"
  192. fi
  193. if [ "${discordalert}" == "on" ]&&[ -n "${discordalert}" ]; then
  194. alert_discord.sh
  195. elif [ "${discordalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  196. fn_print_warn_nl "Discord alerts not enabled"
  197. fn_script_log_warn "Discord alerts not enabled"
  198. elif [ -z "${discordtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  199. fn_print_error_nl "Discord token not set"
  200. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  201. fn_script_error "Discord token not set"
  202. fi
  203. if [ "${emailalert}" == "on" ]&&[ -n "${email}" ]; then
  204. alert_email.sh
  205. elif [ "${emailalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  206. fn_print_warn_nl "Email alerts not enabled"
  207. fn_script_log_warn "Email alerts not enabled"
  208. elif [ -z "${email}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  209. fn_print_error_nl "Email not set"
  210. fn_script_log_error "Email not set"
  211. fi
  212. if [ "${gotifyalert}" == "on" ]&&[ -n "${gotifyalert}" ]; then
  213. alert_gotify.sh
  214. elif [ "${gotifyalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  215. fn_print_warn_nl "Gotify alerts not enabled"
  216. fn_script_log_warn "Gotify alerts not enabled"
  217. elif [ -z "${gotifytoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  218. fn_print_error_nl "Gotify token not set"
  219. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  220. fn_script_error "Gotify token not set"
  221. elif [ -z "${gotifywebhook}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  222. fn_print_error_nl "Gotify webhook not set"
  223. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  224. fn_script_error "Gotify webhook not set"
  225. fi
  226. if [ "${iftttalert}" == "on" ]&&[ -n "${iftttalert}" ]; then
  227. alert_ifttt.sh
  228. elif [ "${iftttalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  229. fn_print_warn_nl "IFTTT alerts not enabled"
  230. fn_script_log_warn "IFTTT alerts not enabled"
  231. elif [ -z "${ifttttoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  232. fn_print_error_nl "IFTTT token not set"
  233. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  234. fn_script_error "IFTTT token not set"
  235. fi
  236. if [ "${mailgunalert}" == "on" ]&&[ -n "${mailgunalert}" ]; then
  237. alert_mailgun.sh
  238. elif [ "${mailgunalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  239. fn_print_warn_nl "Mailgun alerts not enabled"
  240. fn_script_log_warn "Mailgun alerts not enabled"
  241. elif [ -z "${mailguntoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  242. fn_print_error_nl "Mailgun token not set"
  243. echo -e "* https://docs.linuxgsm.com/alerts/mailgun"
  244. fn_script_error "Mailgun token not set"
  245. fi
  246. if [ "${pushbulletalert}" == "on" ]&&[ -n "${pushbullettoken}" ]; then
  247. alert_pushbullet.sh
  248. elif [ "${pushbulletalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  249. fn_print_warn_nl "Pushbullet alerts not enabled"
  250. fn_script_log_warn "Pushbullet alerts not enabled"
  251. elif [ -z "${pushbullettoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  252. fn_print_error_nl "Pushbullet token not set"
  253. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  254. fn_script_error "Pushbullet token not set"
  255. fi
  256. if [ "${pushoveralert}" == "on" ]&&[ -n "${pushoveralert}" ]; then
  257. alert_pushover.sh
  258. elif [ "${pushoveralert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  259. fn_print_warn_nl "Pushover alerts not enabled"
  260. fn_script_log_warn "Pushover alerts not enabled"
  261. elif [ -z "${pushovertoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  262. fn_print_error_nl "Pushover token not set"
  263. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  264. fn_script_error "Pushover token not set"
  265. fi
  266. if [ "${telegramalert}" == "on" ]&&[ -n "${telegramtoken}" ]; then
  267. alert_telegram.sh
  268. elif [ "${telegramalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  269. fn_print_warn_nl "Telegram Messages not enabled"
  270. fn_script_log_warn "Telegram Messages not enabled"
  271. elif [ -z "${telegramtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  272. fn_print_error_nl "Telegram token not set."
  273. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  274. fn_script_error "Telegram token not set."
  275. elif [ -z "${telegramchatid}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  276. fn_print_error_nl "Telegram chat id not set."
  277. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  278. fn_script_error "Telegram chat id not set."
  279. fi
  280. if [ "${rocketchatalert}" == "on" ]&&[ -n "${rocketchatalert}" ]; then
  281. alert_rocketchat.sh
  282. elif [ "${rocketchatalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  283. fn_print_warn_nl "Rocketchat alerts not enabled"
  284. fn_script_log_warn "Rocketchat alerts not enabled"
  285. elif [ -z "${rocketchattoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  286. fn_print_error_nl "Rocketchat token not set"
  287. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  288. fn_script_error "Rocketchat token not set"
  289. fi
  290. if [ "${slackalert}" == "on" ]&&[ -n "${slackalert}" ]; then
  291. alert_slack.sh
  292. elif [ "${slackalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  293. fn_print_warn_nl "Slack alerts not enabled"
  294. fn_script_log_warn "Slack alerts not enabled"
  295. elif [ -z "${slacktoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  296. fn_print_error_nl "Slack token not set"
  297. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  298. fn_script_error "Slack token not set"
  299. fi