alert.sh 12 KB

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