#!/bin/bash
PATH="/bin:/sbin:/usr/bin:/usr/sbin"

#
# Copyright (C) 2017 Tarantool AUTHORS: please see the [AUTHORS](AUTHORS.md) file.
#

INSTANCE_NAMES=$( systemctl --no-pager --no-legend list-units 'tarantool@*' | cut -f 1 -d ' '| sed 's|tarantool@||; s|\.service||' )
EXIT_CODE=0

get_metric_val(){
	INSTANCE_NAME=$1
	METRIC=$2

	VAL=$( echo "${METRIC}" | tarantoolctl enter $INSTANCE_NAME 2>/dev/null | grep '^- ' | cut -f 2-255 -d ' ' )
	if [[ -z $VAL ]]; then
		echo "can't get $METRIC from $INSTANCE_NAME" >&2
		return 1
	fi

	echo $VAL
}

get_metric_vals(){
	INSTANCE_NAME=$1
	METRIC=$2

	VAL=$( echo "${METRIC}" | tarantoolctl enter $INSTANCE_NAME 2>/dev/null | grep ' - ' | cut -c5- )
	if [[ -z $VAL ]]; then
		echo "can't get $METRIC from $INSTANCE_NAME" >&2
		return 1
	fi

	echo $VAL
}

EXIT_CODE=0

for INSTANCE_NAME in $INSTANCE_NAMES; do

	# check arena
	VAL=$( get_metric_val $INSTANCE_NAME "tonumber(box.slab.info().arena_used_ratio:sub(1,-2))" )
	EXIT_CODE=$(( $EXIT_CODE | $? ))
	if [[ ${VAL%%.*} -gt 85 ]]; then
		echo "$INSTANCE_NAME arena_used_ratio more than 85%"
		EXIT_CODE="1"
	fi
	VAL=""

	# check status
	VAL=$( get_metric_val $INSTANCE_NAME "box.info().status" )
	EXIT_CODE=$(( $EXIT_CODE | $? ))
	if [[ "$VAL" != "running" ]]; then
		echo "instance $INSTANCE_NAME - status not running"
		EXIT_CODE="1"
	fi

	# replication_status
	VAL=$( get_metric_vals $INSTANCE_NAME "require('stat').check_replica { exclude = {'follow'} }" )
	EXIT_CODE=$(( $EXIT_CODE | $? ))
	for UUID in $VAL; do
	    echo "instance $INSTANCE_NAME replica $UUID - replication status is not equal \"follow\""
	    EXIT_CODE="1"
	done

	VAL=$( get_metric_vals $INSTANCE_NAME "require('stat').check_replica { include = {'follow'}, lag = 10 }" )
    EXIT_CODE=$(( $EXIT_CODE | $? ))
    for UUID in $VAL; do
	    echo "instance $INSTANCE_NAME replica $UUID - replication lag more 10s ($VAL)"
	    EXIT_CODE="1"
	done

done
exit $EXIT_CODE
