2 import os, time, sys, glob, re, smtplib, socket
3 from email.MIMEText
import MIMEText
4 from traceback
import print_exc, format_exc
5 from datetime
import datetime
6 from subprocess
import Popen,PIPE
7 sys.stdout = os.fdopen(sys.stdout.fileno(),
'w', 0)
10 TFILEDONEDIR = sys.argv[2]
11 COLLECTDIR = sys.argv[3]
12 ORIGINALDONEDIR =sys.argv[4]
18 EMAILINTERVAL = 15 * 60
19 SENDMAIL =
"/usr/sbin/sendmail"
20 HOSTNAME = socket.gethostname().lower()
21 EXEDIR = os.path.dirname(__file__)
22 STOP_FILE =
"%s/.stop" % EXEDIR
29 procid =
"[%s/%d]" % (__file__.rsplit(
"/", 1)[-1], os.getpid())
30 print datetime.now(), procid, msg % args
33 fsStats=os.statvfs(path)
34 size=fsStats.f_bsize*fsStats.f_blocks
35 available=fsStats.f_bavail*fsStats.f_bsize
37 usedPer=float(used)/size
38 return (size,available,used,usedPer)
42 size=os.stat(path).st_blksize
43 for directory,subdirs,files
in os.walk(path):
44 dStats=os.lstat(directory)
45 size+=(dStats[stat.ST_NLINK]-1)*dStats[stat.ST_SIZE]
47 fStats=os.lstat(
"%s/%s" % (directory,f))
48 fSize=fStats[stat.ST_SIZE]
53 def sendmail(body="Hello from producerFileCleanner",subject= "Hello!"):
54 scall = Popen(
"%s -t" % SENDMAIL, shell=
True, stdin=PIPE)
55 scall.stdin.write(
"To: %s\n" % EMAIL)
56 scall.stdin.write(
"Subject: producerFileCleaner problem on server %s\n" %
58 scall.stdin.write(
"\n")
59 scall.stdin.write(
"%s\n" % body)
63 logme(
"ERROR: Sendmail exit with status %s", rc)
68 if os.path.exists(STOP_FILE):
69 logme(
"INFO: Stop file found, quitting")
75 diskSize,userAvailable,diskUsed,diskPUsage=
getDiskUsage(TFILEDONEDIR)
79 diskSize,userAvailable,diskUsed,diskPUsage=
getDiskUsage(
"/home")
82 if diskPUsage < PRODUCER_DU_TOP:
86 quota=long(diskSize*PRODUCER_DU_BOT/100)
87 delQuota=diskUsed-quota
88 if delQuota > doneSize:
90 if now - EMAILINTERVAL > lastEmailSent:
91 msg=
"ERROR: Something is filling up the disks, %s does not" \
92 " have enough files to get to the Bottom Boundary of" \
93 " %.2f%%" % (TFILEDONEDIR,PRODUCER_DU_BOT)
97 logme(
"ERROR: Something is filling up the disks, %s does not" \
98 " have enough files to get to the Bottom Boundary of" \
99 " %.2f%%", TFILEDONEDIR, PRODUCER_DU_BOT)
103 for directory,subdirs,files
in os.walk(TFILEDONEDIR):
105 for f
in sorted(files,key=
lambda a: a[a.rfind(
"_R",1)+2:a.rfind(
"_R",1)+11]):
106 fMatch=re.match(
r"(DQM|Playback|Playback_full)_V[0-9]{4}_([0-9a-zA-Z]+)_R([0-9]{9})(_T[0-9]{8}|)\.root",f)
108 subSystem=fMatch.group(2)
110 destDir=
"%s/%sxxxx/%sxx/DQM_V0001_%s_R%s.root" % (ORIGINALDONEDIR,run[0:5],run[0:7],subSystem,run)
111 fullFName=
"%s/%s" % (directory,f)
112 if os.stat(fullFName).st_size+aDelQuota > delQuota:
115 FILE_LIST.append(fullFName)
116 aDelQuota+=os.stat(fullFName).st_size
117 if not os.path.exists(destDir):
118 logme(
"WARNING: No subsystem file in repository %s for"
119 " file %s, deleting any way" %
120 (ORIGINALDONEDIR, fullFName))
123 logme(
"INFO: Found %d files to be deleted", len(FILE_LIST))
126 for directory,subdirs,files
in os.walk(COLLECTDIR):
129 logme(
"ERROR: Output directory %s, must not contain"
130 " subdirectories, cleanning", COLLECTDIR)
133 fullSdName=
"%s/%s" % (directory,sd)
134 for sdRoot,sdDirs,sdFiles
in os.walk(fullSdName,topdown=
False):
138 logme(
"INFO: File %s has been removed", f)
140 logme(
"ERROR: Problem deleting file: [Errno %d] %s, '%s'",
141 e.errno, e.strerror, e.filename)
145 logme(
"INFO: File %s has been removed" , sdRoot)
147 logme(
"ERROR: Problem deleting directory: [Errno %d] %s, '%s'",
148 e.errno, e.strerror, e.filename)
151 if re.match(
r"(DQM|Playback|Playback_full)_V[0-9]{4}_([a-zA-Z]+)_R([0-9]{9})_T[0-9]{8}\.root", f):
154 if re.match(
r".*\.tmp",f):
157 fullFName=
"%s/%s" % (directory, f)
158 FILE_LIST.append(fullFName)
161 TMP_LIST=glob.glob(
"%s/*.tmp" % COLLECTDIR)
162 TMP_LIST.sort(reverse=
True,key=
lambda x: os.stat(x).st_mtime)
163 len(TMP_LIST) > 0
and TMP_LIST.pop(0)
164 FILE_LIST.extend(TMP_LIST)
171 logme(
"INFO: File %s has been removed", f)
173 logme(
"ERROR: Problem deleting file: [Errno %d] %s, '%s'",
174 e.errno, e.strerror, e.filename)
175 if os.path.dirname(f)
not in DIR_LIST
and COLLECTDIR
not in os.path.dirname(f):
176 DIR_LIST.append(os.path.dirname(f))
182 logme(
"INFO: Directory %s has been removed", d)
184 logme(
"ERROR: Directory delition failed: [Errno %d] %s, '%s'",
185 e.errno, e.strerror, e.filename)
187 except KeyboardInterrupt, e:
191 logme(
'ERROR: %s', e)
192 sendmail (
'ERROR: %s\n%s' % (e, format_exc()))
194 if now - EMAILINTERVAL > lastEmailSent:
195 sendmail (
'ERROR: %s\n%s' % (e, format_exc()))