[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

sort_mirrors_by_speed v 0.2



hi

since I had a few nice comments about the above script, I post a
second revised version.


this script takes as its argument  the file README.mirrors
 (that can be downloaded from the debian ftp sites), extracts the list
 of hosts and pings them all (it takes roughly 60 seconds, since
 it parallelizes) then sorts the output by fastest transmit time

a.Mennucci

--------------------------------- script follows ------------------------
#!/bin/sh

#  sort_mirrors_by_speed v 0.2
#          by A.Mennucci Nov 97
#
# this program is subject to the
#  GNU general public license

#
# this program will scan the file in $1 using
#  grep "\.[a-zA-Z]*:" $1 | awk '{print $1}' 
#  to find mirror site host names.
#
#  the string before the  :  character is considered to be a 
#   mirror site host name 
#
#  Mirror sites are pinged and statistics  are collected and sorted
#

# TODO 
#  with an option like  --archie , this would be great
#  to sort the output of archie
#
#

############# max number of coesisting pings
u=`ulimit -u`
MAX_PINGS=`/usr/bin/expr $u - 6  `
if [ $MAX_PINGS -le 0  ] ; then
 MAX_PINGS=1
fi

#I do not know how to get the max resident memory
# that would help in computing this

if [ $MAX_PINGS -gt 30  ] ; then
 MAX_PINGS=30
fi

########## ditto
PING_PACKETS=20

##########note that ping adds 8 bytes to a packet
PACKET_SIZE=1016

##############
umask 066

if [ "$1" = "-h" -o "$1" = "--help" ] ; then
 echo Usage: $0 mirrorfile
 echo " Argument must be the file README.mirrors "
 echo "      containing a list of debian mirrors."
 echo " This program will ping all mirrors with $PING_PACKETS packets"
 echo "  to test their speed and reliability, and then will try to "
 echo "  compute the speed of the connection. It will"
 echo "  sort the result, put it in file mirrors_by_speed and show the best."
  exit 0
fi


if [ x$TMPDIR = x ] ; then 
 tmpdir=/tmp
else
 tmpdir=$TMPDIR
fi

# warning: using the directory /tmp and running this script
# as root may be  a potential security problem: I do not guarantee security

if ! [ -w  $tmpdir ] ; then
 echo $0 ERROR dir $tmpdir is not writable
 echo please create it if it does not exist
 exit 1 
fi

tmpdir=$tmpdir/sort_m_$$_`dd if=/dev/random count=1 bs=16  2> /dev/null |\
  sed -n l | /usr/bin/tr --delete --complement "[:alnum:]" `

if ! mkdir $tmpdir ; then
 echo $0: cannot make dir $tmpdir
 exit 1
fi 

## this doesnt work as I expected, alas 
#trap  "/bin/rm -r $tmpdir ; exit" SIGTERM
#trap  "/bin/rm -r $tmpdir ; exit" SIGQUIT
#trap  "/bin/rm -r $tmpdir ; exit" SIGINT
#trap -p

if [ ! -r "$1" -o "$1" = "" ] ; then
 echo $0 ERROR Give as argument the file README.mirrors 
 echo "      containing a list of debian mirrors"
 exit 1 
fi
if [ -r mirrors_by_speed ] ; then
  echo $0 ERROR the file  mirrors_by_speed already exists
  exit 1
fi  
################ here we go



n=0

echo -n "Testing "
grep "\.[a-zA-Z]*:" $1 | awk '{print $1}'   | \
   wc --lines |  tr "\n" " "
echo " debian mirror sites for speed and reliability."

echo Parallelizing: up to $MAX_PINGS pings at a time.

echo -n "Tests done : "

for i in ` grep "\.[a-zA-Z]*:" $1 | awk '{print $1}'  ` ;
 do
  n=`expr $n + 1 `
  touch  $tmpdir/__$n
   (
   h=$n
   #echo -n "i$h "
   # note that ping adds 8 bytes to a packet
   if ping -s $PACKET_SIZE -p A7C3 -c $PING_PACKETS `echo "$i" |\
        cut -d: -f1  `  >> $tmpdir/__$h   2>> /dev/null  ; then
    #if [ ! -r  $tmpdir/__$h ]  ; then exit ; fi
    ms=`grep "round-trip" $tmpdir/__$h |\
        cut -d/ -f 4  | cut -d. -f1  `
    #if [ ! -r  $tmpdir/__$h ]  ; then exit ; fi	
    r=`grep "received" $tmpdir/__$h |\
        cut -d"," -f 3 | cut -d% -f1  `
    #if [ ! -r  $tmpdir/__$h ]  ; then exit ; fi	
    b=`expr \( $PACKET_SIZE + 8 \) \* \( 100 - $r \)  \* 10 / $ms  `
    #if [ ! -r  $tmpdir/__$h ]  ; then exit ; fi	
    echo  "$b b/s, $ms ms/p, $r% loss, SITE $i" >> mirrors_by_speed
    #echo  "$b bytes/sec, $ms ms, $r% loss, SITE $i" 
   else
    echo  "0 PING ERROR FOR SITE $i " >> mirrors_by_speed
   fi 
   #if [ -r  $tmpdir/__$h ]  ; then rm $tmpdir/__$h ; fi
   rm $tmpdir/__$h
   echo -n "$h "
   ) &
   
   # we do not want to flood the net; we test $MAX_PINGS sites at a time

   # this is taken from an idea by    Gertjan Klein <gklein@xs4all.nl>
   
      while [  `/bin/ls -1f $tmpdir | /usr/bin/wc --lines` \
                -ge `/usr/bin/expr $MAX_PINGS + 2 ` ]
      do
           /usr/bin/sleep 2
      done
	      
   if ! [ -w $tmpdir ] ; then 
    echo ABORTED
    exit
   fi 
 done

wait
echo "."
echo "Fastest mirrors are (b/s=Bytes/sec, ms/p=millisec/packet): "
mv mirrors_by_speed   mirrors_by_speed~
sort -nr  mirrors_by_speed~ >  mirrors_by_speed 


head  mirrors_by_speed 

rm -r $tmpdir mirrors_by_speed~


--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
debian-user-request@lists.debian.org . 
Trouble?  e-mail to templin@bucknell.edu .


Reply to: