YBee24 / Yasmina-and-Lucien-Shell-Problem-Project-Friday-26-July

0 stars 0 forks source link

create top 3 cpu processes function #9

Open Luciensday opened 1 month ago

YBee24 commented 1 month ago

!/bin/bash

Function to check CPU usage

check_cpu_usage() { echo "CPU Usage:"

Display average CPU load over the last 1, 5, and 15 minutes

uptime | awk '{printf "1 min: %-6s 5 min: %-6s 15 min: %-6s\n", $(NF-2), $(NF-1), $NF}'
echo ""

}

Function to find the top 5 CPU-consuming processes

top_cpu_processes() { echo "Top 5 CPU-consuming processes:" ps aux --sort=-%cpu | awk 'NR==1 || NR<=6 {printf "%-10s %-8s %-8s %-8s %s\n", $1, $2, $3, $4, $11}' echo "" }

Function to check memory usage

check_memory_usage() { echo "Memory Usage:"

Display memory usage using free command

free -h | awk 'NR==1 || NR==2 {printf "%-10s %-10s %-10s %-10s\n", $1, $2, $3, $4}'
echo ""

}

Function to find the top 5 memory-consuming processes

top_memory_processes() { echo "Top 5 memory-consuming processes:" ps aux --sort=-%mem | awk 'NR==1 || NR<=6 {printf "%-10s %-8s %-8s %-8s %s\n", $1, $2, $3, $4, $11}' echo "" }

Function to check disk usage

check_disk_usage() { echo "Disk Usage Information:" df -h | awk 'NR==1 || /^\/dev\// {printf "%-20s %-10s %-10s %-10s %-10s %-10s\n", $1, $2, $3, $4, $5, $6}' echo "" echo "Total Disk Usage:" df -h --total | awk 'END {printf "Total Size: %-10s Total Used: %-10s Total Available: %-10s Total Usage: %-10s\n", $2, $3, $4, $5}' echo "" }

Function to generate the system report

generate_report() { echo "========================" echo "System Resource Report" echo "========================" echo ""

check_cpu_usage
top_cpu_processes
check_memory_usage
top_memory_processes
check_disk_usage

}

Call the function to generate the report

generate_report