testing machine responsiveness
Tim Arnold
tiarno at sas.com
Fri Oct 6 09:10:07 EDT 2006
I have a bunch of processes that I farm out over several HPux machines on
the network. There are 60 machines to choose from and I want to
(1) find out which ones are alive (the 'ping' method below) and
(2) sort them by their current load (the 'get' method below, using the rup
command)
I'm no expert--I bet what I'm doing could be done better. I'd appreciate any
tips, caveats, etc.
Thanks in advance for looking at the code.
--Tim Arnold
Say the host names are in a global list tmpList...
#---- The final sorted list of cpus is called as:
cpuList = [x[1] for x in Machines().get()]
#----
class Machines(object):
' List of available, alive machines. '
def __init__(self):
global tmpList
self.asList = [y for y in tmpList if self.ping(y)]
self.asString = ' '.join(self.asList)
def ping(self, cpu):
''' Determine whether a machine is alive.
tcp connect to machine, port 7 (echo port).
Response within 3 seconds -> return true
'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
try:
s.connect((cpu,7))
except:
return 0
try:
s.send('test')
s.recv(128)
s.close()
return 1
except:
return 0
def get(self, maxLoad=3.0,):
''' return sorted list of available machines, sorted on load.
Optionally, specify maxload, default is no more than 3.0
'''
tmpDict = {}
retList = []
try:
rup = os.popen('rup %s | sort -n -t, -k4 | grep day' %
(self.asString))
except OSError:
return self.asList
for s in rup:
(name, t0) = s.split(' ',1)
(t1,t2,t3,avgLoad,t4) = s.split(',')
load = float(avgLoad)
if load < maxLoad:
tmpDict['%s.com' % name] = load
for (l, n) in [(v[1], v[0]) for v in tmpDict.items()]:
retList.append((l, n))
retList.sort()
return retList
More information about the Python-list
mailing list