4
0

alert.sh 13 KB

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