alert_email.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_gameserver(){
  10. #
  11. # Quake Live Server Details
  12. # =====================================
  13. # Server name: ql-server
  14. # Server IP: 1.2.3.4:27960
  15. # RCON password: CHANGE_ME
  16. # Server password: NOT SET
  17. # Maxplayers: 16
  18. # Status: OFFLINE
  19. {
  20. echo -e ""
  21. echo -e "${gamename} Server Details"
  22. echo -e "================================="
  23. # Server name
  24. echo -e "Server name: ${servername}"
  25. # Server ip
  26. echo -e "Server IP: ${ip}:${port}"
  27. # Server password
  28. if [ -n "${serverpassword}" ]; then
  29. echo -e "Server password: ********"
  30. fi
  31. # RCON password
  32. if [ -n "${rconpassword}" ]; then
  33. echo -e "RCON password: ********"
  34. fi
  35. # Admin password
  36. if [ -n "${adminpassword}" ]; then
  37. echo -e "Admin password: ********"
  38. fi
  39. # Stats password (Quake Live)
  40. if [ -n "${statspassword}" ]; then
  41. echo -e "Stats password: ********"
  42. fi
  43. # Maxplayers
  44. if [ -n "${maxplayers}" ]; then
  45. echo -e "Maxplayers: ${maxplayers}"
  46. fi
  47. # Game mode
  48. if [ -n "${gamemode}" ]; then
  49. echo -e "Game mode: ${gamemode}"
  50. fi
  51. # Game world
  52. if [ -n "${gameworld}" ]; then
  53. echo -e "Game world: ${gameworld}"
  54. fi
  55. # Tick rate
  56. if [ -n "${tickrate}" ]; then
  57. echo -e "Tick rate: ${tickrate}"
  58. fi
  59. # TeamSpeak dbplugin
  60. if [ -n "${dbplugin}" ]; then
  61. echo -e "dbplugin: ${dbplugin}"
  62. fi
  63. # Online status
  64. if [ "${status}" == "0" ]; then
  65. echo -e "Status: OFFLINE"
  66. else
  67. echo -e "Status: ONLINE"
  68. fi
  69. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${emaillog}" > /dev/null 2>&1
  70. }
  71. fn_alert_email_template_logs(){
  72. echo -e ""
  73. echo -e "${servicename} Logs"
  74. echo -e "================================="
  75. if [ -n "${lgsmlog}" ]; then
  76. echo -e "\nScript log\n==================="
  77. if [ ! "$(ls -A ${lgsmlogdir})" ]; then
  78. echo "${lgsmlogdir} (NO LOG FILES)"
  79. elif [ ! -s "${lgsmlog}" ]; then
  80. echo "${lgsmlog} (LOG FILE IS EMPTY)"
  81. else
  82. echo "${lgsmlog}"
  83. tail -25 "${lgsmlog}"
  84. fi
  85. echo ""
  86. fi
  87. if [ -n "${consolelog}" ]; then
  88. echo -e "\nConsole log\n===================="
  89. if [ ! "$(ls -A ${consolelogdir})" ]; then
  90. echo "${consolelogdir} (NO LOG FILES)"
  91. elif [ ! -s "${consolelog}" ]; then
  92. echo "${consolelog} (LOG FILE IS EMPTY)"
  93. else
  94. echo "${consolelog}"
  95. tail -25 "${consolelog}" | awk '{ sub("\r$", ""); print }'
  96. fi
  97. echo ""
  98. fi
  99. if [ -n "${gamelogdir}" ]; then
  100. echo -e "\nServer log\n==================="
  101. if [ ! "$(ls -A ${gamelogdir})" ]; then
  102. echo "${gamelogdir} (NO LOG FILES)"
  103. else
  104. echo "${gamelogdir}"
  105. # dos2unix sed 's/\r//'
  106. tail "${gamelogdir}"/* 2>/dev/null | grep -v "==>" | sed '/^$/d' | sed 's/\r//'| tail -25
  107. fi
  108. echo ""
  109. fi
  110. }
  111. fn_print_dots "Sending Email alert: ${email}"
  112. sleep 0.5
  113. fn_script_log_info "Sending Email alert: ${email}"
  114. info_distro.sh
  115. info_config.sh
  116. info_glibc.sh
  117. check_ip.sh
  118. postdetails=1
  119. info_messages.sh
  120. emaillog="${emaillog}"
  121. if [ -f "${emaillog}" ]; then
  122. rm "${emaillog}"
  123. fi
  124. {
  125. fn_info_message_head
  126. fn_info_message_distro
  127. fn_info_message_performance
  128. fn_info_message_disk
  129. fn_info_message_gameserver
  130. fn_alert_email_template_logs
  131. } | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"| tee -a "${emaillog}" > /dev/null 2>&1
  132. if [ -n "${emailfrom}" ]; then
  133. mail -s "${alertsubject}" -r "${emailfrom}" "${email}" < "${emaillog}"
  134. else
  135. mail -s "${alertsubject}" "${email}" < "${emaillog}"
  136. fi
  137. exitcode=$?
  138. if [ "${exitcode}" == "0" ]; then
  139. fn_print_ok_nl "Sending Email alert: ${email}"
  140. fn_script_log_pass "Sending Email alert: ${email}"
  141. else
  142. fn_print_fail_nl "Sending Email alert: ${email}"
  143. fn_script_log_fatal "Sending Email alert: ${email}"
  144. fi