2011-04-18 11:32:19 +02:00
import MySQLdb
import time
import subprocess
import StringIO
import re
from wol import *
import os
import signal
from Util import *
def isMachineResponding ( machineName ) :
( returnCode , stdout , stderr ) = executeProgram ( [ ' ping ' , ' -o ' , ' -t ' , ' 1 ' , machineName ] )
#log( 'isMachineResponding : result of command %s : %d' % (command, returnCode) )
if returnCode == 0 :
return True
else :
bMachineNameIsNotKnown = ( returnCode == 68 )
bMachineIsNotResponding = ( returnCode == 2 )
if bMachineIsNotResponding == False :
bBUG_00000004_IS_STILL_ALIVE = True
if bBUG_00000004_IS_STILL_ALIVE == True and returnCode == 142 :
log ( ' isMachineResponding : bug00000004 Unexpected return code : returnCode= %d , stdout= " %s " , stderr= " %s " , machineName = %s ' % ( returnCode , stdout , stderr , machineName ) )
# don't stop the program until we understand bug00000004
elif bBUG_00000004_IS_STILL_ALIVE == True and returnCode == - 14 : # I had this error code on 07/09/2009 20:38 but I don't know yet what that means
log ( ' isMachineResponding : bug00000004 Unexpected return code : returnCode= %d , stdout= " %s " , stderr= " %s " , machineName = %s ' % ( returnCode , stdout , stderr , machineName ) )
# don't stop the program until we understand bug00000004
else :
log ( ' isMachineResponding : Unexpected return code : returnCode= %d , stdout= " %s " , stderr= " %s " , machineName = %s ' % ( returnCode , stdout , stderr , machineName ) )
assert ( False )
return False
def machineNameToMacAddress ( machineName ) :
2011-09-22 10:59:23 +02:00
conn = MySQLdb . connect ( ' simpatix10 ' , ' simpadb_reader ' , ' ' , ' simpadb ' )
2011-04-18 11:32:19 +02:00
assert ( conn )
sqlQuery = """ SELECT mac_address FROM ethernet_cards WHERE machine_name= ' """ + machineName + """ ' AND type= ' normal ' """
#print sqlQuery
conn . query ( sqlQuery )
r = conn . store_result ( )
row = r . fetch_row ( 0 )
assert ( len ( row ) == 1 )
#print 'row =', row
macAddress = row [ 0 ] [ 0 ]
#print macAddress
conn . close ( )
return macAddress
def getLightOutManagementIpAddress ( machineName ) :
"""
the light out management ip of servers allows to talk to the server even when it ' s asleep
"""
2011-09-22 10:59:23 +02:00
conn = MySQLdb . connect ( ' simpatix10 ' , ' simpadb_reader ' , ' ' , ' simpadb ' )
2011-04-18 11:32:19 +02:00
assert ( conn )
sqlQuery = """ SELECT ip_address_1,ip_address_2,ip_address_3,ip_address_4 FROM ethernet_cards WHERE machine_name= ' """ + machineName + """ ' AND type= ' light_out_management ' """
#print sqlQuery
conn . query ( sqlQuery )
r = conn . store_result ( )
row = r . fetch_row ( 0 )
assert ( len ( row ) == 1 )
#print 'row =', row
ipAddress = ( ' %s . %s . %s . %s ' ) % ( row [ 0 ] [ 0 ] , row [ 0 ] [ 1 ] , row [ 0 ] [ 2 ] , row [ 0 ] [ 3 ] )
#print macAddress
conn . close ( )
return ipAddress
def getClusterMachinesNames ( ) :
clusterMachinesNames = [ ]
2011-09-22 10:59:23 +02:00
conn = MySQLdb . connect ( ' simpatix10 ' , ' simpadb_reader ' , ' ' , ' simpadb ' )
2011-04-18 11:32:19 +02:00
assert ( conn )
sqlQuery = """ SELECT name FROM machines WHERE affectation= ' cluster ' """
#print sqlQuery
conn . query ( sqlQuery )
r = conn . store_result ( )
rows = r . fetch_row ( 0 )
for row in rows :
#print row
clusterMachinesNames . append ( row [ 0 ] )
conn . close ( )
return clusterMachinesNames
def machineSupportsIpmi ( machineName ) :
if ( machineName == ' simpatix ' ) or ( machineName == ' simpatix01 ' or ( machineName == ' simpa-mac2 ' ) ) :
# the command ipmitool sensor on simpatix doesn't work :
# Unabled to establish a session with the BMC.
# Command failed due to Unknown (0xFFFEF921) (0xFFFEF921)
return False
return True
def putToSleep ( machineName ) :
# note : pmset must be executed as root
( returnCode , stdout , stderr ) = executeCommand ( [ ' ssh ' , machineName , ' pmset sleepnow ' ] )
"""
print returnCode
print ' stdout : '
print stdout
print ' stderr : '
print stderr
"""
assert ( returnCode == 0 )
# check if the command succeeded by looking at the output (that's the only way I found)
f = StringIO . StringIO ( stdout )
line = f . readline ( )
f . close ( )
matchObj = re . match ( ' ^Sleeping now... ' , line )
if matchObj :
return True
else :
return False
def wakeUp ( machineName ) :
macAddress = machineNameToMacAddress ( machineName )
wake_on_lan ( macAddress )
return True
def isNonRespondingMachineSleeping ( machineName ) :
"""
note : crappy method to detect if the machine is sleeping ( if other methods are available , I would be very interested )
"""
wakeUp ( machineName )
time . sleep ( 120 )
if isMachineResponding ( machineName ) :
putToSleep ( machineName )
time . sleep ( 30 ) # allow a little time to make sure the machine is ready to receive other wake on lan messages
return True
else :
return False
if __name__ == ' __main__ ' :
"""
for i in range ( 30 ) :
machineName = ' simpatix %d ' % ( i + 10 )
print ' lom ip of %s is %s ' % ( machineName , getLightOutManagementIpAddress ( machineName ) )
"""
wakeUp ( ' simpatix21 ' )
#print putToSleep('simpatix13')
#print isNonRespondingMachineSleeping('simpatix13')