DCache rc cleaner

From SysadminWiki

By using the DCache python interface, you can create some usefull script for the day to day exploitation.

One example is the rc cleaner which will destroy or restart suspended RCs regarding some conditions.

Here is the full code. Download it (https://www.sysadmin.hep.ac.uk/svn/fabric-management/dcache/admintools/rc_cleaner.py) from SVN repository

#!/usr/local/bin/python

#####################################
#
#  COPYRIHGT : Jonathan Schaeffer, CCIN2P3
#
import commands
import adminDoor
import dCacheObjs
import sys
from re import match
from os import remove
from tempfile import mkstemp

pattern = '.*Suspended.*'

if len(sys.argv) == 2 :
       pattern = sys.argv[1]

print '\033[1;32m * \033[0;37mFinding rcs with pattern : '+pattern

adD=adminDoor.adminDoor()
output = adD.exec_cmd(['cd PoolManager\n','rc ls "'+pattern+'"'])[2:-4]

rc_ls=[]
for i in output:
       if match('^[0-9A-F]',i.strip()):
               r=dCacheObjs.rc()
               r.populate(i)
               if match('^.*\{104,Pool is disabled\}',i.strip()):
                       rc_ls.append(r)
               elif match('^.*\{1010,Suspend\}',i.strip()):
                       print('\033[1;32m >> \033[0;37mError 1010, retrying '+r.id)
                       adD.exec_cmd(['cd PoolManager','rc retry '+r.id])
               elif match('^.*\{0,\}$',i.strip()):
                       rc_ls.append(r)
               elif match('^.*\{149,No .*\}',i.strip()):
                       adD.exec_cmd(['cd PoolManager','rc retry '+r.id])
               elif match('^.*\{67,\}',i.strip()):
                       print('\033[1;32m >> \033[0;37mError 67, retrying '+r.id)
                       adD.exec_cmd(['cd PoolManager','rc retry '+r.id])

for r in rc_ls:
       # Check if pool is enable
       print r.PnfsId
       if adD.exec_cmd(['cd PoolManager','psu ls pool -l '+r.PoolCandidate])[2].rfind('enabled=true')>0:
               # Check if file exist
               if  adD.exec_cmd(['cd PnfsManager','pathfinder '+r.PnfsId])[3].rfind("FileNotFoundCacheException")>=0 :
                       print('\033[1;32m >> \033[0;37mDestroying rc '+r.id)
                       adD.exec_cmd(['cd PoolManager','rc destroy '+r.id])
               else :
                       print('\033[1;32m >> \033[0;37mRetying rc '+r.id)
                       adD.exec_cmd(['cd PoolManager','rc retry '+r.id])

The RCs are listed and tested. The conditions fit our needs at CCIN2P3, maybe you will hgave different use cases to add here. It is pretty simple to change the behaviour of this script.