Last active
September 19, 2023 15:50
-
-
Save Riebart/aae0f98b05be5b54ede6a85dd09f9790 to your computer and use it in GitHub Desktop.
Some basic network monitoring scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Perform some basic DNS, TCP, TLS, and ICMP testing targeting a remote endpoint or two, | |
# to spot transient issues you'd normally miss in 5-minute aggregate date | |
# | |
# DNS-TCP pings are emitted as a CSV to stdout, floodping is emitted to stderr | |
# Example of how to run it and capture both outputs. | |
# NOTE: In the stderr redirect target shell, the stdout gets captured by the stdout subshell, | |
# so we need to foce that to stderr. | |
# sudo bash monitor.sh "dontfloodmewithicmp.com" "floodmewithicmp.com" \ | |
# > >(tee -a `date +%F`.tcping.csv) 2> >(tee -a `date +%F`.pingf.csv >&2) | |
# Examples of how to run it and capture both outputs. | |
# sudo bash monitor.sh "dontfloodmewithicmp.com" > >(tee -a `date +%F`.tcping.csv) 2> >(tee -a `date +%F`.pingf.csv) | |
dnstcp_target="$1" | |
if [ "$1" != "" ] | |
then | |
(echo "unixtime,host,rtt" | |
while [ true ] | |
do | |
echo "`date -u +%s.%N`,${dnstcp_target},`( | |
time nc -vzw2 "${dnstcp_target}" 443) 2>&1 | | |
grep --line-buffered real | tr 's' 'm' | | |
cut -d 'm' -f2`" | |
sleep 0.5 | |
done) & | |
dnstcp_pid="$!" | |
fi | |
flood_ping_target="$2" | |
if [ "$2" != "" ] | |
then | |
( | |
echo "unixtime,num_transmitted,num_received,percent_loss,rtt_min,rtt_avg,rtt_max,rtt_mdev,ipg,ewma" >&2 | |
while [ true ] | |
do | |
date -u +"%s.%N"; sudo ping -f "${flood_ping_target}" -w 10 | |
done | | |
sed --unbuffered -n '/^[0-9]*\.[0-9]*/{:a;{/\nrtt/!{N;ba}};s/\n/ /gp}' | tr '/' ' ' | | |
sed --unbuffered -n 's/^\([^ ]*\) .* \([0-9]*\) packets trans.* \([0-9]*\) recei.* \([0-9.]*\). packet loss.* = \([^ ]*\) \([^ ]*\) \([^ ]*\) \([^ ]*\) ms.* ewma \([^ ]* [^ ]*\) .*$/\1,\2,\3,\4,\5,\6,\7,\8,\9/p' | | |
while read line | |
do | |
# `tr` has no ability to line-buffer, which is insane. | |
echo "$line" | tr ' ' ',' >&2 | |
done >&2 | |
) >&2 & | |
flood_ping_pid="$!" | |
fi | |
finish() { | |
pkill -9 -P $dnstcp_pid | |
pkill -9 -P $flood_ping_pid | |
kill -9 $dnstcp_pid | |
kill -9 $flood_ping_pid | |
exit 0 | |
} | |
trap 'finish' SIGINT | |
if [ "$dnstcp_pid" != "" ] | |
then | |
wait "$dnstcp_pid" | |
fi | |
if [ "$flood_ping_pid" != "" ] | |
then | |
wait "$flood_ping_pid" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment