2 from __future__
import print_function
3 import os, time, sys, glob, re, smtplib, socket
4 from email.MIMEText
import MIMEText
5 from traceback
import print_exc, format_exc
6 from datetime
import datetime
7 from subprocess
import Popen,PIPE
8 sys.stdout = os.fdopen(sys.stdout.fileno(),
'w', 0)
11 TFILEDONEDIR = sys.argv[2]
12 COLLECTDIR = sys.argv[3]
13 ORIGINALDONEDIR =sys.argv[4]
19 EMAILINTERVAL = 15 * 60
20 SENDMAIL =
"/usr/sbin/sendmail"
21 HOSTNAME = socket.gethostname().lower()
22 EXEDIR = os.path.dirname(__file__)
23 STOP_FILE =
"%s/.stop" % EXEDIR
30 procid =
"[%s/%d]" % (__file__.rsplit(
"/", 1)[-1], os.getpid())
31 print(datetime.now(), procid, msg % args)
34 fsStats=os.statvfs(path)
35 size=fsStats.f_bsize*fsStats.f_blocks
36 available=fsStats.f_bavail*fsStats.f_bsize
38 usedPer=
float(used)/size
39 return (size,available,used,usedPer)
43 size=os.stat(path).st_blksize
44 for directory,subdirs,files
in os.walk(path):
45 dStats=os.lstat(directory)
46 size+=(dStats[stat.ST_NLINK]-1)*dStats[stat.ST_SIZE]
48 fStats=os.lstat(
"%s/%s" % (directory,f))
49 fSize=fStats[stat.ST_SIZE]
54 def sendmail(body="Hello from producerFileCleanner",subject= "Hello!"):
55 scall = Popen(
"%s -t" % SENDMAIL, shell=
True, stdin=PIPE)
56 scall.stdin.write(
"To: %s\n" % EMAIL)
57 scall.stdin.write(
"Subject: producerFileCleaner problem on server %s\n" %
59 scall.stdin.write(
"\n")
60 scall.stdin.write(
"%s\n" % body)
64 logme(
"ERROR: Sendmail exit with status %s", rc)
69 if os.path.exists(STOP_FILE):
70 logme(
"INFO: Stop file found, quitting")
76 diskSize,userAvailable,diskUsed,diskPUsage=
getDiskUsage(TFILEDONEDIR)
80 diskSize,userAvailable,diskUsed,diskPUsage=
getDiskUsage(
"/home")
83 if diskPUsage < PRODUCER_DU_TOP:
87 quota=
int(diskSize*PRODUCER_DU_BOT/100)
88 delQuota=diskUsed-quota
89 if delQuota > doneSize:
91 if now - EMAILINTERVAL > lastEmailSent:
92 msg=
"ERROR: Something is filling up the disks, %s does not" \
93 " have enough files to get to the Bottom Boundary of" \
94 " %.2f%%" % (TFILEDONEDIR,PRODUCER_DU_BOT)
98 logme(
"ERROR: Something is filling up the disks, %s does not" \
99 " have enough files to get to the Bottom Boundary of" \
100 " %.2f%%", TFILEDONEDIR, PRODUCER_DU_BOT)
104 for directory,subdirs,files
in os.walk(TFILEDONEDIR):
106 for f
in sorted(files,key=
lambda a: a[a.rfind(
"_R",1)+2:a.rfind(
"_R",1)+11]):
107 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)
109 subSystem=fMatch.group(2)
111 destDir=
"%s/%sxxxx/%sxx/DQM_V0001_%s_R%s.root" % (ORIGINALDONEDIR,run[0:5],run[0:7],subSystem,run)
112 fullFName=
"%s/%s" % (directory,f)
113 if os.stat(fullFName).st_size+aDelQuota > delQuota:
116 FILE_LIST.append(fullFName)
117 aDelQuota+=os.stat(fullFName).st_size
118 if not os.path.exists(destDir):
119 logme(
"WARNING: No subsystem file in repository %s for"
120 " file %s, deleting any way" %
121 (ORIGINALDONEDIR, fullFName))
124 logme(
"INFO: Found %d files to be deleted", len(FILE_LIST))
127 for directory,subdirs,files
in os.walk(COLLECTDIR):
130 logme(
"ERROR: Output directory %s, must not contain"
131 " subdirectories, cleanning", COLLECTDIR)
134 fullSdName=
"%s/%s" % (directory,sd)
135 for sdRoot,sdDirs,sdFiles
in os.walk(fullSdName,topdown=
False):
139 logme(
"INFO: File %s has been removed", f)
140 except Exception
as e:
141 logme(
"ERROR: Problem deleting file: [Errno %d] %s, '%s'",
142 e.errno, e.strerror, e.filename)
146 logme(
"INFO: File %s has been removed" , sdRoot)
147 except Exception
as e:
148 logme(
"ERROR: Problem deleting directory: [Errno %d] %s, '%s'",
149 e.errno, e.strerror, e.filename)
152 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):
155 if re.match(
r".*\.tmp",f):
158 fullFName=
"%s/%s" % (directory, f)
159 FILE_LIST.append(fullFName)
162 TMP_LIST=glob.glob(
"%s/*.tmp" % COLLECTDIR)
163 TMP_LIST.sort(reverse=
True,key=
lambda x: os.stat(x).st_mtime)
164 len(TMP_LIST) > 0
and TMP_LIST.pop(0)
165 FILE_LIST.extend(TMP_LIST)
172 logme(
"INFO: File %s has been removed", f)
173 except Exception
as e:
174 logme(
"ERROR: Problem deleting file: [Errno %d] %s, '%s'",
175 e.errno, e.strerror, e.filename)
176 if os.path.dirname(f)
not in DIR_LIST
and COLLECTDIR
not in os.path.dirname(f):
177 DIR_LIST.append(os.path.dirname(f))
183 logme(
"INFO: Directory %s has been removed", d)
184 except Exception
as e:
185 logme(
"ERROR: Directory delition failed: [Errno %d] %s, '%s'",
186 e.errno, e.strerror, e.filename)
188 except KeyboardInterrupt
as e:
191 except Exception
as e:
192 logme(
'ERROR: %s', e)
193 sendmail (
'ERROR: %s\n%s' % (e, format_exc()))
195 if now - EMAILINTERVAL > lastEmailSent:
196 sendmail (
'ERROR: %s\n%s' % (e, format_exc()))