alert.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. 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. elif [ "${alert}" == "info" ]; then
  232. fn_alert_info
  233. fi
  234. # Generate alert log.
  235. fn_alert_log
  236. # Generates the more info link.
  237. if [ "${postalert}" == "on" ]&&[ -n "${postalert}" ]; then
  238. exitbypass=1
  239. command_postdetails.sh
  240. fn_firstcommand_reset
  241. unset exitbypass
  242. elif [ "${postalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  243. fn_print_warn_nl "More Info not enabled"
  244. fn_script_log_warn "More Info alerts not enabled"
  245. fi
  246. if [ "${discordalert}" == "on" ]&&[ -n "${discordalert}" ]; then
  247. alert_discord.sh
  248. elif [ "${discordalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  249. fn_print_warn_nl "Discord alerts not enabled"
  250. fn_script_log_warn "Discord alerts not enabled"
  251. elif [ -z "${discordtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  252. fn_print_error_nl "Discord token not set"
  253. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  254. fn_script_error "Discord token not set"
  255. fi
  256. if [ "${emailalert}" == "on" ]&&[ -n "${email}" ]; then
  257. alert_email.sh
  258. elif [ "${emailalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  259. fn_print_warn_nl "Email alerts not enabled"
  260. fn_script_log_warn "Email alerts not enabled"
  261. elif [ -z "${email}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  262. fn_print_error_nl "Email not set"
  263. fn_script_log_error "Email not set"
  264. fi
  265. if [ "${gotifyalert}" == "on" ]&&[ -n "${gotifyalert}" ]; then
  266. alert_gotify.sh
  267. elif [ "${gotifyalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  268. fn_print_warn_nl "Gotify alerts not enabled"
  269. fn_script_log_warn "Gotify alerts not enabled"
  270. elif [ -z "${gotifytoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  271. fn_print_error_nl "Gotify token not set"
  272. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  273. fn_script_error "Gotify token not set"
  274. elif [ -z "${gotifywebhook}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  275. fn_print_error_nl "Gotify webhook not set"
  276. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  277. fn_script_error "Gotify webhook not set"
  278. fi
  279. if [ "${iftttalert}" == "on" ]&&[ -n "${iftttalert}" ]; then
  280. alert_ifttt.sh
  281. elif [ "${iftttalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  282. fn_print_warn_nl "IFTTT alerts not enabled"
  283. fn_script_log_warn "IFTTT alerts not enabled"
  284. elif [ -z "${ifttttoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  285. fn_print_error_nl "IFTTT token not set"
  286. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  287. fn_script_error "IFTTT token not set"
  288. fi
  289. if [ "${mailgunalert}" == "on" ]&&[ -n "${mailgunalert}" ]; then
  290. alert_mailgun.sh
  291. elif [ "${mailgunalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  292. fn_print_warn_nl "Mailgun alerts not enabled"
  293. fn_script_log_warn "Mailgun alerts not enabled"
  294. elif [ -z "${mailguntoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  295. fn_print_error_nl "Mailgun token not set"
  296. echo -e "* https://docs.linuxgsm.com/alerts/mailgun"
  297. fn_script_error "Mailgun token not set"
  298. fi
  299. if [ "${pushbulletalert}" == "on" ]&&[ -n "${pushbullettoken}" ]; then
  300. alert_pushbullet.sh
  301. elif [ "${pushbulletalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  302. fn_print_warn_nl "Pushbullet alerts not enabled"
  303. fn_script_log_warn "Pushbullet alerts not enabled"
  304. elif [ -z "${pushbullettoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  305. fn_print_error_nl "Pushbullet token not set"
  306. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  307. fn_script_error "Pushbullet token not set"
  308. fi
  309. if [ "${pushoveralert}" == "on" ]&&[ -n "${pushoveralert}" ]; then
  310. alert_pushover.sh
  311. elif [ "${pushoveralert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  312. fn_print_warn_nl "Pushover alerts not enabled"
  313. fn_script_log_warn "Pushover alerts not enabled"
  314. elif [ -z "${pushovertoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  315. fn_print_error_nl "Pushover token not set"
  316. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  317. fn_script_error "Pushover token not set"
  318. fi
  319. if [ "${telegramalert}" == "on" ]&&[ -n "${telegramtoken}" ]; then
  320. alert_telegram.sh
  321. elif [ "${telegramalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  322. fn_print_warn_nl "Telegram Messages not enabled"
  323. fn_script_log_warn "Telegram Messages not enabled"
  324. elif [ -z "${telegramtoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  325. fn_print_error_nl "Telegram token not set."
  326. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  327. fn_script_error "Telegram token not set."
  328. elif [ -z "${telegramchatid}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  329. fn_print_error_nl "Telegram chat id not set."
  330. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  331. fn_script_error "Telegram chat id not set."
  332. fi
  333. if [ "${rocketchatalert}" == "on" ]&&[ -n "${rocketchatalert}" ]; then
  334. alert_rocketchat.sh
  335. elif [ "${rocketchatalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  336. fn_print_warn_nl "Rocketchat alerts not enabled"
  337. fn_script_log_warn "Rocketchat alerts not enabled"
  338. elif [ -z "${rocketchattoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  339. fn_print_error_nl "Rocketchat token not set"
  340. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  341. fn_script_error "Rocketchat token not set"
  342. fi
  343. if [ "${slackalert}" == "on" ]&&[ -n "${slackalert}" ]; then
  344. alert_slack.sh
  345. elif [ "${slackalert}" != "on" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  346. fn_print_warn_nl "Slack alerts not enabled"
  347. fn_script_log_warn "Slack alerts not enabled"
  348. elif [ -z "${slacktoken}" ]&&[ "${commandname}" == "TEST-ALERT" ]; then
  349. fn_print_error_nl "Slack token not set"
  350. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  351. fn_script_error "Slack token not set"
  352. fi