feat: complete

This commit is contained in:
TING-JUN WANG 2024-05-01 00:42:02 +08:00
commit 3d82047fd9
2 changed files with 69 additions and 0 deletions

27
mailer.py Normal file
View File

@ -0,0 +1,27 @@
import smtplib
from email.mime.text import MIMEText
class Mailer():
def __init__(self, google_owner, google_code):
self.google_owner = google_owner
self.google_code = google_code
def send(self, sender, receiver, subject, msg):
msg = MIMEText(msg, 'html')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
smtp = smtplib.SMTP('smtp.gmail.com', 587)
smtp.ehlo()
smtp.starttls()
smtp.login(self.google_owner, self.google_code)
status = smtp.send_message(msg)
smtp.quit()
if status == {}:
return True
else:
return False

42
main.py Normal file
View File

@ -0,0 +1,42 @@
import os
import subprocess
from mailer import Mailer
servers = [5, 7, 9, 10, 12, 18, 19, 23, 24, 25, 26, 27, 30]
GOOGLE_OWNER = "snsd0805@cmlab.csie.ntu.edu.tw"
GOOGLE_CODE = os.environ['GOOGLE_CODE']
if __name__ == '__main__':
mailer = Mailer(GOOGLE_OWNER, GOOGLE_CODE)
msg = "<pre>"
msg += "Every Wednesday 00:30\n" + '- ' * 30 + '\n'
for server in servers:
ssh_key = "/auto/extra/unixmanager5xx/id_rsa_cml"
command = f'ssh cml{server}.csie.ntu.edu.tw -4 -i {ssh_key} -o StrictHostKeyChecking=no -o LogLevel=QUIET -o ConnectTimeout=3 -T tmpstat'
print(command)
msg += f"cml{server}\n"
output = subprocess.getoutput(command)
if output:
partitions = output.split('\n')
for partition in partitions:
device, size, used, available, used_percent, mount = partition.split()
percent_num = int(used_percent[:-1])
if percent_num >= 90:
color = 'red'
elif percent_num >= 80:
color = 'orange'
else:
color = 'green'
msg += f' {mount} {used.rjust(6)}/{size.ljust(6)}(<span style="color: {color}">{used_percent.rjust(4)}</span>) has {available.rjust(5)} remaining\n'
print(f" {mount} {used.rjust(6)}/{size.ljust(6)}({used_percent.rjust(4)}) has {available.rjust(5)} remaining")
msg += '- ' * 30 + '\n'
else:
msg += ' Not Working\n' + '- ' * 30 + '\n'
print(f" Not Working")
msg += "</pre>"
mailer.send('unix_manager@cmlab.csie.ntu.edu.tw', 'unix_manager@cmlab.csie.ntu.edu.tw', 'CML Server /tmp Usage Weekly Report', msg)