From 01d436195197291c07bb074e1e46ab5c4d822179 Mon Sep 17 00:00:00 2001 From: Jean28518 Date: Sat, 27 Apr 2024 11:00:53 +0200 Subject: [PATCH] Add automatic mail messages if ram or cpu is over 80% for over 5 Minutes, also if Harddisk is over 90% #69 --- src/lac/unix/email.py | 2 ++ src/lac/unix/unix_scripts/service.py | 24 +++++++++++++ src/lac/unix/unix_scripts/unix.py | 42 ++++------------------ src/lac/unix/unix_scripts/utils.py | 52 +++++++++++++++++++++++++++- src/lac/unix/views.py | 2 +- 5 files changed, 84 insertions(+), 38 deletions(-) diff --git a/src/lac/unix/email.py b/src/lac/unix/email.py index 1b65a89..bdf2f1c 100644 --- a/src/lac/unix/email.py +++ b/src/lac/unix/email.py @@ -4,6 +4,8 @@ # If this one returns None the mail was sent successfully def send_mail(recipient, subject, message, attachment_path=""): + if not are_mail_settings_configured(): + return "Email settings are not configured" email = EmailMessage(subject=subject, body=message, from_email=settings.EMAIL_HOST_EMAIL, to=[recipient]) # Check also if attachment_path exists if attachment_path != "" and attachment_path is not None and os.path.exists(attachment_path): diff --git a/src/lac/unix/unix_scripts/service.py b/src/lac/unix/unix_scripts/service.py index 68d638a..b7099d7 100644 --- a/src/lac/unix/unix_scripts/service.py +++ b/src/lac/unix/unix_scripts/service.py @@ -34,12 +34,14 @@ def ensure_fingerprint_is_trusted(): counter = 60 hourly_counter = 3600 +five_minute_counter = 300 while True: ## Run every minute ############################################################################################ # If run_service file exists, remove it and run service immediately time.sleep(1) counter += 1 hourly_counter += 1 + five_minute_counter += 1 if os.path.isfile("run_service"): os.remove("run_service") counter = 60 @@ -133,6 +135,22 @@ def ensure_fingerprint_is_trusted(): else: os.system("shutdown now") + ################################################################################################################## + ## RUN EVERY 5 MINUTES ########################################################################################## + ################################################################################################################## + + if five_minute_counter < 300: + continue + five_minute_counter = 0 + + ## CHECK IF CPU, MEMORY IS TOO HIGH ############################################################################# + + if utils.get_cpu_usage(five_min=True) > 0.8: + os.system("curl -X POST -F 'subject=🖥️📈 CPU-Auslastung hoch📈' -F 'message=Die CPU-Auslastung des Servers ist zu hoch. Bitte überprüfen Sie den Server.' localhost:11123/unix/send_mail") + + if utils.get_ram_usage()["ram_percent"] > 0.8: + os.system("curl -X POST -F 'subject=💾📈 RAM-Auslastung hoch📈' -F 'message=Die RAM-Auslastung des Servers ist zu hoch. Bitte überprüfen Sie den Server.' localhost:11123/unix/send_mail") + ################################################################################################################## ## RUN EVERY HOUR ################################################################################################ ################################################################################################################## @@ -141,6 +159,12 @@ def ensure_fingerprint_is_trusted(): continue hourly_counter = 0 + ## CHECK IF DISK SPACE IS LOW #################################################################################### + disks = utils.get_disks_stats() + for disk in disks: + if int(disk["used_percent"]) > 90: + os.system(f"curl -X POST -F 'subject=💿📈 Festplattenauslastung hoch📈' -F 'message=Die Festplattenauslastung des Servers ist zu hoch. Bitte überprüfen Sie den Server.' localhost:11123/unix/send_mail") + ## Get list of upgradable packages os.system("apt list --upgradable > upgradable_packages") diff --git a/src/lac/unix/unix_scripts/unix.py b/src/lac/unix/unix_scripts/unix.py index a694217..a36c66d 100644 --- a/src/lac/unix/unix_scripts/unix.py +++ b/src/lac/unix/unix_scripts/unix.py @@ -206,32 +206,7 @@ def set_backup_enabled(enabled): def get_disks_stats(): - # Return disk name, the mountpoint, the foll size and the used size. - lines = subprocess.getoutput("df -h") - - lines = lines.split("\n") - lines = lines[1:] - disks = [] - for line in lines: - line = line.split(" ") - while '' in line: - line.remove('') - name = line[0] - if "/dev/loop" in name or "udev" in name or "tmpfs" in name or "overlay" in name: - continue - size = line[1] - # If the size is in megabytes, skip this disk, because it is very small - if "M" in size: - continue - disk = {} - disk["name"] = name.replace("/dev/", "") - disk["size"] = size - disk["used"] = line[2] - disk["used_percent"] = line[4].replace("%", "") - disk["mountpoint"] = line[5] - disks.append(disk) - return disks - + return utils.get_disks_stats() def get_system_information(): # Get hostname, ram usage, cpu usage, uptime, os version @@ -240,17 +215,12 @@ def get_system_information(): rv["lw_version"] = get_libre_workspace_version() rv["hostname"] = subprocess.getoutput("hostname") - rv["total_ram"] = subprocess.getoutput("free -h").split("\n")[1].split()[1].replace("Gi", "").replace("Mi", "") - ram_usage = subprocess.getoutput("free -h").split("\n")[1].split()[2] - if "Mi" in ram_usage: - rv["ram_usage"] = str(int(ram_usage.replace("Mi", ""))/1024).replace(".", ",") - else: - rv["ram_usage"] = ram_usage.replace("Gi", "") - rv["ram_percent"] = int(float(rv["ram_usage"].replace(",", ".")) / float(rv["total_ram"].replace(",", ".")) * 100) + v = utils.get_ram_usage() + rv["total_ram"] = v["total_ram"] + rv["ram_usage"] = v["ram_usage"] + rv["ram_percent"] = v["ram_percent"] - load_avg = subprocess.getoutput("cat /proc/loadavg").split(" ")[0] - cpu_number = subprocess.getoutput("nproc") - rv["cpu_usage_percent"] = int(float(load_avg) / float(cpu_number) * 100) + rv["cpu_usage"] = utils.get_cpu_usage() rv["uptime"] = subprocess.getoutput("uptime -p").replace("up", "").replace("minutes", "Minuten").replace("hours", "Stunden").replace("days", "Tage").replace("weeks", "Wochen").replace("months", "Monate").replace("years", "Jahre") rv["uptime"] = rv["uptime"].replace("min", "Min").replace("hour", "Stunde").replace("day", "Tag").replace("week", "Woche").replace("month", "Monat").replace("year", "Jahr") diff --git a/src/lac/unix/unix_scripts/utils.py b/src/lac/unix/unix_scripts/utils.py index c7e7b24..8e46ed2 100644 --- a/src/lac/unix/unix_scripts/utils.py +++ b/src/lac/unix/unix_scripts/utils.py @@ -4,4 +4,54 @@ def is_backup_running(): # Check if a process with the name backup.sh in it is running - return "backup.sh" in subprocess.getoutput("ps -aux") \ No newline at end of file + return "backup.sh" in subprocess.getoutput("ps -aux") + + +def get_ram_usage(): + rv = {} + rv["total_ram"] = subprocess.getoutput("free -h").split("\n")[1].split()[1].replace("Gi", "").replace("Mi", "") + ram_usage = subprocess.getoutput("free -h").split("\n")[1].split()[2] + if "Mi" in ram_usage: + rv["ram_usage"] = str(int(ram_usage.replace("Mi", ""))/1024).replace(".", ",") + else: + rv["ram_usage"] = ram_usage.replace("Gi", "") + rv["ram_percent"] = int(float(rv["ram_usage"].replace(",", ".")) / float(rv["total_ram"].replace(",", ".")) * 100) + + return rv + + +def get_cpu_usage(five_min=False): + if five_min: + load_avg = subprocess.getoutput("cat /proc/loadavg").split(" ")[1] + else: + load_avg = subprocess.getoutput("cat /proc/loadavg").split(" ")[0] + cpu_number = subprocess.getoutput("nproc") + return int(float(load_avg) / float(cpu_number) * 100) + + +def get_disks_stats(): + # Return disk name, the mountpoint, the foll size and the used size. + lines = subprocess.getoutput("df -h") + + lines = lines.split("\n") + lines = lines[1:] + disks = [] + for line in lines: + line = line.split(" ") + while '' in line: + line.remove('') + name = line[0] + if "/dev/loop" in name or "udev" in name or "tmpfs" in name or "overlay" in name: + continue + size = line[1] + # If the size is in megabytes, skip this disk, because it is very small + if "M" in size: + continue + disk = {} + disk["name"] = name.replace("/dev/", "") + disk["size"] = size + disk["used"] = line[2] + disk["used_percent"] = line[4].replace("%", "") + disk["mountpoint"] = line[5] + disks.append(disk) + return disks \ No newline at end of file diff --git a/src/lac/unix/views.py b/src/lac/unix/views.py index fbabbad..1264762 100644 --- a/src/lac/unix/views.py +++ b/src/lac/unix/views.py @@ -256,7 +256,7 @@ def pick_path(request): # We are checking if the remote ip address is 127.0.0.1 def unix_send_mail(request): if request.method != "POST": - return HttpResponse("Unauthorized") + return HttpResponseBadRequest("Only POST requests are allowed") # if ip address 127.0.0.1 return if request.META.get("REMOTE_ADDR", "") != "127.0.0.1":