Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2018 11:02
What would you like to do?
from os import system
import shutil
import telegram
def get_disk_free_space(path):
usage = shutil.disk_usage(path)
free =
percentage = 100*
return (free,percentage)
def map_size(size):
t = size[-1]
v = float(size[:-1])
if t=="K":
return v
elif t=="M":
return 1024*v
elif t=="G":
return 1024*1024*v
def get_folder_sizes(path):
if not path[-1] == "/":
path += "/"
system("du -sh {}* > tmp.txt".format(path))
with open("tmp.txt") as f:
return sorted([line.replace(path,"")[:-1].split("\t")[::-1] for line in f if not line[0] == 'd' and not "lost+found" in line],\
key = lambda x: map_size(x[1]), reverse=True)
token = ""
bot = telegram.Bot(token=token)
paths = ["/home","/cloud"]
threshold = 20
for path in paths:
p = get_disk_free_space(path)[1]
if p < threshold:
p = "{0:.2f}".format(p)
text = "<b>AVISO</b>: partição <b>{}</b> possui apenas <b>{}%</b> de espaço livre. Por favor, verifiquem suas respectivas pastas.\n".format(path,p)
sizes = ["{} - {}".format(s[0],s[1]) for s in get_folder_sizes(path)]
text += "\n".join(sizes)
bot.sendMessage(chat_id="", text=text, parse_mode=telegram.ParseMode.HTML)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment