Skip to content

Custom Commands

To fetch general information about the server

Ubuntu/RHEL
echo -e "\n\033[93m Hostname:\033[0m $(hostname) \n\033[93m IPv4 Address:\033[0m $(hostname -I | awk '{for(i=1;i<=NF;i++) if($i ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) printf "%s ", $i}') \n\033[93m Operating System:\033[0m $(grep -Po '^PRETTY_NAME=\K.*' /etc/os-release | sed 's/^\"\(.*\)\"$/\1/') \n\033[93m Kernel:\033[0m $(uname -r) \n\n\033[93m Current CPU Usage:\033[0m $((100 - $(vmstat 1 2 | tail -1 | awk '{print $15}')))% \n\033[93m Current Memory Usage:\033[0m $(free | awk '/Mem/ {printf "%.2f%%", $3/$2 * 100}') \n\033[93m Current Swap Usage:\033[0m $(free | awk '/Swap/ {if ($2 == 0) print "0.00%"; else printf "%.2f%%", $3/$2 * 100}') \n\033[93m Splunk Status:\033[0m $(/opt/splunkforwarder/bin/splunk status | grep -q 'not running' && echo 'Not Running' || echo 'Running') \n\033[93m Uptime:\033[0m $(uptime -p) \n\n\033[93m Filesystems >80% Usage:\033[0m\n$(df -hPT | awk -v threshold=80 'NR==1 {h="\t"$0; next} $2 ~ /ext4|xfs|btrfs|tmpfs/ && ($6+0)>threshold {if(!p++) print h; print "\t"$0} END {if(!p) print "\tNone above " threshold "%"}')')\n"

CPU Usage

To get the CPU usage by percentage, sorted and show realtime usage

Ubuntu/RHEL
echo -e "\nCPU Status: \n$(top -bn1 | grep '%Cpu(s)') \n\nCurrent CPU Usage: $(top -bn1 | grep '%Cpu(s)' | awk '{ print (100-$8) }')% \n\nTop 5 Processes with Highest CPU Usage: \n$(ps -eo pid,ppid,cmd,%cpu --sort=-%cpu | head -n 5)\n\n"; echo -e "Real-Time Usage:\n$(for i in {1..10}; do echo "$(date '+%r %Z') - $(top -bn1 | grep '%Cpu(s)' | awk '{ print (100-$8) }')%"; sleep 2; done)"

Memory Usage

To get the Memeory usage by percentage and sorted by usage.

Ubuntu/RHEL
echo -e "\n\033[0;36mMemory Details:\033[0m\n$(free -h)\n\n\033[0;36mMemory Usage: \033[0m$(free | awk '/Mem/ {printf "%.2f%%\n", $3/$2 * 100}')\n\n\033[0;36mSwap Usage: \033[0m$(free | awk '/Swap/ {printf "%.2f%%\n", $3/$2 * 100}')\n\n\033[0;36mTop 5 Processes:\033[0m\n$(ps -eo pid,ppid,cmd,%mem --sort=-%mem | head -n 5)\n"
AIX
echo ""; echo "Memory Status:"; svmon -G -O unit=GB | tail -n +3 | head -3; echo ""; echo "Current Memory Usage: $(svmon -G -O unit=GB | awk 'NR==4 {printf "%.2f%%\n", ($3/$2)*100}')"; echo ""; echo "Top 5 Processes:"; ps aux | head -1; ps aux | grep -vE 'ps|sort|head' | sort -rk4,4n | head -5; echo ""

Swap Usage

Ubuntu/RHEL
echo -e "\n\e[1mMemory Status: \e[0m\n$(free -h)\n\n\e[1mCurrent Swap Usage: \e[0m$(free | awk '/Swap/ {printf "%.2f%%\n", $3/$2 * 100}')\n\n\e[1mTop 5 processes sorted by SWAP:\e[0m\n$(top -b -o +SWAP | tail -n +7 | head -n 6)\n"
AIX
svmon -O unit=GB | awk 'NR==5 {printf "%.2f%%\n", ($4/$3)*100}'

Disk Usage

Find files by time
find / -type f -mtime +90;

Here +90 means, more than 90 days.


Find files by size
find / -type f -size +1G

Here +1 means, more than 1 GB, you could M for MB.

 

find files by type
find / -type f -name "*.log"

Here after the -name flag, You can use any file extension to find and list.