#! /bin/bash . /usr/src/m2/framework/bash_functions.sh k_config_details # ---- VARS ----- VERSION="1.0.2" SCRIPT_NAME="Core Failed Queries Import" SKIPPED_SQLS=0 FAILED_QUERIES_DIR=/var/log/m2/core_failed_queries CDR_DATES=/tmp/core_failed_queries_cdr_dates.txt DUPLICATE_CALLS_TMP_FILE=/tmp/core_failed_queries_tmp_import.sql # ---- SETTINGS ---- TEST_MODE=0 EXECUTE_SQLS=0 # should we really execute SQLs to DB? RESYNC_ES=1 # resync ES after SQLs import? CHECK_FOR_CDR_DUPLICATES=0 # before inserting cdr, check if it exists in DB? This slows down script a lot but ensures there are no duplicates # ---- FUNCTIONS ---- text_blue() { echo -e "\033[0;34m$1\033[0m" } text_green() { echo -e "\033[0;32m$1\033[0m" } mysql() { MYSQL_PWD="$DB_PASSWORD" /usr/bin/mysql -h "$ES_DB_HOST" -u "$DB_USERNAME" $P_OPT "$DB_NAME" "$@" } skip_file() { file=$1 file_basename=$(basename $file) SKIPPED_SQLS=$(expr $SKIPPED_SQLS + 1) if [ "$TEST_MODE" == "1" ]; then return 0 fi if [[ $file_basename != *"skipped"* ]]; then mv $file ${file::${#file}-4}_skipped.sql fi } cdr_file_import() { file=$1 file_basename=$(basename $file) cat $file # Check for syntax errors if cat $file | grep -F "You have an error in your SQL syntax" &> /dev/null; then report "SQL file $file_basename contains syntax error, skipping this file" 1 skip_file $file else # Check if CDR is not duplicate if [ "$CHECK_FOR_CDR_DUPLICATES" == "1" ] && [ "$EXECUTE_SQLS" == "1" ]; then mysql -e "CREATE TABLE IF NOT EXISTS calls_failed_cdr_import LIKE calls" mysql -e "TRUNCATE table calls_failed_cdr_import" cp -f $file $DUPLICATE_CALLS_TMP_FILE sed -i "s|INSERT INTO calls|INSERT INTO calls_failed_cdr_import|g" $DUPLICATE_CALLS_TMP_FILE mysql < $DUPLICATE_CALLS_TMP_FILE duplicates=$(mysql -sNe "SELECT COUNT(*) FROM calls_failed_cdr_import JOIN calls ON calls_failed_cdr_import.uniqueid = calls.uniqueid") rm -fr $DUPLICATE_CALLS_TMP_FILE if (( duplicates > 0 )); then report "Duplicates found when trying to insert CDR file $file_basename, skipping this file" 1 skip_file $file return 1 fi fi # Insert CDR to database if [ "$EXECUTE_SQLS" == "1" ]; then mysql_resp=$(mysql -vv < $file | grep "Query OK.*") # Check response if [[ $mysql_resp == *"Query OK"* ]]; then report "SQL file successfully imported" 0 # Fetch dates from CDR SQL file cat $file | grep -Po "\d\d\d\d\-\d\d\-\d\d \d\d:\d\d:\d\d" >> $CDR_DATES rm -fr $file &> /dev/null else report "Failed to import SQL file, skipping it" 1 skip_file $file fi fi fi } users_update_file_import() { file=$1 file_basename=$(basename $file) cat $file # Check for syntax errors if cat $file | grep -F "You have an error in your SQL syntax" &> /dev/null; then report "SQL file $file_basename contains syntax error, skipping this file" 2 skip_file $file else # Execute sql if [ "$EXECUTE_SQLS" == "1" ]; then mysql_resp=$(mysql -vv < $file | grep "Query OK.*") # Check response if [[ $mysql_resp == *"Query OK"* ]]; then report "SQL file successfully imported" 0 rm -fr $file &> /dev/null else report "Failed to import SQL file, skipping it" 1 skip_file $file fi fi fi } process_file() { file=$1 file_basename=$(basename $file) if [[ $file_basename == *"skipped"* ]]; then skip_file $file elif [[ $file_basename == *"cdr_query"* ]]; then report "Processing $(text_blue 'CDR file') $file_basename" 3 cdr_file_import $file elif [[ $file_basename == *"users_update_query"* ]]; then report "Processing $(text_blue 'Users update') file $file_basename" 3 users_update_file_import $file else report "File $file_basename is not configured for import, skipping it" 2 skip_file $file fi } es_resync_cdr_data() { report "Elasticsearch resync is required" 3 # Get min/max dates dates=$(awk '{ if($1 > MAX) { MAX=$1} if(($1 < MIN) || MIN =="") {MIN = $1}} END{print MIN" "MAX }' $CDR_DATES) date_from=$(echo $dates | awk '{ print $1 }') date_till=$(echo $dates | awk '{ print $2 }') # Check if date variable length is correct if [ "${#date_from}" != "10" ]; then report "Variable date_from length is incorrect: $date_from (len: ${#date_from}), should be 10!" 1 report "Elasticsearch will not be resynced" 1 return 1 fi if [ "${#date_till}" != "10" ]; then report "Variable date_till length is incorrect: $date_till (len: ${#date_till}), should be 10!" 1 report "Elasticsearch will not be resynced" 1 return 1 fi report "Resync period $date_from - $date_till" 3 report "Executing Elasticsearch resync" 3 report "/usr/src/m2/elasticsearch/es_resync_by_day.sh $date_from $date_till" 3 /usr/src/m2/elasticsearch/es_resync_by_day.sh $date_from $date_till } report "Starting script $(text_green "$SCRIPT_NAME"), $(date)" 3 if [ "$TEST_MODE" == "1" ]; then report "TEST MODE enabled, SQL files will not be executed to database" 8 fi PATH=$PATH:/usr/sbin:/sbin # Maybe another instance is already running? pidof -o %PPID -x $0 > /dev/null && echo "Script $0 is already running" && exit 1 # Set DB variables set_database_variables # Before doing anything, check if failed queries dir exists if [ ! -e $FAILED_QUERIES_DIR ]; then report "Directory $FAILED_QUERIES_DIR does not exist, exiting..." exit 0 fi # Reset files rm -fr $CDR_DATES &> /dev/null rm -fr $DUPLICATE_CALLS_TMP_FILE &> /dev/null # Check if there are subdirectories if [ "$(ls -d $FAILED_QUERIES_DIR/*/ 2>/dev/null | wc -l)" == "0" ]; then report "No subdirectories found in $FAILED_QUERIES_DIR (probably no failed queries), exiting..." 3 exit 0 fi # Iterate through every dir in main failed queries dir for dir in $(ls -d $FAILED_QUERIES_DIR/*/); do report "-- Checking directory: $(text_blue $(basename $dir))" 3 if [ -z "$(ls -A $dir)" ]; then report "-- Directory $(text_blue $(basename $dir)) is empty, deleting it" 3 if [ "$TEST_MODE" == "0" ]; then rm -fr $dir &> /dev/null fi else for file in $(ls $dir/*.sql); do process_file $file done report "-- Skipped SQL files in directory $(text_blue $(basename $dir)): $SKIPPED_SQLS" 3 SKIPPED_SQLS=0 fi done # Should we resync ES? if [ -s $CDR_DATES ] && [ "$RESYNC_ES" == "1" ] && [ "$EXECUTE_SQLS" == "1" ]; then es_resync_cdr_data fi # Delete tmp files rm -fr $CDR_DATES &> /dev/null rm -fr $DUPLICATE_CALLS_TMP_FILE &> /dev/null report "Script finished" 0