Skip to content

Instantly share code, notes, and snippets.

@vuthaihoc
Last active June 23, 2020 09:54
Show Gist options
  • Save vuthaihoc/e2b05ac6320e0429e32f22f035138c5a to your computer and use it in GitHub Desktop.
Save vuthaihoc/e2b05ac6320e0429e32f22f035138c5a to your computer and use it in GitHub Desktop.
System usage bash
#!/bin/bash
## Use : ss command
## variable for change :
##
##----------
Timetemp='%H%M'
Time=$(date +"${Timetemp}")
Date=$(date +"%y%m%d")
Yest=$(date --date="1 day ago" +"%y%m%d")
#Day=$(date +"%a")
Path="/var/log/system_useage.log"
if [ $Time == "0101" ]; then
mv $Path $Path-$Yest && cat /dev/null > $Path
fi
(date | awk '{printf "[" $1 " " $2 " " $3 " " $6 " " $4 "]\t"}';
w | grep load | awk '{printf $3 " " $4 " CPU : " $10 " " $11 " " $12 " \t"}';
FreeMem=$( free -m | grep "Mem" | awk '{print $7;}' );
printf "RAM free: %s MB\t" $FreeMem;
/usr/sbin/ss -tn state established '( sport = :80 )' | wc -l | awk '{printf "Est_80: " $1 "\t"}';
/usr/sbin/ss -tn state established '( sport = :443 )' | wc -l | awk '{printf "Est_443: " $1 "\t"}';
/usr/sbin/ss -tn state syn-recv | wc -l | awk '{printf "SYN_RECV: " $1 "\t"}';
/usr/sbin/ss -tn state close-wait | wc -l | awk '{printf "CLOSE_WAIT: " $1 "\n"}';
if [[ $FreeMem -lt 800 ]] ; then
echo "<<<<<<<<<<<<<<<Using too much RAM"
ps aux --sort=-%mem | head
echo ">>>>>>>>>>>>>>>"
fi
) >> /var/log/system_useage.log
##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment