#!/bin/sh
debug=0
VERSION=251218
SCRIPT=$(readlink -f $0)
SCRIPTNAME=$(basename $SCRIPT)
SCRIPTDIR=$(dirname $SCRIPT)
if [ "$1" = "-v" ] ; then echo "$VERSION $SCRIPTNAME $SCRIPTDIR";exit 0; fi
if [ -e "$SCRIPTDIR/esc.sh" ] ; then . "$SCRIPTDIR/esc.sh"; fi
LOG=$LOGDIR/log-$SCRIPTNAME.log

#https://developpaper.com/common-operations-built-in-linux-shell-string-get-length-find-replace/
if [ -n "$1" ] ; then
        size=$1
        echo "custom test size: $1 M"
        echo
else
        size=256
        echo "testing with size: $size M"
fi

echo
if [ -n "$2" ] ; then fs=$2; echo "testing datastore: $2"; echo; fi

for entry in $(ls /vmfs/volumes  | grep -vE "BOOTBANK*|LOCKER*|OSDATA*") ; do
        # filter names like: 0365c2a3-9a9a7fb3 (discovered auto mount esxi7)
        # ([0-9]+([a-zA-Z]+[0-9]+)+)-([0-9]+([a-zA-Z]+[0-9]+)+)
        #reg="([a-zA-Z]+[0-9]+){1}-{1}([0-9]+([a-zA-Z]+[0-9]+){1})"
        #reg="^([0-9]+([a-zA-Z]+[0-9]+)+)-([0-9]+([a-zA-Z]+[0-9]+)+)$"
        reg="^([a-zA-Z0-9]{8})-([a-zA-Z0-9]{8})$"
        echo "$entry" | grep -Eq $reg
        if [ $? == 0 ] ; then
                if [ "$debug" == 1 ] ; then echo "ignoring (f:automnt): $entry"; fi
                continue;
        fi
        reg="^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"
        echo "$entry" | grep -Eq $reg
        if [ $? == 0 ] ; then
                if [ "$debug" == 1 ] ; then echo "ignoring (f:guid): $entry"; fi
                continue;
        fi

        if [ -n "$fs" ] && [ "$entry" != "$fs" ] ; then continue; fi
        echo -n "datastore: $entry speed: "
        if [ "$debug" == 1 ] ; then echo; continue; fi;
        touch "/vmfs/volumes/$entry/testfile" >/dev/null 2>&1
        if [ $? != 0 ]; then echo "write-error"; continue; fi
        var=$( (time dd if=/dev/zero of=/vmfs/volumes/$entry/testfile bs=1M count=$size)  2>&1 > /dev/null | grep real | awk {'print $3'} )
        sec=${var:0:-1}
        #echo size: $size time:$sec
        awk -v a=$size -v b=$sec 'BEGIN {printf "%.1f MB/s \n", (a / b) }'
        rm /vmfs/volumes/$entry/testfile
done

echo
exit

