alert.sh 12 KB

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