Skip to content

Instantly share code, notes, and snippets.

@gcorrao
Created June 10, 2011 17:57
Show Gist options
  • Save gcorrao/1019371 to your computer and use it in GitHub Desktop.
Save gcorrao/1019371 to your computer and use it in GitHub Desktop.
bash- script that detects high CPU and runs a "system scan"
#!/bin/bash
INTERVAL=$1
THRESHOLD=$2
if [ "" == "$INTERVAL" -o "" == "$THRESHOLD" ]; then
echo "You must insert interval and cpu threshold"
exit 1
fi
while [ 1 ]; do
VM_STAT=`vmstat 1 2`
VM_CURR_VALUE=`echo "$VM_STAT" | tail -n 1`
CPU_IDLE=`echo "$VM_CURR_VALUE" | awk {'print $15'}`
CPU_USE=$((100-$CPU_IDLE))
if [ $CPU_USE -ge $THRESHOLD ]; then
LOG_DIR=$HOSTNAME-`date +"%Y%m%d_%H%M%S"`
mkdir -p $LOG_DIR
df -h >$LOG_DIR/df 2>&1 &
free -m >$LOG_DIR/free 2>&1 &
w >$LOG_DIR/w 2>&1 &
echo "$VM_STAT" >$LOG_DIR/vmstat 2>&1 &
netstat -tpan >$LOG_DIR/netstat 2>&1 &
ps -fea >$LOG_DIR/ps 2>&1 &
top -n 1 -b -c >$LOG_DIR/top 2>&1 &
/usr/sbin/lsof >$LOG_DIR/lsof 2>&1 &
iostat -xn >$LOG_DIR/iostat 2>&1 &
mpstat -P ALL >$LOG_DIR/mpstat 2>&1 &
hdparm -t /dev/xvda5 >$LOG_DIR/hdparm 2>&1 &
#RESIN_ONLY
if [ -d /data1/resin ]; then
echo >/data1/resin/log/jvm.log
RESIN_PID=`ps -fea | grep com.caucho.server.resin.Resin | grep -v perl | grep -v grep | awk {'print $2'}`
kill -3 $RESIN_PID
sleep 2
cp /data1/resin/log/jvm.log $LOG_DIR/jvm.log
fi
sleep 60
fi
sleep $INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment