Profiling Hosts

Mbm329 05:17, March 12, 2012 (UTC)

If you have various hosts that have differing traits, you may find that it's helpful to do one thing or a set of things on each of them. I've found that typing a lot of hosts on the commandline can waste a lot of time, and become excrutiatingly boring. So here's a script that will work on Linux and HPUX along with a data file to give an example of how it works.

Configuration
Here's an example configuration file you can give your hosts any particular trait you wish. I keep this configuration in ~/.hosts host1 hpux ia  test  web  sg   vgconf_notfull media_agent host2 hpux ia  prod  db   sg   vgconf_notfull no_media_agent host3 linux amd prod web  nosg vgconf_full    media_agent host4 hpux pa  prod  app  nosg vgconf_notfull media_agent host5 linux x86 prod web  nosg vgconf_notfull no_media_agent

Script
Here's the script that will read in the configuration file and produce a list of environment variables. Each containing a list of hosts that have that particular trait. I call this script ~/bin/loadvars
 * 1) !/bin/sh

if [ ! -f ~/.hosts ] ;then exit 1 fi
 * 1) check to see if required file exists
 * 2) ~/.hosts takes the format of: "hostname label label label ..."

uniq_label_list=$(for i in $(cut -d\ -f2- ~/.hosts) ;do echo ${i} ;done | sort -du) for label in ${uniq_label_list} ;do eval export ${label}='' done
 * 1) make all labels null to start to keep out dups

label_list='' while read host host_labels ;do host=":${host}" ##put a : at the front as eval will not like un-escaped spaces label_list="${label_list} ${host_labels}" ##create a complete list of label names for label in ${host_labels} ;do eval ${label}=$(echo \${${label}-:})${host} ##assign the host to a variable of the label name containing a list of hosts that have the same label done done<~/.hosts

for label in ${uniq_label_list} ;do eval ${label}=\'$(eval echo "\$${label}" | sed 's/:/\ /g ;s/^\ //g')\' ##replace the :'s w/ spaces for any future for-loops later eval export ${label} ##dont forget to export for any subshells done

unset host label_list uniq_label_list label
 * 1) cleanup variables used in this script

Loading it on login
Place this block of code in your ~/.profile so that the environment variables load on each login. Also, if you have a cron that is dependent on the profiled variables, you will need to place this block of code at the top of the script that is being run from cron. if [ -f ~/bin/loadvars ] ;then . ~/bin/loadvars fi

Example output
Here's some example output to show how it can be used. $ echo $hpux host1 host2 host4

$ for i in $hpux ;do echo "$i \c" ;ssh $i "sudo uptime" ;done host1  4:36pm  up 114 days,  5:17,  2 users,  load average: 0.01, 0.01, 0.01 host2  4:38pm  up 111 days, 13:01,  2 users,  load average: 0.24, 0.28, 0.22 host4  4:35pm  up 40 days, 23:46,  13 users,  load average: 0.17, 0.16, 0.18