Always-Try(정보보안 및 일상)

Linux 시스템 상태 확인 쉘 스크립트 (bash shell) 본문

Unclassified

Linux 시스템 상태 확인 쉘 스크립트 (bash shell)

Always-Try 2022. 2. 19. 02:49
#!/bin/bash
echo -e "-------------------------------System Information----------------------------"
echo -e "Hostname:\t\t"`hostname`
echo -e "uptime:\t\t\t"`uptime | awk '{print $3,$4}' | sed 's/,//'`
echo -e "Manufacturer:\t\t"`cat /sys/class/dmi/id/chassis_vendor`
echo -e "Product Name:\t\t"`cat /sys/class/dmi/id/product_name`
echo -e "Version:\t\t"`cat /sys/class/dmi/id/product_version`
echo -e "Serial Number:\t\t"`cat /sys/class/dmi/id/product_serial`
echo -e "Machine Type:\t\t"`vserver=$(lscpu | grep Hypervisor | wc -l); if [ $vserver -gt 0 ]; then echo "VM"; else echo "Physical"; fi`
echo -e "Operating System:\t"`hostnamectl | grep "Operating System" | cut -d ' ' -f5-`
echo -e "Kernel:\t\t\t"`uname -r`
echo -e "Architecture:\t\t"`arch`
echo -e "Active User:\t\t"`w | cut -d ' ' -f1 | grep -v USER | xargs -n1`
echo -e "System Main IP:\t\t"`hostname -I`
echo -e "System Date & Time:\t"`date`
echo ""
echo -e "-------------------------------CPU/Memory Usage------------------------------"
echo -e "CPU Model:\t\t"`awk -F':' '/^model name/ {print $2}' /proc/cpuinfo | uniq | sed -e 's/^[ \t]*//'`
echo -e "CPU Core:\t\t"`grep -c processor /proc/cpuinfo`" Core"
echo -e "CPU Processor:\t\t"`grep ^processor /proc/cpuinfo | wc -l`" Processor"
echo -e "CPU Usage:\t\t"`cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' |  awk '{print $0}' | head -1`
echo ""

echo -e "---------------Checking for Currently Mounted File System[s]-----------------"
echo -e `df -H | grep -vE '^Filesystem|tmpfs|cdrom'`
echo ""

echo -e "-----------Mounted File System[s] Utilization (Percentage Used)--------------"
echo -e "( 0-90% = OK/HEALTHY, 90-95% = WARNING, 95-100% = CRITICAL )"
echo -e "Percentage Used\t\tmounted File System[s]:"
filesystem=$(df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }')
usep=$(echo $filesystem | awk '{ print $1}' | cut -d'%' -f1  )
partition=$(echo $filesystem | awk '{ print $2 }' )
if [ $usep -lt 90 ]; then
  echo -e "$usep\t\t\t$partition\t\t--------OK/HEALTHY"
elif [ $usep -ge 90 -a $usep -lt 95 ]; then
  echo -e "$usep\t\t\t$partition\t\t\t--------WARNING"
else
  echo -e "$usep\t\t\t$partiotion\t\t\t--------CRITICAL"
fi
echo ""

echo -e "------------------------Checking For INode Usage-----------------------------"
echo -e "( 0-90% = OK/HEALTHY, 90-95% = WARNING, 95-100% = CRITICAL )"
echo -e "Percentage Used\t\tmounted File System[s]:"
filesystem=$(df -i | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }')
usep=$(echo $filesystem | awk '{ print $1}' | cut -d'%' -f1  )
partition=$(echo $filesystem | awk '{ print $2 }' )
if [ $usep -lt 90 ]; then
  echo -e "$usep\t\t\t$partition\t\t--------OK/HEALTHY"
elif [ $usep -ge 90 -a $usep -lt 95 ]; then
  echo -e "$usep\t\t\t$partition\t\t\t--------WARNING"
else
  echo -e "$usep\t\t\t$partiotion\t\t\t--------CRITICAL"
fi
echo ""

echo -e "-------------------------------SWAP Details----------------------------------"
free -h
echo ""

echo -e "-------------------Checking for Processor Utilization------------------------"
mpstat
echo ""

echo -e "------------------------Checking for Load Average----------------------------"
uptime=$(uptime)
echo $uptime | cut -c 36-
echo ""

echo -e "--------------------Top 5 Memory Resource Hog Processesge--------------------"
ps -eo %mem,%cpu,pid,ppid,user,stat,cmd --sort=-%mem | head -6
echo ""

echo -e "----------------------Top 5 CPU Resource Hog Processesge---------------------"
ps -eo %mem,%cpu,pid,ppid,user,stat,cmd --sort=-%cpu | head -6
echo ""

참고)

- 테스트 OS는 Centos 7이며, mpstat의 경우 설치 의존성 있을 수 있음. 

Comments