alert.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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_message_script
  22. fn_info_message_backup
  23. fn_info_message_commandlineparms
  24. fn_info_message_ports_edit
  25. fn_info_message_ports
  26. fn_info_logs
  27. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${alertlog}" > /dev/null 2>&1
  28. }
  29. fn_alert_test(){
  30. fn_script_log_info "Sending alert: test"
  31. alerttitle="Alert - ${selfname} - Test"
  32. alertemoji="🚧"
  33. alertsound="1"
  34. alerttriggermessage="Testing LinuxGSM Alert. No action to be taken."
  35. # Green
  36. alertcolourhex="#cdcd00"
  37. alertcolourdec="13487360"
  38. }
  39. fn_alert_stopping(){
  40. fn_script_log_info "Sending alert: Stopping"
  41. alerttype="short"
  42. alerttitle="Alert - ${selfname} - Stopping"
  43. alertemoji="🟡"
  44. alertsound="1"
  45. alerttriggermessage="${selfname} is stopping."
  46. # Amber
  47. alertcolourhex="#ffbf00"
  48. alertcolourdec="16760576"
  49. }
  50. fn_alert_stopped(){
  51. fn_script_log_info "Sending alert: Stopped"
  52. alerttype="short"
  53. alerttitle="Alert - ${selfname} - Stopped"
  54. alertemoji="🔴"
  55. alertsound="1"
  56. alerttriggermessage="${selfname} has stopped."
  57. # Red
  58. alertcolourhex="#cd0000"
  59. alertcolourdec="13434880"
  60. }
  61. fn_alert_restart(){
  62. fn_script_log_info "Sending alert: Restarted: ${selfname}, ${executable} is not running"
  63. alerttitle="Alert - ${selfname} - Restarted"
  64. alertemoji="🚨"
  65. alertsound="2"
  66. alerttriggermessage="${selfname} is not running. Game Server has been restarted."
  67. # Red
  68. alertcolourhex="#cd0000"
  69. alertcolourdec="13434880"
  70. }
  71. fn_alert_restart_query(){
  72. fn_script_log_info "Sending alert: Restarted: ${selfname}"
  73. alerttype="short"
  74. alerttitle="Alert - ${selfname} - Restarted"
  75. alertemoji="🚨"
  76. alertsound="2"
  77. alerttriggermessage="Unable to query ${selfname}. Game server has been restarted."
  78. # Red
  79. alertcolourhex="#cd0000"
  80. alertcolourdec="13434880"
  81. }
  82. fn_alert_update(){
  83. fn_script_log_info "Sending alert: Game server updated: ${selfname}"
  84. alerttitle="Alert - ${selfname} - Game server updated"
  85. alerttype="short"
  86. alertemoji="🎉"
  87. alertsound="1"
  88. alerttriggermessage="${selfname} has received a game server update: ${localbuild}."
  89. # Green
  90. alertcolourhex="#00cd00"
  91. alertcolourdec="52480"
  92. }
  93. fn_alert_check_update(){
  94. fn_script_log_info "Sending alert: Game server update available"
  95. alerttitle="Alert - ${selfname} - Game server Update available"
  96. alerttype="short"
  97. alertemoji="💿"
  98. alertsound="1"
  99. alerttriggermessage="Game server update available for ${selfname}: ${remotebuild}."
  100. # Blue
  101. alertcolourhex="#1e90ff"
  102. alertcolourdec="2003199"
  103. }
  104. fn_alert_update_linuxgsm(){
  105. fn_script_log_info "Sending alert: LinuxGSM updated: ${selfname}"
  106. alerttitle="Alert - ${selfname} - LinuxGSM updated"
  107. alerttype="short"
  108. alertemoji="🎉"
  109. alertsound="1"
  110. alerttriggermessage="${selfname} has received a LinuxGSM update."
  111. # Green
  112. alertcolourhex="#00cd00"
  113. alertcolourdec="52480"
  114. }
  115. fn_alert_permissions(){
  116. fn_script_log_info "Sending alert: Permissions error"
  117. alerttitle="Alert - ${selfname}: Permissions error"
  118. alertemoji="❗"
  119. alertsound="2"
  120. alerttriggermessage="${selfname} has permissions issues."
  121. # Red
  122. alertcolourhex="#cd0000"
  123. alertcolourdec="13434880"
  124. }
  125. fn_alert_config(){
  126. fn_script_log_info "Sending alert: New _default.cfg"
  127. alerttitle="Alert - ${selfname} - New _default.cfg"
  128. alertemoji="📄"
  129. alertsound="1"
  130. alerttriggermessage="${selfname} has received a new _default.cfg. Check file for changes."
  131. # Blue
  132. alertcolourhex="#1e90ff"
  133. alertcolourdec="2003199"
  134. }
  135. fn_alert_wipe(){
  136. fn_script_log_info "Sending alert: Wiped: ${selfname} wiped"
  137. alerttitle="Alert - ${selfname} - Wiped"
  138. alertemoji="🧹"
  139. alertsound="1"
  140. alerttriggermessage="${selfname} as been wiped."
  141. # Green
  142. alertcolourhex="#00cd00"
  143. alertcolourdec="52480"
  144. }
  145. fn_alert_info(){
  146. fn_script_log_info "Sending info alert"
  147. alerttitle="Alert - ${selfname} - Info"
  148. alertemoji="👾"
  149. alertsound="1"
  150. alerttriggermessage="Game server information."
  151. # Green
  152. alertcolourhex="#1e90ff"
  153. alertcolourdec="2003199"
  154. }
  155. # Gather info required for alert.
  156. info_distro.sh
  157. info_game.sh
  158. query_gamedig.sh
  159. # Allow Alert to display gamedig info if available.
  160. if [ "${querystatus}" != "0" ]; then
  161. if [ -n "${maxplayers}" ]; then
  162. alertplayerstitle="Maxplayers"
  163. alertplayers="${maxplayers}"
  164. fi
  165. else
  166. if [ -n "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  167. alertplayerstitle="Current Players"
  168. alertplayers="${gdplayers}/${gdmaxplayers}"
  169. elif [ -n "${gdplayers}" ]&&[ -n "${maxplayers}" ]; then
  170. alertplayerstitle="Current Players"
  171. alertplayers="${gdplayers}/${maxplayers}"
  172. elif [ -z "${gdplayers}" ]&&[ -n "${gdmaxplayers}" ]; then
  173. alertplayerstitle="Current Players"
  174. alertplayers="-1/${gdmaxplayers}"
  175. elif [ -n "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]; then
  176. alertplayerstitle="Current Players"
  177. alertplayers="${gdplayers}/∞"
  178. elif [ -z "${gdplayers}" ]&&[ -z "${gdmaxplayers}" ]&&[ -n "${maxplayers}" ]; then
  179. alertplayerstitle="Maxplayers"
  180. alertplayers="${maxplayers}"
  181. fi
  182. fi
  183. if [ -z "${alertplayers}" ]; then
  184. alertplayerstitle="Current Players"
  185. alertplayers="Unknown"
  186. fi
  187. if [ -n "${gdmap}" ]; then
  188. alertmap="${gdmap}"
  189. else
  190. alertmap="Unknown"
  191. fi
  192. if [ -n "${gdversion}" ]; then
  193. alertversion="${gdversion}"
  194. else
  195. alertversion="Unknown"
  196. fi
  197. if [ "${postalert}" != "on" ]; then
  198. alertmoreinfourl="not enabled"
  199. fi
  200. alertdate="$(date)"
  201. # Images
  202. mapimagestatus="$(curl -o /dev/null -s -w "%{http_code}\n" https://raw.githubusercontent.com/${githubuser}/game-server-map-images/main/${shortname}/${alertmap}.jpg)"
  203. if [ -n "${gdmap}" ]&&[ "${mapimagestatus}" == "200" ]; then
  204. alertimage="https://raw.githubusercontent.com/${githubuser}/game-server-map-images/main/${shortname}/${gdmap}.jpg"
  205. alertimagealt="${gdmap}"
  206. elif [ -n "${appid}" ]; then
  207. alertimage="https://cdn.cloudflare.steamstatic.com/steam/apps/${gameappid}/header.jpg"
  208. alertimagealt="${gamename} header"
  209. else
  210. alertimage="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameheaders/${shortname}-header.jpg"
  211. alertimagealt="${gamename} header"
  212. fi
  213. alerticon="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameicons/${shortname}-icon.png"
  214. alerticonalt="${gamename} icon"
  215. if [ "${alert}" == "permissions" ]; then
  216. fn_alert_permissions
  217. elif [ "${alert}" == "stopping" ]; then
  218. fn_alert_stopping
  219. elif [ "${alert}" == "stopped" ]; then
  220. fn_alert_stopped
  221. elif [ "${alert}" == "restart" ]; then
  222. fn_alert_restart
  223. elif [ "${alert}" == "restartquery" ]; then
  224. fn_alert_restart_query
  225. elif [ "${alert}" == "test" ]; then
  226. fn_alert_test
  227. elif [ "${alert}" == "update" ]; then
  228. fn_alert_update
  229. elif [ "${alert}" == "check-update" ]; then
  230. fn_alert_check_update
  231. elif [ "${alert}" == "config" ]; then
  232. fn_alert_config
  233. elif [ "${alert}" == "wipe" ]; then
  234. fn_alert_wipe
  235. elif [ "${alert}" == "info" ]; then
  236. fn_alert_info
  237. fi
  238. # Generate alert log.
  239. fn_alert_log
  240. # Generates the more info link.
  241. if [ "${postalert}" == "on" ]&&[ -n "${postalert}" ]; then
  242. exitbypass=1
  243. command_postdetails.sh
  244. fn_firstcommand_reset
  245. unset exitbypass
  246. elif [ "${postalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  247. fn_print_warn_nl "More Info not enabled"
  248. fn_script_log_warn "More Info alerts not enabled"
  249. fi
  250. if [ "${discordalert}" == "on" ]&&[ -n "${discordalert}" ]; then
  251. alert_discord.sh
  252. elif [ "${discordalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  253. fn_print_warn_nl "Discord alerts not enabled"
  254. fn_script_log_warn "Discord alerts not enabled"
  255. elif [ -z "${discordtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  256. fn_print_error_nl "Discord token not set"
  257. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  258. fn_script_error "Discord token not set"
  259. fi
  260. if [ "${emailalert}" == "on" ]&&[ -n "${email}" ]; then
  261. alert_email.sh
  262. elif [ "${emailalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  263. fn_print_warn_nl "Email alerts not enabled"
  264. fn_script_log_warn "Email alerts not enabled"
  265. elif [ -z "${email}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  266. fn_print_error_nl "Email not set"
  267. fn_script_log_error "Email not set"
  268. fi
  269. if [ "${gotifyalert}" == "on" ]&&[ -n "${gotifyalert}" ]; then
  270. alert_gotify.sh
  271. elif [ "${gotifyalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  272. fn_print_warn_nl "Gotify alerts not enabled"
  273. fn_script_log_warn "Gotify alerts not enabled"
  274. elif [ -z "${gotifytoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  275. fn_print_error_nl "Gotify token not set"
  276. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  277. fn_script_error "Gotify token not set"
  278. elif [ -z "${gotifywebhook}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  279. fn_print_error_nl "Gotify webhook not set"
  280. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  281. fn_script_error "Gotify webhook not set"
  282. fi
  283. if [ "${iftttalert}" == "on" ]&&[ -n "${iftttalert}" ]; then
  284. alert_ifttt.sh
  285. elif [ "${iftttalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  286. fn_print_warn_nl "IFTTT alerts not enabled"
  287. fn_script_log_warn "IFTTT alerts not enabled"
  288. elif [ -z "${ifttttoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  289. fn_print_error_nl "IFTTT token not set"
  290. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  291. fn_script_error "IFTTT token not set"
  292. fi
  293. if [ "${mailgunalert}" == "on" ]&&[ -n "${mailgunalert}" ]; then
  294. alert_mailgun.sh
  295. elif [ "${mailgunalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  296. fn_print_warn_nl "Mailgun alerts not enabled"
  297. fn_script_log_warn "Mailgun alerts not enabled"
  298. elif [ -z "${mailguntoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  299. fn_print_error_nl "Mailgun token not set"
  300. echo -e "* https://docs.linuxgsm.com/alerts/mailgun"
  301. fn_script_error "Mailgun token not set"
  302. fi
  303. if [ "${pushbulletalert}" == "on" ]&&[ -n "${pushbullettoken}" ]; then
  304. alert_pushbullet.sh
  305. elif [ "${pushbulletalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  306. fn_print_warn_nl "Pushbullet alerts not enabled"
  307. fn_script_log_warn "Pushbullet alerts not enabled"
  308. elif [ -z "${pushbullettoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  309. fn_print_error_nl "Pushbullet token not set"
  310. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  311. fn_script_error "Pushbullet token not set"
  312. fi
  313. if [ "${pushoveralert}" == "on" ]&&[ -n "${pushoveralert}" ]; then
  314. alert_pushover.sh
  315. elif [ "${pushoveralert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  316. fn_print_warn_nl "Pushover alerts not enabled"
  317. fn_script_log_warn "Pushover alerts not enabled"
  318. elif [ -z "${pushovertoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  319. fn_print_error_nl "Pushover token not set"
  320. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  321. fn_script_error "Pushover token not set"
  322. fi
  323. if [ "${telegramalert}" == "on" ]&&[ -n "${telegramtoken}" ]; then
  324. alert_telegram.sh
  325. elif [ "${telegramalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  326. fn_print_warn_nl "Telegram Messages not enabled"
  327. fn_script_log_warn "Telegram Messages not enabled"
  328. elif [ -z "${telegramtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  329. fn_print_error_nl "Telegram token not set."
  330. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  331. fn_script_error "Telegram token not set."
  332. elif [ -z "${telegramchatid}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  333. fn_print_error_nl "Telegram chat id not set."
  334. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  335. fn_script_error "Telegram chat id not set."
  336. fi
  337. if [ "${rocketchatalert}" == "on" ]&&[ -n "${rocketchatalert}" ]; then
  338. alert_rocketchat.sh
  339. elif [ "${rocketchatalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  340. fn_print_warn_nl "Rocketchat alerts not enabled"
  341. fn_script_log_warn "Rocketchat alerts not enabled"
  342. elif [ -z "${rocketchattoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  343. fn_print_error_nl "Rocketchat token not set"
  344. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  345. fn_script_error "Rocketchat token not set"
  346. fi
  347. if [ "${slackalert}" == "on" ]&&[ -n "${slackalert}" ]; then
  348. alert_slack.sh
  349. elif [ "${slackalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  350. fn_print_warn_nl "Slack alerts not enabled"
  351. fn_script_log_warn "Slack alerts not enabled"
  352. elif [ -z "${slacktoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  353. fn_print_error_nl "Slack token not set"
  354. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  355. fn_script_error "Slack token not set"
  356. fi