You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.7 KiB

  1. #!/bin/sh
  2. #
  3. # adblock send mail script for msmtp
  4. # written by Dirk Brenken (dev@brenken.org)
  5. # Please note: you have to install and configure the package 'msmtp' before using this script.
  6. # This is free software, licensed under the GNU General Public License v3.
  7. # You should have received a copy of the GNU General Public License
  8. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  9. LC_ALL=C
  10. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. mail_ver="1.0.4"
  12. mail_daemon="$(command -v msmtp)"
  13. mail_profile="adb_notify"
  14. #mail_debug="--debug"
  15. mail_rc=1
  16. # mail header & mail receiver check
  17. #
  18. mail_receiver=""
  19. mail_sender="no-reply@adblock"
  20. mail_topic="${HOSTNAME}: adblock notification"
  21. mail_head="From: ${mail_sender}\nTo: ${mail_receiver}\nSubject: ${mail_topic}\nReply-to: ${mail_sender}\nMime-Version: 1.0\nContent-Type: text/html\nContent-Disposition: inline\n\n"
  22. if [ -z "${mail_receiver}" ]
  23. then
  24. logger -p "err" -t "adblock-notify-${mail_ver}[${$}]" "please supply/customize the 'mail_receiver' in '/etc/adblock/adblock.notify'"
  25. exit ${mail_rc}
  26. fi
  27. # mail daemon check
  28. #
  29. if [ ! -x "${mail_daemon}" ]
  30. then
  31. mail_daemon="$(command -v sendmail)"
  32. fi
  33. # info preparation
  34. #
  35. sys_info="$(strings /etc/banner 2>/dev/null; ubus call system board | sed -e 's/\"release\": {//' | sed -e 's/^[ \t]*//' | sed -e 's/[{}\",]//g' | sed -e 's/[ ]/ \t/' | sed '/^$/d' 2>/dev/null)"
  36. adb_info="$(/etc/init.d/adblock status 2>/dev/null)"
  37. if [ -f "/var/log/messages" ]
  38. then
  39. log_info="$(awk '/adblock-/{NR=1;max=79;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max),"&#8629;"} else {print " ",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}' /var/log/messages)"
  40. else
  41. log_info="$(logread -e "adblock-" | awk '{NR=1;max=79;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max),"&#8629;"} else {print " ",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
  42. fi
  43. # mail body
  44. #
  45. mail_text="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  46. mail_text="${mail_text}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
  47. mail_text="${mail_text}\n\n<strong>++\n++ Adblock Information ++\n++</strong>\n${adb_info}"
  48. mail_text="${mail_text}\n\n<strong>++\n++ Logfile Information ++\n++</strong>\n${log_info}"
  49. mail_text="${mail_text}</pre></body></html>"
  50. # send mail
  51. #
  52. if [ -x "${mail_daemon}" ]
  53. then
  54. printf "%b" "${mail_head}${mail_text}" 2>/dev/null | "${mail_daemon}" ${mail_debug} -a "${mail_profile}" "${mail_receiver}" >/dev/null 2>&1
  55. mail_rc=${?}
  56. logger -p "info" -t "adblock-notify-${mail_ver}[${$}]" "mail sent to '${mail_receiver}' with rc '${mail_rc}'"
  57. else
  58. logger -p "err" -t "adblock-notify-${mail_ver}[${$}]" "msmtp mail daemon not found"
  59. fi
  60. exit ${mail_rc}