alert.sh 9.5 KB

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