alert.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 module for managing alerts.
  7. moduleselfname="$(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_distro.sh
  12. info_game.sh
  13. info_messages.sh
  14. if [ -f "${alertlog}" ]; then
  15. rm -f "${alertlog:?}"
  16. fi
  17. {
  18. fn_info_message_head
  19. fn_info_message_distro
  20. fn_info_message_server_resource
  21. fn_info_message_gameserver_resource
  22. fn_info_message_gameserver
  23. fn_info_logs
  24. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | tee -a "${alertlog}" > /dev/null 2>&1
  25. }
  26. fn_alert_test() {
  27. fn_script_log_info "Sending alert: Testing LinuxGSM Alert. No action to be taken"
  28. alertaction="Tested"
  29. alertemoji="🚧"
  30. alertsound="1"
  31. alertmessage="Testing ${selfname} LinuxGSM Alert. No action to be taken."
  32. # Green
  33. alertcolourhex="#cdcd00"
  34. alertcolourdec="13487360"
  35. }
  36. # Running command manually
  37. fn_alert_stopped() {
  38. fn_script_log_info "Sending alert: ${selfname} has stopped"
  39. alertaction="Stopped"
  40. alertemoji="❌"
  41. alertsound="1"
  42. alertmessage="${selfname} has been stopped."
  43. # Red
  44. alertcolourhex="#cd0000"
  45. alertcolourdec="13434880"
  46. }
  47. fn_alert_started() {
  48. fn_script_log_info "Sending alert: ${selfname} has started"
  49. alertaction="Started"
  50. alertemoji="✔️"
  51. alertsound="1"
  52. alertmessage="${selfname} has been started."
  53. # Green
  54. alertcolourhex="#00cd00"
  55. alertcolourdec="52480"
  56. }
  57. fn_alert_restarted() {
  58. fn_script_log_info "Sending alert: ${selfname} has restarted"
  59. alertaction="Restarted"
  60. alertemoji="🗘"
  61. alertsound="1"
  62. alertmessage="${selfname} has been restarted."
  63. # Green
  64. alertcolourhex="#00cd00"
  65. alertcolourdec="52480"
  66. }
  67. # Failed monitor checks
  68. fn_alert_monitor_session() {
  69. fn_script_log_info "Sending alert: ${selfname} is not running. Game server has been restarted"
  70. alertaction="Restarted"
  71. alertemoji="🚨"
  72. alertsound="2"
  73. alertmessage="${selfname} is not running. Game server has been restarted."
  74. # Red
  75. alertcolourhex="#cd0000"
  76. alertcolourdec="13434880"
  77. }
  78. fn_alert_monitor_query() {
  79. fn_script_log_info "Sending alert: Unable to query ${selfname}. Game server has been restarted"
  80. alertaction="Restarted"
  81. alertemoji="🚨"
  82. alertsound="2"
  83. alertmessage="Unable to query ${selfname}. Game server has been restarted."
  84. # Red
  85. alertcolourhex="#cd0000"
  86. alertcolourdec="13434880"
  87. }
  88. # Update alerts
  89. fn_alert_update() {
  90. fn_script_log_info "Sending alert: ${selfname} has received a game server update: ${localbuild}"
  91. alertaction="Updated"
  92. alertemoji="🎉"
  93. alertsound="1"
  94. alertmessage="${selfname} has received a game server update: ${localbuild}."
  95. # Green
  96. alertcolourhex="#00cd00"
  97. alertcolourdec="52480"
  98. }
  99. fn_alert_check_update() {
  100. fn_script_log_info "Sending alert: ${gamename} update available: ${remotebuildversion}"
  101. alertaction="Checked for Update"
  102. alertemoji="🎉"
  103. alertsound="1"
  104. alertmessage="${gamename} update available: ${remotebuildversion}"
  105. # Blue
  106. alertcolourhex="#1e90ff"
  107. alertcolourdec="2003199"
  108. }
  109. fn_alert_update_linuxgsm() {
  110. fn_script_log_info "Sending alert: ${selfname} has received an LinuxGSM update"
  111. alertaction="Updated"
  112. alertemoji="🎉"
  113. alertsound="1"
  114. alertbody="${gamename} update available"
  115. alertmessage="${selfname} has received an LinuxGSM update and been restarted."
  116. # Green
  117. alertcolourhex="#00cd00"
  118. alertcolourdec="52480"
  119. }
  120. fn_alert_backup() {
  121. fn_script_log_info "Sending alert: ${selfname} has been backed up"
  122. alertaction="Backed Up"
  123. alertemoji="📂"
  124. alertsound="1"
  125. alertmessage="${selfname} has been backed up."
  126. # Green
  127. alertcolourhex="#00cd00"
  128. alertcolourdec="52480"
  129. }
  130. fn_alert_permissions() {
  131. fn_script_log_info "Sending alert: ${selfname} has permissions issues"
  132. alertaction="Checked Permissions"
  133. alertemoji="❗"
  134. alertsound="2"
  135. alertmessage="${selfname} has permissions issues."
  136. # Red
  137. alertcolourhex="#cd0000"
  138. alertcolourdec="13434880"
  139. }
  140. fn_alert_config() {
  141. fn_script_log_info "Sending alert: ${selfname} has received a new _default.cfg"
  142. alertaction="Updated _default.cfg"
  143. alertemoji="🎉"
  144. alertsound="1"
  145. alertmessage="${selfname} has received a new _default.cfg."
  146. # Blue
  147. alertcolourhex="#1e90ff"
  148. alertcolourdec="2003199"
  149. }
  150. fn_alert_wipe() {
  151. fn_script_log_info "Sending alert: ${selfname} has been wiped"
  152. alertaction="Wiped"
  153. alertemoji="🧹"
  154. alertsound="1"
  155. alertmessage="${selfname} has been wiped."
  156. # Green
  157. alertcolourhex="#00cd00"
  158. alertcolourdec="52480"
  159. }
  160. fn_alert_info() {
  161. fn_script_log_info "Sending alert: ${selfname} info"
  162. alerttitle="LinuxGSM Alert - ${selfname} - Info"
  163. alertaction="Queried"
  164. alertemoji="📄"
  165. alertsound="1"
  166. alertmessage="${selfname} info."
  167. # Blue
  168. alertcolourhex="#1e90ff"
  169. alertcolourdec="2003199"
  170. }
  171. # Images
  172. alerticon="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameicons/${shortname}-icon.png"
  173. if [ "${alert}" == "permissions" ]; then
  174. fn_alert_permissions
  175. elif [ "${alert}" == "monitor-session" ]; then
  176. fn_alert_monitor_session
  177. elif [ "${alert}" == "monitor-query" ]; then
  178. fn_alert_monitor_query
  179. elif [ "${alert}" == "test" ]; then
  180. fn_alert_test
  181. elif [ "${alert}" == "update" ]; then
  182. fn_alert_update
  183. elif [ "${alert}" == "check-update" ]; then
  184. fn_alert_check_update
  185. elif [ "${alert}" == "config" ]; then
  186. fn_alert_config
  187. elif [ "${alert}" == "wipe" ]; then
  188. fn_alert_wipe
  189. elif [ "${alert}" == "info" ]; then
  190. fn_alert_info
  191. elif [ "${alert}" == "started" ]; then
  192. fn_alert_started
  193. elif [ "${alert}" == "stopped" ]; then
  194. fn_alert_stopped
  195. elif [ "${alert}" == "restarted" ]; then
  196. fn_alert_restarted
  197. elif [ "${alert}" == "update-linuxgsm" ]; then
  198. fn_alert_update_linuxgsm
  199. elif [ "${alert}" == "backup" ]; then
  200. fn_alert_backup
  201. else
  202. fn_print_fail_nl "Missing alert type"
  203. fn_script_log_fail "Missing alert type"
  204. core_exit.sh
  205. fi
  206. alerttitle="${alertemoji} ${alertaction} - ${servername} ${alertemoji}"
  207. # Generate alert log.
  208. fn_alert_log
  209. # Generates the more info link.
  210. if [ "${postalert}" == "on" ] && [ -n "${postalert}" ]; then
  211. exitbypass=1
  212. command_postdetails.sh
  213. fn_firstcommand_reset
  214. unset exitbypass
  215. elif [ "${postalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  216. fn_print_warn_nl "More Info not enabled"
  217. fn_script_log_warn "More Info alerts not enabled"
  218. fi
  219. if [ "${discordalert}" == "on" ] && [ -n "${discordalert}" ]; then
  220. alert_discord.sh
  221. elif [ "${discordalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  222. fn_print_warn_nl "Discord alerts not enabled"
  223. fn_script_log_warn "Discord alerts not enabled"
  224. elif [ -z "${discordtoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  225. fn_print_error_nl "Discord token not set"
  226. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  227. fn_script_error "Discord token not set"
  228. fi
  229. if [ "${emailalert}" == "on" ] && [ -n "${email}" ]; then
  230. alert_email.sh
  231. elif [ "${emailalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  232. fn_print_warn_nl "Email alerts not enabled"
  233. fn_script_log_warn "Email alerts not enabled"
  234. elif [ -z "${email}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  235. fn_print_error_nl "Email not set"
  236. fn_script_log_error "Email not set"
  237. fi
  238. if [ "${gotifyalert}" == "on" ] && [ -n "${gotifyalert}" ]; then
  239. alert_gotify.sh
  240. elif [ "${gotifyalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  241. fn_print_warn_nl "Gotify alerts not enabled"
  242. fn_script_log_warn "Gotify alerts not enabled"
  243. elif [ -z "${gotifytoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  244. fn_print_error_nl "Gotify token not set"
  245. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  246. fn_script_error "Gotify token not set"
  247. elif [ -z "${gotifywebhook}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  248. fn_print_error_nl "Gotify webhook not set"
  249. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  250. fn_script_error "Gotify webhook not set"
  251. fi
  252. if [ "${iftttalert}" == "on" ] && [ -n "${iftttalert}" ]; then
  253. alert_ifttt.sh
  254. elif [ "${iftttalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  255. fn_print_warn_nl "IFTTT alerts not enabled"
  256. fn_script_log_warn "IFTTT alerts not enabled"
  257. elif [ -z "${ifttttoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  258. fn_print_error_nl "IFTTT token not set"
  259. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  260. fn_script_error "IFTTT token not set"
  261. fi
  262. if [ "${pushbulletalert}" == "on" ] && [ -n "${pushbullettoken}" ]; then
  263. alert_pushbullet.sh
  264. elif [ "${pushbulletalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  265. fn_print_warn_nl "Pushbullet alerts not enabled"
  266. fn_script_log_warn "Pushbullet alerts not enabled"
  267. elif [ -z "${pushbullettoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  268. fn_print_error_nl "Pushbullet token not set"
  269. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  270. fn_script_error "Pushbullet token not set"
  271. fi
  272. if [ "${pushoveralert}" == "on" ] && [ -n "${pushoveralert}" ]; then
  273. alert_pushover.sh
  274. elif [ "${pushoveralert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  275. fn_print_warn_nl "Pushover alerts not enabled"
  276. fn_script_log_warn "Pushover alerts not enabled"
  277. elif [ -z "${pushovertoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  278. fn_print_error_nl "Pushover token not set"
  279. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  280. fn_script_error "Pushover token not set"
  281. fi
  282. if [ "${telegramalert}" == "on" ] && [ -n "${telegramtoken}" ]; then
  283. alert_telegram.sh
  284. elif [ "${telegramalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  285. fn_print_warn_nl "Telegram Messages not enabled"
  286. fn_script_log_warn "Telegram Messages not enabled"
  287. elif [ -z "${telegramtoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  288. fn_print_error_nl "Telegram token not set."
  289. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  290. fn_script_error "Telegram token not set."
  291. elif [ -z "${telegramchatid}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  292. fn_print_error_nl "Telegram chat id not set."
  293. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  294. fn_script_error "Telegram chat id not set."
  295. fi
  296. if [ "${rocketchatalert}" == "on" ] && [ -n "${rocketchatalert}" ]; then
  297. alert_rocketchat.sh
  298. elif [ "${rocketchatalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  299. fn_print_warn_nl "Rocketchat alerts not enabled"
  300. fn_script_log_warn "Rocketchat alerts not enabled"
  301. elif [ -z "${rocketchatwebhook}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  302. fn_print_error_nl "Rocketchat token not set"
  303. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  304. fn_script_error "Rocketchat token not set"
  305. fi
  306. if [ "${slackalert}" == "on" ] && [ -n "${slackalert}" ]; then
  307. alert_slack.sh
  308. elif [ "${slackalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  309. fn_print_warn_nl "Slack alerts not enabled"
  310. fn_script_log_warn "Slack alerts not enabled"
  311. elif [ -z "${slacktoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  312. fn_print_error_nl "Slack token not set"
  313. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  314. fn_script_error "Slack token not set"
  315. fi