recipe_gentoo/recipes/recipe_check_ipam_config.sh
2019-03-31 10:57:45 +02:00

54 lines
2.1 KiB
Bash
Executable file

echo "-------------------------------------------------"
echo -e "---------------- ${BLUE}CHECK IPAM CONFIG${NC} --------------"
echo -e "-------------------------------------------------\n"
RES_AUTHENT=$(${CURL} -k -X POST --user ${USER_IPAM}:${PASSWORD_IPAM} ${URL}/user/ 2>/dev/null)
CODE_RETOUR_RES_AUTHENT=$(echo ${RES_AUTHENT} | jq '.code')
TOKEN=$(echo ${RES_AUTHENT} | jq -r '.data.token')
# Check if IPAM API request succeded
# If IPAM API connection FAILED
if [ ${CODE_RETOUR_RES_AUTHENT} != 200 ] || [ ! -n "${TOKEN}" ]; then
echo -e "${RED}Connection to IPAM API FAILED or empty TOKEN : check KO${NC}\n"
# If IPAM API connection SUCCEDED
else
# Get list of IP to check
# WARNING, if one of variable is empty, ALL IP may be checked (but we just want to check our public ranges and admin IPs)
IPS_TO_CHECK=$(ip addr show scope global | awk '/inet/ { sub(/\/.*$/, "", $2); print $2 }' | sort | uniq | grep -e "^${IPV4_ADMIN_NETWORK}" -e "^${IPV6_ADMIN_NETWORK}" -e "^${RANGE_IPV4_1_NETWORK}" -e "^${RANGE_IPV4_2_NETWORK}" -e "^${RANGE_IPV6_NETWORK}" )
# Return Code
RC=$?
# If no IP to check found
if [ $RC -ne 0 ]
then
IPAM_CONFIG_CHECK_FAILED="${IPAM_CONFIG_CHECK_FAILED} No IP to check in IPAM, check the IP configuration"
echo -e "${RED}NO IP to check in IPAM : check KO${NC}\n"
else
# Check if all found IP are recorded in IPAM
for ip_to_check in ${IPS_TO_CHECK}
do
echo -e "Check if ${BLUE}${ip_to_check}${NC} is recorded in IPAM"
# Request one IP
REQUEST_ONE_IP=$(curl -k --header "Content-type: application/x-www-form-urlencoded" --header "token: ${TOKEN}" -X GET "${URL}/addresses/search/fd00:1e02:40::1/" 2>/dev/null | jq -r .data[].ip &>/dev/null)
# Return Code
RC=$?
# If IP not found in IPAM
if [ $RC -ne 0 ]
then
IPAM_CONFIG_CHECK_FAILED="${IPAM_CONFIG_CHECK_FAILED} ${ip_to_check} NOT FOUND in IPAM or problem with API connection ;"
echo -e "${RED}${ip_to_check} NOT FOUND in IPAM or problem with API connection : check KO${NC}\n"
# If IP found in IPAM
else
echo -e "${GREEN}${ip_to_check}FOUND in IPAM : check OK${NC}\n"
fi
done
fi
fi