alert.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. 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_messages_head
  19. fn_info_messages_distro
  20. fn_info_messages_server_resource
  21. fn_info_messages_gameserver_resource
  22. fn_info_messages_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_update_request() {
  100. fn_script_log_info "Sending alert: ${selfname} has requested an update and needs to be restarted."
  101. alertaction="Updating"
  102. alertemoji="🎉"
  103. alertsound="1"
  104. alertmessage="${selfname} has requested an update and needs to be restarted."
  105. # Blue
  106. alertcolourhex="#1e90ff"
  107. alertcolourdec="2003199"
  108. }
  109. fn_alert_check_update() {
  110. fn_script_log_info "Sending alert: ${gamename} update available: ${remotebuildversion}"
  111. alertaction="Update available"
  112. alertemoji="🎉"
  113. alertsound="1"
  114. alertmessage="${gamename} update available: ${remotebuildversion}"
  115. # Blue
  116. alertcolourhex="#1e90ff"
  117. alertcolourdec="2003199"
  118. }
  119. fn_alert_update_linuxgsm() {
  120. fn_script_log_info "Sending alert: ${selfname} has received an LinuxGSM update"
  121. alertaction="Updated"
  122. alertemoji="🎉"
  123. alertsound="1"
  124. alertbody="${gamename} update available"
  125. alertmessage="${selfname} has received an LinuxGSM update and been restarted."
  126. # Green
  127. alertcolourhex="#00cd00"
  128. alertcolourdec="52480"
  129. }
  130. fn_alert_backup() {
  131. fn_script_log_info "Sending alert: ${selfname} has been backed up"
  132. alertaction="Backed Up"
  133. alertemoji="📂"
  134. alertsound="1"
  135. alertmessage="${selfname} has been backed up."
  136. # Green
  137. alertcolourhex="#00cd00"
  138. alertcolourdec="52480"
  139. }
  140. fn_alert_permissions() {
  141. fn_script_log_info "Sending alert: ${selfname} has permissions issues"
  142. alertaction="Checked Permissions"
  143. alertemoji="❗"
  144. alertsound="2"
  145. alertmessage="${selfname} has permissions issues."
  146. # Red
  147. alertcolourhex="#cd0000"
  148. alertcolourdec="13434880"
  149. }
  150. fn_alert_config() {
  151. fn_script_log_info "Sending alert: ${selfname} has received a new _default.cfg"
  152. alertaction="Updated _default.cfg"
  153. alertemoji="🎉"
  154. alertsound="1"
  155. alertmessage="${selfname} has received a new _default.cfg."
  156. # Blue
  157. alertcolourhex="#1e90ff"
  158. alertcolourdec="2003199"
  159. }
  160. fn_alert_wipe() {
  161. fn_script_log_info "Sending alert: ${selfname} has been wiped"
  162. alertaction="Wiped"
  163. alertemoji="🧹"
  164. alertsound="1"
  165. alertmessage="${selfname} has been wiped."
  166. # Green
  167. alertcolourhex="#00cd00"
  168. alertcolourdec="52480"
  169. }
  170. fn_alert_info() {
  171. fn_script_log_info "Sending alert: ${selfname} info"
  172. alerttitle="LinuxGSM Alert - ${selfname} - Info"
  173. alertaction="Queried"
  174. alertemoji="📄"
  175. alertsound="1"
  176. alertmessage="${selfname} info."
  177. # Blue
  178. alertcolourhex="#1e90ff"
  179. alertcolourdec="2003199"
  180. }
  181. # Images
  182. alerticon="https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/gameicons/${shortname}-icon.png"
  183. if [ "${alert}" == "permissions" ]; then
  184. fn_alert_permissions
  185. elif [ "${alert}" == "monitor-session" ]; then
  186. fn_alert_monitor_session
  187. elif [ "${alert}" == "monitor-query" ]; then
  188. fn_alert_monitor_query
  189. elif [ "${alert}" == "test" ]; then
  190. fn_alert_test
  191. elif [ "${alert}" == "update" ]; then
  192. fn_alert_update
  193. elif [ "${alert}" == "update-request" ]; then
  194. fn_alert_update_request
  195. elif [ "${alert}" == "check-update" ]; then
  196. fn_alert_check_update
  197. elif [ "${alert}" == "config" ]; then
  198. fn_alert_config
  199. elif [ "${alert}" == "wipe" ]; then
  200. fn_alert_wipe
  201. elif [ "${alert}" == "info" ]; then
  202. fn_alert_info
  203. elif [ "${alert}" == "started" ]; then
  204. fn_alert_started
  205. elif [ "${alert}" == "stopped" ]; then
  206. fn_alert_stopped
  207. elif [ "${alert}" == "restarted" ]; then
  208. fn_alert_restarted
  209. elif [ "${alert}" == "update-linuxgsm" ]; then
  210. fn_alert_update_linuxgsm
  211. elif [ "${alert}" == "backup" ]; then
  212. fn_alert_backup
  213. else
  214. fn_print_fail_nl "Missing alert type"
  215. fn_script_log_fail "Missing alert type"
  216. core_exit.sh
  217. fi
  218. alerttitle="${alertemoji} ${alertaction} - ${servername} ${alertemoji}"
  219. # Generate alert log.
  220. fn_alert_log
  221. # Generates the more info link.
  222. if [ "${postalert}" == "on" ] && [ -n "${postalert}" ]; then
  223. exitbypass=1
  224. command_postdetails.sh
  225. fn_firstcommand_reset
  226. unset exitbypass
  227. elif [ "${postalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  228. fn_print_warn_nl "More Info not enabled"
  229. fn_script_log_warn "More Info alerts not enabled"
  230. fi
  231. if [ "${discordalert}" == "on" ] && [ -n "${discordalert}" ]; then
  232. alert_discord.sh
  233. elif [ "${discordalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  234. fn_print_warn_nl "Discord alerts not enabled"
  235. fn_script_log_warn "Discord alerts not enabled"
  236. elif [ -z "${discordtoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  237. fn_print_error_nl "Discord token not set"
  238. echo -e "* https://docs.linuxgsm.com/alerts/discord"
  239. fn_script_error "Discord token not set"
  240. fi
  241. if [ "${emailalert}" == "on" ] && [ -n "${email}" ]; then
  242. alert_email.sh
  243. elif [ "${emailalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  244. fn_print_warn_nl "Email alerts not enabled"
  245. fn_script_log_warn "Email alerts not enabled"
  246. elif [ -z "${email}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  247. fn_print_error_nl "Email not set"
  248. fn_script_log_error "Email not set"
  249. fi
  250. if [ "${gotifyalert}" == "on" ] && [ -n "${gotifyalert}" ]; then
  251. alert_gotify.sh
  252. elif [ "${gotifyalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  253. fn_print_warn_nl "Gotify alerts not enabled"
  254. fn_script_log_warn "Gotify alerts not enabled"
  255. elif [ -z "${gotifytoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  256. fn_print_error_nl "Gotify token not set"
  257. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  258. fn_script_error "Gotify token not set"
  259. elif [ -z "${gotifywebhook}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  260. fn_print_error_nl "Gotify webhook not set"
  261. echo -e "* https://docs.linuxgsm.com/alerts/gotify"
  262. fn_script_error "Gotify webhook not set"
  263. fi
  264. if [ "${iftttalert}" == "on" ] && [ -n "${iftttalert}" ]; then
  265. alert_ifttt.sh
  266. elif [ "${iftttalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  267. fn_print_warn_nl "IFTTT alerts not enabled"
  268. fn_script_log_warn "IFTTT alerts not enabled"
  269. elif [ -z "${ifttttoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  270. fn_print_error_nl "IFTTT token not set"
  271. echo -e "* https://docs.linuxgsm.com/alerts/ifttt"
  272. fn_script_error "IFTTT token not set"
  273. fi
  274. if [ "${pushbulletalert}" == "on" ] && [ -n "${pushbullettoken}" ]; then
  275. alert_pushbullet.sh
  276. elif [ "${pushbulletalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  277. fn_print_warn_nl "Pushbullet alerts not enabled"
  278. fn_script_log_warn "Pushbullet alerts not enabled"
  279. elif [ -z "${pushbullettoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  280. fn_print_error_nl "Pushbullet token not set"
  281. echo -e "* https://docs.linuxgsm.com/alerts/pushbullet"
  282. fn_script_error "Pushbullet token not set"
  283. fi
  284. if [ "${pushoveralert}" == "on" ] && [ -n "${pushoveralert}" ]; then
  285. alert_pushover.sh
  286. elif [ "${pushoveralert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  287. fn_print_warn_nl "Pushover alerts not enabled"
  288. fn_script_log_warn "Pushover alerts not enabled"
  289. elif [ -z "${pushovertoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  290. fn_print_error_nl "Pushover token not set"
  291. echo -e "* https://docs.linuxgsm.com/alerts/pushover"
  292. fn_script_error "Pushover token not set"
  293. fi
  294. if [ "${telegramalert}" == "on" ] && [ -n "${telegramtoken}" ]; then
  295. alert_telegram.sh
  296. elif [ "${telegramalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  297. fn_print_warn_nl "Telegram Messages not enabled"
  298. fn_script_log_warn "Telegram Messages not enabled"
  299. elif [ -z "${telegramtoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  300. fn_print_error_nl "Telegram token not set."
  301. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  302. fn_script_error "Telegram token not set."
  303. elif [ -z "${telegramchatid}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  304. fn_print_error_nl "Telegram chat id not set."
  305. echo -e "* https://docs.linuxgsm.com/alerts/telegram"
  306. fn_script_error "Telegram chat id not set."
  307. fi
  308. if [ "${rocketchatalert}" == "on" ] && [ -n "${rocketchatalert}" ]; then
  309. alert_rocketchat.sh
  310. elif [ "${rocketchatalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  311. fn_print_warn_nl "Rocketchat alerts not enabled"
  312. fn_script_log_warn "Rocketchat alerts not enabled"
  313. elif [ -z "${rocketchatwebhook}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  314. fn_print_error_nl "Rocketchat token not set"
  315. #echo -e "* https://docs.linuxgsm.com/alerts/slack"
  316. fn_script_error "Rocketchat token not set"
  317. fi
  318. if [ "${slackalert}" == "on" ] && [ -n "${slackalert}" ]; then
  319. alert_slack.sh
  320. elif [ "${slackalert}" != "on" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  321. fn_print_warn_nl "Slack alerts not enabled"
  322. fn_script_log_warn "Slack alerts not enabled"
  323. elif [ -z "${slacktoken}" ] && [ "${commandname}" == "TEST-ALERT" ]; then
  324. fn_print_error_nl "Slack token not set"
  325. echo -e "* https://docs.linuxgsm.com/alerts/slack"
  326. fn_script_error "Slack token not set"
  327. fi