Skip to content

Instantly share code, notes, and snippets.

@mzz2017
Last active April 1, 2021 15:50
Show Gist options
  • Save mzz2017/3e18ddaccd054add3d4a434b7f973f25 to your computer and use it in GitHub Desktop.
Save mzz2017/3e18ddaccd054add3d4a434b7f973f25 to your computer and use it in GitHub Desktop.
glider_ping_check
#!/bin/bash
loss_tolerance=15.0
avg_tolerance=20.0
max_tolerance=30.0
mdev_tolerance=5
test_count=10
timeout_second=2
if [ -z $FORWARDER_ADDR ]
then
echo "Please set environment variable FORWARDER_ADDR"
exit 1
fi
command -v bc || (echo "bc: command not found" && exit 2)
stat=$(ping $FORWARDER_ADDR -c $test_count -s 56 -W $timeout_second -n|tail -n 2)
loss=$(echo $stat|cut -d, -f3|cut -d% -f1|awk '{gsub(/^\s+|\s+$/, "");print}')
if [ $(echo "$loss <= $loss_tolerance"|bc) -eq 0 ]
then echo "$loss > loss_tolerance($loss_tolerance)" && exit 1
fi
avg=$(echo $stat|cut -d= -f2|cut -d' ' -f2|cut -d/ -f2)
max=$(echo $stat|cut -d= -f2|cut -d' ' -f2|cut -d/ -f3)
mdev=$(echo $stat|cut -d= -f2|cut -d' ' -f2|cut -d/ -f4)
if [ -z $avg ]
then exit 1
fi
if [ $(echo "$avg <= $avg_tolerance"|bc) -eq 0 ]
then echo "$avg > avg_tolerance($avg_tolerance)" && exit 1
else if [ $(echo "$max <= $max_tolerance"|bc) -eq 0 ]
then echo "$max > max_tolerance($max_tolerance)" && exit 1
else if [ $(echo "$mdev <= $mdev_tolerance"|bc) -eq 0 ]
then echo "$mdev > mdev_tolerance($mdev_tolerance)" && exit 1
fi
echo "ping_check: $FORWARDER_ADDR: avg/max/mdev/loss: $avg/$max/$mdev/$loss%"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment