<p>I am trying to list the cpu and memory usage of all the nodes in kubernetes and echo &quot;load exceed&quot; if the memory or cpu limit exceed some digit. I am listing the cpu and memory this command but how to apply the logic to echo that <code>load exceeded</code></p>
<p><code>kubectl describe nodes | grep -A 3 &quot;Resource .*Requests .*Limits&quot;</code></p>
<p>Output:</p>
<pre><code>Resource                    Requests      Limits
  --------                    --------      ------
  cpu                         360m (18%)    13 (673%)
  memory                      2800Mi (84%)  9Gi (276%)
--
  Resource                    Requests          Limits
  --------                    --------          ------
  cpu                         1430m (74%)       22300m (1155%)
  memory                      2037758592 (58%)  15426805504 (441%)
--
  Resource                    Requests     Limits
  --------                    --------     ------
  cpu                         240m (12%)   5 (259%)
  memory                      692Mi (20%)  3Gi (92%)
--
  Resource                    Requests      Limits
  --------                    --------      ------
  cpu                         930m (48%)    3100m (160%)
  memory                      1971Mi (59%)  3412Mi (102%)
--
  Resource                    Requests     Limits
  --------                    --------     ------
  cpu                         270m (13%)   7 (362%)
  memory                      922Mi (27%)  4Gi (122%)
--
  Resource                    Requests      Limits
  --------                    --------      ------
  cpu                         530m (27%)    5 (259%)
  memory                      1360Mi (40%)  3Gi (92%)
--
  Resource                    Requests      Limits
  --------                    --------      ------
  cpu                         440m (22%)    5250m (272%)
  memory                      1020Mi (30%)  3884Mi (116%)

</code></pre>

<p>Try this to extract the attributes you want by matching a regex pattern</p>
<pre class="lang-sh prettyprint-override"><code>kubectl describe nodes | grep -E -A 3 &quot;Resource|Requests|Limits&quot;
</code></pre>
<p>You can extend it like this to extract <code>CPU</code> or <code>MEMORY</code> values</p>
<pre><code>grep -E -A 3 &quot;Resource|Requests|Limits&quot; | awk '/cpu/{print $2}'
</code></pre>
<p><strong>EDIT</strong>
To print if the limit is exceeded or not (example for cpu exceeding 1),</p>
<pre><code>grep -E -A 3 &quot;Limits&quot; | awk '/cpu/{if($2 &gt; 1) print &quot;Limit Exceeded&quot;; else print &quot;Within Limits&quot;;}'
</code></pre>
<p>You will have to do</p>
<pre><code>| awk '/memory/{print $2}' | awk -vFS=&quot;&quot; '{print $1}'
</code></pre>
<p>to extract the number from memory before applying the condition as it gives the metric <code>G</code> with the number.</p>
<p><strong>EDIT 2</strong></p>
<p>This can give you the ratio based on the provided output of your grep command.</p>
<p>CPU</p>
<pre><code>| awk '/cpu/{print $1,$2,$4}' | awk '{if($3 ~ /[0-9]$/) {print $1,$2/($3*1000)} else {print $1,$2/$3}}'
</code></pre>
<p>Output</p>
<pre><code>cpu 0.0276923
cpu 0.0641256
cpu 0.048
cpu 0.3
cpu 0.0385714
cpu 0.106
cpu 0.0838095
</code></pre>
<p>MEMORY</p>
<pre><code>| awk '/memory/{print $1,$2,$4}' | awk '{if($3 ~ /Gi$/) {print $1,$2/($3*1024)} else {print $1,$2/$3}}'
</code></pre>
<p>Output</p>
<pre><code>memory 0.303819
memory 0.132092
memory 0.22526
memory 0.577667
memory 0.225098
memory 0.442708
memory 0.262616
</code></pre>
