Created
June 29, 2016 12:02
-
-
Save khmarochos/5860aec4be31b5f21d08ce80a32bf593 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function log { | |
echo "[$(date +%F\ %T)]" $* >&2 | |
} | |
function warn { | |
log "[!]" $* | |
} | |
function die { | |
warn $* | |
exit ${EXIT_CODE_ERROR} | |
} | |
LOG_DIR='/var/log/mtr' | |
MTR_CNT='1000' | |
MTR_INT='0.1' | |
PATH='/sbin:/bin:/usr/sbin:/usr/bin' | |
if [[ ! -d "${LOG_DIR}" ]]; then | |
if [[ -f "${LOG_DIR}" ]]; then | |
die "The ${LOG_DIR} is present, but it's not a directory" | |
fi | |
else | |
mkdir -p "${LOG_DIR}" | |
if [[ "${?}" -ne 0 ]]; then | |
die "Can't create the ${LOG_DIR} directory" | |
fi | |
fi | |
for TARGET in 8.8.8.8 8.8.4.4; do | |
MTR_RESULT="$(mtr -r -c "${MTR_CNT}" -i "${MTR_INT}" "${TARGET}")" | |
if [[ "${?}" -ne 0 ]]; then | |
die "Can't trace the route to ${TARGET}" | |
fi | |
TIMESTAMP="$(date +%Y%m%d%H%M%S)" | |
RATES=() | |
echo "${MTR_RESULT}" > "${LOG_DIR}/result-${TARGET}-${TIMESTAMP}" | |
for HOP in $(cat "${LOG_DIR}/result-${TARGET}-${TIMESTAMP}" | sed -n -r 's/^[[:space:]]*[[:digit:]]+\.[[:space:]]+([[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3})[[:space:]]+([[:digit:]]{1,3}\.[[:digit:]])%?[[:space:]]+.*$/\1:\2/gp'); do | |
HOST="$(echo "${HOP}" | cut -d ':' -f 1)" | |
RATE="$(echo "${HOP}" | cut -d ':' -f 2)" | |
RATES+=($(printf '%.0f' "${RATE}")) | |
done | |
RATE_PEAK=0 | |
RATE_LAST=100 | |
for (( I=${#RATES[@]}-1; I>=0 ;I-- )); do | |
[[ ${RATES[I]} -gt $((${RATE_LAST} * 4)) ]] && continue | |
[[ ${RATES[I]} -gt ${RATE_PEAK} ]] && RATE_PEAK="${RATES[I]}" | |
RATE_LAST="${RATES[I]}" | |
done | |
if [[ ${RATE_PEAK} -gt 1 ]]; then | |
cat "${LOG_DIR}/result-${TARGET}-${TIMESTAMP}" | mail -r 'xxx@xxx' -s "Too many packets have been lost while tracing the route to ${TARGET}" "xxx@xxx" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment