Created
December 20, 2018 11:02
-
-
Save lucaspg96/19a2546e22c620453b4e463c30f9ef18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import system | |
import shutil | |
import telegram | |
def get_disk_free_space(path): | |
usage = shutil.disk_usage(path) | |
free = usage.free/1024/1024/1024 | |
percentage = 100*usage.free/usage.total | |
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