First to be cleared. Commitment for me is agreed with voice operator where I am obligated to send to them some amount of minutes. After that I need to change to another termination gateway. This is my monthly job and I am curious it is possible to done by automatic.
You could write a bash script that queries the DB every 1 minute and if the target minute is reached, then DO THE SWITCH… (check out sample script below)
To do the switching between termination gateways, you could simply create a Routing Plan having Max rerouting attempts == 1, then give higher priority to the dialpeers of the 1st Termination GW/Contractor. If Total minutes is reached, simply disabling the 1st GW/Contractor causes the new call attempts to be routed via the 2nd GW/Contractor.
THE SCRIPT BELOW WAS NOT TESTED BUT GIVES YOU A FRAMEWORK TO START FROM
#!/bin/bash
export PGPASSWORD='POSTGRES_PSSWD'
DB_CDR="cdr"
current_date=$(date "+%Y %m %d")
while getopts c:g:t: option; do
case "${option}" in
c) contractor_id=${OPTARG} ;;
g) gateway_id=${OPTARG} ;;
t) minutes_treshold=${OPTARG} ;;
esac
done
update_contractor() {
# UPDATE
curl --location --request PUT "http://127.0.0.1:6666/api/rest/admin/contractors/{$1}" \
--header "Content-Type: application/vnd.api+json" \
--header "Authorization: Bearer $2" \
--data-raw "{
\"data\": {
\"type\": \"contractors\",
\"id\": $1,
\"attributes\": {
\"enabled\": $3
}
}
}"
}
jwt_token=$(curl --location --request POST 'http://127.0.0.1:6666/api/rest/admin/auth/' \
--header 'Content-Type: application/json' \
--data-raw '{
"auth": {
"username": "api_user",
"password": "api_password"
}
}' | jq '.jwt' | sed -e 's/"//g')
# STEP 1
total_minutes=$(psql -X -A -d $DB_CDR -U $DB_CDR -h localhost -p 5432 -t -c "SELECT row_to_json(t3) FROM (SELECT SUM(duration) AS total_minutes FROM cdr.cdr WHERE TO_CHAR(time_start,'YYYY MM DD')=$current_date AND customer_id=$contractor_id ) t3")
if [[ "$total_minutes" -ge "$minutes_treshold" ]];
then
update_contractor "$contractor_id" "$jwt_token" false
fi