Nutanix : Cluster Details with Php
Provide a quick and dirty way to get basic cluster details in Php from the command line. That sound great. And indeed, very feasible making good use of REST API calls.
Under The Hood
The big picture behind the scene is, a REST API v2 call is populating a variable with JSON output. Then, this variable is queried to get the relevant data to be displayed. Another REST API v1 is done to count the amount of VMs. As easy as that.
This is how to get there
#!/usr/bin/php
<?PHP
$clusterConnect=array(
"username" => "api_call",
"password" => "NotImportant!",
"ip" => "192.168.8.189"
);
// ---------------------------------------------------------------------------
// Function to populate a return variable (JSON text) with all cluster details
// ---------------------------------------------------------------------------
function getNutanixClusterDetails($clusterConnect)
{
$API="/api/nutanix/v2.0/cluster/";
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, $clusterConnect["username"].":".$clusterConnect["password"]);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, "https://".$clusterConnect["ip"].":9440/".$API);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
// ---------------------------------------------------------------------------
// Function to populate a return variable (JSON text) with total VMs details
// ---------------------------------------------------------------------------
function getNutanixVMsCount($clusterConnect)
{
$API="PrismGateway/services/rest/v1/vms/?count=1";
$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, $clusterConnect["username"].":".$clusterConnect["password"]);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, "https://".$clusterConnect["ip"].":9440/".$API);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
// ---------------------------------------------------------------------------
// Display a string in Nutanix Green!!!
// ---------------------------------------------------------------------------
function ColorOutput($string)
{
return ("\033[32m".$string."\033[0m");
}
$VMs=json_decode(getNutanixVMsCount($clusterConnect));
$cluster=json_decode(getNutanixClusterDetails($clusterConnect));
// Layout cluster details on screen
// Note : this can be improved using some padding functions but I'm sure you can do it ;)
print("+--------------------------------------------------+\n");
print("| Cluster Name : ".ColorOutput($cluster -> name)." | Cluster model : ".ColorOutput($cluster -> rackable_units[0] -> model_name)." |\n");
print("+--------------------------------------------------+\n");
print("| VIP : ".ColorOutput($cluster -> cluster_external_ipaddress)." |\n");
print("| Data Service IP : ".ColorOutput($cluster -> cluster_external_data_services_ipaddress)." |\n");
print("+--------------------------+-----------------------+\n");
// size of array block_serials equal to number of blocks
$blocks=count($cluster -> block_serials);
print("| Number of Block(s) : ".ColorOutput($blocks)." | Number of nodes : ".ColorOutput($cluster -> num_nodes)." |\n");
print("+--------------------------+-----------------------+\n");
print("| AOS version : ".ColorOutput($cluster -> version)." |\n");
print("| NCC version : ".ColorOutput($cluster -> ncc_version)." |\n");
print("| Hypervisor : ".ColorOutput($cluster -> hypervisor_types[0])." |\n");
print("+--------------------------------------------------+\n");
print("| Total VMs (CVMs included): ".ColorOutput($VMs -> metadata -> totalEntities)." |\n");
print("+--------------------------------+-----------------+\n");
// Convert free storage from bytes to TB
$free_storage=round($cluster -> usage_stats -> {'storage.free_bytes'}/ 1024 ** 4,2);
print("| Storage free space : ".ColorOutput($free_storage)." TB | |\n");
$total_cluster_storage=round($cluster -> usage_stats -> {'storage.capacity_bytes'} / 1024 ** 4,2);
print("| Total Cluster size : ".ColorOutput($total_cluster_storage)." TB | ");
$freeStorageinPercent=round($free_storage/$total_cluster_storage*100,1);
print(ColorOutput($freeStorageinPercent)." % Free |\n");
print("+--------------------------------+-----------------+\n");
?>
This is a quick an dirty way, I'm not a professional developer. I only enjoy writing some useful scripts to simplify my life. It can be enhanced by many ways, like adding padding functionalities on the output but seriously, who cares ? ;)
Do not forget to make the script executable so, it can be run from the command line.
Enjoy !
Comments
Post a Comment
Thank you for your message, it has been sent to the moderator for review...