Sunday, June 06, 2004

UNIX Shell Agents #2

Here is the second installment on my idea of UNIX Shell based Mobile-Agents. This time, I apply it to find the load-averages on a set of hosts on the network. You could do this on your own by rshing to each of those hosts, executing top command, appending the output to some file, and later processing that file to determine the most lightly loaded host on the network.

I do the same using UNIX Shell Agents. master_agent.sh reads in the list of hosts to be visited from HostList.txt, and spawns a worker_agent.sh for each of those hosts to move to that host and execute top. The output from each of the worker-agents is collected in a file, which is later analyzed by master-agent.

I'm a bit lazy to actually put down the assumptions and error checking in the agents. This sample is meant to be just a demo of the idea.

Your output might look something like this:

Load Averages on some.host5.com: 0.05, 0.05, 0.07 08:41:55
Load Averages on some.host1.com: 0.05, 0.06, 0.07 18:11:44
Load Averages on some.host3.com: 0.08, 0.29, 0.28 18:11:54
Load Averages on some.host4.com: 0.46, 0.36, 0.47 18:11:53
Load Averages on some.host6.com: 0.59, 0.58, 0.82 08:41:58
Load Averages on some.host2.com: 9.21, 9.38, 9.95 18:11:51

Here's the code:

-----------start master_agent.sh------------


#############################################################
#
# Unix Shell Agents
# by Siddharth Uppal
#
# Description: This shell script reads in the list of
# hosts to be visited from a file, and the command to be
# executed on each of them. It then creates a worker agent
# and sends it to each of those hosts to execute the
# specified command on them, and collects the information
# from each of the worker-agents into a file. The
# information, thus collected in the file,is later processed.
#
#############################################################


#
# Initialization
#
HostListFile="/u/uppals/PlayGround/HostList.txt"
HostList=`cat $HostListFile`
Command='top -n 0'
OutputFile="/tmp/KnowledgeBase.$$.txt"

# For each host in host list,
# send a worker-agent to it,
# and collect the information from all the agents into a file.
for i in $HostList
do
./worker_agent.sh $i $Command | sed "s/load averages/Load Averages on $i/g" >> $OutputFile
done

# Process the collected information
cat $OutputFile | grep "Load Averages" | sort +4

# Cleanup
rm $OutputFile

-----------end master_agent.sh--------------

-----------start worker_agent.sh------------


#! /bin/sh
#############################################################
#
# Unix Shell Agents
# by Siddharth Uppal
#
# Description: This shell script is a worker agent which
# moves to the specified host and executes the specified
# command at the host.
#
# Usage: worker_agent.sh
#
#
#############################################################

#
# Initialization
#
HostName=$1
Command=$2 # The command to execute at target host

if [ "$HostName" != "" ]
then # We're not on the target host
# Move to the host whose name was specified on the
# command-line and perform the requested operation.
rsh $HostName $AgentSource "" $Command
else # We're on the target host
$Command
fi

-----------end worker_agent.sh--------------

-----------start HostList.txt---------------
some.host1.com
some.host2.com
some.host3.com
some.host4.com
some.host5.com
some.host6.com
-----------end HostList.txt-----------------