#!/bin/sh # # Log status message to IRC bot. Fall back to sending mail if bot is sick. # # Maximilian Wilhelm # -- Mon, 02 Jun 2014 10:02:40 +0200 # set -e STATUS_BOT_ADDR="{{ salt['pillar.get']('ffho:status_bot:addr') }}" STATUS_BOT_PORT="{{ salt['pillar.get']('ffho:status_bot:port') }}" TECH_C_MAIL="{{ salt['pillar.get']('ffho:tech_c_mail') }}" temp_file="$(mktemp)" trap "rm -f -- \"${temp_file}\"" EXIT 1 2 15 # Specific source address given? src_ip="" if [ "$1" = "-s" ]; then shift # Valid local IP address given? if [ "$1" ] && ip route get "$1" | grep -q "dev lo" 2>/dev/null; then src_ip="$1" shift fi fi if [ $# = 0 -o "$1" = "-" ]; then cat > "${temp_file}" else echo "$@" > "${temp_file}" fi # If there is no data, there's nothing we can do *sniff* if [ ! -s "${temp_file}" ]; then exit 0 fi nc_opts="" if echo ${STATUS_BOT_ADDR} | grep -q ":"; then if echo ${src_ip} | grep -vq ":"; then src_ip=$(ip a s dev lo 2>/dev/null | awk '/inet6/ { sub ("/128","",$2); print $2 }' | grep ":ffff:") || true fi else if echo ${src_ip} | grep -qv "^[0-9]\+\."; then src_ip=$(ip a s dev lo 2>/dev/null | awk '/inet 10.132.255./ { sub ("/32","",$2); print $2 }') || true fi fi if [ "${src_ip}" ]; then nc_opts="-s ${src_ip}" fi if ! nc ${nc_opts} "${STATUS_BOT_ADDR}" "${STATUS_BOT_PORT}" < "${temp_file}"; then mail -s "FF-log from {{ grains['id'] }}" ${TECH_C_MAIL} < "${temp_file}" fi