File: //scripts/checkGeometra4ProdWarn.sh
#!/bin/bash
PROJECT_DIR="/data/geometra-4-server-prod"
Warning_LIST="$PROJECT_DIR/WarningInstance.list"
LOG="$PROJECT_DIR/logs/autoHealWarningInstance.log"
TOP_PROC_LOG="$PROJECT_DIR/logs/HighUsageProcList.log"
cd $PROJECT_DIR
/root/.ebcli-virtual-env/executables/eb health | grep "Warning\|Degraded\|Severe" | grep "i-" | awk '{print $1}'> $Warning_LIST
if [ -s $Warning_LIST ]
then
cat $Warning_LIST | while read INSTANCE
do
# Uncomment the following lines if you want to reboot instances
# echo "$(date) | Rebooting Warn Instance: $INSTANCE" >> $LOG
# /usr/local/bin/aws ec2 reboot-instances --instance-ids $INSTANCE
/root/.ebcli-virtual-env/executables/eb ssh -n 1 --command "sudo ps -eo pcpu,pid,user,args|sort -k1 -r | head -10"|grep -v "INFO\|PID" > $TOP_PROC_LOG
# echo "TOP USAGE PROCESS:$(cat $TOP_PROC_LOG)"
PROCESS=`cat $TOP_PROC_LOG|awk '$1>40'`
echo -n "PROCESSLIST: $PROCESS"
PRO=`printf '%s' "$(cat $TOP_PROC_LOG | awk '$1>40' | awk '{printf "%s\\n", $0}')"`
# printf '%s' "$(cat logs/HighUsageProcList.log | awk '{printf "%s\\n", $0}')" > logs/test.log
# Uncomment the following lines if you want to kill processes
# echo "Kill Process"
# PROC_TO_KILL=`cat $TOP_PROC_LOG | awk '$1>40' | awk '{printf $2 " "}'`
# /root/.ebcli-virtual-env/executables/eb ssh -n 1 --command "sudo kill -9 $PROC_TO_KILL"
# echo "Notify"
# curl --location --request POST 'https://marketing.wp.karmanye.in/api/Karmanye/send-message' --header 'Content-Type: application/json; charset=utf-8' --header 'Accept: application/json' --header 'Authorization: Bearer $2b$10$f7yAQHm3Y1eOxs92aiEJ9eohJ0AdUQ2YwXxUARzk3sVLgKIVw0k2q' -d '{ "phone": "919033595842", "message": "*Geometra-4-Server-Prod Warning Instance: '"$INSTANCE"'* \n\n*Rebooting Instance for AutoHealing* \n*Process:*\n'"$(printf "%s" $PRO)"'"}'
done
else
echo "$(date) | Everything OK!"
# eb ssh -n 1 --command "sudo ps -aux | grep npm"
fi