ff_log_to_bot 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/sh
  2. #
  3. # Log status message to IRC bot. Fall back to sending mail if bot is sick.
  4. #
  5. # Maximilian Wilhelm <max@rfc2324.org>
  6. # -- Mon, 02 Jun 2014 10:02:40 +0200
  7. #
  8. set -e
  9. STATUS_BOT_ADDR="{{ salt['pillar.get']('ffho:status_bot:addr') }}"
  10. STATUS_BOT_PORT="{{ salt['pillar.get']('ffho:status_bot:port') }}"
  11. TECH_C_MAIL="{{ salt['pillar.get']('ffho:tech_c_mail') }}"
  12. temp_file="$(mktemp)"
  13. trap "rm -f -- \"${temp_file}\"" EXIT 1 2 15
  14. # Specific source address given?
  15. src_ip=""
  16. if [ "$1" = "-s" ]; then
  17. shift
  18. # Valid local IP address given?
  19. if [ "$1" ] && ip route get "$1" | grep -q "dev lo" 2>/dev/null; then
  20. src_ip="$1"
  21. shift
  22. fi
  23. fi
  24. if [ $# = 0 -o "$1" = "-" ]; then
  25. cat > "${temp_file}"
  26. else
  27. echo "$@" > "${temp_file}"
  28. fi
  29. # If there is no data, there's nothing we can do *sniff*
  30. if [ ! -s "${temp_file}" ]; then
  31. exit 0
  32. fi
  33. nc_opts=""
  34. if echo ${STATUS_BOT_ADDR} | grep -q ":"; then
  35. if echo ${src_ip} | grep -vq ":"; then
  36. src_ip=$(ip a s dev lo 2>/dev/null | awk '/inet6/ { sub ("/128","",$2); print $2 }' | grep ":ffff:") || true
  37. fi
  38. else
  39. if echo ${src_ip} | grep -qv "^[0-9]\+\."; then
  40. src_ip=$(ip a s dev lo 2>/dev/null | awk '/inet 10.132.255./ { sub ("/32","",$2); print $2 }') || true
  41. fi
  42. fi
  43. if [ "${src_ip}" ]; then
  44. nc_opts="-s ${src_ip}"
  45. fi
  46. if ! nc ${nc_opts} "${STATUS_BOT_ADDR}" "${STATUS_BOT_PORT}" < "${temp_file}"; then
  47. mail -s "FF-log from {{ grains['id'] }}" ${TECH_C_MAIL} < "${temp_file}"
  48. fi