PDA

View Full Version : Monitor webpage load time/response in miliseconds continuously, bash script for Linux



Fli
03-14-2017, 09:04 PM
Here is the Linux bash script that can be run as a cron task.
Each time script is executed (by a cronjob you set), it will write number of miliseconds that yours defined webpage taken to load into file.
Moreover if it takes longer time than you define, script will send you an e-mail with various details about server to find what is causing high load on the server.

crontab -e

* * * * * sh /path/to/scriptname.sh 1>/dev/null

Bash script:

# check site load speed
site=http://site.tld/
#how many miliseconds is bad site load time
badtime=2000
[email protected]
logfilename="site_speed_history"

curl -H 'Cache-Control: no-cache' --max-time 30 -s -w '\nLookup time:\t%{time_namelookup}\nConnect time:\t%{time_connect}\nAppCon time:\t%{time_appconnect}\nRedirect time:\t%{time_redirect}\nPreXfer time:\t%{time_pretransfer}\nStartXfer time:\t%{time_starttransfer}\n\nTotal time:\t%{time_total}\n' -o /dev/null "$site$suffix" 2>/dev/null|tail -n 1|awk '{print $3}'|sed 's|\.||g' >> time

timerefined="$(cat time|head -c 5|grep -o '[0-9]*'|sed 's/^0*//')"
echo "$timerefined" >> $logfilename

if [[ "$timerefined" -gt "$badtime" ]];then
echo "$site load took $timerefined miliseconds.

$(cat /tmp/nodewatch_stats|grep -E \"cpu|CPU|3400\")

$(ps auxf)

$(free -mt)

$(cat /proc/net/nf_conntrack)

$(netstat -tlnp)

Local connections:
$(netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head)

All server connections:
$(netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n)

HTTPD connections: netstat -anpt| grep http
$(netstat -anpt| grep httpd)

Sumary of http IPs connected:
$(netstat -alntp | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n)

Number of connections per port (consider disabling service on that port if under attack):
$(netstat -tuna | awk -F':+| +' 'NR>2{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n)

$(tail /var/log/lfd.log)

This is a script at $(hostname)"|mail -s "$site loading slow" $adminmail

else
echo "Load time $timerefined is below $badtime miliseconds. ok"
fi

rm time

If you modify this script or have similar script, you can share your version there. Thank You