Looking to take Nutanix for a free test drive?
Click on the following to see what it's all about:

Get Cluster Realtime CPU Memory Usage without Prism

September 2, 2020

by Tank Pang

Intended Audience Level: Beginner/Intro

Code Sample Type: Snippet

Nutanix Technologies: Prism Element

Minimum Product Version: 5.5

Script/Code Language: Bash Shell

REST API Sample? No

REST API Version: N/A

Get the cluster realtime CPU and memory usage information which are same as Prism shows.

Code Sample Details

Note this section may be empty, if additional code sample details are not available.

Note: This code sample is intended for use within a Nutanix CVM (Controller Virtual Machine) SSH session.

#!/bin/bash
#
#.notes
#################################################################################
    #   Get cluster realtime CPU and memory usage information
    #   Script Version: 1.0.0
#################################################################################
#.prerequisites
#    1. AOS 5.5 or above.
#    2. Run on bash shell.
#.synopsis
#    Get the cluster realtime CPU and memory usage information which are same as Prism shows.
#.disclaimer
#   This code is intended as a standalone example.  Subject to licensing restrictions defined on nutanix.dev, this can be downloaded, copied and/or modified in any way you see fit.
#
#   Please be aware that all public code samples provided by Nutanix are unofficial in nature, are provided as examples only, are unsupported and will need to be heavily scrutinized and potentially modified before they can be used in a production environment.  All such code samples are provided on an as-is basis, and Nutanix expressly disclaims all warranties, express or implied.
#
#   All code samples are © Nutanix, Inc., and are provided as-is under the MIT license. (https://opensource.org/licenses/MIT)
#
####################################################################

#####################################################
#  Get cluster ID
#####################################################
cluster_id=`ncli cluster info | grep "Cluster Id" | sed -e 's/.*://'i`

#####################################################
#  Get cluster memory usage and transfer to proper unit
#####################################################
echo "=====Getting Cluster Memory Usage==========="

#Get memory usage value string
mem_usage_value=`arithmos_cli master_get_real_time_stats entity_type=cluster entity_id=$((cluster_id)) field_name=hypervisor_memory_usage_ppm | grep value | awk '{print $2}'`

#Translate memory usage to percentage
mem_usage_perct=`printf "%.2f" $(echo "$mem_usage_value/10000" | bc -l)`

#Get memory capacity value string
cls_mem_byte=`arithmos_cli master_get_stats entity_type=cluster entity_id=$((cluster_id)) | grep memory_capacity_bytes | awk '{print $2}'`

#Translate memory capacity value to TB format
cls_mem_tb=`printf "%.2f" $(echo "$cls_mem_byte/(1024^4)" | bc -l)`

#If memory capacity TB format less than 1, show as GB format. Otherwise keep it as TB
#Display memory usage and capacity result
if [ `echo "$cls_mem_tb < 1" | bc` -eq 1 ]; then
 cls_mem_gb=`printf "%.2f" $(echo "$cls_mem_byte/(1024^3)" | bc -l)`
 echo "Cluster Realtime Memory Usage: $mem_usage_perct% of $cls_mem_gb GB"
else
 echo "Cluster Realtime Memory Usage: $mem_usage_perct% of $cls_mem_tb TB"
fi
echo

#####################################################
#  Get cluster CPU usage and transfer to proper unit
#####################################################
echo "=====Getting Cluster CPU Usage==========="

#Get CPU usage value string
cpu_usage_value=`arithmos_cli master_get_real_time_stats entity_type=cluster entity_id=$((cluster_id)) field_name=hypervisor_cpu_usage_ppm | grep value | awk '{print $2}'`

#Translate CPU usage as percentage
cpu_usage_perct=`printf "%.2f" $(echo "$cpu_usage_value/10000" | bc -l)`

#Get CPU capacity values of each node and summarize
cls_cpu_hz=`arithmos_cli master_get_entities  entity_type=node requested_field_name_list=cpu.capacity_hz | grep attribute_value_int | awk '{print $2}' | awk '{SUM += $1} END {print SUM}'`

#Translate CPU capacity into GHz format
cls_cpu_ghz=`printf "%.2f" $(echo "$cls_cpu_hz/(1000^3)" | bc -l)`

#Display CPU usage and capacity result
echo "Cluster Realtime CPU Usage: $cpu_usage_perct% of $cls_cpu_ghz GHz"

Nutanix CALM DSL LAB 1.0
AVAILABLE NOW!

logo_calm

Have you wanted to learn the Nutanix Calm DSL, but haven't been sure where to start?
Check out the Nutanix Calm DSL Lab 1.0 now!