alert.sh 11 KB

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