Skip to content

Commit

Permalink
Add automatic mail messages if ram or cpu is over 80% for over 5 Minu…
Browse files Browse the repository at this point in the history
…tes, also if Harddisk is over 90% #69
  • Loading branch information
Jean28518 committed Apr 27, 2024
1 parent 21c6387 commit 01d4361
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 38 deletions.
2 changes: 2 additions & 0 deletions src/lac/unix/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 24 additions & 0 deletions src/lac/unix/unix_scripts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ################################################################################################
##################################################################################################################
Expand All @@ -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")

Expand Down
42 changes: 6 additions & 36 deletions src/lac/unix/unix_scripts/unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
52 changes: 51 additions & 1 deletion src/lac/unix/unix_scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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
2 changes: 1 addition & 1 deletion src/lac/unix/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 01d4361

Please sign in to comment.