diff --git a/plogical/IncScheduler.py b/plogical/IncScheduler.py index eff196b29..0fd8e94c2 100644 --- a/plogical/IncScheduler.py +++ b/plogical/IncScheduler.py @@ -5,6 +5,7 @@ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CyberCP.settings") import django django.setup() +from plogical.getSystemInformation import SystemInformation from IncBackups.IncBackupsControl import IncJobs from IncBackups.models import BackupJob from random import randint @@ -68,7 +69,16 @@ def startBackup(type): logging.statusWriter(IncScheduler.logPath, 'Job Description:\n\n Destination: %s, Frequency: %s.\n ' % ( job.destination, job.frequency), 1) if job.frequency == type: + + + + ### now run backups for web in job.jobsites_set.all(): + + ### Lets first update disk usage of all sites, to see if enough space for backups + + IncScheduler.CalculateAndUpdateDiskUsage() + logging.statusWriter(IncScheduler.logPath, 'Backing up %s.' % (web.website), 1) extraArgs = {} diff --git a/plogical/backupUtilities.py b/plogical/backupUtilities.py index 1dfae6401..776902f5c 100755 --- a/plogical/backupUtilities.py +++ b/plogical/backupUtilities.py @@ -2048,11 +2048,29 @@ def submitBackupCreation(tempStoragePath, backupName, backupPath, backupDomain): ## /home/cyberpanel/1047.xml - metaPath ## /home/backup/ - CPHomeStorage + status = os.path.join(backupPath, 'status') + + ### Lets first see if enough space for backups is available + + from plogical.IncScheduler import IncScheduler + import json + from plogical.getSystemInformation import SystemInformation + + IncScheduler.CalculateAndUpdateDiskUsage() + + website = Websites.objects.get(domain=backupDomain) + DiskUsageOfSite = json.loads(website.config)['DiskUsage'] + used_disk, free_disk, percent_used = SystemInformation.GetRemainingDiskUsageInMBs() + + if float(free_disk) <= float(DiskUsageOfSite): + command = f"echo 'Disk space exceeded the website size. [2065][5009]' > %s" + ProcessUtilities.executioner(command, website.externalApp) + return 0 ### - status = os.path.join(backupPath, 'status') + website = Websites.objects.get(domain=backupDomain) ## diff --git a/plogical/getSystemInformation.py b/plogical/getSystemInformation.py index f490d4411..80ce83746 100755 --- a/plogical/getSystemInformation.py +++ b/plogical/getSystemInformation.py @@ -99,6 +99,18 @@ def cpuRamDisk(): return SystemInfo + @staticmethod + def GetRemainingDiskUsageInMBs(): + import psutil + + total_disk = psutil.disk_usage('/').total / (1024 * 1024) # Total disk space in MB + used_disk = psutil.disk_usage('/').used / (1024 * 1024) # Used disk space in MB + free_disk = psutil.disk_usage('/').free / (1024 * 1024) # Free disk space in MB + percent_used = psutil.disk_usage('/').percent # Percentage of disk used + + return used_disk, free_disk, percent_used + + @staticmethod def populateOLSReport(): SystemInformation.olsReport = open("/tmp/lshttpd/.rtreport", "r").readlines() diff --git a/plogical/test.py b/plogical/test.py index 19919d85d..15d9387ea 100644 --- a/plogical/test.py +++ b/plogical/test.py @@ -1,28 +1,19 @@ -import docker +import psutil -# Create a Docker client -client = docker.from_env() +def get_disk_usage(): + total_disk = psutil.disk_usage('/').total / (1024 * 1024) # Total disk space in MB + used_disk = psutil.disk_usage('/').used / (1024 * 1024) # Used disk space in MB + free_disk = psutil.disk_usage('/').free / (1024 * 1024) # Free disk space in MB + percent_used = psutil.disk_usage('/').percent # Percentage of disk used -# Define the label to filter containers -label_filter = {'name': 'cyberplanner-new'} + return { + "current_disk_usage_mb": used_disk, + "current_disk_free_mb": free_disk, + "percentage_disk_used": percent_used + } -# List containers matching the label filter -containers = client.containers.list(filters=label_filter) - -# Print container information -for container in containers: - print(f"Container ID: {container.id}, Name: {container.name}, Status: {container.status}") - - # Get volume information for the container - volumes = container.attrs['HostConfig']['Binds'] if 'HostConfig' in container.attrs else [] - for volume in volumes: - print(f"Volume: {volume}") - - # # Fetch last 50 logs for the container - # logs = container.logs(tail=50).decode('utf-8') - # print(f"Last 50 Logs:\n{logs}") - - # Get exposed ports for the container - ports = container.attrs['HostConfig']['PortBindings'] if 'HostConfig' in container.attrs else {} - for port in ports: - print(f"Exposed Port: {port}") +# Usage example: +disk_info = get_disk_usage() +print("Current disk usage (MB):", disk_info["current_disk_usage_mb"]) +print("Current disk free (MB):", disk_info["current_disk_free_mb"]) +print("Percentage of disk used:", disk_info["percentage_disk_used"], "%")