Personal tools
You are here: Home SE Status Test PFNs Script
Document Actions

Test PFNs Script

by admin last modified 2006-04-05 12:23

The attached script tests all the PFNs in the PNFS. Requires a mounted PNFS partition and a working dccp. You must alter the first couple lines of the script for your site!

Click here to get the file

Size 2.9 kB - File type text/x-python

File contents

from threading import Timer
from popen2 import Popen3
from email.MIMEText import MIMEText
import signal, os, smtplib, popen2, time 


prefix='/pnfs/unl.edu/data4'

dccp_prefix = 'dcap://thpc-1.unl.edu'

email_recipient = 'bbockelm@math.unl.edu'
email_from = 'bbockelm@math.unl.edu'

max_files_per_email = 10
smtp_host = 'localhost'
timeout = 150.0

class PnfsTester:

  def __init__( self ) :
    self.files = []

  def get_info( self, filename ):
    dirname,file = os.path.split(filename)
    cmd = 'cat ' + dirname + "/'.(use)(2)(" + file + ")'";
    #print cmd
    r,w = popen2.popen4(cmd)

    info = r.readlines()
    if len(info) == 1:
      return "dir", None
    if len(info) == 2 or len(info) == 0:
      return "none", None
    if len(info) >= 3:
      info = [ i.strip() for i in info[2:] ]
      return info[0], info
    raise IndexError, str(info) + " " + filename

  def visitor( self, arg, dirname, names ):
    for i in names:
      self.files.append(dirname+'/' + i)

  def doWalk( self ):
    print "Starting tree walk."
    os.path.walk( prefix, self.visitor, None )
    print "Starting system transfer tests."
    badFiles = []
    counter = 0
    for i in self.files:
      info, other = self.get_info( i )
      if info == 'none':
        badFiles.append( i )
      elif info != 'dir':
        if not  self.testFile( i ):
          badFiles.append( i )
          print "Bad file: " + i
      if counter == max_files_per_email:
        counter = 0
        sendEmail( badFiles )
        badFiles = []
      counter += 1
    self.sendEmail( badFiles )
    return badFiles

  def sendEmail( self, badFiles ):
    if len( badFiles ) == 0:
      return
    msg_text = "The following files seem to be bad in PNFS:\n\n"
    for i in badFiles:
      msg_text += str(i) + '\n'
    msg = MIMEText(msg_text)
    msg['Subject'] = 'Found badfiles on PNFS' 
    msg['From'] = email_from
    msg['To'] = email_recipient

    s = smtplib.SMTP(smtp_host)
    s.sendmail(email_from, [email_recipient], msg.as_string())
    s.quit()

  def cancelTransfer( self ):
     #self.timer = Timer( 5, os.kill, [ self.pid, signal.SIGKILL ] )
     os.kill( self.pid, signal.SIGTERM )
     print "Killed stale transfer!"
     
  def testFile( self, i ):
    self.timer = Timer( timeout, self.cancelTransfer )
    self.timer.start()
    cmd = 'dccp ' + dccp_prefix + i + ' ' + '/dev/null'
    print "Testing file: " + cmd
    myProcess = Popen3( cmd )
    self.pid = myProcess.pid
    #status = os.waitpid(self.pid, os.WNOHANG)
    #try:
   #   while ( status == (0,0) ):
   #     time.sleep(.2)
   #     status = os.waitpid(self.pid, os.WNOHANG) 
   # except OSError: 
   #   pass
    #returnVal = status[1]
    returnVal = myProcess.wait( )
    self.timer.cancel()
    if (returnVal != 0):
      return False
    return True

if __name__ == '__main__':
  test = ['a','b','c']
  us = PnfsTester()
  #us.sendEmail( test )
  us.doWalk()


Powered by Plone, the Open Source Content Management System