#!/bin/bash
# Find out how many mongo connections does a deployment creates
# define variables
export PATH=$PATH:/usr/local/bin
CONTEXT="$1"
DEPLOYMENT="$2"
CONNECTIONS_COUNTER=0
REQUESTED_PORT=27017
# switch kubectl context
kubectl config set-context ${CONTEXT}
CURRENT_CONTEXT=$(kubectl config current-context | cut -d"_" -f2)
# create an array of pods for the requested deployment
read -a DEPLOYMENT_PODS <<< $(kubectl get pods | grep ${DEPLOYMENT} | cut -d" " -f1 | tr '\n' ' ')
# loop through the pods and exec to get the current amount of open ports to mongo
for pod in ${DEPLOYMENT_PODS[@]}; do
TOTAL_PER_POD=$(kubectl exec -it $pod -- grep -v "rem_address" /proc/net/tcp | awk 'function hextodec(str,ret,n,i,k,c){
ret = 0
n = length(str)
for (i = 1; i <= n; i++) {
c = tolower(substr(str, i, 1))
k = index("123456789abcdef", c)
ret = ret * 16 + k
}
return ret
} {x=hextodec(substr($3,index($3,":")-2,2)); for (i=5; i>0; i-=2) x = x"."hextodec(substr($3,i,2))}{print x":"hextodec(substr($3,index($3,":")+1,4))}' | grep ${REQUESTED_PORT} | wc -l)
((CONNECTIONS_COUNTER=CONNECTIONS_COUNTER+TOTAL_PER_POD))
done
echo "`date +%T-%d-%m-%y` - ${#DEPLOYMENT_PODS[@]} ${DEPLOYMENT} pods in ${CURRENT_CONTEXT} have ${CONNECTIONS_COUNTER} open connections to mongo" >> ${CURRENT_CONTEXT}_${DEPLOYMENT}.log
Created
March 24, 2022 19:34
-
-
Save denzhel/f29ce8372acec6f564f005077630339b to your computer and use it in GitHub Desktop.
kubernetes get open ports per pod
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment