#! /bin/bash . /usr/src/m2/framework/bash_functions.sh VERSION="1.0.0" if [ -e /etc/mor/system.conf ]; then SYSTEM="mor" else SYSTEM="m2" fi # Configuration variables CHECK_LAST=7 RESYNC_TIMEOUT_MINUTES=30 TEST_MODE=0 DEBUG=0 # Global variables ES_SYNC_CONTROL=$(sed 's/ //g' /etc/$SYSTEM/system.conf | awk -F"=" '/es_sync_control/{print $2}') MAIN_DB_IP=`sed 's/ //g' /etc/$SYSTEM/system.conf | grep MAIN_DB_IP | awk -F"=" '{print $2}'` ES_DB_HOST="" CURRENT_DATETIME="" HOURS_ARRAY= mysql() { DATA=$(MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$ES_DB_HOST" -u "$DB_USERNAME" $P_OPT "$DB_NAME" "$@") echo $DATA } mysql_date_format() { echo $1 | sed 's|T| |' } check_if_another_sync_is_running() { # Check if another sync is running es_sync_is_running=$(MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$ES_DB_HOST" -u "$DB_USERNAME" $P_OPT "$DB_NAME" -e "show processlist" | grep -F "/*ES interval sync*/" | wc -l) if [ "$es_sync_is_running" != "0" ]; then report "Elasticsearch sync interval is already running, try again later" 2 exit 0 else # Double check in 3 seconds sleep 3 es_sync_is_running=$(MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$ES_DB_HOST" -u "$DB_USERNAME" $P_OPT "$DB_NAME" -e "show processlist" | grep -F "/*ES interval sync*/" | wc -l) if [ "$es_sync_is_running" != "0" ]; then report "Elasticsearch sync interval is already running, try again later" 2 exit 0 fi fi } ############################################################################### #################################### MAIN ##################################### ############################################################################### PATH=$PATH:/usr/sbin:/sbin:/usr/bin # Maybe another instance is already running? pidof -o %PPID -x $0 > /dev/null && echo "Script $0 is already running" && exit 1 if [[ -z $ES_SYNC_CONTROL ]] || [[ "$ES_SYNC_CONTROL" != "1" ]]; then report "ES sync control script is not enabled. Exiting" 3 exit 0 fi if ! /bin/ps auxf | grep -F java | grep -Fq elasticsearch; then report "ES is not runnnig. Exiting..." 3 exit 0 fi # Check arguments for arg in "$@" do arg_lower=`echo "$arg" | awk '{print tolower($0)}'` # Show debug messages? if [ "$arg_lower" == "debug" ]; then DEBUG=1 fi # Test mode? if [ "$arg_lower" == "test" ]; then TEST_MODE=1 fi done report "Starting ES Sync Check By Hour script, version: $VERSION" 0 set_database_variables if [ "$MAIN_DB_IP" != "" ]; then ES_DB_HOST=$MAIN_DB_IP else ES_DB_HOST=$DB_HOST fi if [ "$TEST_MODE" == "1" ]; then report "TEST MODE enabled, only searching for mismatches" 8 fi if [ "$TEST_MODE" == "0" ]; then check_if_another_sync_is_running fi report "Current datetime: $(date +"%Y-%m-%d %H:%M:%S")" 3 report "Checking last $CHECK_LAST hours" 3 # Format hours array for hour in $(seq 1 $CHECK_LAST); do HOURS_ARRAY="$HOURS_ARRAY $(date +'%Y-%m-%dT%H:00:00' -d "$(expr $hour + 1) hour ago")" done for hour in $HOURS_ARRAY; do # Format hours from=$hour till=$(echo $hour | sed 's|:00:00|:59:59|') # Get counts db_count=$(mysql -sNe "SELECT count(id) FROM calls WHERE calldate BETWEEN '$(mysql_date_format $from)' AND '$(mysql_date_format $till)'") es_count=$(curl -s -XGET "localhost:9200/${DB_NAME}/calls/_count?q=calldate:\[$from+TO+$till\]&pretty" | grep count | cut -d"," -f1 | cut -d" " -f5) # Check if db count is a number if ! [[ "$db_count" =~ ^[0-9]+$ ]] ; then report "DB count is not a number: [$db_count]" 1 exit 1 fi # Check if es count is a number if ! [[ "$es_count" =~ ^[0-9]+$ ]] ; then report "ES count is not a number: [$es_count]" 1 exit 1 fi if [ "$db_count" == "$es_count" ]; then report "$(mysql_date_format $from) - $(mysql_date_format $till) [ES: $es_count, DB: $db_count, MATCHES]" 0 else report "$(mysql_date_format $from) - $(mysql_date_format $till) [ES: $es_count, DB: $db_count, MISMATCH]" 1 if [ "$TEST_MODE" == "0" ]; then report "Resyncing $(mysql_date_format $from) - $(mysql_date_format $till)" 3 # Resync interval elasticsearch resync interval "$(mysql_date_format $from)" "$(mysql_date_format $till)" &> /dev/null start_time=$(date +%s) # Wait till sync is finished while [ $es_count -lt $db_count ] && [ $db_count -gt 0 ]; do current_time=$(date +%s) total_seconds_running=$(expr $current_time - $start_time) minutes_running=$(expr $total_seconds_running / 60) seconds_running=$(expr $total_seconds_running - $minutes_running \* 60) if [ $minutes_running -gt $RESYNC_TIMEOUT_MINUTES ]; then report "Tried to resync but failed to finish in $RESYNC_TIMEOUT_MINUTES minutes (ES: $es_count, DB: $db_count)" 1 report "Exiting script..." 1 exit 1 fi prev_es_count=$es_count es_count=$(curl -s -XGET "localhost:9200/${DB_NAME}/calls/_count?q=calldate:\[$from+TO+$till\]&pretty" | grep count | cut -d"," -f1 | cut -d" " -f5) if [ "$prev_es_count" != "$es_count" ] || [ "$DEBUG" == "1" ]; then report " -- progress $es_count/$db_count (elapsed time ${minutes_running}min ${seconds_running}sec)" 3 fi sleep 3 done fi fi done report "Script finished" 0