# ---------------------------------------------------------------------- # Copyright (c) 2006, 2007 dCache.ORG (R) # (All rights reserved) # # This program is free software; you can redistribute it and/or # modify it under the terms of version 2 of the GNU General Public # License published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, contact Novell, Inc. # ---------------------------------------------------------------------- # from org.pcells.services.connection import * from dmg.protocols.ssh import * from dmg.cells.nucleus import NoRouteToCellException from java.lang import System from java.io import File from java.io import FileNotFoundException import string import sys import re import thread class OurDomainEventListener( DomainEventListener ) : def connectionOpened( self , connection ) : # # As soon as we are connected we query the PoolManager # for all currently active pools. # connection.sendObject( \ "PoolManager" , \ "psu ls -l pgroup" , \ ourMessageListener , \ 98 ) ; def connectionClosed( self , connection ) : print "Connection closed" def connectionOutOfBand( self , connection , subject ) : print "Connection out of band" class Pool : global poolGroupMapping def __init__( self , poolName ) : self.poolName = poolName self.paras = {} self.isOk = 0 self.isDefined = 0 def addProperties( self , poolString ) : self.poolString = poolString if isinstance( poolString , NoRouteToCellException ) : self.isOk = 0 else : self.isOk = 1 for i in string.split(poolString,"\n") : y = string.split(i) if ( len(y) > 3 ) : self.paras[y[0]] = y[3] elif ( len(y) > 2 ) : self.paras[y[0]] = y[2] def details( self ) : if self.isOk : if self.paras["LargeFileStore"] == "Precious" : rc = "No-HSM" else : rc = "HSM" regexp = re.compile( r"\[(?P.*)\]" ) used = regexp.search(self.paras["Used"]) usedSpace = string.atof(used.group("usedSpace")) #return "%-20s %-10s %20f %s"%(self.poolName,rc,usedSpace,poolGroupMapping.get(self.poolName)) return "%-20s %-10s %20f"%(self.poolName,rc,usedSpace) else : return "%-20s %10s"%(self.poolName,"Cell Not Found") def __cmp__( self , otherObj ) : if otherObj == None : return -1 if self.poolName < otherObj.poolName : return -1 elif self.poolName > otherObj.poolName : return 1 return 0 class PoolGroupContainer : def __init__( self , initString ) : self.poolGroups = {} state = 0 for line in string.split(initString,"\n") : if string.strip(line) == "" : continue splittedLine = string.split(line) if state == 0 : groupName = splittedLine[0] dir = [] dir.append( groupName ) dir.append( [] ) dir.append( [] ) self.poolGroups[groupName] = dir state = 1 elif state == 1 : state = 2 elif state == 2 : if splittedLine[0] == "poolList" : state = 3 else : self.poolGroups[groupName][1].append(splittedLine[0]) elif state == 3 : if len(splittedLine) == 1 : groupName = splittedLine[0] dir = [] dir.append( groupName ) dir.append( [] ) dir.append( [] ) self.poolGroups[groupName] = dir state = 1 else : self.poolGroups[groupName][2].append(splittedLine[0]) self.reshuffle() def reshuffle( self ) : self.pools = {} for x in self.poolGroups.values() : poolGroup = x[0] for pool in x[2] : if self.pools.get(pool) == None : self.pools[pool] = [] self.pools[pool].append(poolGroup) def get( self , poolName ) : return self.pools.get( poolName , "" ) def details(self) : print self.poolGroups print "--------------" print self.pools class OurDomainConnectionListener( DomainConnectionListener ) : # # this class only has one object in the application which # is used whenever data arrives from the channel. # global poolGroupMapping def __init__( self ) : self.expectedReplies = 0 self.repliesArrived = 0 self.listOfPools = {} self.mapping = {} self.poolGroups = None def domainAnswerArrived( self , obj , id ) : if id == 98 : poolGroupMapping = PoolGroupContainer( obj ) #self.poolGroups = PoolGroupContainer( obj ) connection.sendObject( \ "PoolManager" , \ "psu ls pool" , \ ourMessageListener , \ 99 ) ; elif id == 99 : # # this is the list of defined pools. # Subsequently ask for the active ones. # defPoolList = string.split( obj , "\n" ) ; for poolName in defPoolList : if string.strip(poolName) == "" : continue definedPool = Pool( poolName ) definedPool.isDefined = 1 self.listOfPools[poolName] = definedPool connection.sendObject( \ "PoolManager" , \ "xgetcellinfo" , \ ourMessageListener , \ 100 ) ; elif id == 100 : # # this is the list of actie pools. # as soon as we know how many pools we expect # we start the pool reply counter. # regexp = re.compile( r".*\[(?P.*)\].*" ) pools = regexp.search(obj.toString()) if pools == None : print "Syntax error in return : "+obj return poolList = string.split( pools.group("poolList") , "," ) ; poolList = poolList[:-1] self.expectedReplies=len(poolList) poolCounter = 0 for poolName in poolList : poolCounter = poolCounter + 1 self.mapping[poolCounter]=poolName connection.sendObject( poolName , "info" , ourMessageListener , 1000+poolCounter ) else : # # get the pool name from our 'id' to name mapping # self.repliesArrived = self.repliesArrived + 1 poolName = self.mapping[id-1000] # # get the pool object from theour listOfPools # internalPool = self.listOfPools[poolName] # # if not found, create a new one # if internalPool == None : internalPool = Pool( poolName ) # # add the properties # internalPool.addProperties( obj ) # # continue unless we got all replies # if self.expectedReplies == self.repliesArrived : sortedList = self.listOfPools.values() sortedList.sort() for pool in sortedList : print pool.details() System.exit(0) #---------------------------------------------------------------------- # # Main Routine # #---------------------------------------------------------------------- # if len(sys.argv) < 5 : print "Usage : ... " System.exit(4) # # Define the ListenerObject for all incoming message # ourMessageListener = OurDomainConnectionListener() poolGroupMapping = None # # Define Host and portnumber # connection=Ssh1DomainConnection(sys.argv[1],int(sys.argv[2])) connection.setLoginName(sys.argv[3]) # # Establish the secret : either password or identity file # secret=sys.argv[4] if secret[0] == "/" : connection.setPassword("") try : connection.setIdentityFile( File(secret) ) except FileNotFoundException, e : print "Authentication Failed ...",e.getMessage() System.exit(1) else : connection.setPassword(secret) connection.addDomainEventListener( OurDomainEventListener() ) # # and now go # try : connection.go() except SshAuthenticationException, e : print "Authentication Failed ...",e.getMessage() System.exit(1)