Public
Snippet $5 authored by Christian Gerdes

Bash compare 2 varnish stat output files (varnishstat -1)

varnishstatcompare.sh
#!/bin/bash
# Author christian.gerdes@lightsinline.se
# Compares to output files generated by "varnishstat -1 > file1.dat"
# Uses the file last modified timestamps to calculate the time between the files
# Outputs values that have changed only, the change between the files and per sec

if [ $# -lt 2 ]
	then
		echo "Need at least 2 files."
		exit 0
fi

if ! [ -a $1 ]
	then
		echo "File $1 does not exist"
		exit 0
fi

if ! [ -a $2 ]
	then
		echo "File $2 does not exist"
		exit 0
fi

if [ $1 -nt $2 ]
	then
		echo "File $1 is newer than file $2"
		exit 0
fi

TS1=$(stat -c "%Y" $1)
TS2=$(stat -c "%Y" $2)

TS=$[ $TS2 - $TS1 ]

echo "Time diff is $TS seconds"
echo
echo "$(printf '%-40s' NAME) $(printf '%10s' CHANGE)   PER SEC"

X=1
LINE=$(printf '%sq;d' $X)
NAME1=$(sed $LINE $1 | awk '{print $1}')
VAL1=$(sed $LINE $1 | awk '{print $2}')
NAME2=$(sed $LINE $2 | awk '{print $1}')
VAL2=$(sed $LINE $2 | awk '{print $2}')

while [ -n "$NAME1" ]; do
	if [ "$NAME1" != "$NAME2" ]
		then
			echo "Files do not match (each line has to be the same variable in the two files)"
			exit 0
	fi
	VALDIFF=$[ $VAL2 - $VAL1 ]
	if [ $VALDIFF -ne 0 ]
		then
			VALDIFFPS=$(awk "BEGIN { print $VALDIFF/$TS }")
			echo "$(printf '%-40s' $NAME1) $(printf '%10s' $VALDIFF)   $VALDIFFPS"
	fi
	X=$[ $X + 1 ]
	LINE=$(printf '%sq;d' $X)
	NAME1=$(sed $LINE $1 | awk '{print $1}')
	VAL1=$(sed $LINE $1 | awk '{print $2}')
	NAME2=$(sed $LINE $2 | awk '{print $1}')
	VAL2=$(sed $LINE $2 | awk '{print $2}')
done
echo
echo "Done"