Sindbad~EG File Manager

Current Path : /opt/dedrads/
Upload File :
Current File : //opt/dedrads/filescan

#!/usr/lib/rads/venv/bin/python3
import os
import shutil
from subprocess import run, PIPE, DEVNULL
from datetime import datetime


# Print date
def show_date():
    print(datetime.now().strftime("%a %b %d %H:%M:%S %Y"))


# Show total disk usage and print
def show_disk_usage(fs='/'):
    print(f"\nDisk Usage for {fs}:\n")
    try:
        total, used, free = shutil.disk_usage(fs)
        disk_usage_percent = (used / total) * 100 if total > 0 else 0
        print(f"Total: {total // (2**30)} GB")
        print(f"Used:  {used // (2**30)} GB")
        print(f"Free:  {free // (2**30)} GB")
        print(f"Usage: {disk_usage_percent:.2f}%")
    except Exception as e:
        print(f"Failed to retrieve disk usage info: {e}")


# Show inode usage (like df -i)
def show_inode_usage(fs='/'):
    print(f"\nInode Usage for {fs}:\n")
    try:
        stats = os.statvfs(fs)
        total_inodes = stats.f_files
        free_inodes = stats.f_ffree
        used_inodes = total_inodes - free_inodes
        inode_usage_percent = (
            (used_inodes / total_inodes) * 100 if total_inodes > 0 else 0
        )

        print(f"Total Inodes: {total_inodes}")
        print(f"Used Inodes:  {used_inodes}")
        print(f"Free Inodes:  {free_inodes}")
        print(f"Usage:        {inode_usage_percent:.2f}%")
    except Exception as e:
        print(f"Failed to retrieve inode info: {e}")


# Find the largest directories and print
def find_largest_directories(basedir="/"):
    print(f"\nLargest Directories for {basedir} (≥ 1 GB):\n")
    cmd = (
        f"du --human-readable --max-depth=2 --threshold={2**30} "
        f"--exclude='/proc/*' --exclude='/home/virtfs/*' {basedir} "
        "| sort -hr | head -15"
    )
    try:
        result = run(
            cmd, stdout=PIPE, stderr=DEVNULL, shell=True, text=True, check=True
        )
    except Exception as e:
        print(f"Failed to retrieve largest directories: {e}")

    for line in result.stdout.splitlines():
        size, path = line.split(maxsplit=1)
        print(f"{size:>6}\t{path}")


# Find largest files and print
def find_largest_files(basedir="/"):
    print(f"\nLargest Files for {basedir}:\n")
    cmd = (
        f"find {basedir} "
        "-xdev -path /home/virtfs -prune -o -type f -printf '%s %p\n' "
        "| sort -nr "
        "| head -n 20 "
        "| numfmt --to=iec"
    )
    try:
        result = run(
            cmd, stdout=PIPE, stderr=DEVNULL, shell=True, text=True, check=True
        )
    except Exception as e:
        print(f"Error running command: {e}")

    for line in result.stdout.splitlines():
        size, path = line.split(maxsplit=1)
        print(f"{size:>6}\t{path}")


# Full listing of all directories in /backup
def print_backup_directory_contents():
    print("\nFull Directory Tree of /backup (Directories Only):\n")
    try:
        for root, dirs, _ in os.walk('/backup'):
            for name in dirs:
                print(os.path.join(root, name))
    except Exception as e:
        print(f"Failed to list directories in /backup: {e}")


# Show /backup disk usage, largest dirs and files
def find_backup_info(basedir='/backup'):
    print("\nDisk Usage for /backup:\n")
    find_largest_directories(basedir)
    find_largest_files(basedir)
    print_backup_directory_contents()


# The main function
def main():
    fs = '/'
    show_date()
    show_disk_usage(fs)
    show_inode_usage(fs)
    find_largest_directories(fs)
    find_largest_files(fs)

    # Add backup section
    find_backup_info()


if __name__ == "__main__":
    main()

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists