Troubleshooting scripts

Some useful scripts to help during various issues:

log high cpu issues
memory
Apache high number of connections
high number of mysql connections
log useful details for a webserver
slow loading site.

log high cpu issues

#!/bin/bash
l=`cat /proc/loadavg | awk '{print $2}'| awk -F. '{print $1}'`
out=~/loadmon.log
if [ $l -gt 8 ];
then
date >> $out
vmstat 1 10 >> $out
top -b | head -15 >> $out
echo “###” >> $out
fi

a slightly different one for memory

#!/bin/bash
l=`free -m | head -3 | tail -1| awk '{print $4}'`
out=/root/loadmon.log
if [ $l -lt 2000 ];
then
date >> $out
echo “###” >> $out
free -m >> $out
echo “###” >> $out
vmstat 1 10 >> $out
echo “###” >> $out
top -b | head -15 >> $out
echo “###” >> $out
ps aux | head -1 >> $out
ps aux | sort -nk +4 | tail >> $out
#/etc/rc.d/init.d/httpd fullstatus >> $out
echo “###” >> $out
fi

a to log when appache hits a high number of connections

#!/bin/bash
log_path=/root/scripts/number_of_appache_users.log
if [ $number -gt 150 ]
then
date >> $log_path
echo “number of indivdual connections” >> $log_path
/usr/sbin/apachectl fullstatus | awk '{print $11}' | grep -v "::1" | grep -v ^$ | sort | uniq -c | sort -n | tail >> $log_path
echo "pages being view" >> $log_path
/usr/sbin/apachectl fullstatus | awk '{print $11,$12,$13,$14}' | grep -v “::1″ | grep -v ^$ | sort | uniq -c |sort -n >> $log_path
fi

high number of mysql connections

echo date >> /root/scripts/max.connections.txt
date >> /root/scripts/max.connections.txt
echo "number of connections " >> /roo/scripts/max.connections.txt
mysql -e "show full processlist;" | wc -l >> /root/scripts/max.connections.txt
num1=`mysql -e "show full processlist;" | wc -l`
if [ $num1 -gt 50 ];
then
#echo "123" >> /root/scripts/max.connections.txt
mysql -e "show full processlist;" >> /root/scripts/max.connections.txt
fi

to have a bash script run and log useful details for a webserver:

FILE=/root/logs/output.log
vmstat 1 10 >> $FILE
top -b -n1 >> $FILE
/etc/rc.d/init.d/httpd fullstatus >> $FILE
/etc/rc.d/init.d/httpd fullstatus | awk '{print $11}' |grep -v ^$ | sort | uniq -c | sort -k1 >> $FILE

slow loading site.

using curl to check speed of a site