skip to Main Content

I’m just doing small bash script to get k8s node status whether its Ready/NotReady but below script is working fine but output is not user friendly, something i want produce the output in box format.

#!/bin/bash

# colors
blanc="33[1;37m"
gray="33[0;37m"
magento="33[0;35m"
red="33[1;31m"
green="33[1;32m"
amarillo="33[1;33m"
azul="33[1;34m"
rescolor="e[0m"

listNodes=$(kubectl get nodes | awk 'NR>1{print $1}')
readarray  node_arr <<<  $listNodes

ok=0
notok=0
echo -e "nSit Down and Wait  U1F602 :n"
for i in ${node_arr[@]}
do 
echo -ne "$i ... " 
status=$(kubectl get node $i | grep $i | awk '{print $2}')
    if [[ ! $status =~ ^Ready$|^NotReady$  ]]  ; then
        echo -e "e[1;31mOh Shit !"$rescolor""
    else
        echo -e "e[1;32mOK!"$rescolor""
    fi
done

echo -e "nNODE STATUS:n"
echo "+---------------+---------------+"
printf "|$green%-15s$rescolor|$red%-15s$rescolor|n" "Names" "Status"
echo "+---------------+---------------+"
printf "|%-15s|%-15s|n" "$ok" "$notok"
echo "+---------------+---------------+"
echo -e "n"

Current Output

Sit Down and Wait  😂 :

node-1.compute.internal ... OK!
node-2.compute.internal ... OK!

NODE STATUS:

+---------------+---------------+
|Names          |Status         |
+---------------+---------------+
|0              |0              |
+---------------+---------------+

Expected Output

Instead of printing output into the screen, I want make it inside box.

Sit Down and Wait  😂 :

node-1.compute.internal ... OK!
node-2.compute.internal ... OK!

NODE STATUS:

+---------------------------+---------------+
|Names                      |Status         |
+---------------------------+---------------+
|node-1.compute.internal    | Ready         |
+---------------------------+---------------+

2

Answers


  1. better to use the printf command with formatting options to align the columns and add borders! it is used with the format specifier %27s to align the Names column and %15s to align the Status column.

    #!/bin/bash
    
    # Colors
    blanc="33[1;37m"
    gray="33[0;37m"
    magento="33[0;35m"
    red="33[1;31m"
    green="33[1;32m"
    amarillo="33[1;33m"
    azul="33[1;34m"
    rescolor="e[0m"
    
    listNodes=$(kubectl get nodes | awk 'NR>1{print $1}')
    readarray node_arr <<< $listNodes
    
    ok=0
    notok=0
    echo -e "nSit Down and Wait  U1F602 :n"
    for i in "${node_arr[@]}"; do
      echo -ne "$i ... "
      status=$(kubectl get node "$i" | grep "$i" | awk '{print $2}')
      if [[ ! $status =~ ^Ready$|^NotReady$ ]]; then
        echo -e "e[1;31mOh Shit !"$rescolor""
      else
        echo -e "e[1;32mOK!"$rescolor""
      fi
    done
    
    echo -e "nNODE STATUS:n"
    echo "+---------------------------+---------------+"
    printf "|%-27s|%-15s|n" "Names" "Status"
    echo "+---------------------------+---------------+"
    for i in "${node_arr[@]}"; do
      status=$(kubectl get node "$i" | grep "$i" | awk '{print $2}')
      printf "|%-27s|%-15s|n" "$i" "$status"
    done
    echo "+---------------------------+---------------+"
    echo -e "n"
    
    Login or Signup to reply.
  2. Slightly changed from Freeman’s answer which gave an error due to use of quotation marks. This runs successfully on my Ubuntu/K8s server:

    #!/bin/bash
    
    # Colors
    blanc="33[1;37m"
    gray="33[0;37m"
    magento="33[0;35m"
    red="33[1;31m"
    green="33[1;32m"
    amarillo="33[1;33m"
    azul="33[1;34m"
    rescolor="e[0m"
    
    listNodes=$(kubectl get nodes | awk 'NR>1{print $1}')
    readarray node_arr <<< $listNodes
    
    ok=0
    notok=0
    echo -e "nSit Down and Wait  U1F602 :n"
    for i in ${node_arr[@]}; do
      echo -ne "$i ... "
      status=$(kubectl get node "$i" | grep "$i" | awk '{print $2}')
      if [[ ! $status =~ ^Ready$|^NotReady$ ]]; then
        echo -e "e[1;31mOh Shit !"$rescolor""
      else
        echo -e "e[1;32mOK!"$rescolor""
      fi
    done
    
    echo -e "nNODE STATUS:n"
    echo "+---------------------------+---------------+"
    printf "|%-27s|%-15s|n" "Names" "Status"
    echo "+---------------------------+---------------+"
    for i in ${node_arr[@]} ; do
      status=$(kubectl get node "$i" | grep "$i" | awk '{printf $2}')
      printf "|%-27s|%-15s|n" "$i" "$status"
    done
    echo "+---------------------------+---------------+"
    echo -e "n"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search