diff --git a/l2com/cep4-usage.py b/l2com/cep4-usage.py new file mode 100755 index 0000000000000000000000000000000000000000..c944dc14550a469c49894c09b80b2691d2d7868f --- /dev/null +++ b/l2com/cep4-usage.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python +# +# +import os, sys, re +import numpy as np +import matplotlib.pyplot as plt +import subprocess +import datetime + +targetdir = "/data/projects" +globdir = "/data" + +def human_size (size, f): + if (size/f < 1): return "%ldB" % (size) + if (size/f/f < 1): return "%.1fk" % (size/f) + if (size/f/f/f < 1): return "%.1fM" % (size/f/f) + if (size/f/f/f/f < 1): return "%.1fG" % (size/f/f/f) + if (size/f/f/f/f/f < 1): return "%.1fT" % (size/f/f/f/f) + +def duh(path): + """ + disk usage in human readable format (e.g. '2.1GB') + """ + return subprocess.check_output(['du','-sh', path]).split()[0].decode('utf-8') + +def duB(path): + """ + disk usage in bytes format + """ + return subprocess.check_output(['du','-sB 1', path]).split()[0].decode('utf-8') + +text = "" +p=os.popen('df -H %s' % (globdir)) +for line in p.readlines(): text += line + +projects = {} +files = [f for f in os.listdir(targetdir) if os.path.isdir(os.path.join(targetdir, f)) and f[0] != "."] + +text += "\n " + "-"*55 +text += "\n %-30s %10s\n" % ("Project", "Size") +text += " " + "-"*55 +text += "\n" +for f in files: + dirsize = duh(os.path.join(targetdir, f)) + dirsize_in_bytes = duB(os.path.join(targetdir, f)) + projects[f] = (dirsize, dirsize_in_bytes) + text += " %-30s %10s\n" % (f, dirsize) +text += " " + "-"*55 + +labels = np.array([str(ii) for ii in projects.keys()]) +sizes=[float(s) for (d, s) in projects.values()] +total=np.sum(sizes) +sizes_percent=np.array([s*100./total for s in sizes]) +text += "\n %-30s %10s" % ("Total", human_size(total, 1024)) + +now = datetime.datetime.utcnow() +updated = "Updated: %s UTC" % (now) + +pie_labels=[] +pie_sizes=[] +rest_size = 0. +for (l,s) in zip(labels, sizes_percent): + if s<1.0: rest_size += s + else: + pie_labels.append(l) + pie_sizes.append(s) + +pie_labels.append("others") +pie_sizes.append(rest_size) + +fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5)) +fig.suptitle("CEP4 /data volume usage") +ax1.annotate(text, xycoords="axes fraction", xy=(0.0, 0.99), bbox=dict(facecolor='0.95', boxstyle="round"), ha='left', va='top', multialignment="left", zorder=2, fontsize=10) +ax1.annotate(updated, xycoords="axes fraction", xy=(0.0, 0.0), ha='left', va='bottom', multialignment="left", zorder=2, fontsize=8) +ax1.axis('off') +ax2.pie(pie_sizes, labels=pie_labels, autopct='%1.1f%%') +#, pctdistance=1.25, labeldistance=.6) +plt.tight_layout() +plt.gcf().set_size_inches(10, 5) # Width=10in, Height=5in +plt.savefig("cep4-usage.png", bbox_inches='tight', dpi=100) # output figure size will be (10,5)x100 in pixels +plt.show()