alert_email.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/bin/bash
  2. # LGSM alert_email.sh function
  3. # Author: Daniel Gibbs
  4. # Website: https://gameservermanagers.com
  5. # Description: Sends email alert including server details and logs.
  6. local commandname="ALERT"
  7. local commandaction="Alert"
  8. local function_selfname="$(basename $(readlink -f "${BASH_SOURCE[0]}"))"
  9. fn_details_email(){
  10. #
  11. # Failure reason: Testing bb2-server email alert
  12. # Action Taken: Sent test email...hello is this thing on?
  13. echo -e "${alertbody}" >> "${emaillog}"
  14. }
  15. fn_details_os(){
  16. #
  17. # Distro Details
  18. # =====================================
  19. # Distro: Ubuntu 14.04.4 LTS
  20. # Arch: x86_64
  21. # Kernel: 3.13.0-79-generic
  22. # Hostname: hostname
  23. # tmux: tmux 1.8
  24. # GLIBC: 2.19
  25. {
  26. echo -e ""
  27. echo -e "Distro Details"
  28. echo -e "================================="
  29. echo -e "Distro: ${distroname}"
  30. echo -e "Arch: ${arch}"
  31. echo -e "Kernel: ${kernel}"
  32. echo -e "Hostname: $HOSTNAME"
  33. echo -e "tmux: ${tmuxv}"
  34. echo -e "GLIBC: ${glibcversion}"
  35. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${emaillog}" > /dev/null 2>&1
  36. }
  37. fn_details_performance(){
  38. #
  39. # Performance
  40. # =====================================
  41. # Uptime: 55d, 3h, 38m
  42. # Avg Load: 1.00, 1.01, 0.78
  43. #
  44. # Mem: total used free
  45. # Physical: 741M 656M 85M
  46. # Swap: 0B 0B 0B
  47. {
  48. echo -e ""
  49. echo -e "Performance"
  50. echo -e "================================="
  51. echo -e "Uptime: ${days}d, ${hours}h, ${minutes}m"
  52. echo -e "Avg Load: ${load}"
  53. echo -e ""
  54. echo -e "Mem: total used free"
  55. echo -e "Physical: ${physmemtotal} ${physmemused} ${physmemfree}"
  56. echo -e "Swap: ${swaptotal} ${swapused} ${swapfree}"
  57. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${emaillog}" > /dev/null 2>&1
  58. }
  59. fn_details_disk(){
  60. #
  61. # Storage
  62. # =====================================
  63. # Filesystem: /dev/disk/by-uuid/320c8edd-a2ce-4a23-8c9d-e00a7af2d6ff
  64. # Total: 15G
  65. # Used: 8.4G
  66. # Available: 5.7G
  67. # Serverfiles: 961M
  68. {
  69. echo -e ""
  70. echo -e "Storage"
  71. echo -e "================================="
  72. echo -e "Filesystem: ${filesystem}"
  73. echo -e "Total: ${totalspace}"
  74. echo -e "Used: ${usedspace}"
  75. echo -e "Available: ${availspace}"
  76. echo -e "Serverfiles: ${filesdirdu}"
  77. if [ -d "${backupdir}" ]; then
  78. echo -e "Backups: ${backupdirdu}"
  79. fi
  80. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${emaillog}" > /dev/null 2>&1
  81. }
  82. fn_details_gameserver(){
  83. #
  84. # Quake Live Server Details
  85. # =====================================
  86. # Server name: ql-server
  87. # Server IP: 1.2.3.4:27960
  88. # RCON password: CHANGE_ME
  89. # Server password: NOT SET
  90. # Slots: 16
  91. # Status: OFFLINE
  92. {
  93. echo -e ""
  94. echo -e "${gamename} Server Details"
  95. echo -e "================================="
  96. # Server name
  97. echo -e "Server name: ${servername}"
  98. # Server ip
  99. echo -e "Server IP: ${ip}:${port}"
  100. # Server password
  101. if [ -n "${serverpassword}" ]; then
  102. echo -e "Server password: ********"
  103. fi
  104. # RCON password
  105. if [ -n "${rconpassword}" ]; then
  106. echo -e "RCON password: ********"
  107. fi
  108. # Admin password
  109. if [ -n "${adminpassword}" ]; then
  110. echo -e "Admin password: ********"
  111. fi
  112. # Stats password (Quake Live)
  113. if [ -n "${statspassword}" ]; then
  114. echo -e "Stats password: ********"
  115. fi
  116. # Slots
  117. if [ -n "${slots}" ]; then
  118. echo -e "Slots: ${slots}"
  119. fi
  120. # Game mode
  121. if [ -n "${gamemode}" ]; then
  122. echo -e "Game mode: ${gamemode}"
  123. fi
  124. # Game world
  125. if [ -n "${gameworld}" ]; then
  126. echo -e "Game world: ${gameworld}"
  127. fi
  128. # Tick rate
  129. if [ -n "${tickrate}" ]; then
  130. echo -e "Tick rate: ${tickrate}"
  131. fi
  132. # TeamSpeak dbplugin
  133. if [ -n "${dbplugin}" ]; then
  134. echo -e "dbplugin: ${dbplugin}"
  135. fi
  136. # Online status
  137. if [ "${status}" == "0" ]; then
  138. echo -e "Status: OFFLINE"
  139. else
  140. echo -e "Status: ONLINE"
  141. fi
  142. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${emaillog}" > /dev/null 2>&1
  143. }
  144. fn_alert_email_template_logs(){
  145. {
  146. echo -e ""
  147. echo -e "${servicename} Logs"
  148. echo -e "================================="
  149. if [ -n "${scriptlog}" ]; then
  150. echo -e "\nScript log\n==================="
  151. if [ ! "$(ls -A ${scriptlogdir})" ]; then
  152. echo "${scriptlogdir} (NO LOG FILES)"
  153. elif [ ! -s "${scriptlog}" ]; then
  154. echo "${scriptlog} (LOG FILE IS EMPTY)"
  155. else
  156. echo "${scriptlog}"
  157. tail -25 "${scriptlog}"
  158. fi
  159. echo ""
  160. fi
  161. if [ -n "${consolelog}" ]; then
  162. echo -e "\nConsole log\n===================="
  163. if [ ! "$(ls -A ${consolelogdir})" ]; then
  164. echo "${consolelogdir} (NO LOG FILES)"
  165. elif [ ! -s "${consolelog}" ]; then
  166. echo "${consolelog} (LOG FILE IS EMPTY)"
  167. else
  168. echo "${consolelog}"
  169. tail -25 "${consolelog}" | awk '{ sub("\r$", ""); print }'
  170. fi
  171. echo ""
  172. fi
  173. if [ -n "${gamelogdir}" ]; then
  174. echo -e "\nServer log\n==================="
  175. if [ ! "$(ls -A ${gamelogdir})" ]; then
  176. echo "${gamelogdir} (NO LOG FILES)"
  177. else
  178. echo "${gamelogdir}"
  179. tail "${gamelogdir}"/* | grep -v "==>" | sed '/^$/d' | tail -25
  180. fi
  181. echo ""
  182. fi
  183. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${emaillog}" > /dev/null 2>&1
  184. }
  185. fn_print_dots "Sending alert: ${email}"
  186. fn_script_log_info "Sending alert: ${email}"
  187. info_distro.sh
  188. info_config.sh
  189. info_glibc.sh
  190. check_ip.sh
  191. emaillog="${emaillog}"
  192. if [ -f "${emaillog}" ]; then
  193. rm "${emaillog}"
  194. fi
  195. fn_details_email
  196. fn_details_os
  197. fn_details_performance
  198. fn_details_disk
  199. fn_details_gameserver
  200. fn_alert_email_template_logs
  201. mail -s "${alertsubject}" "${email}" < "${emaillog}"
  202. exitcode=$?
  203. if [ "${exitcode}" == "0" ]; then
  204. fn_print_ok_nl "Sending alert: ${email}"
  205. fn_script_log_pass "Sending alert: ${email}"
  206. else
  207. fn_print_fail_nl "Sending alert: ${email}"
  208. fn_script_log_fatal "Sending alert: ${email}"
  209. fi