alert.sh 11 KB

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