Commit c8d2c215 authored by Kubernetes's avatar Kubernetes

v1.0.0 release

parent ad7dd8f0
#!/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
#!/bin/bash
#
# name: pingmon
# version: 1.0.0
# description: Performs connectivity testing and saves detailed reports when something goes wrong
# author: Volodymyr Melnyk <v.melnyk@tucha.ua>
#
# We need to set "exit immediately" mode, so the script run will be terminated
set -e
# There's no other way to kill oneself from inside of a function
_top_pid="${$}"
# ...
trap "exit 13" TERM
# This function is used for logging purpose, the 1st parameter chooses the output stream
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
}
# This function is used to send a warning
function _warn {
_log 2 "[!]" $*
}
# This function is used to terminate the script run when something is wrong
function _die {
_warn $*
_die_silently
}
# ...
function _die_silently {
kill -s TERM "${_top_pid}"
}
# This function is used to print out the usage information and terminate the script run
function _incorrect_usage {
cat 1>&2 <<-__END_OF_USAGE__
$*, the correct usage model is as follows:
${0} -m <mtr|traceroute> -t <target>
__END_OF_USAGE__
_die_silently
}
# This function is used to perform tests with mtr
function test_mtr {
mtr -n -r -c 10 "${1}" > "${2}"
tail -n 1 "${1}" | 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" } }'
}
# This function is used to perform tests with traceroute and ping
function test_traceroute {
nodes=()
traceroute -n "${1}" > "${2}/traceroute.txt"
read -a nodes <<< $(cat "${2}/traceroute.txt" | awk --posix '/^[[:space:]]*[[:digit:]]+[[:space:]]+[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}[[:space:]]+.*$/ { print $2 }')
[ "${nodes[${#nodes[@]}-1]}" != "${1}" ] && nodes+=("${1}")
node_number=0
for node in "${nodes[@]}"; do
node_number=$((node_number+1))
{ ping -c 10 -i 0.1 ${node} > "${2}/ping-${node_number}-${node}.txt"; } &
done
wait
cat "${2}/ping-${node_number}-${nodes[$node_number-1]}.txt" | awk '/^[[:digit:]]+ packets transmitted, [[:digit:]]+ received, [[:digit:]]+(\.[[:digit:]]+)?% packet loss, time [[:digit:]]+ms$/ { percent = $6 + 0; if (percent > 0) { print percent } else { print "OK" } }'
}
# Determine the working directories
top_directory="/tmp/pingmon"
log_directory="${top_directory}/log"
run_directory="${top_directory}/run"
# Create the directories if needed
[ ! -d "${top_directory}" ] && mkdir "${top_directory}"
[ ! -d "${log_directory}" ] && mkdir "${log_directory}"
[ ! -d "${run_directory}" ] && mkdir "${run_directory}"
# Parse parameters
while getopts "m:t:" option; do
case "${option}" in
m)
mode="${OPTARG}"
;;
t)
target="${OPTARG}"
;;
*)
_incorrect_usage "The '${option}' parameter is unknown"
;;
esac
done
[ -z "$(echo "${target}" | grep -E '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$')" ] \
&& _incorrect_usage "'${target}' doesn't look like an IPv4-address"
[ "${mode}" != "mtr" -a "${mode}" != "traceroute" ] \
&& _incorrect_usage "The '${mode}' mode is unknown"
[ "${mode}" == "mtr" -a ! -x "$(which mtr)" ] \
&& _die "please, install mtr"
[ "${mode}" == "traceroute" -a ! -x "$(which traceroute)" ] \
&& _die "please, install traceroute"
# Perform tests
while :; do
tmp_directory=$(mktemp -d "${run_directory}/XXXXXXXX")
case "${mode}" in
"mtr")
packet_loss_rate=$(test_mtr "${target}" "${tmp_directory}/mtr.txt")
;;
"traceroute")
packet_loss_rate=$(test_traceroute "${target}" "${tmp_directory}")
;;
*)
_die "that shouldn't have happened :-)"
;;
esac
if [ "${packet_loss_rate}" != "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 ${packet_loss_rate}%"
[ ! -d "${log_directory}/${date}" ] && mkdir "${log_directory}/${date}"
mv ${tmp_directory} "${log_directory}/${date}/${time}"
fi
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