Eu tentei este approch, batendo o vmstat em um loop while para que eu pudesse facilmente sair quando eu terminei o meu teste, em seguida, analisar o arquivo de log coletado para csv para facilitar a importação de gráficos. Funcionou, mas vmstats 1ª linha foi quase sempre o mesmo, então isso era inútil. Eu tive que executar o vmstat manualmente e coletar os logs em um arquivo e processá-los depois.
#!/bin/bash
OUTPUT="/dropinbox/vmstat_$(hostname)_$(date +%Y-%m-%d_%H-%M).csv"
echo Starting Recording...
echo Press Q to exit.
# create blank temp file
echo '' > /tmp/vmstat.log
while true; do
# -t time -a active and inactive memory reporting -n no repeate headers -S M size
vmstat 1 1 -tanwS M >> /tmp/vmstat.log
# In the following line -t for timeout, -N for just 1 character
# -t 0.25 therefore loop will run 4 times a second.
read -t 0.25 -N 1 input
if [[ $input = "q" ]] || [[ $input = "Q" ]]; then
# The following line is for the prompt to appear on a new line.
echo Finshed
break
fi
done
# remove blank lines
sed -i '/^$/d' /tmp/vmstat.log
# remove headers
sed -i '/procs/d' /tmp/vmstat.log
# Keep 1 line of the 2nd headers
grep -m1 r /tmp/vmstat.log > /tmp/headers
tr -s ' ' , < /tmp/headers | sed s/,// > $OUTPUT
# remove all 2nd line headers
sed -i '/r/d' /tmp/vmstat.log
# process columns into csv output missing out column 18 (Date) as this got in myway
awk 'BEGIN { OFS = "," }!($18="")' /tmp/vmstat.log |tr -s ',' >> $OUTPUT
cat -vet $OUTPUT
echo finished saving to $OUTPUT