Dado seu requisito relativamente simples, sim, um simples ping servirá. Você não precisa usar o Google como host de teste. Se o seu ISP tiver um site público, o que é bastante provável nos dias de hoje, use-o.
Aqui está um script [bash] que eu pretendia usar com um projeto antigo e pausado. É apenas ping
s um host e coleta estatísticas, que são impressas sob demanda no log do sistema. As únicas dependências são ping
e bc
. Observe que você precisará de uma versão não muito antiga de ping
, ou seja, um ping
com reconhecimento de recursos do Linux.
O script usa valores de assinatura e intervalo predefinidos e foi projetado para ser executado de forma interativa ou em segundo plano. no caso de você precisar de algum registro; um comando externo precisa ser dado. Você vai ter detecção de flapping e link up / down detecção pelo mesmo preço ;-). Você tem ajuda embutida no caso de precisar. Não hesite em perguntar se você tem alguma dúvida.
Eu escrevi como um exercício, mantendo a pegada de memória o mais baixo possível. Eu me lembro de planejar fazer isso funcionar, mas existe uma base que eu não consegui me livrar até agora. Esperando que você ache útil, aqui vai:
#!/bin/bash
#
# ping.sh
#
# Copyright 2014 VinzC <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
# Runs in the background ping'ing given addresses. May send
# notifications through an external routine that is given as
# an argument.
#
# Uses environment variables:
# * LOG_FILE (log file for debug purposes, silent otherwise)
#
# Symlinked as /usr/bin/anemon-ping
VERSION=1.0.3
AUTHOR="VinzC <[email protected]>"
DATE="Jan 2014"
# Background ping, keep only relevant information from the
# response, add timestamp in seconds. For use with graphing.
# Default padding for ICMP packets
ICMP_PADDING_DEFAULT=ffeab16b00b1e2
# Default number of seconds between ICMP probes
ICMP_INTERVAL_DEFAULT=300
# Number of samples to declare a host state steady
ICMP_STEADY_THRESHOLD=3
# Number of samples in which a host is allowed to go down
# 3 times before being flagged as flapping. (Why 3? 2 is
# not enough to make it a habbit, 3 is...)
ICMP_FLAPPING_THRESHOLD=24
# Number of samples after which dupe and damaged packets
# are gone for good. Default matches 24 hours.
ICMP_NET_ERROR_THRESHOLD=288
# Host state change command. The command is called with two or
# three arguments: host name and state, plus optional arguments.
# See usage for events.
host_cmd=false
# True if results shall be written to the standard output
verbose=false
# State variables
host_state=1 # Bit0: 1=up, 0=down
# Bit1: 1=flapping, 0=normal
host_unavail_state=0 # State counters
host_flap_state=0
host_warning_state=0
rtt_min= # Ping statistics
rtt_max=
rtt_avg=
icmp_seq=0 # Number of requests
icmp_lost=0 # Count of losses
icmp_dupes= # Network errors (counters)
icmp_damaged=
usage()
{
cat <<EOF
USAGE
${0##*/} [-i interval] [-p pattern] [-P string] [-I interface]
[-s packetsize] [-W timeout] [-v] [--host-command=CMD]
[--flapping-threshold=N] [--error-threshold=N] destination
${0##*/} -h
ARGUMENTS
Short options are a subset of standard ping arguments with the
exception of -P and -v. Both -p and -P define a custom padding.
Unlike ping's -p, option -P accepts any alphanumeric string,
which will be converted to a hexadecimal string. The resulting
pattern is eventually truncated to 16 bytes.
Option -v switches to verbose mode. In this mode echo replies
are sent to the standard output. This is useful for testing.
-h prints this help page.
--host-command=CMD
Run CMD on events. The first argument is the destination
parameter, the second is the event name. Some events may
have additional parameters.
All events except the echo reply are sent to the system
log. Events are:
"start" The monitoring process has been started. The
external script can initialize its working
context, e.g. create a round-robin database
to store echo reply TTL values. An additional
argument is passed with the interval value if
option -i was specified. The default interval
matches RRD default step, i.e. 300 seconds.
"stop" The monitoring process is stopping.
"icmp" Echo reply. Additional arguments are timestamp
and roudtrip time. This event occurs every 5
minutes by default and can be changed with -i.
"up" Host is up, steadily.
"down" Host is down, steadily.
"flapping" Host state is unstable.
"damaged" Damaged packets were detected.
"dupes" Duplicate packets were detected.
--flapping-threshold=N
The script attempts to detect flapping interfaces. It uses
a discrete formula to keep a reasonable maximum delay in
cases flapping occurs frequently, which must be considered
a critical situation. Flapping detection is done as soon
as no response from the remote host is received. No echo
reply for more than 3 samples is a steady "down" state.
An interface that has been flapping will take longer to be
considered steady again. The value of --flapping-threshold
sets that delay to the same amount of samples.
The default value for N is 24. A bigger value will make an
flapping interface wait proportionally longer before it is
marked "up" again.
--error-threshold=N
Linux only: the background ping process informs whenever
duplicated or damaged packets are received from a remote
host. Such network errors are notified no more than once
in N samples.
INTERACTIVE CONTROL
The script reacts to signal SIGHUP to print statistics on the
PING process running in the background. If verbose mode is
enabled (-v) statistics are sent to the standard output. They
are sent to the system log otherwise.
Example:
${0##*/} --host-command=... host &
PID=$!
...
kill -HUP $PID
PING statistics include min, avg and max roundtrip times and
percentage of lost echo replies. Individual figures are also
shown for damaged and duplicate packets if there are any.
Note that the average value is a running average that uses a
discrete averaging algorithm, i.e.:
avg(i) = [ avg(i-1) + x(i-1) ] / 2
SEE ALSO
man ping, ping(8)
EOF
exit 0
}
help()
{
cat <<EOF
${0##*/} version $VERSION, © $DATE by $AUTHOR
Monitors a remote host periodically sending ICMP packets from a ping
process running in the background. The script can execute a custom
command each time an important network condition occurs. The main
purpose is to record echo reply times in a round-robin database for
further graphing.
EOF
usage
}
bc()
{
# Workaround to print the leading zero for values <1
/usr/bin/bc | sed 's/^\./0./g'
}
# Parse command line arguments. Only parse new or overriden arguments.
# Used to determine the remote host, mainly.
parse_args()
{
# Need a F@!#^}G temporary variable to check getopt return code!
args="$(getopt \
-o i:I:p:P:s:W:hv \
-l host-command:,flapping-threshold:,error-threshold \
-- "$@")" && eval set -- "$args" && unset args || return $?
# Now check the remaining arguments
while [ -n "$1" ]; do
[ "$1" = "--" ] || case $1 in
--host-command)
host_cmd=$2; shift;;
--flapping-threshold)
ICMP_FLAPPING_THRESHOLD=$2; shift;;
--error-threshold)
ICMP_NET_ERROR_THRESHOLD=$2; shift;;
-i)
ICMP_INTERVAL=$2; shift;;
-p)
ICMP_PADDING=$2; shift;;
-P)
ICMP_PADDING=$(printf "$2" | od -A n -t x1 | \
sed -r -e 's:\s+::g' -e 's:.::33g'); shift;;
-I)
ICMP_IFACE=$2; shift;;
-s)
ICMP_PKTSIZE=$2; shift;;
-W)
ICMP_TIMEOUT=$2; shift;;
-v)
verbose=true;;
-h)
help;;
*)
ICMP_HOST="$1";;
esac; shift
done
[ -n "$ICMP_HOST" ] || usage 1>&2
}
logger()
{
# Write to standard output in verbose mode, to syslog otherwise
$verbose && echo "$@" || \
/usr/bin/logger -t "${0##*/}[$$]" "$@"
}
set_response_time()
{
# Call external command or prompt to the console in verbose mode
$host_cmd $ICMP_HOST "icmp" $1 $3 && ! $verbose || \
printf "%d: seq=%d, time=%s\n" "$1" "$2" "$3"
return 0
}
set_state_up()
{
# Clear flapping state ans set (steady) up flag
host_state=0x01
# Call the external notification function and log host state
$host_cmd $ICMP_HOST "up"
logger "Host interface or host @ $ICMP_HOST is now up."
}
set_state_down()
{
# Clear up flag only
host_state=$(( host_state & 0xFE ))
# Call the external notification function and log host state
$host_cmd $ICMP_HOST "down"
logger "Host interface or host @ $ICMP_HOST is down!"
}
set_state_flapping()
{
# Set flapping and down flags
host_state=2
# Call the external notification function and log host state
$host_cmd $ICMP_HOST "flapping"
logger "Host interface or host @ $ICMP_HOST is unstable!"
}
set_host_message()
{
# Reset error counter to the maximum
host_warning_state=$ICMP_NET_ERROR_THRESHOLD
# Call the external notification function and log host state
$host_cmd $ICMP_HOST $1
logger "Errors received from host interface or host @ $ICMP_HOST ($1)!"
}
print_stats()
{
if [ $icmp_seq -eq 0 ]; then
logger "PING $ICMP_HOST: no packet sent"
else
local icmp_received=$(( icmp_seq - icmp_lost ))
local icmp_losses=$( echo "scale=2; 100 * $icmp_lost / $icmp_seq" | bc )
logger "PING $ICMP_HOST: $icmp_seq packets sent, $icmp_lost lost, ${icmp_losses}% loss${rtt_min:+; rtt min/avg/max = $rtt_min/$rtt_avg/$rtt_max ms}${icmp_dupes:+, $icmp_dupes dupes}${icmp_damaged:+, $icmp_damaged bad CRC}"
fi
}
echo_reply()
{
# First argument is time in seconds (icmp_seq is global)
local TM=$1 ttl time msg; shift
# Evaluate the remaining arguments as expressions
eval "$@"
# No time variable means host is not responding
[ -z "$time" ] && return 1
# Update statistics: average, minimum and maximum RTT
rtt_avg=$( echo "scale=3; (${rtt_avg:-$time} + $time)/2" | bc )
rtt_min=$( echo "scale=3; rtt_min=${rtt_min:-$time}; if ($time < rtt_min) $time else rtt_min" | bc )
rtt_max=$( echo "scale=3; rtt_max=${rtt_max:-$time}; if ($time > rtt_max) $time else rtt_max" | bc )
# Decrement the state counter if greater than zero
[ $host_unavail_state -ne 0 ] && \
host_unavail_state=$(( host_unavail_state - 1 ))
# The host is not up if:
# - it is flapping (bit 1 of the state flag) and the flapping
# counter is greater than 0 OR
# - the state counter is greater than 0 (non flapping case).
# As long as one of these condition is true, the state flag
# will not be set to UP (bit 0 set, bit 1 cleared).
#
# Once the state counter reaches zero (steady "up" state) and
# the host state is no longer flapping, change the state flag.
# Also don't change the state flag if bit 0 was already set.
[ $(( host_state & 0x02 )) -ne 0 ] && [ $host_flap_state -ne 0 ] || \
[ $host_unavail_state -ne 0 ] || \
[ $(( host_state & 0x01 )) -ne 0 ] || \
set_state_up
# Warn if damaged or duplicate packets. Don't warn
# again until the warning counter reaches zero. Treat
# damaged and dupe packets alike for both are very
# unlikely to occur at the same time.
if [ -n "$msg" ]; then
eval rtt_$msg=$(( rtt_$msg + 1 ))
[ $host_warning_state -eq 0 ] && set_host_message $msg
fi
# Run external command to store response time
set_response_time $TM $icmp_seq $time
return 0
}
no_response()
{
# Store the number of lost replies
icmp_lost=$(( icmp_lost + 1 ))
# FLAPPING DETECTION
# ------------------
# Increment flapping state using a discrete low-pass formula
# to prevent excessive values. Handle flapping only if host
# has just come down, don't wait for a steady "down" state.
[ $host_unavail_state -eq 0 ] && \
[ $(( host_flap_state=(3*host_flap_state + 7*ICMP_FLAPPING_THRESHOLD) / 8 )) -gt $ICMP_FLAPPING_THRESHOLD ] && \
set_state_flapping
# Increment host state until it reaches the threshold, which
# marks the steady "down" state. Only then call the external
# command to allow notifying the host is "down". Just don't
# call the command more than once if the host is still down
# by the next time.
[ $host_unavail_state -lt $ICMP_STEADY_THRESHOLD ] && \
[ $(( host_unavail_state=host_unavail_state + 1 )) -eq $ICMP_STEADY_THRESHOLD ] && \
[ $(( host_state & 0x03 )) -eq 1 ] && \
set_state_down
}
# Parse command-line arguments and set globals
parse_args "$@" || exit $?
# Redirect stderr to LOG_FILE if defined
[ -z "$LOG_FILE" ] || exec 2>$LOG_FILE
# Print PING statistics upon receiving SIGUSR1
trap print_stats HUP
# Send even "stop" upon terminating
trap "printf '\n'; $host_cmd $ICMP_HOST stop; print_stats" INT QUIT TERM ABRT
# Notify monitoring starts
$host_cmd $ICMP_HOST "start"
# 1. filter out lines keeping only those that include response
# times and those about non responding hosts.
# 2. Stick units to response times and keep only the multiplier
# if it's different from "m"
# 3. Keep only the integer part of the timestamp, erase garbage
# before the relevant information (var=value)
# 4. Warn about damaged and duplicate packets
#
# Make sure sed does NOT buffer output (hence -u)
while read R; do
echo_reply $R || no_response
# Decrement other state variables until it reaches zero
[ $host_flap_state -eq 0 ] || \
host_flap_state=$(( host_flap_state - 1 ))
[ $host_warning_state -eq 0 ] || \
host_warning_state=$(( host_warning_state - 1 ))
# Downside: need bash for process redirection, which is needed
# to access state variables outside the while/read loop...
done < <(LC_ALL=POSIX /bin/ping -OD \
${ICMP_IFACE:+-I $ICMP_IFACE} \
${ICMP_TIMEOUT:+-W $ICMP_TIMEOUT} \
${ICMP_PKTSIZE:+-s $ICMP_PKTSIZE} \
-i ${ICMP_INTERVAL:-$ICMP_INTERVAL_DEFAULT} \
-p ${ICMP_PADDING:-$ICMP_PADDING_DEFAULT} $ICMP_HOST |
sed -rnu \
-e '/no answer|[0-9]+ bytes from/!d' \
-e 's@(time=[0-9.]+)\s+m?(\w*)s@@g' \
-e 's@\(DUP\!\)@msg="dupes"@g' \
-e 's@\(BAD CHECKSUM\!\)@msg="damaged"@g' \
-e 's@\[(\w+)\.\w+\][a-zA-Z0-9():. \-]+\s+@ @gp')