Advertisement
mohas8

Untitled

Dec 1st, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.88 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # mtop - A simple system monitoring tool
  4.  
  5. # Define color codes
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[0;33m'
  9. BLUE='\033[0;34m'
  10. MAGENTA='\033[0;35m'
  11. CYAN='\033[0;36m'
  12. WHITE='\033[0;37m'
  13. NC='\033[0m' # No Color
  14.  
  15. # Function to display CPU usage
  16. function cpu_usage() {
  17.     echo -e "${CYAN}==================== CPU Usage ====================${NC}"
  18.     mpstat 1 1 | awk '/Average:/ {printf "${YELLOW}:User   %.2f%%\nSystem: %.2f%%\nIdle: %.2f%%\n${NC}", $3, $5, $12;}'
  19.     echo ""
  20. }
  21.  
  22. # Function to display Memory usage
  23. function memory_usage() {
  24.     echo -e "${CYAN}==================== Memory Usage ====================${NC}"
  25.     free -h | awk 'NR==2{printf "${YELLOW}Used: %s\nFree: %s\nTotal: %s\n${NC}", $3, $4, $2;}'
  26.     echo ""
  27. }
  28.  
  29. # Function to display Disk usage
  30. function disk_usage() {
  31.     echo -e "${CYAN}==================== Disk Usage ====================${NC}"
  32.     df -h | awk 'NR>1{printf "%-20s: ${YELLOW}%s used, %s available${NC}\n", $1, $3, $4;}'
  33.     echo ""
  34. }
  35.  
  36. # Function to display Top Processes
  37. function top_processes() {
  38.     echo -e "${CYAN}==================== Top Processes ====================${NC}"
  39.     echo -e "${WHITE}USER                %MEM       %CPU       COMMAND${NC}"
  40.     echo "-----------------------------------------------------"
  41.     ps aux --sort=-%mem | awk 'NR<=10{printf "%-20s %-10s %-10s %-10s\n", $1, $3, $4, $11;}'
  42.     echo ""
  43. }
  44.  
  45. # Function to display Network Usage
  46. function network_usage() {
  47.     echo -e "${CYAN}==================== Network Usage ====================${NC}"
  48.     echo -e "${WHITE}Interface\tReceived\t\tSent${NC}"
  49.     echo "-----------------------------------------------------"
  50.     for iface in $(ls /sys/class/net/); do
  51.         if [ "$iface" != "lo" ]; then
  52.             rx_bytes=$(cat /sys/class/net/$iface/statistics/rx_bytes)
  53.             tx_bytes=$(cat /sys/class/net/$iface/statistics/tx_bytes)
  54.             echo -e "$iface\t\t$(numfmt --to=iec-i --suffix=B $rx_bytes)\t$(numfmt --to=iec-i --suffix=B $tx_bytes)"
  55.         fi
  56.     done
  57.     echo ""
  58. }
  59.  
  60. # Function to display Uptime
  61. function system_uptime() {
  62.     echo -e "${CYAN}==================== System Uptime ====================${NC}"
  63.     uptime | awk -F', ' '{printf "${YELLOW}Uptime: %s\n${NC}", $1;}'
  64.     echo ""
  65. }
  66.  
  67. # Function to display Load Average
  68. function load_average() {
  69.     echo -e "${CYAN}==================== Load Average ====================${NC}"
  70.     uptime | awk -F'load average:' '{printf "${YELLOW}Load Average: %s\n${NC}", $2;}'
  71.     echo ""
  72. }
  73.  
  74. # Main loop to refresh the output
  75. while true; do
  76.     clear
  77.     echo -e "${GREEN}==================== mtop ====================${NC}"
  78.     cpu_usage
  79.     memory_usage
  80.     disk_usage
  81.     network_usage
  82.     system_uptime
  83.     load_average
  84.     top_processes
  85.     echo -e "${MAGENTA}Press [CTRL+C] to stop...${NC}"
  86.     sleep 1  # Reduced sleep duration for more real-time output
  87. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement