alert_email.sh 6.2 KB

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