DEV Community

hatem ben tayeb
hatem ben tayeb

Posted on


Bash: writing a simple pod checker

I was working with Kubernetes and I just want to check my pods in the current workspace, in a funny way πŸ˜…, I will use bash for just one reason, it's the Linux native languages no need for other languages and libraries

Actually, I was inspired by a cool tool called popeye, you can find it here.

Make sure you have Kubectl installed and an existing cluster as well, here is my implementation

  • Define your favorite colors
Enter fullscreen mode Exit fullscreen mode

You can find more about colors in bash here.

  • Get list pods into an Array
listPods=$(kubectl get po | awk 'NR>1{print $1}')
#echo "$listPods"
readarray  arr <<<  $listPods
Enter fullscreen mode Exit fullscreen mode

NR>1 will skip the first line and print $1 will print the first words (separated by a space) on all lines.

  • Looping over the array and check the status
echo -e "\nSit Down and Wait  \U1F602 :\n"
for i in ${arr[@]}
echo -ne "$i ... " 
status=$(kubectl get po $i | grep $i | awk '{print $3}')
    if [[ ! $status =~ ^Running$|^Completed$  ]]  ; then
        echo -e "\e[1;31mOh Shit !"$rescolor""
        notify-send "Pods Health" "$i was  FUCKED" -t 10000 
        let notok=notok+1
        echo -e "\e[1;32mOK!"$rescolor""
        #notify-send "Pod $i Is Good :)"
      let ok=ok+1
Enter fullscreen mode Exit fullscreen mode

The ok and notok are used to count the number of the running/not running pods , the ${arr[@]} prints out the whole array, the notify-send will create a notification on your system is one of the pods are fked up** .

  • Print out the summary
echo -e "\nSTATS:\n"
echo "+---------------+---------------+"
printf  "|$green%-15s$rescolor|$red%-15s$rescolor|\n" "Healthy Pods" "Unhealthy Pods"
echo "+---------------+---------------+"
printf  "|%-15s|%-15s|\n" "$ok" "$notok"
echo "+---------------+---------------+"
echo -e "\n"
Enter fullscreen mode Exit fullscreen mode
  • Run the script

Download the scripts with CURL :

curl --output
chmod +x
Enter fullscreen mode Exit fullscreen mode
  • Demo


Original blog:

The scripts don't do much but it was a result of a boring 3 hours on this pandemic πŸ₯²

Top comments (0)

You guessed it

The most popular item in our shop is the stickers. Grab a pack today (with free shipping)!