commit 3d82047fd928b1d24ba06c140e89a47035e58191 Author: TING-JUN WANG Date: Wed May 1 00:42:02 2024 +0800 feat: complete diff --git a/mailer.py b/mailer.py new file mode 100644 index 0000000..30f7d26 --- /dev/null +++ b/mailer.py @@ -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 + diff --git a/main.py b/main.py new file mode 100644 index 0000000..a435071 --- /dev/null +++ b/main.py @@ -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 = "
"
+    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)}({used_percent.rjust(4)}) 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 += "
" + mailer.send('unix_manager@cmlab.csie.ntu.edu.tw', 'unix_manager@cmlab.csie.ntu.edu.tw', 'CML Server /tmp Usage Weekly Report', msg) +