alert_email.sh 5.5 KB

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