Created
December 10, 2014 03:25
-
-
Save dotmanila/c10220a06b386d4d6229 to your computer and use it in GitHub Desktop.
Data Collection Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
dir=$1 | |
duration=$2 | |
outputdir="${dir}/second_samples_output_$(date +%Y-%m-%d_%H-%M-%S)" | |
min_free_bytes=1073741824 # 1GB | |
min_free_pct=10 # 10% | |
function usage { | |
echo "" | |
echo "Usage: $0 dir/ {duration|ctrlc}" | |
echo "" | |
echo "Arguments:" | |
echo " - duration is in minutes, ctrlc option can be given to collect data forever (or until ctrl-c)" | |
echo " - dir is the directory to store the data. In the directory a subdirectory with a timestamp will be created" | |
echo "" | |
echo "Examples:" | |
echo " - $0 /home/gryp ctrlc # until ctrl-c" | |
echo " - $0 /tmp/ 60 # 60 minutes" | |
echo "" | |
} | |
bg_jobs=(); | |
if [ $# -ne 2 ]; then | |
echo "ERROR: Invalid amount of arguments given" | |
usage | |
exit 1 | |
fi | |
function kill_collection() { | |
echo "Collecting last pieces of data ... wait a bit." | |
date "+%Y-%m-%d %H:%M:%S" > ${outputdir}/collection_stop | |
for pid in "${bg_jobs[@]}" | |
do | |
echo "Killing pid $pid" | |
kill -9 $pid | |
wait $pid | |
done | |
exit 1 | |
} | |
function check_disk_space() { | |
set -e | |
local disk_space=$(df -P -k $dir | tail -n 1) | |
local free_bytes=$(echo $disk_space | perl -ane 'print $F[3] * 1024') | |
local pct_used=$(echo $disk_space | perl -ane 'print ($F[4] =~ m/(\d+)/)') | |
local pct_free=$((100 - $pct_used)) | |
local real_free_bytes=$free_bytes | |
local real_pct_free=$pct_free | |
set +e | |
if [ $free_bytes -lt $min_free_bytes -o $pct_free -lt $min_free_pct ]; then | |
echo "ERROR: Not enough free disk space: | |
Limit: ${min_free_pct}% free, ${min_free_bytes} bytes free | |
Actual: ${real_pct_free}% free, ${real_free_bytes} bytes free | |
" | |
kill_collection | |
fi | |
return 0 # disk space is OK | |
} | |
function check_binary() { | |
binary=$1 | |
which $binary &> /dev/null | |
if [ "$?" != "0" ]; then | |
echo "ERROR: Could not find $binary or it is not executable" | |
exit 1 | |
fi | |
} | |
check_binary mpstat | |
check_binary vmstat | |
check_binary iostat | |
check_binary mysqladmin | |
check_binary mysql | |
check_binary cat | |
## get mysql version, to see if we should fetch 5.6 specific data | |
MYSQLVERSION=`mysql -Ne"select replace(left(version(), 3), '.', '');"` | |
if [ "$duration" == 'ctrlc' ]; then | |
echo "Will collect data until ctrlc is pressed"; | |
else | |
if [[ "$duration" =~ ^[0-9]+$ ]]; then | |
echo "Will collect data for $duration minutes"; | |
else | |
echo "ERROR: Invalid duration given: $duration"; | |
usage | |
exit 1 | |
fi | |
fi | |
if [ ! -d ${outputdir} ] | |
then | |
mkdir -p ${outputdir} | |
fi | |
# before we start let's check disk space first | |
check_disk_space | |
echo "Writing all data to $outputdir" | |
trap kill_collection INT | |
date "+%Y-%m-%d %H:%M:%S" > ${outputdir}/collection_start | |
## PUT ALL THE NECESSARY TESTS IN HERE | |
mpstat 1 > ${outputdir}/mpstat.out & | |
bg_jobs+=("$!"); | |
vmstat 1 > ${outputdir}/vmstat.out & | |
bg_jobs+=("$!"); | |
iostat -mx 1 > ${outputdir}/iostat.out & | |
bg_jobs+=("$!"); | |
mysqladmin ext -i1 > ${outputdir}/mysqladmin.out & | |
bg_jobs+=("$!"); | |
# uncomment to enable | |
#./pcs-collect-queryresponsetime.sh > ${outputdir}/queryresponsetime.out & | |
# bg_jobs+=("$!"); | |
function capture_innodb_metrics() { | |
interval=1 | |
while [ ! -f ${outputdir}/collection_stop ] | |
do | |
mysql -e "select name, subsystem, count, status from information_schema.INNODB_METRICS" \ | |
>> $outputdir/innodb_metrics.out | |
echo "" >> $outputdir/innodb_metrics.out | |
sleep ${interval} | |
done | |
} | |
if [ "$MYSQLVERSION" -ge "56" ]; then | |
capture_innodb_metrics & | |
bg_jobs+=("$!"); | |
fi | |
function capture_innodb_status () { | |
interval=60 | |
while [ ! -f ${outputdir}/collection_stop ] | |
do | |
mysql -e "show engine innodb status\G" \ | |
>> $outputdir/innodb_status.out | |
sleep ${interval} | |
done | |
} | |
capture_innodb_status & | |
bg_jobs+=("$!"); | |
function capture_slave_status () { | |
interval=1 | |
mysql -e "show slave status" \ | |
>> $outputdir/slave_status.out | |
sleep ${interval} | |
while [ ! -f ${outputdir}/collection_stop ] | |
do | |
mysql --skip-column-names -e "show slave status" \ | |
>> $outputdir/slave_status.out | |
sleep ${interval} | |
done | |
} | |
capture_slave_status & | |
bg_jobs+=("$!"); | |
function collect_diskstats { | |
INTERVAL=1 | |
while true; do | |
sleep=$(date +%s.%N | awk "{print $INTERVAL - (\$1 % $INTERVAL)}") | |
sleep $sleep | |
date +"TS %s.%N %F %T" | |
cat /proc/diskstats | |
done | |
} | |
collect_diskstats > ${outputdir}/diskstats.out & | |
bg_jobs+=("$!"); | |
################################ | |
if [ "$duration" == "ctrlc" ]; then | |
while true; | |
do | |
check_disk_space | |
sleep 60; | |
done | |
else | |
counter=0 | |
while [ "$counter" -le "$duration" ]; | |
do | |
check_disk_space | |
counter=$(($counter+1)) | |
sleep 60 | |
done | |
fi | |
kill_collection | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment