#!/bin/bash
for node in `kubectl get node | grep node | awk '{print $1}'`
do
total_cpu=`kubectl describe node $node | grep cpu | awk '{print $2}' | sed -n '1p'`
used_cpuM=`kubectl describe node $node | grep "cpu " | awk '{print $2}'| cut -d 'm' -f 1`
used_cpu=`expr $used_cpuM / 1024`
used_cpu_ratio=`kubectl describe node $node | grep "cpu " | awk '{print $3}'`
total_memK=`kubectl describe node $node | grep " memory:" | awk '{print $2}' | sed -n '1p' | cut -d 'K' -f 1`
total_mem=`expr $total_memK / 1024 / 1024`
used_memMm=`kubectl describe node $node | grep " memory " | awk '{print $2}'`
Mm=`echo $used_memMm | grep M | wc -l`
if [ $Mm == 0 ];then
if [ `echo $used_memMm | grep m | wc -l` == 0 ];then
used_mem=`expr $used_memMm / 1024 / 1024 / 1024`
else
used_mem_Mm=`echo $used_memMm | cut -d 'm' -f 1`
used_mem=`expr $used_mem_Mm / 1024 / 1024 / 1024 / 1024`
fi
else
used_mem_Mm=`echo $used_memMm | cut -d 'M' -f 1`
used_mem=`expr $used_mem_Mm / 1024`
fi
used_mem_ratio=`kubectl describe node $node | grep " memory " | awk '{print $3}'`
echo "--------"
echo "$node"
echo "cpu: [ total:$total_cpu ] [ used:$used_cpu $used_cpu_ratio ]"
echo "mem: [ total:$total_mem ] [ used:$used_mem $used_mem_ratio ]"
done