#!/bin/bash
#Generating different number of tcp connections (from 1 to 6K) and collect the throughput, Cpu usage and number of re-transmitted packet info.
#And also format the test results using CSV.
#Prerequisites: iperf3, sysstat(for sar), dos2unix
server_ip=$1
duration=600
threads=(1 2 4 8 16 32 64 128 256 512 1024 2000 3000 4000 5000 6000)
i=0
logs_dir=~/logs
[ -d $logs_dir ] && rm -rf $logs_dir
mkdir $logs_dir
[ -f ~/teststatus.txt ] && rm -rf ~/teststatus.txt
killall iperf3
echo "IPERF test started" > ~/teststatus.txt
while [ "x${threads[$i]}" != "x" ]
do
	port=8001
	number_of_connections=${threads[$i]}
	echo "Current no of connections: $number_of_connections"
	echo "IPERF test running for $number_of_connections connections" >> ~/teststatus.txt
	presenttestlog=$logs_dir/${threads[$i]}
	mkdir $presenttestlog
	count=1
	bash ./perf-statistics.sh $duration $presenttestlog &
	while [ $number_of_connections -gt 64 ]; do
		number_of_connections=$(($number_of_connections-64))
		options="-p $port -P 64 -t $duration"
		file_tag=`echo $options| sed s/-//g| sed "s/ /_/g"`
		log_file=`echo ${threads[$i]}-$count-$file_tag-iperf.log| sed "s/-/_/g"`
		echo $log_file
		iperf3 -c $server_ip $options > $presenttestlog/$log_file &
		port=$((port+1))
		count=$((count+1))
	done
	options="-p $port -P $number_of_connections -t $duration"
	file_tag=`echo $options| sed s/-//g| sed "s/ /_/g"`
	log_file=`echo ${threads[$i]}-$count-$file_tag-iperf.log| sed "s/-/_/g"`
	echo $log_file
	iperf3 -c $server_ip $options > $presenttestlog/$log_file
	i=$(($i + 1))
	port=$(($port + 1))
	sleep 10
done
echo "IPERF test completed" >> ~/teststatus.txt

#formatting logs into csv file:
mkdir $logs_dir/csvs
cd $logs_dir/
options=(`ls */*iperf.log | sed s/\.log//`)
echo "CSV test started" >> ~/teststatus.txt
count=0
while [ "x${options[count]}" != "x" ]
do
	echo "${options[count]}"
	cat ${options[count]}.log | grep SUM > /dev/null
	if [ $? -ne 0 ]; then
		cat ${options[count]}.log | grep sec | awk '!($1="")' | awk '!($1="")' | awk '{$1=$1}1' OFS=","| sed s/\(// | sed s/\)//|sed s/-/,/ | sed s/%//>$logs_dir/csvs/`basename ${options[count]}`.csv
	else
		cat ${options[count]}.log | grep SUM | awk '!($1="")' | awk '{$1=$1}1' OFS=","| sed s/\(// | sed s/\)//|sed s/-/,/ | sed s/%//>$logs_dir/csvs/`basename ${options[count]}`.csv
	fi
	count=$(( $count + 1 ))
done
echo "CSV test completed" >> ~/teststatus.txt
cd ~
tar -cvf logs.tar logs > /dev/null
echo "PERF test completed" >> ~/teststatus.txt

exit 0


#!/bin/bash
#Running iperf3 server for 8001 to 8100 ports
killall iperf3
for i in {8001..8100}
do
	iperf3 -s -D -p $i
done
count=$(pgrep iperf3 | wc -l)
if [ $count -eq 100 ]; then
	echo "iperf server is running and available from 8001 to 8100 ports"
else
	echo "iperf server is not running as a daemon"
fi


#!/bin/bash
#This script will connect iperf client to server for executing network performance tests using different iperf options
#And also format the test results using CSV.
server_ip=$1
readarray  options < perf-cloudharmony-options.txt
logs_dir=~/logs
[ -d $logs_dir ] && rm -rf $logs_dir
mkdir $logs_dir
count=0
killall iperf3
echo "IPERF test started" > ~/teststatus.txt
while [ "x${options[count]}" != "x" ]
do
	echo ${options[$count]}
	echo "IPERF test ${options[$count]} running" >> ~/teststatus.txt
	file_tag=`echo ${options[$count]}| sed s/-//g| sed "s/ /_/g"`
	echo $file_tag
	iperf3 -c $server_ip ${options[$count]} > $logs_dir/$file_tag.log
	count=$(( $count + 1 ))
	sleep 30
done
echo "IPERF test completed" >> ~/teststatus.txt

#formatting iperf3logs into csv:
mkdir $logs_dir/csvs
cd $logs_dir/
options=(`ls *.log | grep "_u_"|sed s/\.log//`)
echo "CSV test started" >> ~/teststatus.txt
count=0
while [ "x${options[count]}" != "x" ]
do
	echo "${options[count]}"
	tail -n+5 ${options[count]}.log | awk '!($1="")' | awk '!($1="")' | awk '{$1=$1}1' OFS=","| sed s/\(// | sed s/\)//| sed s/-/,/ | sed s/%//>$logs_dir//csvs/${options[count]}.csv
	count=$(( $count + 1 ))
done

options=(`ls *.log | grep -v "_u_"|sed s/\.log//`)

count=0
while [ "x${options[count]}" != "x" ]
do
	echo "${options[count]}"
	cat ${options[count]}.log  | grep SUM |awk '!($1="")'  | awk '{$1=$1}1' OFS=","| sed s/\(// | sed s/\)//|sed s/-/,/ | sed s/%//>$logs_dir//csvs/${options[count]}.csv
	count=$(( $count + 1 ))
done
echo "CSV test completed" >> ~/teststatus.txt
cd ~
tar -cvf logs.tar logs
echo "PERF test completed" >> ~/teststatus.txt

exit 0


#!/bin/bash
#starting iperf server as a daemon
killall iperf3
iperf3 -s -D -p 8004
#
# Verify that the iperf server is running
pgrep iperf3
if [ $? -ne 0 ]; then
	echo "iperf server is not running as a daemon"
else
	echo "iperf server is running as a daemon"
fi


#!/bin/bash
#V-SHISAV@MICROSOFT.COM

#HOW TO PARSE THE ARGUMENTS.. SOURCE - http://stackoverflow.com/questions/4882349/parsing-shell-script-arguments

while echo $1 | grep ^- > /dev/null; do
    eval $( echo $1 | sed 's/-//g' | tr -d '\012')=$2
    shift
    shift
done
packageName=$install
isUpdate=$update
isRemote=$isRemote
isLocal=$isLocal
fileName=$file
CurrentDate=`date`

installFromFolder()
{
while echo $1 | grep ^- > /dev/null; do
    eval $( echo $1 | sed 's/-//g' | tr -d '\012')=$2
    shift
    shift
done
sourceFolder=$sourceFolder
cd $sourceFolder
ls
if [ -e ./configure ]; then
    chmod +x configure && ./configure && make && make install
    exitVal=$?
elif [ -e ./setup.py ]; then
    python setup.py build && python setup.py install
    exitVal=$?
fi
cd ..
return $exitVal
}

getFolderName()
{
while echo $1 | grep ^- > /dev/null; do
    eval $( echo $1 | sed 's/-//g' | tr -d '\012')=$2
    shift
    shift
done

compressedFile=$compressedFile
if [[ "$compressedFile" == *.tar.gz ]]; then
    folderName=${compressedFile%.tar.gz*}
elif [[ "$compressedFile" == *.tar ]]; then
    folderName=${compressedFile%.tar.bz2*}
elif [[ "$compressedFile" == *.tar ]]; then
    folderName=${compressedFile%.tar*}
fi
echo $folderName
}

installFromCompressedFile()
{
#{
#   extract the source code.
#   invoke : decompressFile -file $fileName
#   search for installation files in extracted folder. Like - setup.py / configure / make etc.
#   invoke "appropriate" installation method.. [installPythonSourceCode() / installMakeSourceCode() / ...]
#   return exit code.
while echo $1 | grep ^- > /dev/null; do
    eval $( echo $1 | sed 's/-//g' | tr -d '\012')=$2
    shift
    shift
done

compressedFile=$compressedFile
decompressFile -fileToDecompress $compressedFile
installFolder=`getFolderName -compressedFile $compressedFile`
echo "$installFolder"
installFromFolder -sourceFolder $installFolder
exitVal=$?

return $exitVal
}

decompressFile()
{
#   determine the extraction method depending on *.tar, *.tar.gz, *.bz2, *.gzip etc.
#   execute : tar -xzf / gunzip / bzip etc. and untar the compressed file.
#   return exit code.
while echo $1 | grep ^- > /dev/null; do
    eval $( echo $1 | sed 's/-//g' | tr -d '\012')=$2
    shift
    shift
done

fileToDecompress=$fileToDecompress
if [[ "$installFile" == *.tar ]]; then
    tar -xzvf $fileToDecompress
        #installFromCompressedFile
    exitVal=$?
elif [[ "$installFile" == *.tar.gz ]]; then
    tar -xvf $fileToDecompress
    exitVal=$?
fi

if [ $exitVal == 0 ]; then
    echo "Decompresssion Successfull"
else
    echo "Decompresssion Failed"
fi
return $exitVal

}
#other installation methods support can be added depending on requirement.

installLocalFile()
{
while echo $1 | grep ^- > /dev/null; do
    eval $( echo $1 | sed 's/-//g' | tr -d '\012')=$2
    shift
    shift
done

installFile=$LocalFile
if [[ ( "$installFile" == *.tar ) || ( "$installFile" == *.tar.gz ) || ( "$installFile" == *.tar.bz2 ) ]]; then
    echo "provided file $installFile"
    echo "Installing from copressed file"
    installFromCompressedFile -compressedFile $installFile
    exitVal=$?
elif [[ "$installFile" == *.rpm ]]; then
    echo "provided file $installFile"
    echo "Installing rpm file"
    rpm -ivh $installFile
    exitVal=$?
elif [[ "$installFile" == *.deb ]]; then
    echo "provided file $installFile"
    echo "Installing deb file"
    dpkg -i $installFile
    exitVal=$?
elif [ -z $installFile ]; then
    echo "Please provide file with -file <filename> argument"
    exitVal=1
else
    echo "Unknowin file type.."
    exitVal=1
fi
return $exitVal
}
installRemotePackage()
{
while echo $1 | grep ^- > /dev/null; do
    eval $( echo $1 | sed 's/-//g' | tr -d '\012')=$2
    shift
    shift
done
packageName=$install
        if [ -e /etc/debian_version ]; then
                apt-get install --force-yes -y $packageName
                installStatus=$?
                if [ $installStatus = 0 ]; then
                        echo "Install $packageName : SUCCESS"
                        exitVal=0
                else
                        echo "Install $packageName : FAILED"
                        exitVal=1
                fi
        fi
        if [ -e /etc/redhat-release ]; then
                sed -i 's/exclude=kernel/#exclude=kernel/' /etc/yum.conf
                yum install --nogpgcheck -y $packageName
                installStatus=$?
                if [ $installStatus = "0" ]; then
                        echo "Install $packageName : SUCCESS"
                        exitVal=0
                else
                        echo "Install $packageName : FAILED"
                        exitVal=1
                fi
        fi
        if [ -e /etc/SuSE-release ]; then
                zypper --non-interactive --no-gpg-checks install $packageName
                installStatus=$?
                if [ $installStatus = "0" ]; then
                        echo "Install $packageName : SUCCESS"
                        exitVal=0
                else
                        echo "Install $packageName : FAILED"
                        exitVal=1
                fi
        fi
return $exitVal
}
updateDistro()
{
    if [ -e /etc/debian_version ]; then
        apt-get --force-yes -y update
        installStatus=$?
        if [ $installStatus = 0 ]; then
            echo "Install Update : SUCCESS"
            exitVal=0
        else
        echo "Install Update : FAILED"
        exitVal=1
        fi
    fi
    if [ -e /etc/redhat-release ]; then
            sed -i 's/exclude=kernel/#exclude=kernel/' /etc/yum.conf
        yum --nogpgcheck -y update
        installStatus=$?
        if [ $installStatus = "0" ]; then
            echo "Install Update : SUCCESS"
            exitVal=0
        else
            echo "Install Update : FAILED"
            exitVal=1
        fi
    fi
    if [ -e /etc/SuSE-release ]; then
        zypper --non-interactive --no-gpg-checks update
        installStatus=$?
        if [ $installStatus = "0" ]; then
            echo "Install Update : SUCCESS"
            exitVal=0
        else
            echo "Install Update : FAILED"
            exitVal=1
        fi
    fi

return $exitVal
}

# main body starts here..

if [ "$isUpdate" = "yes" ]; then
    updateDistro
    exitVal=$?
    exit $exitVal
fi
if [ -z $packageName ]; then
    echo "No packages are given to install. Please use -package <Package Name>"
    exitVal=0
else
    if [ -z $isLocal ]; then
        echo "Please mention -isLocal yes/no"
    elif [ "$isLocal" = "no" ]; then
            installRemotePackage -install $packageName
            exitVal=$?
    elif [ "$isLocal" = "yes" ]; then
            installLocalFile -LocalFile $fileName
            exitVal=$?
    else
        echo "Please provide -isLocal yes or -isLocal no [CaSe SensitivE]"
    fi
fi
exit $exitVal


#!/bin/bash
#This script will reports network statistics and CPU usage
duration=$1
filename=$2
#Displays network devices vital statistics for eth0 and lo
sar -n DEV 1 $duration 2>&1 >> $filename/sar.log &
for i in $(seq 1 $duration)
do
#Displays the CPU usage
top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}' >> $filename/cpu_usage.log
sleep 1
done


#!/bin/bash

########################################################################
#
# Linux on Hyper-V and Azure Test Code, ver. 1.0.0
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
# Licensed under the Apache License, Version 2.0 (the ""License"");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
########################################################################

#######################################################################
#
# performance_mc.sh
#
# Description:
#     This tool uses the memslap to Load testing and benchmarking a server
#     More info : http://docs.libmemcached.org/bin/memaslap.html
#
#
# In general the script does the following:
#
# 1.Install memcached server on mentioned servers and starts memcached server service.
# 2.Benchmarks mem servers with memslap tool.
#
# Parameters:
# MC_VERSION=1.4.25
# MC_SERVERS="Server1:11211,Server2:11211"
# MC_CONCURRENCY=10
# MC_EXECUTE_NUMBER=10000
# MC_INITIAL_LOAD=1000
#######################################################################

ICA_TESTRUNNING="TestRunning"
ICA_TESTCOMPLETED="TestCompleted"
ICA_TESTABORTED="TestAborted"
ICA_TESTFAILED="TestFailed"

#
# Function definitions
#
LogMsg()
{
    echo `date "+%a %b %d %T %Y"` ": ${1}"
}

UpdateTestState()
{
    echo $1 > ~/state.txt
}

#######################################################################
#
# LinuxRelease()
#
#######################################################################
LinuxRelease()
{
    DISTRO=`grep -ihs "buntu\|Suse\|Fedora\|Debian\|CentOS\|Red Hat Enterprise Linux" /etc/{issue,*release,*version}`

    case $DISTRO in
        *buntu*)
            echo "UBUNTU";;
        Fedora*)
            echo "FEDORA";;
        CentOS*)
            echo "CENTOS";;
        *SUSE*)
            echo "SLES";;
        Red*Hat*)
            echo "RHEL";;
        Debian*)
            echo "DEBIAN";;
    esac
}

######################################################################
#
#  Description:
#    Perform distro specific memcached configuratoin and tool installation steps
#    and then run the benchmark tool
#
#######################################################################
ConfigUbuntuMemC()
{
    #
    # Note: A number of steps will use SSH to issue commands to the
    #       MC_SERVER.  This requires that the SSH keys be provisioned
    #       in advanced, and strict mode be disabled for both the SSH
    #       server and client.
    #

    LogMsg "Info: Running Ubuntu Config on client VM."
    LogMsg "Checking if memcached is installed or not.."
    memcslapInstalled=`which memcslap`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing memcslap"
        apt-get -y install libmemcached-tools
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install memcslap"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
        LogMsg "memcslap installed on client."
    fi
    arr=$(echo $MC_SERVERS | tr "," "\n")
    for MCS in $arr
    do
        currentServer=${MCS/:11211/}
		LogMsg "Info: -----------------------------------------"
		LogMsg "Info: memcached server installation on server ${currentServer}"
		ssh root@${currentServer} "apt-get install -y memcached"
        ssh root@${currentServer} "sed --in-place -e 's/-l 127.0.0.1//g' /etc/memcached.conf"
        ssh root@${currentServer} "service memcached restart"
        if [ $? -ne 0 ]; then
            msg="Error: Unable to install package to server ${currentServer}"
            LogMsg "${msg}"
            echo "${msg}" >> ./summary.log
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
        LogMsg "Info: memcached Server started successfully on ${currentServer}"

    done
}

ConfigRHELMemC()
{
    LogMsg "Info: Running CENTOS/RHEL Config on client VM."
    LogMsg "Checking if memcached is installed or not.."
    memslap --help > /dev/null
    if [ $? -ne 0 ]; then
        LogMsg "Installing memslap"
        yum install -y libmemcached
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install memcslap"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
        LogMsg "memslap installed on client."
    fi
    arr=$(echo $MC_SERVERS | tr "," "\n")
    for MCS in $arr
    do
        currentServer=${MCS/:11211/}
		LogMsg "Info: -----------------------------------------"
		LogMsg "Info: memcached server installation on server ${currentServer}"
		ssh root@${currentServer} "yum install -y memcached"
        ssh root@${currentServer} "service memcached restart"
        if [ $? -ne 0 ]; then
            msg="Error: Unable to install package to server ${currentServer}"
            LogMsg "${msg}"
            echo "${msg}" >> ./summary.log
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
        LogMsg "Info: memcached Server started successfully on ${currentServer}"

    done
}

ConfigRHELMemSlap()
{
    #
    # Note: MemSlap is client for memcached to benchmark memcached server.
    #        This is long running client so far.
    #

    LogMsg "Info: Running RHEL"
    LogMsg "Info: -----------------------------------------"
    LogMsg "Info: memslap installation"
    LogMsg "Info: Installing required packages first"
    yum install -y wget gcc make gcc-c++
    if [ $? -ne 0 ]; then
        msg="Error: Unable to install client packages"
        LogMsg "${msg}"
        echo "${msg}" >> ./summary.log
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    LogMsg "Info: Download memslap package"
    pkg=(`ssh root@${SERVER} "ls /root/ | grep ${MEMSLAP_VERSION}"`)
    echo $pkg
    if [ -z "$pkg" ]; then
        LogMsg "Downloading memslap package ${MEMSLAP_VERSION}"
        #exit 1
            wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz
            if [ $? -ne 0 ]; then
                    msg="Error: Unable to download memslap package"
                    LogMsg "${msg}"
                    echo "${msg}" >> ./summary.log
                    UpdateTestState $ICA_TESTFAILED
                    exit 1
            fi
    fi

    LogMsg "Info: Untar and Config ZK server"
    tar -xvzf libmemcached-1.0.18.tar.gz
    cd libmemcached-1.0.18/
    ./configure
    make
    make install

    if [ $? -ne 0 ]; then
                msg="Error: Unable install memslap"
                LogMsg "${msg}"
                echo "${msg}" >> ./summary.log
                UpdateTestState $ICA_TESTFAILED
                exit 1
        fi
    LogMsg "Info: memslap client setup successfully"
}

#######################################################################
#
# Main script body
#
#######################################################################

cd ~
UpdateTestState $ICA_TESTRUNNING
LogMsg "Starting test"

#
# Delete any old summary.log file
#
LogMsg "Cleaning up old summary.log"
if [ -e ~/summary.log ]; then
    rm -f ~/summary.log
fi

touch ~/summary.log

#
# Source the constants.sh file
#
LogMsg "Sourcing constants.sh"
if [ -e ~/constants.sh ]; then
    . ~/constants.sh
else
    msg="Error: ~/constants.sh does not exist"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    UpdateTestState $ICA_TESTABORTED
    exit 10
fi

#
# Make sure the required test parameters are defined
#

if [ "${MC_VERSION:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the MC_VERSION test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    UpdateTestState $ICA_TESTFAILED
    exit 20
fi

if [ "${MC_SERVERS:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the MC_SERVER test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    UpdateTestState $ICA_TESTFAILED
    exit 20
fi

if [ "${MC_CONCURRENCY:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the MC_CONCURRENCY test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    MC_CONCURRENCY=100
fi

if [ "${MC_EXECUTE_NUMBER:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the MC_EXECUTE_NUMBER test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    MC_EXECUTE_NUMBER=10000
fi

if [ "${MC_INITIAL_LOAD:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the MC_INITIAL_LOAD test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    MC_INITIAL_LOAD=10000
fi

LogMsg "Info: Test run parameters"
echo "MC_VERSION        = ${MC_VERSION}"
echo "MC_SERVERS        = ${MC_SERVERS}"
echo "MC_CONCURRENCY    = ${MC_CONCURRENCY}"
echo "MC_EXECUTE_NUMBER = ${MC_EXECUTE_NUMBER}"
echo "MC_INITIAL_LOAD   = ${MC_INITIAL_LOAD}"

#
# Configure MC server - this has distro specific behaviour
#
distro=`LinuxRelease`
case $distro in
    "CENTOS" | "RHEL")
        ConfigRHELMemC
		testString="memslap --servers=${MC_SERVERS} --concurrency=${MC_CONCURRENCY} --execute-number=${MC_EXECUTE_NUMBER} --initial-load=${MC_INITIAL_LOAD} --flush"
    ;;
    "UBUNTU")
        ConfigUbuntuMemC
		testString="memcslap --servers=${MC_SERVERS} --concurrency=${MC_CONCURRENCY} --execute-number=${MC_EXECUTE_NUMBER} --initial-load=${MC_INITIAL_LOAD} --flush"
    ;;
    "DEBIAN")
		LogMsg "Debian distro not yet supported"
        #ConfigDebianMemC
    ;;
    "SLES")
		LogMsg "SLES distro not yet supported"
        #ConfigSlesMemC
    ;;
     *)
        msg="Error: Distro '${distro}' not supported"
        LogMsg "${msg}"
        echo "${msg}" >> ~/summary.log
        UpdateTestState "TestAborted"
        exit 1
    ;;
esac

LogMsg "Running memslap tests with cmd: ${testString}"
eval $testString
if [ $? -ne 0 ]; then
    msg="Error: Error in running tests"
    LogMsg "${msg}"
    echo "${msg}" >> ./summary.log
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

#
# If we made it here, everything worked.
# Indicate success
#
LogMsg "Test completed successfully"
UpdateTestState $ICA_TESTCOMPLETED

exit 0


#!/bin/bash

#######################################################################
#
# Linux on Hyper-V and Azure Test Code, ver. 1.0.0
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
# Licensed under the Apache License, Version 2.0 (the ""License"");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
#######################################################################

#######################################################################
#
# performance_md.sh
#
# Description:
#    Install and configure mongodb so the mongoperf benchmark can
#    be run.  This script needs to be run on single VM.
#
#    mongo perf needs Java runtime is also installed.
#
#    Installing and configuring Hadoop consists of the following
#    steps:
#
#     1. Install a Java JDK
#     2. Download the MongoDB tar.gz archive
#     3. Unpackage the Mongo archive
#     4. Move the mongo directory to /usr/bin/
#     5. Update the ~/.bashrc file with mongodb specific exports
#     8. Start mongoperf test
#######################################################################

#
# Constants/Globals
#
ICA_TESTRUNNING="TestRunning"      # The test is running
ICA_TESTCOMPLETED="TestCompleted"  # The test completed successfully
ICA_TESTABORTED="TestAborted"      # Error during the setup of the test
ICA_TESTFAILED="TestFailed"        # Error occurred during the test

CONSTANTS_FILE="/root/constants.sh"
SUMMARY_LOG=~/summary.log

MONGODB_VERSION="2.4.0"
MONGODB_ARCHIVE="mongodb-${MONGODB_VERSION}.tar.gz"
MONGODB_URL="http://apache.cs.utah.edu/mongodb/common/mongodb-${MONGODB_VERSION}/${MONGODB_ARCHIVE}"

MONGODB_URL="https://archive.apache.org/dist/mongodb/core/mongodb-${MONGODB_VERSION}/${MONGODB_ARCHIVE}"
CONFIG_SCRIPT="/root/perf_mongodbterasort.sh"

#######################################################################
#
# LogMsg()
#
#######################################################################
LogMsg()
{
    echo `date "+%b %d %Y %T"` : "${1}"    # Add the time stamp to the log message
    echo "${1}" >> ~/mongodb.log
}

#######################################################################
#
# UpdateTestState()
#
#######################################################################
UpdateTestState()
{
    echo "${1}" > ~/state.txt
}

#######################################################################
#
# UpdateSummary()
#
#######################################################################
UpdateSummary()
{
    echo "${1}" >> ~/summary.log
}

#######################################################################
#
# TimeToSeconds()
#
#######################################################################
TimeToSeconds()
{
    read -r h m s <<< $(echo $1 | tr ':' ' ')
    #echo $(((h*60*60)+(m*60)+s))
    echo `echo "${h}*60*60+${m}*60+${s}" | bc`
}

#######################################################################
#
# LinuxRelease()
#
#######################################################################
LinuxRelease()
{
    DISTRO=`grep -ihs "buntu\|Suse\|Fedora\|Debian\|CentOS\|Red Hat Enterprise Linux" /etc/{issue,*release,*version}`

    case $DISTRO in
        *buntu*)
            echo "UBUNTU";;
        Fedora*)
            echo "FEDORA";;
        CentOS*)
            echo "CENTOS";;
        *SUSE*)
            echo "SLES";;
        Red*Hat*)
            echo "RHEL";;
        Debian*)
            echo "DEBIAN";;
    esac
}

#######################################################################
#
# ConfigRhel()
#
#######################################################################
ConfigRhel()
{
    LogMsg "ConfigRhel"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        yum -y install java-1.7.0-openjdk
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install Java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi

    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    LogMsg "Create JAVA_HOME variable"

    javaConfig=`echo "" | update-alternatives --config java | grep "*"`
    tokens=( $javaConfig )
    javaPath=${tokens[2]}
    if [ ! -e $javaPath ]; then
        LogMsg "Error: Unable to find the Java install path"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp=`dirname $javaPath`
    JAVA_HOME=`dirname $temp`
    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    #
    # This is a hack so we can use the same mongodb config on all Linux
    # distros.  With RHEL, localhost fails.  By setting the hostname
    # to localhost, then the default config works in RHEL.
    # Need to revisit this to find a better solution.
    #
    #hostname localhost
}

#######################################################################
#
# ConfigSles()
#
#######################################################################
ConfigSles()
{
    LogMsg "ConfigSles"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        zypper --non-interactive install jre-1.7.0
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi

    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    javaConfig=`update-alternatives --config java`
    tempHome=`echo $javaConfig | cut -f 2 -d ':' | cut -f 2 -d ' '`

    if [ ! -e $tempHome ]; then
        LogMsg "Error: The Java directory '${tempHome}' does not exist"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp1=`dirname $tempHome`
    JAVA_HOME=`dirname $temp1`

    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    #
    # Depending on how the user configs the SLES system, we may or may not
    # need the following workaround to allow mongodb to use localhost
    #
    #hostname localhost
}

#######################################################################
#
# ConfigUbuntu()
#
#######################################################################
ConfigUbuntu()
{
    LogMsg "ConfigUbuntu"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        apt-get -y install default-jdk
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi

    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    javaConfig=`update-alternatives --config java`
    tempHome=`echo $javaConfig | cut -f 2 -d ':' | cut -f 2 -d ' '`
    if [ ! -e $tempHome ]; then
        LogMsg "Error: The Java directory '${tempHome}' does not exist"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp1=`dirname $tempHome`
    temp2=`dirname $temp1`
    JAVA_HOME=`dirname $temp2`
    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi
}

#######################################################################
#
# Main script body
#
#######################################################################

cd ~

UpdateTestState $ICA_TESTRUNNING
LogMsg "Updated test case state to running"

rm -f ~/summary.log
touch ~/summary.log

if [ -e ${CONSTANTS_FILE} ]; then
    source ${CONSTANTS_FILE}
else
    errMsg="Error: missing ${CONSTANTS_FILE} file"
    LogMsg "${errMsg}"
    echo "${errMsg}" >> ~/summary.log
    UpdateTestState $ICA_TESTABORTED
    exit 10
fi

if [ ! ${MONGODB_VERSION} ]; then
    MONGODB_VERSION=3.2.3
	errMsg="The MONGODB_VERSION test parameter is not defined. Setting as ${MONGODB_VERSION} "
    LogMsg "${errMsg}"
    echo "${errMsg}" >> ~/summary.log
fi

if [ ! ${nThreads} ]; then
    nThreads=16
	LogMsg "Info : nThreads not defined in constants.sh. Setting as ${nThreads}"

fi
if [ ! ${fileSizeMB} ]; then
    fileSizeMB=1000
	LogMsg "Info : fileSizeMB not defined in constants.sh. Setting as ${fileSizeMB}"
fi
if [ ! ${readTest} ]; then
    readTest=true
    LogMsg "Info : readTest not defined in constants.sh. Setting as ${readTest}"
fi
if [ ! ${writeTest} ]; then
    writeTest=true
    LogMsg "Info : writeTest not defined in constants.sh. Setting as ${writeTest}"
fi
if [ ! ${recSizeKB} ]; then
    recSizeKB=8
    LogMsg "Info : recSizeKB not defined in constants.sh. Setting as ${recSizeKB}"
fi
if [ ! ${testDurationSeconds} ]; then
    testDurationSeconds=60
    LogMsg "Info : testDurationSeconds not defined in constants.sh. Setting as ${testDurationSeconds}"
fi

LogMsg "Info : MONGODB_VERSION = ${MONGODB_VERSION}"
MONGODB_ARCHIVE="mongodb-linux-x86_64-${MONGODB_VERSION}.tgz"
MONGODB_URL="https://fastdl.mongodb.org/linux/${MONGODB_ARCHIVE}"
LogMsg "Info : nThreads = ${nThreads}"
LogMsg "Info : fileSizeMB = ${fileSizeMB}"
LogMsg "Info : readTest = ${readTest}"
LogMsg "Info : writeTest = ${writeTest}"
LogMsg "Info : recSizeKB = ${recSizeKB}"
LogMsg "Info : testDurationSeconds = ${testDurationSeconds}"

#
# Install Java
#
distro=`LinuxRelease`
case $distro in
    "CENTOS" | "RHEL")
        ConfigRhel
    ;;
    "UBUNTU")
        ConfigUbuntu
    ;;
    "DEBIAN")
        LogMsg "Debian is not supported"
        UpdateTestState "TestAborted"
        UpdateSummary "  Distro '${distro}' is not currently supported"
        exit 1
    ;;
    "SLES")
        ConfigSles
    ;;
     *)
        LogMsg "Distro '${distro}' not supported"
        UpdateTestState "TestAborted"
        UpdateSummary " Distro '${distro}' not supported"
        exit 1
    ;;
esac

#
# Download Hadoop
#
LogMsg "Downloading MangoDB if we do not have a local copy"

if [ ! -e "/root/${MONGODB_ARCHIVE}" ]; then
    LogMsg "Downloading Hadoop from ${MONGODB_URL}"
    wget "${MONGODB_URL}"
    if [ $? -ne 0 ]; then
        LogMsg "Error: Unable to download mongodb from ${MONGODB_URL}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    LogMsg "MangoDB successfully downloaded"
fi

#
# Untar and install Hadoop
#
LogMsg "Extracting the mongodb archive"

tar -zxvf ./${MONGODB_ARCHIVE}
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to extract mongodb from its archive"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

if [ ! -e "/root/mongodb-linux-x86_64-${MONGODB_VERSION}" ]; then
    LogMsg "Error: The expected mongodb directory '/root/mongodb-linux-x86_64-${MONGODB_VERSION}' was not created when extracting mongodb"
    UpdateTestState $sICA_TESTFAILED
    exit 1
fi

#
# Move the mongodb directory to where it should be
#
LogMsg "Move the mongodb directory to /usr/local/mongodb"

if [ -e /usr/local/mongodb ]; then
    rm -rf /usr/local/mongodb
fi

mv "/root/mongodb-linux-x86_64-${MONGODB_VERSION}" /usr/local/mongodb
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to move mongodb to the /usr/local/mongodb directory"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Check if MangoDB specific exports are in the .bashrc file"

grep -q "mangodb exports start" ~/.bashrc
if [ $? -ne 0 ]; then
    LogMsg "Hadoop exports not found in ~/.bashrc, adding them"
    echo "" >> ~/.bashrc
    echo "# mango exports start" >> ~/.bashrc
    echo "export JAVA_HOME=${JAVA_HOME}" >> ~/.bashrc
    echo "export MONGODB_INSTALL=/usr/local/mongodb" >> ~/.bashrc
    echo "export PATH=\$PATH:\$MONGODB_INSTALL/bin" >> ~/.bashrc
    echo "# MangoDB exports end" >> ~/.bashrc
fi

#
# Sourcing the update .bashrc
#
source ~/.bashrc
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to source .bashrc"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

#  {
#    nThreads:8,
#    fileSizeMB:128,
#    r:true,
#    w:false,
#    recSizeKB:8
#  }

if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to mongoperfConfigJSON"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

#LogMsg "run mongoperf."

#
# Update the configuration in core-site.xml
#
LogMsg "Updating mongoperfConfigJSON for Write test"

echo "{" > ./mongoperfConfigJSON
echo "    nThreads:${nThreads}," >> ./mongoperfConfigJSON
echo "    fileSizeMB:${fileSizeMB}," >> ./mongoperfConfigJSON
echo "    r:false," >> ./mongoperfConfigJSON
echo "    w:true," >> ./mongoperfConfigJSON
echo "    recSizeKB:${recSizeKB}" >> ./mongoperfConfigJSON
echo "}" >> ./mongoperfConfigJSON

LogMsg "Info : Run mongoperf for Write test..."

timeout ${testDurationSeconds} /usr/local/mongodb/bin/mongoperf < mongoperfConfigJSON
if [ $? -ne 124 ]; then
	msg="Error: Unable to run mongoperf test data"
	LogMsg "${msg}"
	echo "${msg}" >> ~/summary.log
	UpdateTestState $ICA_TESTFAILED
	exit 1
fi

#LogMsg "run mongoperf."

#
# Update the configuration in core-site.xml
#
LogMsg "Updating mongoperfConfigJSON for read test"

echo "{" > ./mongoperfConfigJSON
echo "    nThreads:${nThreads}," >> ./mongoperfConfigJSON
echo "    fileSizeMB:${fileSizeMB}," >> ./mongoperfConfigJSON
echo "    r:true," >> ./mongoperfConfigJSON
echo "    w:false," >> ./mongoperfConfigJSON
echo "    recSizeKB:${recSizeKB}" >> ./mongoperfConfigJSON
echo "}" >> ./mongoperfConfigJSON

LogMsg "Info : Run mongoperf for read test..."

timeout ${testDurationSeconds} /usr/local/mongodb/bin/mongoperf < mongoperfConfigJSON
if [ $? -ne 124 ]; then
        msg="Error: Unable to run mongoperf test data"
        LogMsg "${msg}"
        echo "${msg}" >> ~/summary.log
        UpdateTestState $ICA_TESTFAILED
        exit 1
fi

#LogMsg "run mongoperf."
#
# Update the configuration in core-site.xml
#
LogMsg "Updating mongoperfConfigJSON for ReadWrite test"

echo "{" > ./mongoperfConfigJSON
echo "    nThreads:${nThreads}," >> ./mongoperfConfigJSON
echo "    fileSizeMB:${fileSizeMB}," >> ./mongoperfConfigJSON
echo "    r:${readTest}," >> ./mongoperfConfigJSON
echo "    w:${writeTest}," >> ./mongoperfConfigJSON
echo "    recSizeKB:${recSizeKB}" >> ./mongoperfConfigJSON
echo "}" >> ./mongoperfConfigJSON

LogMsg "Info : Run mongoperf  for ReadWrite test..."

timeout ${testDurationSeconds} /usr/local/mongodb/bin/mongoperf < mongoperfConfigJSON
if [ $? -ne 124 ]; then
        msg="Error: Unable to run mongoperf test data"
        LogMsg "${msg}"
        echo "${msg}" >> ~/summary.log
        UpdateTestState $ICA_TESTFAILED
        exit 1
fi

#
# If we made it here, everything worked.
#
UpdateTestState $ICA_TESTCOMPLETED
exit 0


#!/bin/bash

#######################################################################
#
# Linux on Hyper-V and Azure Test Code, ver. 1.0.0
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
# Licensed under the Apache License, Version 2.0 (the ""License"");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
#######################################################################

#######################################################################
#
# performance_middleware_fio.sh
#
# Description:
#    Install fio so the fio benchmark can
#    be run.  This script needs to be run on single VM.
#
#    steps:
#
#     1. Install a fio
#     2. Start fio benchmark test on given disk
#
# Author: Sivakanth
# Email: v-sirebb@microsoft.com
#######################################################################

#
# Constants/Globals
#
ICA_TESTRUNNING="TestRunning"      # The test is running
ICA_TESTCOMPLETED="TestCompleted"  # The test completed successfully
ICA_TESTABORTED="TestAborted"      # Error during the setup of the test
ICA_TESTFAILED="TestFailed"        # Error occurred during the test

CONSTANTS_FILE="constants.sh"
SUMMARY_LOG=~/summary.log

#######################################################################
#
# LogMsg()
#
#######################################################################
LogMsg()
{
    echo `date "+%b %d %Y %T"` : "${1}"    # Add the time stamp to the log message
    echo "${1}" >> ~/Fio.log
}

#######################################################################
#
# UpdateTestState()
#
#######################################################################
UpdateTestState()
{
    echo "${1}" > ~/state.txt
}

#######################################################################
#
# UpdateSummary()
#
#######################################################################
UpdateSummary()
{
    echo "${1}" >> ~/summary.log
}

#######################################################################
#
# TimeToSeconds()
#
#######################################################################
TimeToSeconds()
{
    read -r h m s <<< $(echo $1 | tr ':' ' ')
    #echo $(((h*60*60)+(m*60)+s))
    echo `echo "${h}*60*60+${m}*60+${s}" | bc`
}

#######################################################################
#
# LinuxRelease()
#
#######################################################################
LinuxRelease()
{
    DISTRO=`grep -ihs "buntu\|Suse\|Fedora\|Debian\|CentOS\|Red Hat Enterprise Linux" /etc/{issue,*release,*version}`

    case $DISTRO in
        *buntu*)
            echo "UBUNTU";;
        Fedora*)
            echo "FEDORA";;
        CentOS*)
            echo "CENTOS";;
        *SUSE*)
            echo "SLES";;
        Red*Hat*)
            echo "RHEL";;
        Debian*)
            echo "DEBIAN";;
    esac
}

set -u
set -x

RunFIO()
{
	testfilesize=${testfilesize}
	FILEIO="--size=$testfilesize --direct=1 --ioengine=libaio --filename=fiodata --overwrite=1  "

	####################################
	#All run config set here
	#

	#Log Config

	mkdir -p  $HOMEDIR/FIOLog/jsonLog
	mkdir -p  $HOMEDIR/FIOLog/iostatLog
	mkdir -p  $HOMEDIR/FIOLog/sarLog
	mkdir -p  $HOMEDIR/FIOLog/vmstatLog
	mkdir -p  $HOMEDIR/FIOLog/blktraceLog

	#LOGDIR="${HOMEDIR}/FIOLog"
	JSONFILELOG="${LOGDIR}/jsonLog"
	IOSTATLOGDIR="${LOGDIR}/iostatLog"
	VMSTATLOGDIR="${LOGDIR}/vmstatLog"
	SAROGDIR="${LOGDIR}/sarLog"
	BLKTRACELOGDIR="${LOGDIR}/blktraceLog"
	LOGFILE="${LOGDIR}/fio-test.log.txt"

	#redirect blktrace files directory
	Resource_mount=$(mount -l | grep /sdb1 | awk '{print$3}')
	blk_base="${Resource_mount}/blk-$(date +"%m%d%Y-%H%M%S")"
	mkdir $blk_base
	#
	#
	#Test config
	#
	#

	#All possible values for file-test-mode are randread randwrite read write

	iteration=0
	numjobs=1
	testmodes=${testmodes}
	ioSize=${ioSizes}
	ioDepth=${ioDepths}
	ioruntime=${ioruntime}

	####################################
	echo "Test log created at: ${LOGFILE}"
	LogMsg "===================================== Starting Run $(date +"%x %r %Z") ================================"
	echo "===================================== Starting Run $(date +"%x %r %Z") ================================" >> $LOGFILE

	chmod 666 $LOGFILE
	LogMsg "Preparing Files: $FILEIO"
	echo "Preparing Files: $FILEIO" >> $LOGFILE
	# Remove any old files from prior runs (to be safe), then prepare a set of new files.
	rm fiodata
	LogMsg "--- Kernel Version Information ---"
	echo "--- Kernel Version Information ---" >> $LOGFILE
	uname -a >> $LOGFILE
	cat /proc/version >> $LOGFILE
	cat /etc/*-release >> $LOGFILE
	echo "--- PCI Bus Information ---" >> $LOGFILE
	lspci >> $LOGFILE
	echo "--- Drive Mounting Information ---" >> $LOGFILE
	mount >> $LOGFILE
	echo "--- Disk Usage Before Generating New Files ---" >> $LOGFILE
	df -h >> $LOGFILE
	fio --cpuclock-test >> $LOGFILE
	fio $FILEIO --readwrite=read --bs=1M --runtime=1 --iodepth=128 --numjobs=8 --name=prepare >> $LOGFILE
	echo "--- Disk Usage After Generating New Files ---" >> $LOGFILE
	df -h >> $LOGFILE
	echo "=== End Preparation  $(date +"%x %r %Z") ===" >> $LOGFILE

	####################################
	#Trigger run from here
	for testmode in $testmodes; do
		for ioSize in $ioSizes; do
			for ioDepth in $ioDepths; do
				if [ $ioDepth -ge 8 ]
				then
					numjobs=8
				else
					numjobs=$ioDepth
				fi
				iostatfilename="${IOSTATLOGDIR}/iostat-fio-${testmode}-${ioSize}K-${ioDepth}td.txt"
				nohup iostat -x $ioruntime -t -y > $iostatfilename &
				iostatPID=$!
				sarfilename="${SAROGDIR}/sar-fio-${testmode}-${ioSize}K-${ioDepth}td.txt"
				nohup sar -n DEV 1 $ioruntime 2>&1 > $sarfilename &
				sarPID=$!
				vmstatfilename="${VMSTATLOGDIR}/vmstat-fio-${testmode}-${ioSize}K-${ioDepth}td.txt"
				nohup vmstat 1 $ioruntime 2>&1 > $vmstatfilename &
				vmstatPID=$!
				echo "-- iteration ${iteration} ----------------------------- ${testmode} test, ${ioSize}K bs, ${ioDepth} threads, ${numjobs} jobs, $ioruntime seconds ------------------ $(date +"%x %r %Z") ---" >> $LOGFILE
				jsonfilename="${JSONFILELOG}/fio-result-${testmode}-${ioSize}K-${ioDepth}td.json"
				fio $FILEIO --readwrite=$testmode --bs=${ioSize}K --runtime=$ioruntime --iodepth=$ioDepth --numjobs=$numjobs --output-format=json --output=$jsonfilename --name="iteration"${iteration} >> $LOGFILE
				kill -9 $iostatPID $sarPID $vmstatPID
				iteration=$(( iteration+1 ))
			done
		done
	done
	####################################
	echo "===================================== Completed Run $(date +"%x %r %Z") ================================" >> $LOGFILE
	rm fiodata
	echo "Test logs are located at ${LOGDIR}"
}

ConfigCentOS7()
{
	fioCentOS7pkg="fio-2.1.10-1.el7.rf.x86_64.rpm"
	LogMsg "INFO: CentOS7: installing required packages"
	yum install -y wget sysstat mdadm blktrace
	if [ $? -ne 0 ]; then
		LogMsg "Error: Unable to install wget tar sysstat mdadm blktrace"
		UpdateTestState $ICA_TESTABORTED
		exit 1
	fi
	mount -t debugfs none /sys/kernel/debug

	installed=`which fio`
	if [ ! $installed ]; then
        LogMsg "INFO: Installing fio"

		fiolPkg=$(ls | grep ${fioCentOS7pkg})
		if [ -z "$fiolPkg" ]; then
			wget "http://pkgs.repoforge.org/fio/${fioCentOS7pkg}"
		fi
		yum install -y ${fioCentOS7pkg}
		if [ $? -ne 0 ]; then
			LogMsg "Error: Unable to install fio"
			UpdateTestState $ICA_TESTABORTED
			exit 1
		fi
    fi
}

ConfigCentOS6()
{
	fioCentOS6pkg="fio-2.1.10-1.el6.rf.x86_64.rpm"
	LogMsg "INFO: CentOS6: installing required packages"
	yum install -y wget sysstat mdadm blktrace
	if [ $? -ne 0 ]; then
		LogMsg "Error: Unable to install wget tar sysstat mdadm blktrace"
		UpdateTestState $ICA_TESTABORTED
		exit 1
	fi
	mount -t debugfs none /sys/kernel/debug

	installed=`which fio`
	if [ ! $installed ]; then
        LogMsg "INFO: Installing fio"

		fiolPkg=$(ls | grep ${fioCentOS6pkg})
		if [ -z "$fiolPkg" ]; then
			wget "http://pkgs.repoforge.org/fio/${fioCentOS6pkg}"
		fi
		yum install -y libibverbs.x86_64
		yum install -y ${fioCentOS6pkg}
		if [ $? -ne 0 ]; then
			LogMsg "Error: Unable to install fio"
			UpdateTestState $ICA_TESTABORTED
			exit 1
		fi
    fi
}

ConfigUbuntu()
{
	LogMsg "INFO: Ubuntu installing required packages"

	apt-get update -y
	if [ $? -ne 0 ]; then
		LogMsg "Error: Unable to apt-get update"
		UpdateTestState $ICA_TESTABORTED
		exit 1
	fi
	apt-get install -y wget tar sysstat blktrace
	if [ $? -ne 0 ]; then
		LogMsg "Error: Unable to install wget tar sysstat mdadm blktrace"
		UpdateTestState $ICA_TESTABORTED
		exit 1
	fi
	mount -t debugfs none /sys/kernel/debug

	installed=`which fio`
	if [ ! $installed ]; then
        LogMsg "INFO: Installing fio"

		apt-get install -y fio ;
		if [ $? -ne 0 ]; then
			LogMsg "Error: Unable to install fio"
			UpdateTestState $ICA_TESTABORTED
			exit 1
		fi
    fi
}

ConfigSLES()
{
	fioCentOS6pkg="fio-2.1.2-2.1.3.x86_64.rpm"
	LogMsg "INFO: SLES: installing required packages"
	zypper --non-interactive install wget sysstat mdadm blktrace
	if [ $? -ne 0 ]; then
		LogMsg "Error: Unable to install wget tar sysstat mdadm blktrace"
		UpdateTestState $ICA_TESTABORTED
		exit 1
	fi
	mount -t debugfs none /sys/kernel/debug

	installed=`which fio`
	if [ ! $installed ]; then
        LogMsg "INFO: Installing fio"

		fiolPkg=$(ls | grep ${fioCentOS6pkg})
		if [ -z "$fiolPkg" ]; then
			wget "ftp://195.220.108.108/linux/opensuse/distribution/13.1/repo/oss/suse/x86_64/${fioCentOS6pkg}"
		fi
		(echo i;) | zypper --non-interactive install ${fioCentOS6pkg}
		if [ $? -ne 0 ]; then
			LogMsg "Error: Unable to install fio"
			UpdateTestState $ICA_TESTABORTED
			exit 1
		fi
    fi
}

CreateRAID0()
{
	disks=$(ls -l /dev | grep sd[c-z]$ | awk '{print $10}')
	#disks=(`fdisk -l | grep 'Disk.*/dev/sd[a-z]' |awk  '{print $2}' | sed s/://| sort| grep -v "/dev/sd[ab]$" `)

	LogMsg "INFO: Check and remove RAID first"
	mdvol=$(cat /proc/mdstat | grep "active raid" | awk {'print $1'})
	if [ -n "$mdvol" ]; then
		echo "/dev/${mdvol} already exist...removing first"
		umount /dev/${mdvol}
		mdadm --stop /dev/${mdvol}
		mdadm --remove /dev/${mdvol}
		mdadm --zero-superblock /dev/sd[c-z][1-5]
	fi

	LogMsg "INFO: Creating Partition"
	count=0
	for disk in ${disks}
	do
		echo "formatting disk /dev/${disk}"
		(echo d; echo n; echo p; echo 1; echo; echo; echo t; echo fd; echo w;) | fdisk /dev/${disk}
		count=$(( $count + 1 ))
	done

	LogMsg "INFO: Creating RAID"
	mdadm --create ${mdVolume} --level 0 --raid-devices ${count} /dev/sd[c-z][1-5]
	time mkfs -y $1 -F ${mdVolume}
	mkdir ${mountDir}
	mount -o nobarrier ${mdVolume} ${mountDir}
	if [ $? -ne 0 ]; then
		LogMsg "Error: Unable to create raid"
		UpdateTestState $ICA_TESTABORTED
		exit 1
	fi

	#LogMsg "INFO: adding fstab entry"
	#echo "${mdVolume}	${mountDir}	ext4	defaults	1 1" >> /etc/fstab
}

CreateLVM()
{
	disks=$(ls -l /dev | grep sd[c-z]$ | awk '{print $10}')
	#disks=(`fdisk -l | grep 'Disk.*/dev/sd[a-z]' |awk  '{print $2}' | sed s/://| sort| grep -v "/dev/sd[ab]$" `)

	#LogMsg "INFO: Check and remove LVM first"
	vgExist=$(vgdisplay)
	if [ -n "$vgExist" ]; then
		umount ${mountDir}
		lvremove -A n -f /dev/${vggroup}/lv1
		vgremove ${vggroup} -f
	fi

	LogMsg "INFO: Creating Partition"
	count=0
	for disk in ${disks}
	do
		echo "formatting disk /dev/${disk}"
		(echo d; echo n; echo p; echo 1; echo; echo; echo t; echo fd; echo w;) | fdisk /dev/${disk}
		count=$(( $count + 1 ))
	done

	LogMsg "INFO: Creating LVM with all data disks"
	pvcreate /dev/sd[c-z][1-5]
	vgcreate ${vggroup} /dev/sd[c-z][1-5]
	lvcreate -l 100%FREE -i 12 -I 64 ${vggroup} -n lv1
	time mkfs -t $1 -F /dev/${vggroup}/lv1
	mkdir ${mountDir}
	mount -o nobarrier /dev/${vggroup}/lv1 ${mountDir}
	if [ $? -ne 0 ]; then
		LogMsg "Error: Unable to create LVM "
		UpdateTestState $ICA_TESTABORTED
		exit 1
	fi

	#LogMsg "INFO: adding fstab entry"
	#echo "${mdVolume}	${mountDir}	ext4	defaults	1 1" >> /etc/fstab
}

############################################################
#	Main body
############################################################

#cd ~

UpdateTestState $ICA_TESTRUNNING
LogMsg "Updated test case state to running"

rm -f ~/summary.log
touch ~/summary.log

if [ -e ${CONSTANTS_FILE} ]; then
    source ${CONSTANTS_FILE}
else
    errMsg="Error: missing ${CONSTANTS_FILE} file"
    LogMsg "${errMsg}"
    echo "${errMsg}" >> ~/summary.log
    UpdateTestState $ICA_TESTABORTED
    exit 10
fi

HOMEDIR=$HOME
mv $HOMEDIR/FIOLog/ $HOMEDIR/FIOLog-$(date +"%m%d%Y-%H%M%S")/
mkdir -p $HOMEDIR/FIOLog
LOGDIR="${HOMEDIR}/FIOLog"
touch ${LOGDIR}/fio-test.log.txt
#mdVolume="/dev/md1"
#vggroup="vg1"
mountDir="/sdc1mnt/fio"
testdisk=${testdisk}
echo "Disk for FIO benchmark test $testdisk" && (echo n; echo p; echo 1; echo; echo; echo t; echo 83; echo w;) | fdisk $testdisk && time mkfs.ext4 ${testdisk}1 && echo "${testdisk}1 disk format: Success" && mkdir -p $mountDir && mount -o nobarrier ${testdisk}1 $mountDir && echo "${testdisk}1 disk mount: Success on $mountDir"
if [ $? -ne 0 ]; then
    LogMsg "Error: Disk for FIO benchmark test $testdisk: FAILED"
    UpdateTestState $ICA_TESTABORTED
    exit 1
fi
LogMsg "Disk for FIO benchmark test $testdisk is mounted on $mountDir: Success"

cd ${HOMEDIR}

DISTRO=`grep -ihs "buntu\|Suse\|Fedora\|Debian\|CentOS\|Red Hat Enterprise Linux" /etc/{issue,*release,*version}`

case $DISTRO in
	Ubuntu*)
		echo "UBUNTU"
		ConfigUbuntu
		;;
	Fedora*)
		echo "FEDORA";;
	*release*7.*)
		echo "CENTOS 7.*"
		ConfigCentOS7
		;;
	*release*6.*)
		echo "CENTOS 6.*"
		ConfigCentOS6
		;;
	*SUSE*)
		echo "SLES"
		ConfigSLES
		;;
	Red*Hat*)
		echo "RHEL";;
	Debian*)
		echo "DEBIAN";;
esac
#Creating RAID before triggering test
#CreateRAID0 ext4
#CreateLVM ext4

#Run test from here
LogMsg "*********INFO: Starting test execution*********"
cd ${mountDir}
mkdir sampleDIR
RunFIO
if [ $? -ne 0 ]; then
	LogMsg "Error: FIO run: FAILED"
	UpdateTestState $ICA_TESTFAILED
	exit 1
fi
LogMsg "*********INFO: Script execution reach END. Completed !!!*********"
UpdateTestState $ICA_TESTCOMPLETED
exit 0


#!/bin/bash

#######################################################################
#
# Linux on Hyper-V and Azure Test Code, ver. 1.0.0
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
# Licensed under the Apache License, Version 2.0 (the ""License"");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
#######################################################################

#######################################################################
#
# performance_mongodb.sh
#
# Description:
#    Install and configure mongodb so the ycsb benchmark can
#    be run.  This script needs to be run on single VM.
#
#    mongo perf needs Java runtime is also installed.
#
#    Installing and configuring Hadoop consists of the following
#    steps:
#
#     1. Install a Java JDK
#     2. Download the MongoDB tar.gz archive
#     3. Unpackage the Mongo archive
#     4. Move the mongo directory to /usr/bin/
#     5. Update the ~/.bashrc file with mongodb specific exports
#     8. Start ycsb test
#######################################################################

#
# Constants/Globals
#
ICA_TESTRUNNING="TestRunning"      # The test is running
ICA_TESTCOMPLETED="TestCompleted"  # The test completed successfully
ICA_TESTABORTED="TestAborted"      # Error during the setup of the test
ICA_TESTFAILED="TestFailed"        # Error occurred during the test

CONSTANTS_FILE="/root/constants.sh"
SUMMARY_LOG=~/summary.log

MONGODB_VERSION="2.4.0"
MONGODB_ARCHIVE="mongodb-linux-x86_64-${MONGODB_VERSION}.tgz"
MONGODB_URL="http://fastdl.mongodb.org/linux/${MONGODB_ARCHIVE}"

#######################################################################
#
# LogMsg()
#
#######################################################################
LogMsg()
{
    echo `date "+%b %d %Y %T"` : "${1}"    # Add the time stamp to the log message
    echo "${1}" >> ~/mongodb.log
}

#######################################################################
#
# UpdateTestState()
#
#######################################################################
UpdateTestState()
{
    echo "${1}" > ~/state.txt
}

#######################################################################
#
# UpdateSummary()
#
#######################################################################
UpdateSummary()
{
    echo "${1}" >> ~/summary.log
}

#######################################################################
#
# TimeToSeconds()
#
#######################################################################
TimeToSeconds()
{
    read -r h m s <<< $(echo $1 | tr ':' ' ')
    #echo $(((h*60*60)+(m*60)+s))
    echo `echo "${h}*60*60+${m}*60+${s}" | bc`
}

#######################################################################
#
# LinuxRelease()
#
#######################################################################
LinuxRelease()
{
    DISTRO=`grep -ihs "buntu\|Suse\|Fedora\|Debian\|CentOS\|Red Hat Enterprise Linux" /etc/{issue,*release,*version}`

    case $DISTRO in
        *buntu*)
            echo "UBUNTU";;
        Fedora*)
            echo "FEDORA";;
        CentOS*)
            echo "CENTOS";;
        *SUSE*)
            echo "SLES";;
        *Red*Hat*)
            echo "RHEL";;
        Debian*)
            echo "DEBIAN";;
    esac
}

#######################################################################
#
# ConfigRhel()
#
#######################################################################
ConfigRhel()
{
    LogMsg "ConfigRhel"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        yum -y install java-1.7.0-openjdk
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install Java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi
	ssh root@${MD_SERVER} "yum -y install java-1.7.0-openjdk"
    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    LogMsg "Create JAVA_HOME variable"

    javaConfig=`echo "" | update-alternatives --config java | grep "*"`
    tokens=( $javaConfig )
    javaPath=${tokens[3]}
    javaPath2=${javaPath#"("}
    javaPath2=${javaPath2%")"}
    if [ ! -e $javaPath2 ]; then
        LogMsg "Error: Unable to find the Java install path"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp=`dirname $javaPath2`
    JAVA_HOME=`dirname $temp`
    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    #
    # This is a hack so we can use the same mongodb config on all Linux
    # distros.  With RHEL, localhost fails.  By setting the hostname
    # to localhost, then the default config works in RHEL.
    # Need to revisit this to find a better solution.
    #
    #hostname localhost
}

#######################################################################
#
# ConfigSles()
#
#######################################################################
ConfigSles()
{
    LogMsg "ConfigSles"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        zypper --non-interactive install jre-1.7.0
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi
	ssh root@${MD_SERVER} "zypper --non-interactive install jre-1.7.0"
    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    javaConfig=`update-alternatives --config java`
    tempHome=`echo $javaConfig | cut -f 2 -d ':' | cut -f 2 -d ' '`

    if [ ! -e $tempHome ]; then
        LogMsg "Error: The Java directory '${tempHome}' does not exist"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp1=`dirname $tempHome`
    JAVA_HOME=`dirname $temp1`

    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    #
    # Depending on how the user configs the SLES system, we may or may not
    # need the following workaround to allow mongodb to use localhost
    #
    #hostname localhost
}

#######################################################################
#
# ConfigUbuntu()
#
#######################################################################
ConfigUbuntu()
{
    LogMsg "ConfigUbuntu"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"
        apt-get update
        apt-get -y install default-jdk
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi
	ssh root@${MD_SERVER} "apt-get update"
	ssh root@${MD_SERVER} "apt-get -y install default-jdk sysstat"
	apt-get -y install default-jdk sysstat
    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    javaConfig=`update-alternatives --config java`
    tempHome=`echo $javaConfig | cut -f 2 -d ':' | cut -f 2 -d ' '`
    if [ ! -e $tempHome ]; then
        LogMsg "Error: The Java directory '${tempHome}' does not exist"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp1=`dirname $tempHome`
    temp2=`dirname $temp1`
    JAVA_HOME=`dirname $temp2`
    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi
}

#######################################################################
#
# Main script body
#
#######################################################################

cd ~

UpdateTestState $ICA_TESTRUNNING
LogMsg "Updated test case state to running"

rm -f ~/summary.log
touch ~/summary.log

if [ -e ${CONSTANTS_FILE} ]; then
    source ${CONSTANTS_FILE}
else
    errMsg="Error: missing ${CONSTANTS_FILE} file"
    LogMsg "${errMsg}"
    echo "${errMsg}" >> ~/summary.log
    UpdateTestState $ICA_TESTABORTED
    exit 10
fi

if [ ! ${MONGODB_VERSION} ]; then
	errMsg="The MONGODB_VERSION test parameter is not defined. Setting as ${MONGODB_VERSION} "
    LogMsg "${errMsg}"
    echo "${errMsg}" >> ~/summary.log
fi

if [ ! ${MD_SERVER} ]; then
    nThreads=16
	#nThreads=8
	LogMsg "Info : nThreads not defined in constants.sh. Setting as ${nThreads}"
fi

#
# Install Java
#
distro=`LinuxRelease`
case $distro in
    "CENTOS" | "RHEL")
        ConfigRhel
    ;;
    "UBUNTU")
        ConfigUbuntu
    ;;
    "DEBIAN")
        LogMsg "Debian is not supported"
        UpdateTestState "TestAborted"
        UpdateSummary "  Distro '${distro}' is not currently supported"
        exit 1
    ;;
    "SLES")
        ConfigSles
    ;;
     *)
        LogMsg "Distro '${distro}' not supported"
        UpdateTestState "TestAborted"
        UpdateSummary " Distro '${distro}' not supported"
        exit 1
    ;;
esac

#
# Download MongoDB to server and start mongodb server.
#
LogMsg "Downloading MangoDB if we do not have a local copy"

if [ ! -e "/root/${MONGODB_ARCHIVE}" ]; then
    LogMsg "Downloading Hadoop from ${MONGODB_URL}"
    ssh root@${MD_SERVER} "wget ${MONGODB_URL}"
    if [ $? -ne 0 ]; then
        LogMsg "Error: Unable to download mongodb from ${MONGODB_URL}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi
    LogMsg "MangoDB successfully downloaded"
fi

#
# Untar and install Hadoop
#
LogMsg "Extracting the mongodb archive"

ssh root@${MD_SERVER} "tar xfvz mongodb-linux-x86_64-${MONGODB_VERSION}.tgz"
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to extract mongodb from its archive"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Download YCSB on client VM"
curl -O --location https://github.com/brianfrankcooper/YCSB/releases/download/0.5.0/ycsb-0.5.0.tar.gz
if [ $? -ne 0 ]; then
	LogMsg "Error: Unable to download YCSB"
	UpdateTestState $ICA_TESTFAILED
	exit 1
fi
LogMsg "Extract YCSB on client VM"
tar xfvz ycsb-0.5.0.tar.gz
if [ $? -ne 0 ]; then
	LogMsg "Error: Unable to download YCSB"
	UpdateTestState $ICA_TESTFAILED
	exit 1
fi

LogMsg "Check if MangoDB specific exports are in the .bashrc file"

grep -q "mangodb exports start" ~/.bashrc
if [ $? -ne 0 ]; then
    LogMsg "MongoDB exports not found in ~/.bashrc, adding them"
    echo "" >> ~/.bashrc
    echo "# mango exports start" >> ~/.bashrc
    echo "export JAVA_HOME=${JAVA_HOME}" >> ~/.bashrc
    echo "# MangoDB exports end" >> ~/.bashrc
fi

#
# Sourcing the update .bashrc
#
source ~/.bashrc
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to source .bashrc"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Starting MongoDB on ${MD_SERVER}"

#Preparing the mounted disk for mongodb test
diskName=`ssh root@server-vm fdisk -l | grep 'Disk.*/dev/sd[a-z]' |awk  '{print $2}' | sed s/://| sort| grep -v "/dev/sd[ab]$"`
if [ $? -ne 0 ]; then
    LogMsg "Error: Disk for mongodb benchmark test $diskName: FAILED"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

mountdir="/sdc1mnt/mongodb"
ssh root@server-vm "echo "Disk for mongodb benchmark test $diskName" && (echo n; echo p; echo 1; echo; echo; echo t; echo 83; echo w;) | fdisk $diskName && time mkfs.ext4 ${diskName}1 && echo "${diskName}1 disk format: Success" && mkdir -p $mountdir && mount -o nobarrier ${diskName}1 $mountdir && echo "${diskName}1 disk mount: Success on $mountdir"  "
if [ $? -ne 0 ]; then
    LogMsg "Error: Disk for mongodb benchmark test $diskName: FAILED"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi
LogMsg "Disk for mongodb benchmark test $diskName is mounted: Success"

ssh root@${MD_SERVER} "killall mongod"
ssh root@${MD_SERVER} "/root/mongodb-linux-x86_64-${MONGODB_VERSION}/bin/mongod --dbpath $mountdir --fork --logpath mongodServerConsole.txt"
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to start mongod server"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Using the asynchronous driver to load the test data"
echo "CMD: /root/ycsb-0.5.0/bin/ycsb load mongodb-async -s -P workloadAzure -p mongodb.url=mongodb://${MD_SERVER}:27017/ycsb?w=0"
/root/ycsb-0.5.0/bin/ycsb load mongodb-async -s -P workloadAzure -p mongodb.url=mongodb://${MD_SERVER}:27017/ycsb?w=0
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable load the test data"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi
LogMsg "Using the asynchronous driver to load the test data: Success"

LogMsg "Using the asynchronous driver to run the test on /root/run-ycsb.sh ${MD_SERVER}"
LogMsg "ycsb benchmark test run: Success"

chmod +x /root/run-ycsb.sh
/root/run-ycsb.sh

#
# If we made it here, everything worked.
#
UpdateTestState $ICA_TESTCOMPLETED
exit 0


#!/bin/bash

#######################################################################
#
# Linux on Hyper-V and Azure Test Code, ver. 1.0.0
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
# Licensed under the Apache License, Version 2.0 (the ""License"");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
#######################################################################

#######################################################################
#
# performance_mongodb.sh
#
# Description:
#    Install and configure mongodb so the mongoperf benchmark can
#    be run.  This script needs to be run on single VM.
#
#    mongo perf needs Java runtime is also installed.
#
#    Installing and configuring Hadoop consists of the following
#    steps:
#
#     1. Install a Java JDK
#     2. Download the MongoDB tar.gz archive
#     3. Unpackage the Mongo archive
#     4. Move the mongo directory to /usr/bin/
#     5. Update the ~/.bashrc file with mongodb specific exports
#     8. Start mongoperf test
#######################################################################

#
# Constants/Globals
#
ICA_TESTRUNNING="TestRunning"      # The test is running
ICA_TESTCOMPLETED="TestCompleted"  # The test completed successfully
ICA_TESTABORTED="TestAborted"      # Error during the setup of the test
ICA_TESTFAILED="TestFailed"        # Error occurred during the test

CONSTANTS_FILE="/root/constants.sh"
SUMMARY_LOG=~/summary.log

MONGODB_VERSION="2.4.0"
MONGODB_ARCHIVE="mongodb-linux-x86_64-${MONGODB_VERSION}.tgz"
MONGODB_URL="http://fastdl.mongodb.org/linux/${MONGODB_ARCHIVE}"

#######################################################################
#
# LogMsg()
#
#######################################################################
LogMsg()
{
    echo `date "+%b %d %Y %T"` : "${1}"    # Add the time stamp to the log message
    echo "${1}" >> ~/mongodb.log
}

#######################################################################
#
# UpdateTestState()
#
#######################################################################
UpdateTestState()
{
    echo "${1}" > ~/state.txt
}

#######################################################################
#
# UpdateSummary()
#
#######################################################################
UpdateSummary()
{
    echo "${1}" >> ~/summary.log
}

#######################################################################
#
# TimeToSeconds()
#
#######################################################################
TimeToSeconds()
{
    read -r h m s <<< $(echo $1 | tr ':' ' ')
    #echo $(((h*60*60)+(m*60)+s))
    echo `echo "${h}*60*60+${m}*60+${s}" | bc`
}

#######################################################################
#
# LinuxRelease()
#
#######################################################################
LinuxRelease()
{
    DISTRO=`grep -ihs "buntu\|Suse\|Fedora\|Debian\|CentOS\|Red Hat Enterprise Linux" /etc/{issue,*release,*version}`

    case $DISTRO in
        *buntu*)
            echo "UBUNTU";;
        Fedora*)
            echo "FEDORA";;
        CentOS*)
            echo "CENTOS";;
        *SUSE*)
            echo "SLES";;
        Red*Hat*)
            echo "RHEL";;
        Debian*)
            echo "DEBIAN";;
    esac
}

#######################################################################
#
# ConfigRhel()
#
#######################################################################
ConfigRhel()
{
    LogMsg "ConfigRhel"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        yum -y install java-1.7.0-openjdk
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install Java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi
	ssh root@${MD_SERVER} "yum -y install java-1.7.0-openjdk"
    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    LogMsg "Create JAVA_HOME variable"

    javaConfig=`echo "" | update-alternatives --config java | grep "*"`
    tokens=( $javaConfig )
    javaPath=${tokens[2]}
    if [ ! -e $javaPath ]; then
        LogMsg "Error: Unable to find the Java install path"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp=`dirname $javaPath`
    JAVA_HOME=`dirname $temp`
    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    #
    # This is a hack so we can use the same mongodb config on all Linux
    # distros.  With RHEL, localhost fails.  By setting the hostname
    # to localhost, then the default config works in RHEL.
    # Need to revisit this to find a better solution.
    #
    #hostname localhost
}

#######################################################################
#
# ConfigSles()
#
#######################################################################
ConfigSles()
{
    LogMsg "ConfigSles"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        zypper --non-interactive install jre-1.7.0
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi
	ssh root@${MD_SERVER} "zypper --non-interactive install jre-1.7.0"
    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    javaConfig=`update-alternatives --config java`
    tempHome=`echo $javaConfig | cut -f 2 -d ':' | cut -f 2 -d ' '`

    if [ ! -e $tempHome ]; then
        LogMsg "Error: The Java directory '${tempHome}' does not exist"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp1=`dirname $tempHome`
    JAVA_HOME=`dirname $temp1`

    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    #
    # Depending on how the user configs the SLES system, we may or may not
    # need the following workaround to allow mongodb to use localhost
    #
    #hostname localhost
}

#######################################################################
#
# ConfigUbuntu()
#
#######################################################################
ConfigUbuntu()
{
    LogMsg "ConfigUbuntu"

    #
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"
        apt-get update
        apt-get -y install default-jdk
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi
	ssh root@${MD_SERVER} "apt-get update"
	ssh root@${MD_SERVER} "apt-get -y install default-jdk"
    #
    # Figure out where Java is installed so we can configure a JAVA_HOME variable
    #
    javaConfig=`update-alternatives --config java`
    tempHome=`echo $javaConfig | cut -f 2 -d ':' | cut -f 2 -d ' '`
    if [ ! -e $tempHome ]; then
        LogMsg "Error: The Java directory '${tempHome}' does not exist"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi

    temp1=`dirname $tempHome`
    temp2=`dirname $temp1`
    JAVA_HOME=`dirname $temp2`
    if [ ! -e $JAVA_HOME ]; then
        LogMsg "Error: Invalid JAVA_HOME computed: ${JAVA_HOME}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi
}

#######################################################################
#
# Main script body
#
#######################################################################

cd ~

UpdateTestState $ICA_TESTRUNNING
LogMsg "Updated test case state to running"

rm -f ~/summary.log
touch ~/summary.log

if [ -e ${CONSTANTS_FILE} ]; then
    source ${CONSTANTS_FILE}
else
    errMsg="Error: missing ${CONSTANTS_FILE} file"
    LogMsg "${errMsg}"
    echo "${errMsg}" >> ~/summary.log
    UpdateTestState $ICA_TESTABORTED
    exit 10
fi

if [ ! ${MONGODB_VERSION} ]; then
	errMsg="The MONGODB_VERSION test parameter is not defined. Setting as ${MONGODB_VERSION} "
    LogMsg "${errMsg}"
    echo "${errMsg}" >> ~/summary.log
fi

if [ ! ${MD_SERVER} ]; then
    nThreads=16
	LogMsg "Info : nThreads not defined in constants.sh. Setting as ${nThreads}"
fi

#
# Install Java
#
distro=`LinuxRelease`
case $distro in
    "CENTOS" | "RHEL")
        ConfigRhel
    ;;
    "UBUNTU")
        ConfigUbuntu
    ;;
    "DEBIAN")
        LogMsg "Debian is not supported"
        UpdateTestState "TestAborted"
        UpdateSummary "  Distro '${distro}' is not currently supported"
        exit 1
    ;;
    "SLES")
        ConfigSles
    ;;
     *)
        LogMsg "Distro '${distro}' not supported"
        UpdateTestState "TestAborted"
        UpdateSummary " Distro '${distro}' not supported"
        exit 1
    ;;
esac

#
# Download MongoDB to server and start mongodb server.
#
LogMsg "Downloading MangoDB if we do not have a local copy"

if [ ! -e "/root/${MONGODB_ARCHIVE}" ]; then
    LogMsg "Downloading Hadoop from ${MONGODB_URL}"
    ssh root@${MD_SERVER} "wget ${MONGODB_URL}"
    if [ $? -ne 0 ]; then
        LogMsg "Error: Unable to download mongodb from ${MONGODB_URL}"
        UpdateTestState $ICA_TESTFAILED
        exit 1
    fi
    LogMsg "MangoDB successfully downloaded"
fi

#
# Untar and install Hadoop
#
LogMsg "Extracting the mongodb archive"

ssh root@${MD_SERVER} "tar xfvz mongodb-linux-x86_64-${MONGODB_VERSION}.tgz"
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to extract mongodb from its archive"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Download YCSB on client VM"
curl -O --location https://github.com/brianfrankcooper/YCSB/releases/download/0.5.0/ycsb-0.5.0.tar.gz
if [ $? -ne 0 ]; then
	LogMsg "Error: Unable to download YCSB"
	UpdateTestState $ICA_TESTFAILED
	exit 1
fi
LogMsg "Extract YCSB on client VM"
tar xfvz ycsb-0.5.0.tar.gz
if [ $? -ne 0 ]; then
	LogMsg "Error: Unable to download YCSB"
	UpdateTestState $ICA_TESTFAILED
	exit 1
fi

LogMsg "Check if MangoDB specific exports are in the .bashrc file"

grep -q "mangodb exports start" ~/.bashrc
if [ $? -ne 0 ]; then
    LogMsg "MongoDB exports not found in ~/.bashrc, adding them"
    echo "" >> ~/.bashrc
    echo "# mango exports start" >> ~/.bashrc
    echo "export JAVA_HOME=${JAVA_HOME}" >> ~/.bashrc
    echo "# MangoDB exports end" >> ~/.bashrc
fi

#
# Sourcing the update .bashrc
#
source ~/.bashrc
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to source .bashrc"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Starting MongoDB on ${MD_SERVER}"
ssh root@${MD_SERVER} "killall mongod"
ssh root@${MD_SERVER} "mkdir -p /tmp/mongodb"
#ssh root@${MD_SERVER} "nohup /root/mongodb-linux-x86_64-${MONGODB_VERSION}/bin/mongod --dbpath /tmp/mongodb > mongodServerConsole.txt &"
ssh root@${MD_SERVER} "/root/mongodb-linux-x86_64-${MONGODB_VERSION}/bin/mongod --dbpath /tmp/mongodb --fork --logpath mongodServerConsole.txt"
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to start mongod server"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Using the asynchronous driver to load the test data"
/root/ycsb-0.5.0/bin/ycsb load mongodb-async -s -P workloadAzure -p mongodb.url=mongodb://${MD_SERVER}:27017/ycsb?w=0
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable load the test data"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

LogMsg "Using the asynchronous driver to load the test data"
/root/ycsb-0.5.0/bin/ycsb run mongodb-async -s -P workloadAzure -p mongodb.url=mongodb://${MD_SERVER}:27017/ycsb?w=0
if [ $? -ne 0 ]; then
    LogMsg "Error: Unable to run test"
    UpdateTestState $ICA_TESTFAILED
    exit 1
fi

#
# If we made it here, everything worked.
#
UpdateTestState $ICA_TESTCOMPLETED
exit 0


#!/bin/bash

########################################################################
#
# Linux on Hyper-V and Azure Test Code, ver. 1.0.0
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
# Licensed under the Apache License, Version 2.0 (the ""License"");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR
# PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
#
# See the Apache Version 2.0 License for specific language governing
# permissions and limitations under the License.
#
########################################################################

#######################################################################
#
# performance_zk.sh
#
# Description:
#     This tool uses the ZooKeeper (zk) python binding to test various operation latencies
#
#
# In general the script does the following:
#
# 1.create a root znode for the test, i.e. /zk-latencies
# 2.attach a zk session to each server in the ensemble (the --servers list)
# 3.run various (create/get/set/delete) operations against each server, note the latencies of operations
# 4.client then cleans up, removing /zk-latencies znode
#
#
# Parameters:
#	ZK_VERSION			zookeeper-server version
#      	ZK_SERVERS:               	comma separated list of host:port (default localhost:2181)
#	ZK_TIMEOUT;			session timeout in milliseconds (default 5000)
#	ZK_ZNODE_SIZE;			data size when creating/setting znodes (default 25)
#	ZK_ZNODE_COUNT;			the number of znodes to operate on in each performance section (default 10000)
#	ZK_FORCE; (optional)		force the test to run, even if root_znode exists -WARNING! don't run this on a real znode or you'll lose it !!!
#	ZK_SYNCHRONOUS;			by default asynchronous ZK api is used, this forces synchronous calls
#	ZK_VERBOSE;			verbose output, include more detail
#
#######################################################################

ICA_TESTRUNNING="TestRunning"
ICA_TESTCOMPLETED="TestCompleted"
ICA_TESTABORTED="TestAborted"
ICA_TESTFAILED="TestFailed"

#
# Function definitions
#

LogMsg()
{
    echo `date "+%a %b %d %T %Y"` ": ${1}"
}

UpdateTestState()
{
    echo $1 > ~/state.txt
}

#######################################################################
#
# LinuxRelease()
#
#######################################################################
LinuxRelease()
{
    DISTRO=`grep -ihs "buntu\|Suse\|Fedora\|Debian\|CentOS\|Red Hat Enterprise Linux" /etc/{issue,*release,*version}`

    case $DISTRO in
        *buntu*)
            echo "UBUNTU";;
        Fedora*)
            echo "FEDORA";;
        CentOS*)
            echo "CENTOS";;
        *SUSE*)
            echo "SLES";;
        Red*Hat*)
            echo "RHEL";;
        Debian*)
            echo "DEBIAN";;
    esac
}

######################################################################
#
# DoSlesAB()
#
# Description:
#    Perform distro specific Apache and tool installation steps for SLES
#    and then run the benchmark tool
#
#######################################################################
ConfigSlesZK()
{
    #
    # Note: A number of steps will use SSH to issue commands to the
    #       APACHE_SERVER.  This requires that the SSH keys be provisioned
    #       in advanced, and strict mode be disabled for both the SSH
    #       server and client.
    #

	LogMsg "Info: Running SLES"
	arr=$(echo $ZK_SERVERS | tr "," "\n")
	for ZK_SERVER in $arr
	do
	#echo "${ZK_SERVER}"
	SERVER=(`echo "${ZK_SERVER}" | awk -F':' '{print $1}'`)
	#echo "${SERVER}"
	#ssh root@${SERVER} "mkdir /root/kk"
	#exit 1
	LogMsg "Info: -----------------------------------------"
	LogMsg "Info: Zookeeper-Server installation on server ${SERVER}"
    	LogMsg "Info: Installing required packages first"
	ssh root@${SERVER} "zypper --non-interactive install wget"
	if [ $? -ne 0 ]; then
		msg="Error: Unable to install package to server ${SERVER}"
		LogMsg "${msg}"
		echo "${msg}" >> ./summary.log
		UpdateTestState $ICA_TESTFAILED
		exit 1
	fi

	#
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        ssh root@${SERVER} "zypper --non-interactive install jre-1.7.0"
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi
        LogMsg "Info: Download ZK package"
	pkg=(`ssh root@${SERVER} "ls /root/ | grep ${ZK_ARCHIVE}"`)
	echo $pkg
	if [ -z "$pkg" ]; then
		LogMsg "Downloading ZK package ${ZK_ARCHIVE}"
		#exit 1
        	ssh root@${SERVER} "wget ${ZK_URL}"
        	if [ $? -ne 0 ]; then
                	msg="Error: Unable to download ZK package to server ${SERVER}"
                	LogMsg "${msg}"
                	echo "${msg}" >> ./summary.log
                	UpdateTestState $ICA_TESTFAILED
                	exit 1
		fi
        fi

	LogMsg "Info: Untar and Config ZK server"
        ssh root@${SERVER} "tar -xzf ./${ZK_ARCHIVE}"
        ssh root@${SERVER} "cp zookeeper-${ZK_VERSION}/conf/zoo_sample.cfg zookeeper-${ZK_VERSION}/conf/zoo.cfg"

	LogMsg "Info: Starting Zookeeper-Server ${SERVER}"
        ssh root@${SERVER} "zookeeper-${ZK_VERSION}/bin/zkServer.sh start"
	if [ $? -ne 0 ]; then
                msg="Error: Unable to start Zookeeper-Server on ${SERVER}"
                LogMsg "${msg}"
                echo "${msg}" >> ./summary.log
                UpdateTestState $ICA_TESTFAILED
                exit 1
        fi

	LogMsg "Info: Server started successfully"

	done
}

ConfigUbuntuZK()
{
    #
    # Note: A number of steps will use SSH to issue commands to the
    #       ZK_SERVER.  This requires that the SSH keys be provisioned
    #       in advanced, and strict mode be disabled for both the SSH
    #       server and client.
    #

	LogMsg "Info: Running Ubuntu"
	arr=$(echo $ZK_SERVERS | tr "," "\n")
	for ZK_SERVER in $arr
	do
	SERVER=(`echo "${ZK_SERVER}" | awk -F':' '{print $1}'`)
	LogMsg "Info: -----------------------------------------"
	LogMsg "Info: Zookeeper-Server installation on server ${SERVER}"
    	LogMsg "Info: Installing required packages first"
	ssh root@${SERVER} "apt-get install -y wget"
	if [ $? -ne 0 ]; then
		msg="Error: Unable to install package to server ${SERVER}"
		LogMsg "${msg}"
		echo "${msg}" >> ./summary.log
		UpdateTestState $ICA_TESTFAILED
		exit 1
	fi

	#
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        ssh root@${SERVER} "apt-get -y install default-jdk"
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi

        LogMsg "Info: Download ZK package"
	pkg=(`ssh root@${SERVER} "ls /root/ | grep ${ZK_ARCHIVE}"`)
	echo $pkg
	if [ -z "$pkg" ]; then
		LogMsg "Downloading ZK package ${ZK_ARCHIVE}"
		#exit 1
        	ssh root@${SERVER} "wget ${ZK_URL}"
        	if [ $? -ne 0 ]; then
                	msg="Error: Unable to download ZK package to server ${SERVER}"
                	LogMsg "${msg}"
                	echo "${msg}" >> ./summary.log
                	UpdateTestState $ICA_TESTFAILED
                	exit 1
		fi
        fi

	LogMsg "Info: Untar and Config ZK server"
        ssh root@${SERVER} "tar -xzf ./${ZK_ARCHIVE}"
        ssh root@${SERVER} "cp zookeeper-${ZK_VERSION}/conf/zoo_sample.cfg zookeeper-${ZK_VERSION}/conf/zoo.cfg"

	LogMsg "Info: Starting Zookeeper-Server ${SERVER}"
        ssh root@${SERVER} "zookeeper-${ZK_VERSION}/bin/zkServer.sh start"
	if [ $? -ne 0 ]; then
                msg="Error: Unable to start Zookeeper-Server on ${SERVER}"
                LogMsg "${msg}"
                echo "${msg}" >> ./summary.log
                UpdateTestState $ICA_TESTFAILED
                exit 1
        fi

	LogMsg "Info: Server started successfully"

	done
}

ConfigRHELZK()
{
    #
    # Note: A number of steps will use SSH to issue commands to the
    #       ZK_SERVER.  This requires that the SSH keys be provisioned
    #       in advanced, and strict mode be disabled for both the SSH
    #       server and client.
    #

	LogMsg "Info: Running RHEL"
	arr=$(echo $ZK_SERVERS | tr "," "\n")
	for ZK_SERVER in $arr
	do
	SERVER=(`echo "${ZK_SERVER}" | awk -F':' '{print $1}'`)
	LogMsg "Info: -----------------------------------------"
	LogMsg "Info: Zookeeper-Server installation on server ${SERVER}"
    	LogMsg "Info: Installing required packages first"
	ssh root@${SERVER} "yum install -y wget"
	if [ $? -ne 0 ]; then
		msg="Error: Unable to install package to server ${SERVER}"
		LogMsg "${msg}"
		echo "${msg}" >> ./summary.log
		UpdateTestState $ICA_TESTFAILED
		exit 1
	fi

	#
    # Install Java
    #
    LogMsg "Check if Java is installed"

    javaInstalled=`which java`
    if [ ! $javaInstalled ]; then
        LogMsg "Installing Java"

        ssh root@${SERVER} "yum -y install java-1.7.0-openjdk"
        if [ $? -ne 0 ]; then
            LogMsg "Error: Unable to install Java"
            UpdateTestState $ICA_TESTFAILED
            exit 1
        fi
    fi

        LogMsg "Info: Download ZK package"
	pkg=(`ssh root@${SERVER} "ls /root/ | grep ${ZK_ARCHIVE}"`)
	echo $pkg
	if [ -z "$pkg" ]; then
		LogMsg "Downloading ZK package ${ZK_ARCHIVE}"
		#exit 1
        	ssh root@${SERVER} "wget ${ZK_URL}"
        	if [ $? -ne 0 ]; then
                	msg="Error: Unable to download ZK package to server ${SERVER}"
                	LogMsg "${msg}"
                	echo "${msg}" >> ./summary.log
                	UpdateTestState $ICA_TESTFAILED
                	exit 1
		fi
        fi

	LogMsg "Info: Untar and Config ZK server"
        ssh root@${SERVER} "tar -xzf ./${ZK_ARCHIVE}"
        ssh root@${SERVER} "cp zookeeper-${ZK_VERSION}/conf/zoo_sample.cfg zookeeper-${ZK_VERSION}/conf/zoo.cfg"

	LogMsg "Info: Starting Zookeeper-Server ${SERVER}"
        ssh root@${SERVER} "zookeeper-${ZK_VERSION}/bin/zkServer.sh start"
	if [ $? -ne 0 ]; then
                msg="Error: Unable to start Zookeeper-Server on ${SERVER}"
                LogMsg "${msg}"
                echo "${msg}" >> ./summary.log
                UpdateTestState $ICA_TESTFAILED
                exit 1
        fi

	LogMsg "Info: Server started successfully"

	done
}

#######################################################################
#
# Main script body
#
#######################################################################

cd ~
UpdateTestState $ICA_TESTRUNNING
LogMsg "Starting test"

#
# Delete any old summary.log file
#
LogMsg "Cleaning up old summary.log"
if [ -e ~/summary.log ]; then
    rm -f ~/summary.log
fi

touch ~/summary.log

#
# Source the constants.sh file
#
LogMsg "Sourcing constants.sh"
if [ -e ~/constants.sh ]; then
    . ~/constants.sh
else
    msg="Error: ~/constants.sh does not exist"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    UpdateTestState $ICA_TESTABORTED
    exit 10
fi

#
# Make sure the required test parameters are defined
#
if [ "${ZK_VERSION:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the ZK_VERSION test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    UpdateTestState $ICA_TESTFAILED
    exit 20
fi

if [ "${ZK_SERVERS:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the ZK_SERVER test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    UpdateTestState $ICA_TESTFAILED
    exit 20
fi

if [ "${ZK_TIMEOUT:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the ZK_TIMEOUT test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    ZK_TIMEOUT=100000
fi

if [ "${ZK_ZNODE_SIZE:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the ZK_ZNODE_SIZE test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    ZK_ZNODE_SIZE=100
fi

if [ "${ZK_ZNODE_COUNT:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the ZK_ZNODE_COUNT test parameter is missing"
    LogMsg "${msg}"
    echo "${msg}" >> ~/summary.log
    ZK_ZNODE_SIZE=100
fi

if [ "${ZK_FORCE:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the ZK_FORCE test parameter is missing"
    LogMsg "${msg}"
    ZK_FORCE=$false
fi

if [ "${ZK_VERBOSE:="UNDEFINED"}" = "UNDEFINED" ]; then
    msg="Error: the ZK_VERBOSE test parameter is missing"
    LogMsg "${msg}"
    ZK_VERBOSE=$false
fi

LogMsg "Info: Test run parameters"
echo "ZK_VERSION               = ${ZK_VERSION}"
echo "ZK_SERVERS               = ${ZK_SERVERS}"
echo "ZK_TIMEOUT    = ${ZK_TIMEOUT}"
echo "ZK_ZNODE_SIZE = ${ZK_ZNODE_SIZE}"
echo "ZK_ZNODE_COUNT = ${ZK_ZNODE_COUNT}"
echo "ZK_FORCE = ${ZK_FORCE}"
echo "ZK_SYNCHRONOUS = ${ZK_SYNCHRONOUS}"
echo "ZK_VERBOSE = ${ZK_VERBOSE}"

LogMsg "Info : ZK_VERSION = ${ZK_VERSION}"
ZK_ARCHIVE="zookeeper-${ZK_VERSION}.tar.gz"
ZK_URL=http://apache.spinellicreations.com/zookeeper/zookeeper-${ZK_VERSION}/${ZK_ARCHIVE}

#
# Configure ZK server - this has distro specific behaviour
#
distro=`LinuxRelease`
case $distro in
    "CENTOS" | "RHEL")
        ConfigRHELZK
    ;;
    "UBUNTU")
        ConfigUbuntuZK
    ;;
    "DEBIAN")
        ConfigDebianZK
    ;;
    "SLES")
        ConfigSlesZK
    ;;
     *)
        msg="Error: Distro '${distro}' not supported"
        LogMsg "${msg}"
        echo "${msg}" >> ~/summary.log
        UpdateTestState "TestAborted"
        exit 1
    ;;
esac

IFS=',' read -ra ADDR <<< "$ZK_SERVERS"
for zk_server in "${ADDR[@]}";
do
	LogMsg "Prepare ZK test cmd for ${zk_server}"
	testBaseString='PYTHONPATH="lib.linux-x86_64-2.6" LD_LIBRARY_PATH="lib.linux-x86_64-2.6" python ./zk-latencies.py '
	#testBaseString='./zk-latencies.py PYTHONPATH=lib.linux-x86_64-2.6 LD_LIBRARY_PATH=lib.linux-x86_64-2.6 '
	testString="${testBaseString}""--servers=${zk_server} --timeout=${ZK_TIMEOUT} --znode_count=${ZK_ZNODE_COUNT} --znode_size=${ZK_ZNODE_SIZE}"

	if [ "${ZK_FORCE}" = true ]; then
		testString="${testString}"" --force"
	fi

	if [ "${ZK_SYNCHRONOUS}" = true ]; then
			testString="${testString}"" --synchronous"
	fi

	if [ "${ZK_VERBOSE}" = true ]; then
		testString="${testString}"" --verbose"
	fi

	LogMsg "Running zookeeper tests with cmd: ${testString}"
	eval $testString
	if [ $? -ne 0 ]; then
		msg="Error: Unable to run test"
		LogMsg "${msg}"
		echo "${msg}" >> ./summary.log
		UpdateTestState $ICA_TESTFAILED
		exit 1
	fi
done

#
# If we made it here, everything worked.
# Indicate success
#
LogMsg "Test completed successfully"
UpdateTestState $ICA_TESTCOMPLETED

exit 0