Pages

Tuesday, January 14, 2020

Linux Script to collect server status



Linux Script to collect server status


[root@tayab]# cat stat.sh
#! /bin/bash
printf "Memory\t\tDisk\t\tCPU\t\tDATE\n"
end=$((SECONDS+36000000000))
while [ $SECONDS -lt $end ]; do
MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')
DATE=$(date)
echo "$MEMORY$DISK$CPU$DATE"
sleep 60
done

[root@tayab]# nohup sh stat.sh >> load.txt &


** If CPU core 12 then run as bellow (100/12 = 8.333)

#! /bin/bash
printf "Memory\t\tDisk\t\tCPU\t\tDATE\n"
end=$((SECONDS+3600000))
while [ $SECONDS -lt $end ]; do
MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')

CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)*8.333}')

DATE=$(date)
echo "$MEMORY$DISK$CPU$DATE"
sleep 60
done