Sometimes you need to monitor a connection from your machine to a specific, single host or network in order to identify which network hop between your machine and the target causes trouble. You can use the following shell script to easily achieve this kind of monitoring.
If the target host is unable to respond to the specified number of ICMP packets, you will get an eMail together with a mtr
to see on which hop the problem occurs.
#!/bin/bash
TARGET=8.8.8.8 # Target host or IP address to be monitored.
MAIL_RECIPIENT=you@example.com # eMail address where to send failures to.
NUMBER_OF_TEST_PACKETS=5 # Number of ICMP packets to be sent to the target. If any fails, you'll get notified.
function do_monitoring {
ping -q -c $NUMBER_OF_TEST_PACKETS $TARGET
status=$?
if [ $status -ne 0 ]; then
mail_traceroute $TARGET
fi
return $status
}
function mail_traceroute {
MTR_OUTPUT=$(mtr -n -r $@)
HOSTNAME=$(hostname -f)
mail -s "Monitoring failure: Connection to $TARGET failed" $MAIL_RECIPIENT <<EOF
Hey,
this is $HOSTNAME calling.
I just sent $NUMBER_OF_TEST_PACKETS ICMP packets to $TARGET and that failed apparently.
Here is a traceroute to $TARGET:
$MTR_OUTPUT
EOF
}
do_monitoring
The resulting eMail looks like this:
Hey,
this is foo.example.com calling.
I just sent two ICMP packets to 1.2.3.4 and that failed apparently.
Here is a traceroute to 1.2.3.4:
HOST: foo.example.com Loss% Snt Last Avg Best Wrst StDev
1.|-- 10.10.13.1 0.0% 10 0.4 0.3 0.2 0.5 0.1
2.|-- 10.179.144.1 0.0% 10 0.7 0.7 0.7 0.8 0.0
3.|-- 212.18.7.63 0.0% 10 2.2 7.0 2.1 51.0 15.4
4.|-- 194.59.190.61 0.0% 10 2.3 11.4 2.2 83.0 25.3
5.|-- 66.249.94.86 0.0% 10 26.3 13.3 9.6 29.1 7.6
6.|-- 216.239.48.117 0.0% 10 10.0 10.0 10.0 10.1 0.0
7.|-- 209.85.254.118 0.0% 10 10.0 12.7 9.8 38.1 8.9
| `|-- 209.85.254.112
8.|-- ??? 100.0 10 0.0 0.0 0.0 0.0 0.0
9.|-- 8.8.8.8 0.0% 10 9.9 9.9 9.9 10.0 0.0
Keep in mind that a hop having 100% loss rate does not necessarily mean that there is trouble. You should read about how traceroute
works to get an idea how to interpret it.
Posted by Thomas Eisenbarth to makandra dev (2013-11-05 18:10)