45 lines
3.7 KiB
Python
45 lines
3.7 KiB
Python
import logging
|
|
|
|
def listServers(serverName):
|
|
# Input validation
|
|
if not type(serverName) is list:
|
|
raise TypeError("listServers: serverName must be a list, '" + str(serverName) + "' is not.")
|
|
# Check if the first item in the list is the header. If this is true (data started at right place), most likely the other data is fine as well.
|
|
if not serverName[0] == 'Bay Server Name Serial Number Status Power UID Partner\r':
|
|
logging.critical(serverName)
|
|
raise ValueError('listServers: Unexpected data recieved.')
|
|
|
|
serverNamePos = [] # Init list
|
|
|
|
# https://stackoverflow.com/a/19842807
|
|
haystack = serverName[1]
|
|
needle = ' '
|
|
for i, _ in enumerate(haystack):
|
|
if haystack[i:i + len(needle)] == needle:
|
|
serverNamePos.append(i) # Add results to list. The last positions of each field are indexed, the next one being the seperator.
|
|
|
|
serverNameFields = len(serverNamePos) # How many fields do we have to process
|
|
|
|
servers = [] # Init, the results of this.
|
|
for srv in range(2, len(serverName)): # Loop through servers. | Adjust for the first two being headers.
|
|
# Fields: [BayNumber "Server Name", "Serial Number", "Status", "Power", "UID Partner"]
|
|
serverNameCurrentTmp = [] # Init
|
|
|
|
for fld in range(serverNameFields):
|
|
if fld == 0: # Special case, as i-1 would loop around.
|
|
serverNameCurrentTmp.append( int( serverName[srv][0:serverNamePos[fld]].strip() ) ) # Also convert to int, as we know it's a number.
|
|
else:
|
|
serverNameCurrentTmp.append( serverName[srv][serverNamePos[fld-1]+1:serverNamePos[fld]].strip() ) # Get ith field by character position.
|
|
if not ( serverNameCurrentTmp[1] == '[Absent]' and serverNameCurrentTmp[4] == '' ): # If name is Absent and power status isn't available, there probably isn't a server there.
|
|
servers.append(serverNameCurrentTmp) # Only append if the bay is actually in use.
|
|
|
|
logging.debug('Servers:')
|
|
logging.debug(servers)
|
|
#breakpoint()
|
|
|
|
return(servers)
|
|
|
|
# Testing data:
|
|
#serverName = ['Bay Server Name Serial Number Status Power UID Partner\r', '--- ------------------------------------------------- --------------- -------- ------- --- -------\r', ' 1 foo-lab-1 OK On Off \r', ' 2 foo-lab-2 CZ302243P9 OK On Off \r', ' 3 foo-lab-3 CZJI441OKP Failed On Off \r', ' 4 kspve1 CZJ18450FK OK On Off \r', ' 5 kspve2-2 OK On Off \r', ' 6 kspve3 OK On Off \r', ' 7 foo-blade OK On Off \r', ' 8 Bar-01 CZ241274CC OK On Off \r', ' 9 baz-sar CZ3217FNYE OK On Off \r', ' 10 baz-sar2 CZ3217FFSS OK On Off \r', ' 11 [Absent] \r', ' 12 bee-bar OK On Off \r', ' 13 [Absent] \r', ' 14 [Absent] \r', ' 15 [Absent] \r', ' 16 [Absent] \r']
|
|
#print(listServers(serverName))
|