9 logging.basicConfig(level=logging.INFO)
12 re_files = re.compile(
r"^run(?P<run>\d+)/run(?P<runf>\d+)_ls(?P<ls>\d+)_.+\.(dat|raw)+(\.deleted)*")
14 m = re_files.match(rl)
19 sort_key = (int(d[
"run"]), int(d[
"runf"]), int(d[
"ls"]), )
26 for root, dirs, files
in os.walk(top, topdown=
True):
28 fp = os.path.join(root, name)
29 rl = os.path.relpath(fp, top)
33 fsize = os.stat(fp).st_size
38 collected.append((sort_key, fp, fsize, ))
41 collected.sort(key=
lambda x: x[0])
44 for sort_key, fp, fsize
in collected:
49 stopSize = stopSize - fsize
53 total = st.f_blocks * st.f_frsize
54 used = total - (st.f_bavail * st.f_frsize)
55 threshold = used - float(total * threshold) / 100
58 return float(x) * 100 / total
60 log.info(
"Using %d (%.02f%%) of %d space, %d (%.02f%%) above %s threshold.",
61 used,
p(used), total, threshold,
p(threshold), string)
65 log.info(
"Done cleaning up for %s threshold.", string)
67 log.info(
"Threshold %s not reached, doing nothing.", string)
71 total = st.f_blocks * st.f_frsize
72 used = total - (st.f_bavail * st.f_frsize)
73 return float(used) * 100 / total
76 def __init__(self, top, thresholds, email_to, fake=True, ):
87 if f.endswith(
".deleted"):
93 log.warning(
"Renaming file (fake): %s -> %s", f,
94 os.path.relpath(fn, os.path.dirname(f)))
96 log.warning(
"Renaming file: %s -> %s", f,
97 os.path.relpath(fn, os.path.dirname(f)))
102 if not f.endswith(
".deleted"):
106 log.warning(
"Truncating file (fake): %s", f)
108 log.warning(
"Truncating file: %s", f)
112 now = datetime.datetime.now()
123 subject =
"Disk out of space (%.02f%%) on %s." % (used_pc, self.
hostname)
124 if "mail2sms" in email:
129 log.info(
"Sending email: %s", repr([
"/bin/mail",
"-s", subject, email]))
130 p = subprocess.Popen([
"/bin/mail",
"-s", subject, email], stdin=subprocess.PIPE, shell=
False)
131 p.communicate(input=text)
145 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
147 sock.bind(
'\0' + pname)
155 time.sleep(delay_seconds)
158 if __name__ ==
"__main__":
169 log.info(
"Already running, exitting.")
177 top =
"/fff.changeme/ramdisk"
183 fake =
not (len(sys.argv) > 1
and sys.argv[1] ==
"doit")
187 thresholds = thresholds,
189 email_to = [
"dmitrijus.bugelskis@cern.ch", ],