#! /bin/bash # script compares ES vs MySQL calls day by day # usage: es_sync_check.sh START_DAY END_DAY example: es_sync_check.sh 2020-07-20 2020-07-30 . /usr/src/m2/framework/bash_functions.sh . /usr/src/m2/framework/settings.sh VERSION="1.1.1" SCRIPT_NAME="ES vs MySQL Sync Calls Compare" DB_PASSWORD=`cat /etc/m2/system.conf | grep dbsecret | awk '{print $3}'` DB_USERNAME=`cat /etc/m2/system.conf | grep dbuser | awk '{print $3}'` DB_NAME=`cat /etc/m2/system.conf | grep dbname | awk '{print $3}'` DB_HOST=`cat /etc/m2/system.conf | grep dbhost | awk '{print $3}'` # ---- MAIN ----- k_start # check for mysql psw notice /usr/bin/mysql -h "$DB_HOST" -u "$DB_USERNAME" --password="$DB_PASSWORD" "$DB_NAME" -e "select id FROM calls LIMIT 1\G;" > /tmp/m 2>&1 if cat /tmp/m | grep "Using a password"; then mysql_silent=0 else mysql_silent=1 fi rm -fr /tmp/m # get date period if [ -z "$1" ]; then date_start=`/usr/bin/mysql -h "$DB_HOST" -u "$DB_USERNAME" --password="$DB_PASSWORD" "$DB_NAME" -e "select min(calldate) from calls\G;" | grep calldate | cut -d" " -f2` else date_start="$1" fi if [ -z "$2" ]; then date_end=`/usr/bin/mysql -h "$DB_HOST" -u "$DB_USERNAME" --password="$DB_PASSWORD" "$DB_NAME" -e "select max(calldate) from calls\G;" | grep calldate | cut -d" " -f2` else date_end="$2" fi time_start="00:00:00" time_end="23:59:59" echo report "Checking period $date_start $date_end" 3 echo echo -e "Day\t\tES\tMySQL" #exit # run through days d=$date_start d_end=$(date -I -d "$date_end + 1 day") while [ "$d" != $d_end ]; do date_current=$d es_count=`curl -s -XGET "localhost:9200/${DB_NAME}/calls/_count?q=calldate:\[$date_current\T$time_start+TO+$date_current\T$time_end\]&pretty" | grep count | cut -d"," -f1 | cut -d" " -f5` if [ $mysql_silent -eq 1 ]; then mysql_count=`/usr/bin/mysql -h "$DB_HOST" -u "$DB_USERNAME" --password="$DB_PASSWORD" "$DB_NAME" -e "select count(id) from calls where calldate between '$date_current $time_start' and '$date_current $time_end'\G;" | grep count | cut -d" " -f2` else mysql_count=`MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$DB_HOST" -u "$DB_USERNAME" "$DB_NAME" -e "select count(id) from calls where calldate between '$date_current $time_start' and '$date_current $time_end'\G;" | grep count | cut -d" " -f2` fi if [ "$es_count" = "$mysql_count" ]; then warning="\e[32mOK\e[39m" echo -e "$d\t$es_count\t$mysql_count\t$warning" else warning="\e[31mMISMATCH!\e[39m" echo -e "$d\t$es_count\t$mysql_count\t$warning" #echo -e "To resync this period run this command \e[31mAND WAIT AT LEAST 15-30 minutes\e[39m, then recheck" #echo -e "elasticsearch resync interval \"$d 00:00:00\" \"$d 23:59:59\"" #sleep #exit 0 fi d=$(date -I -d "$d + 1 day") done echo report "Script $SCRIPT_NAME completed." 0 echo