Commit ad7dd8f0 authored by Kubernetes's avatar Kubernetes

The initial release

parents
#!/bin/bash
set -e
_exit_status_ok=0
_exit_status_error=2
_top_pid="${$}"
trap "exit ${_exit_status_error}" TERM
function _log {
local _output
case "${1}" in
1)
exec 3>&1
;;
2)
exec 3>&2
;;
*)
exec 3>/dev/null
;;
esac
shift
echo "[$(date +%F\ %T)]" $* >&3
}
function _warn {
_log 2 "[!]" $*
}
function _die {
_warn $*
kill -s TERM "${_top_pid}"
}
top_directory="/tmp/mtr_test"
log_directory="${top_directory}/log"
tmp_directory="${top_directory}/tmp"
[ ! -d "${top_directory}" ] && mkdir "${top_directory}"
[ ! -d "${log_directory}" ] && mkdir "${log_directory}"
[ ! -d "${tmp_directory}" ] && mkdir "${tmp_directory}"
target="${1}"
[ -z "$(echo "${target}" | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$')" ] \
&& _die "'${target}' doesn't look like an IPv4-address"
[ ! -x "$(which mtr)" ] \
&& _die "please, install mtr"
while :; do
tmp_file=$(mktemp "${tmp_directory}/XXXXXXXX")
mtr -n -r -c 10 "${target}" > "${tmp_file}"
plr=$(
tail -n 1 "${tmp_file}" | awk --posix '/^[[:space:]]*[[:digit:]]+\.[[:space:]]+[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}[[:space:]]+[[:digit:]]+\.[[:digit:]]+%[[:space:]]+[[:digit:]]+[[:space:]]+[[:digit:]]+\.[[:digit:]]+[[:space:]]+[[:digit:]]+\.[[:digit:]]+[[:space:]]+[[:digit:]]+\.[[:digit:]]+[[:space:]]+[[:digit:]]+\.[[:digit:]]+[[:space:]]+[[:digit:]]+\.[[:digit:]]+[[:space:]]*$/ { percent = $3 + 0; if (percent > 0) { print percent } else { print "OK" } }'
)
if [ "${plr}" != "OK" ]; then
date=$(date +"%Y-%m-%d")
time=$(date +"%H:%M:%S")
_warn "Something was wrong at ${date} ${time}, the packet loss rate between me and ${target} was equal to ${plr}%"
[ ! -d "${log_directory}/${date}" ] && mkdir "${log_directory}/${date}"
cp ${tmp_file} "${log_directory}/${date}/${time}.log"
fi
rm -f "${tmp_file}"
sleep 1
done
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment