Wednesday, July 16, 2008

First bash script

Once opened my mind thru the brave new linux, I decided to take especial look to scripting, that can do powerful things for us, and help linux environment administration, and optimization. Besides that, you can do great stuff for yourself, sky is the limit. Just this last week I bought a japanese book teaching step by step how to program a simple tetris with bash... crazy!!!

Here is my first script, which I had to use to retrieve statistics about HTTP GET (handy for stress testing against webservers and so):

#!/bin/bash
#
# Filename: httpStressTest.sh
#
# Description: Generate simple report for single-threaded http get response
#
# Author: BRAGA, Bruno
#
# Last Updated: 2008/06/29
#
# Remarks:
#
# The following parameters can be used:
#
# --url (Mandatory) define the URL to be tested
# --loop how many connections will be done to build the stats
# if not specified, it will use [10]
# --help displays the help information
#
# Eg. httpStressTest.sh --url http://www.google.com --loop 100
#
#
# Dependencies: gawk, sed, grep, time, wget
#


# Function: get_webpage_dowload_time
#
# Description: retrieve how long (in milliseconds) does the web
# takes to respond.
#
# Parameters: {url}
# The url to be contacted
#
# Remarks: Verify your internet connection before proceeding
#
# Dependencies: gawk, sed, grep, time, wget
#
function get_webpage_dowload_time()
{
     # define the input value
     local url=$1

     # create temp file
     local temp_file=`mktemp`

     # store the server time for accessing url
     (time wget -q $url --delete-after) 2> $temp_file

     # get server time value from temp file
     local elapsed_time=`cat $temp_file | grep real | \
sed "s/real//;s/[\t]//;s/m/./;s/s//" | \
gawk 'BEGIN {FS="."} {x=$1;y=$2;z=$3; res= x*60000 + y*1000 + z; print res}'`

     # remove temp file
     rm $temp_file

     # Done!
     echo $elapsed_time
}

function help()
{
     cat << EOF

Usage: httpStressTest.sh --url {URL} [OPTIONS]

Generate simple report for single-threaded http get response

--url (Mandatory) define the URL to be tested
--loop how many connections will be done to build the stats
if not specified, it will use [10]
--help displays the help information

Example: httpStressTest.sh --url http://www.google.com

Author: BRAGA, Bruno.

BUGS: Feel free to send a message to me at bruno.braga@gmail.com.

EOF
}

# Check the parameters
opts=$(getopt -l -o "help,url,loop")

while [ $# -gt 0 ]; do
     case $1 in
          --help)
               help
               exit 0
               ;;
          --url)
               url=$2
               shift 2
               ;;
          --loop)
               loop=$2
               shift 2
               ;;
          *)
               echo "Invalid option: $1" 1>&2
               exit 1
               ;;
     esac
done

# Exit in case no parameters were provided (url is mandatory)
if [ ! -n "$url" ]; then
     echo "Parameter [url] missing!"
     echo "Try '$progname --help' for more information" 1>&2
     exit 1
fi

# Define defaults
if [ ! -n "$loop" ]
then
     loop=10
fi

# Clear the terminal
clear

echo "Starting URL http get response time statistics test..."
echo
echo "Parameters: url=[$url], loop=[$loop]"
echo

# Loop n tries
for ((i = 1; i <= $loop; i++))
do
     elapsed_time=`get_webpage_dowload_time $url`
     temp="$temp $elapsed_time"
     echo "Test [$i] took [$elapsed_time] milliseconds."
done

# Building statistics...

# Set the array
array=(`echo $temp`)

avg_time=0
max_time=0
min_time=10000000

for value in ${array[*]}
do
     # get average
     avg_time=$(($avg_time + $value/$loop))

     # get min value
     if (( $value < $min_time ))
     then
          min_time=$value
     fi

     # get max value
     if (( $value > $max_time ))
     then
          max_time=$value
     fi
done

echo
echo "-----------"
echo "Total of tests: [$loop]"
echo "max_time=[$max_time], min_time=[$min_time], avg_time=[$avg_time]"
echo
echo "Done!"
exit 0


If you are interested on how I colored the code page (took me a while to find it), here it goes:
use the VIM (what? that simple? YES)... just one note to add is that the VIM rendered HTML is for black background, then you would need to change the color settings by yourself. What I did was to install the GVIM in my machine, and then just export the syntax to html. And that'S the result you see here! Isn't it great?

Sunday, July 6, 2008

Finally Linux addicted!

After more than 10 years of temptation, since good friends (Samuka Pecci) showed me the betas and the terminals (argh, at that moment, didn't attracted me at all), I have finally decided to enter the linux world. Yeah yeah, it took really long time, and work to push me thru it, but I am finally here...

I can say that the new distros are already very competitive with the Windows world, and easy to use/maintain. Still, if you want to reach a little bit lower level of its potential, there is a dark path and considerable learning curve to process it all.

Right now I am using the new version of Ubuntu 8 at home (we don't need anything more than a browser and video to handle all my needs, lol), and I am very satisfied with the graphical interface and usability. I can say that it changed a lot (since the first releases that I dropped out), especially related to the hardware recognition and software installation/update process.

So, here I will put some of my experiences (some codes too), that I think helped me out with to breath in this new world!!!