using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Web.Services.Protocols;
using Vim25Api;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
namespace GetPerfStats
{
class GetPerfStats
{
private static ManagedObjectReference SIMO_REF = new ManagedObjectReference();
private static ManagedObjectReference propCollector;
private static ManagedObjectReference rootFolder;
private static ManagedObjectReference perfMgr;
private static VimService vimService;
private static ServiceContent serviceContent;
private static String STR_SERVICE_INSTANCE = "ServiceInstance";
private static String vimHost;
private static String userName;
private static String password;
private static System.Collections.Hashtable counterInfoMap = new System.Collections.Hashtable();
private static System.Collections.Hashtable counters = new System.Collections.Hashtable();
private static ArrayList SPECIAL_VM_METRICIDS = new ArrayList();
private static void initDefaultPerfCounters()
{
SPECIAL_VM_METRICIDS.Add("disk.provisioned.latest");
SPECIAL_VM_METRICIDS.Add("disk.unshared.latest");
SPECIAL_VM_METRICIDS.Add("disk.used.latest");
SPECIAL_VM_METRICIDS.Add("disk.deviceLatency.average");
}
private static String CSV = "csv";
private static String STAR = "*";
private static String EMPTY = "";
private static String COMMA = ",";
private static String DOT = ".";
//Counters
private static String CPU = "cpu";
private static String CPU_DEFAULT = "cpu.usage.average";
private static String MEM = "mem";
private static String MEM_DEFAULT = "mem.consumed.average";
private static String NET = "net";
private static String NET_DEFAULT = "net.usage.average";
private static String DISK = "disk";
private static String DISK_DEFAULT = "disk.usage.average";
private static String CLUSTER_SERVICES = "clusterServices";
private static String MANAGEMENT_AGENT = "managementAgent";
private static String RESOURCE_SCHEDULER = "rescpu";
private static String SYSTEM = "system";
private static String VM_OPERATIONS = "vmop";
/**
* Set the managed object reference type, and value to ServiceInstance
*/
private static void initSIMORef()
{
SIMO_REF.type = STR_SERVICE_INSTANCE;
SIMO_REF.Value = STR_SERVICE_INSTANCE;
}
/**
*
* @param url
* The URL of the Virtual Center Server
*
* https://<Server IP / host name>/sdk
*
* The method establishes connection with the web service port on
* the server. This is not to be confused with the session
* connection.
*
*/
private static void initVimPort(String url)
{
vimService = new VimService();
try
{
vimService.Url = url;
vimService.CookieContainer = new System.Net.CookieContainer();
}
catch (UriFormatException mue)
{
Console.WriteLine(mue.StackTrace);
}
catch (Exception se)
{
Console.WriteLine(se.StackTrace);
}
}
/*
* This method calls all the initialization methods required in order.
*/
private static void initAll()
{
initDefaultPerfCounters();
// These following methods have to be called in this order.
initSIMORef();
initVimPort(vimHost);
initServiceContent();
try
{
connect(userName, password);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
initPropertyCollector();
initRootFolder();
initPerfMgr();
initPerfCounters();
}
private static void initServiceContent()
{
if (serviceContent == null)
{
try
{
serviceContent = vimService.RetrieveServiceContent(SIMO_REF);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
}
private static void initPropertyCollector()
{
if (propCollector == null)
{
try
{
propCollector = serviceContent.propertyCollector;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
}
private static void initRootFolder()
{
if (rootFolder == null)
{
try
{
rootFolder = serviceContent.rootFolder;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
}
private static void initPerfMgr()
{
if (perfMgr == null)
{
try
{
perfMgr = serviceContent.perfManager;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
}
/**
*
* @param uname
* The user name for the session
* @param pword
* The password for the user
*
* Establishes session with the virtual center server
*
* @throws Exception
*/
private static void connect(String uname, String pword)
{
vimService.Login(serviceContent.sessionManager, uname, pword, null);
}
/**
* Disconnects the user session
*
* @throws Exception
*/
private static void disconnect()
{
vimService.Logout(serviceContent.sessionManager);
}
/**
* This method initializes all the performance counters available on the
* system it is connected to. The performance counters are stored in the
* hashmap counters with group.counter.rolluptype being the key and id being
* the value.
*/
private static void initPerfCounters()
{
try
{
// Create Property Spec
PropertySpec propertySpec = new PropertySpec();
propertySpec.all = false;
propertySpec.pathSet = new String[] { "perfCounter" };
propertySpec.type = "PerformanceManager";
PropertySpec[] propertySpecs = new PropertySpec[] { propertySpec };
// Now create Object Spec
ObjectSpec objectSpec = new ObjectSpec();
objectSpec.obj = perfMgr;
ObjectSpec[] objectSpecs = new ObjectSpec[] { objectSpec };
// Create PropertyFilterSpec using the PropertySpec and ObjectPec
// created above.
PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
propertyFilterSpec.propSet = propertySpecs;
propertyFilterSpec.objectSet = objectSpecs;
PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] { propertyFilterSpec };
ObjectContent[] oCont = vimService.RetrieveProperties(propCollector,
propertyFilterSpecs);
if (oCont != null)
{
foreach (ObjectContent oc in oCont)
{
DynamicProperty[] dps = oc.propSet;
if (dps != null)
{
foreach (DynamicProperty dp in dps)
{
PerfCounterInfo[] pciArr = (PerfCounterInfo[])dp.val;
foreach (PerfCounterInfo pci in pciArr)
{
String fullCounter = pci.groupInfo.key + "." + pci.nameInfo.key + "." + pci.rollupType.ToString();
//Console.WriteLine(fullCounter + " - " + pci.getKey());
counterInfoMap.Add(pci.key, pci);
counters.Add(fullCounter, pci.key);
}
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
}
/**
*
* @return An array of SelectionSpec covering all the entities that provide
* performance statistics. The entities that provide performance
* statistics are VM, Host, Resource pool, Cluster Compute Resource
* and Datastore.
*/
public static SelectionSpec[] buildFullTraversal()
{
TraversalSpec rpToVm = new TraversalSpec();
rpToVm.name = "rpToVm";
rpToVm.type = "ResourcePool";
rpToVm.path = "vm";
rpToVm.skip = false;
rpToVm.skipSpecified = true;
// Recurse through all ResourcePools
TraversalSpec rpToRp = new TraversalSpec();
rpToRp.name = "rpToRp";
rpToRp.type = "ResourcePool";
rpToRp.path = "resourcePool";
rpToRp.skip = false;
rpToRp.skipSpecified = true;
rpToRp.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec() };
rpToRp.selectSet[0].name = "rpToRp";
rpToRp.selectSet[1].name = "rpToVm";
// Traversal through ResourcePool branch
TraversalSpec crToRp = new TraversalSpec();
crToRp.name = "crToRp";
crToRp.type = "ComputeResource";
crToRp.path = "resourcePool";
crToRp.skip = false;
crToRp.skipSpecified = true;
crToRp.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec() };
crToRp.selectSet[0].name = "rpToRp";
crToRp.selectSet[1].name = "rpToVm";
// Traversal through host branch
TraversalSpec crToH = new TraversalSpec();
crToH.name = "crToH";
crToH.type = "ComputeResource";
crToH.path = "host";
crToH.skip = false;
crToH.skipSpecified = true;
// Traversal through hostFolder branch
TraversalSpec dcToHf = new TraversalSpec();
dcToHf.name = "dcToHf";
dcToHf.type = "Datacenter";
dcToHf.path = "hostFolder";
dcToHf.skip = false;
dcToHf.selectSet = new SelectionSpec[] { new SelectionSpec() };
dcToHf.selectSet[0].name = "visitFolders";
// Traversal through vmFolder branch
TraversalSpec dcToVmf = new TraversalSpec();
dcToVmf.name = "dcToVmf";
dcToVmf.type = "Datacenter";
dcToVmf.path = "vmFolder";
dcToVmf.skip = false;
dcToVmf.skipSpecified = true;
dcToVmf.selectSet = new SelectionSpec[] { new SelectionSpec() };
dcToVmf.selectSet[0].name = "visitFolders";
// Recurse through all Hosts
TraversalSpec HToVm = new TraversalSpec();
HToVm.name = "HToVm";
HToVm.type = "HostSystem";
HToVm.path = "vm";
HToVm.skip = false;
HToVm.skipSpecified = true;
HToVm.selectSet = new SelectionSpec[] { new SelectionSpec() };
HToVm.selectSet[0].name = "visitFolders";
// Traversal through vmFolder branch
//Traversal to get to the VM in a VApp
TraversalSpec vAppToVM = new TraversalSpec();
vAppToVM.name = "vAppToVM";
vAppToVM.type = "VirtualApp";
vAppToVM.path = "vm";
//Traversal spec for VApp to VApp
TraversalSpec vAppToVApp = new TraversalSpec();
vAppToVApp.name = "vAppToVApp";
vAppToVApp.type = "VirtualApp";
vAppToVApp.path = "resourcePool";
//SelectionSpec for VApp to VApp recursion
SelectionSpec vAppRecursion = new SelectionSpec();
vAppRecursion.name = "vAppToVApp";
//SelectionSpec to get to a VM in the VApp
//Since it is already defined earlier use it by its name
//Can't use the same object
SelectionSpec vmInVApp = new SelectionSpec();
vmInVApp.name = "vAppToVM";
//SelectionSpec for both VApp to VApp and VApp to VM
SelectionSpec[] vAppToVMSS = new SelectionSpec[] { vAppRecursion, vmInVApp };
vAppToVApp.selectSet = vAppToVMSS;
// Recurse thriugh the folders
TraversalSpec visitFolders = new TraversalSpec();
visitFolders.name = "visitFolders";
visitFolders.type = "Folder";
visitFolders.path = "childEntity";
visitFolders.skip = false;
visitFolders.skipSpecified = true;
visitFolders.selectSet = new SelectionSpec[] { new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec(), new SelectionSpec() };
visitFolders.selectSet[0].name = "visitFolders";
visitFolders.selectSet[1].name = "dcToHf";
visitFolders.selectSet[2].name = "dcToVmf";
visitFolders.selectSet[3].name = "crToH";
visitFolders.selectSet[4].name = "crToRp";
visitFolders.selectSet[5].name = "HToVm";
visitFolders.selectSet[6].name = "rpToVm";
visitFolders.selectSet[7].name = "vAppToVM";
visitFolders.selectSet[8].name = "vAppToVApp";
return new SelectionSpec[] { visitFolders, dcToVmf, dcToHf, crToH, crToRp, rpToRp, HToVm, rpToVm, vAppToVM, vAppToVApp };
}
/**
* @param entityName
* The name of the entity
* @param entityType
* @return ManagedObjectReference of the entity
*/
public static ManagedObjectReference getEntityByName(String entityName, String entityType)
{
ManagedObjectReference retVal = null;
try
{
// Create Property Spec
PropertySpec propertySpec = new PropertySpec();
propertySpec.all = false;
propertySpec.type = entityType;
propertySpec.pathSet = new String[] { "name" };
PropertySpec[] propertySpecs = new PropertySpec[] { propertySpec };
// Now create Object Spec
ObjectSpec objectSpec = new ObjectSpec();
objectSpec.obj = rootFolder;
objectSpec.skip = true;
objectSpec.selectSet = buildFullTraversal();
ObjectSpec[] objectSpecs = new ObjectSpec[] { objectSpec };
// Create PropertyFilterSpec using the PropertySpec and ObjectPec
// created above.
PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
propertyFilterSpec.propSet = propertySpecs;
propertyFilterSpec.objectSet = objectSpecs;
PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[] { propertyFilterSpec };
ObjectContent[] oCont = vimService.RetrieveProperties(propCollector, propertyFilterSpecs);
if (oCont != null)
{
foreach (ObjectContent oc in oCont)
{
ManagedObjectReference mr = oc.obj;
// Console.WriteLine("MOR Type : " + mr.getType());
String entityNm = null;
DynamicProperty[] dps = oc.propSet;
if (dps != null)
{
foreach (DynamicProperty dp in dps)
{
// Console.WriteLine(dp.getName() + " : " +
// dp.getVal());
entityNm = (String)dp.val;
}
}
//Console.WriteLine("Entity Name: " + entityNm);
if (entityNm != null && entityNm.Equals(entityName))
{
retVal = mr;
Console.WriteLine("MOR Type : " + mr.Value);
break;
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
return retVal;
}
/**
* This method retrieves all the performance metrics available for the
* managed entity.
*
* The performance metrics are stored in the hashmap metrics with
* counterId being the key and PerfMetricId being the value.
*
* @param entityMor
* - ManagedObjectReference of the entity
* @param intervalId
* - Integer specifying the interval id
*/
private static PerfMetricId[] getAvailablePerfMetricIds(ManagedObjectReference entityMor, int intervalId)
{
PerfMetricId[] pmArr = null;
try
{
pmArr = vimService.QueryAvailablePerfMetric(perfMgr, entityMor, new DateTime(), false, new DateTime(), false, intervalId, true);
/*
for (PerfMetricId pmid : pmArr) {
Console.WriteLine("-----------------------------");
Console.WriteLine("CounterID: " + pmid.getCounterId());
Console.WriteLine("Instance:***" + pmid.getInstance() + "***");
Console.WriteLine("-----------------------------");
}
*/
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
return pmArr;
}
/**
* This method prints the PerfProviderSummary information
*/
public static void printPerfProviderSummary(PerfProviderSummary pps)
{
Console.WriteLine("-----------------------------");
Console.WriteLine("Managed entity: " + pps.entity.Value);
Console.WriteLine("Current supported: " + pps.currentSupported);
Console.WriteLine("Refresh rate: " + pps.refreshRate);
Console.WriteLine("Summary supported: " + pps.summarySupported);
Console.WriteLine("-----------------------------");
}
/**
* This method is used to construct the PerfQuerySpec required for querying
* performance information related to the managed entity
*
* @param entityMor
* - ManagedObjectReference of the entity
* @param perfCounter
* - The name of the performance counter (cpu.usage.average)
* @param intervalId
* - Integer specifying the interval id
* @return PerfQuerySpec
*/
private static PerfQuerySpec getPerfQuerySpec(ManagedObjectReference entityMor, String perfCounter, int intervalId, PerfMetricId[] pmArr)
{
PerfQuerySpec retVal = null;
try
{
int counterId = 0;
if (counters.ContainsKey(perfCounter))
counterId = (int)counters[perfCounter];
else
{
Console.WriteLine("Counter doesn't exist...........");
pause();
Environment.Exit(1);
}
Console.WriteLine("Counter Id: " + counterId);
List<PerfMetricId> pmidList = new List<PerfMetricId>();
if (pmArr != null)
{
Boolean counterSupported = false;
foreach (PerfMetricId pmId in pmArr)
{
if (pmId.counterId == counterId)
{
counterSupported = true;
//You can filter this list by specific metric id.
//Since you are retrieving stats for all metric ids, you
//can use wild card as well.
//Eg: Create a PerfMetricId object and set its counterId
//to this counterId and set the metricId as "*" to
//retrieve stats for all metricIds
//There is an exception for some performance counters where the
//metric id needs to be set to empty string (check reference doc)
if (SPECIAL_VM_METRICIDS.Contains(perfCounter))
{
pmId.instance = EMPTY;
}
pmidList.Add(pmId);
}
}
if (!counterSupported)
{
Console.WriteLine("This Counter (" + perfCounter + ") is not supported by this entity $$$");
Environment.Exit(1);
}
}
PerfQuerySpec pqSpec = new PerfQuerySpec();
pqSpec.intervalId = intervalId;
//In the case of real time stats, we need to set the max value to 1 to get
//the latest value. For real time stats the refresh interval is 20 seconds.
if (intervalId == 20)
{
pqSpec.maxSample = 1;
//This is to collect the past 1 hour stats for each 20 sec.
//pqSpec.setMaxSample(new Integer(180));
}
PerfMetricId[] pmidArr = new PerfMetricId[pmidList.Count];
pqSpec.metricId = (PerfMetricId[])pmidList.ToArray();
pqSpec.entity = entityMor;
retVal = pqSpec;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
return retVal;
}
/**
* This method retrieves the PerfProviderSummary for a mamaged entity
*
* @param mor
* - ManagedObjectReference of the entity
*/
public static PerfProviderSummary getPerfProviderSummary(ManagedObjectReference mor)
{
PerfProviderSummary retVal = null;
try
{
//Console.WriteLine(mor.getType());
retVal = vimService.QueryPerfProviderSummary(perfMgr, mor);
printPerfProviderSummary(retVal);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
return retVal;
}
/**
* This method retrieves the performance statistics.
*
* @param pqs
* - PerformanceQuerySpec for retrieving the stats
* Eg: cpu.usage.average for VM
*
* @param isCSV
* - Boolean to indicate whether the output should be in CSV
*
* @return PerfEntityMetricBase array
*/
public static PerfEntityMetricBase[] getPerfStats(PerfQuerySpec[] pqsArr, Boolean isCSV)
{
PerfEntityMetricBase[] retVal = null;
try
{
int startTime = (DateTime.Now.Minute * 60 + DateTime.Now.Second) * 1000 + DateTime.Now.Millisecond;
retVal = vimService.QueryPerf(perfMgr, pqsArr);
int endTime = (DateTime.Now.Minute * 60 + DateTime.Now.Second) * 1000 + DateTime.Now.Millisecond;
Console.WriteLine("$$$$$$$$$$$$$$$ Total time for the perf stats query: " + (endTime - startTime) + " ms");
if (retVal != null)
{
Console.WriteLine("PerfEntityMetricBase array length: " + retVal.Length);
foreach (PerfEntityMetricBase pemb in retVal)
{
printPerfEntityMetricBase(pemb, isCSV);
}
}
else
{
Console.WriteLine("PerfEntityMetricBase array returned NULL: ");
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
return retVal;
}
/**
* This method retrieves the performance statistics.
*
* @param pqs
* - PerformanceQuerySpec for retrieving the stats
* Eg: cpu.usage.average for VM
*
* @param isCSV
* - Boolean to indicate whether the output should be in CSV
*
* @return PerfEntityMetricBase array
*/
//This is commented out as the server is throwing an error with this:
//Unidentified parameter "metricId" exists in the PerfQuerySpec
//Need to do more research to find out about this error.
/*
public static PerfCompositeMetric getCompositePerfStats(PerfQuerySpec pqs, boolean isCSV) {
PerfCompositeMetric retVal = null;
try {
if (isCSV) {
pqs.setFormat(STR_CSV);
}
retVal = vimService.queryPerfComposite(perfMgr, pqs);
if (retVal != null) {
PerfEntityMetricBase pemb = retVal.getEntity();
printPerfEntityMetricBase(pemb, isCSV);
PerfEntityMetricBase[] childEntityPembArr = retVal.getChildEntity();
Console.WriteLine("---------------Perf Stats for child entities--------------");
for (PerfEntityMetricBase childEntityPemb : childEntityPembArr) {
printPerfEntityMetricBase(childEntityPemb, isCSV);
}
} else {
Console.WriteLine("PerfEntityMetricBase array returned NULL: ");
}
} catch (Exception e) {
e.printStackTrace();
}
return retVal;
}
*/
private static void printPerfEntityMetricBase(PerfEntityMetricBase pemb, Boolean isCSV)
{
if (isCSV)
{
String csvInfo = ((PerfEntityMetricCSV)pemb).sampleInfoCSV;
PerfMetricSeriesCSV[] csvArr = ((PerfEntityMetricCSV)pemb).value;
Console.WriteLine("PerfEntityMetricCSV Info:" + csvInfo);
foreach (PerfMetricSeriesCSV pmsCSV in csvArr)
{
PerfCounterInfo pci = (PerfCounterInfo)counterInfoMap[pmsCSV.id.counterId];
Console.WriteLine("Counter Info: " + pci.groupInfo.key + "." + pci.nameInfo.key + "." + pci.rollupType + " - " + pci.unitInfo.key);
PerfMetricId perfMetricId = pmsCSV.id;
Console.WriteLine("Instance: " + perfMetricId.instance);
Console.WriteLine("\n PerfMetricSeriesCSV val:" + pmsCSV.value + "\n");
}
}
else
{
PerfMetricSeries[] vals = ((PerfEntityMetric)pemb).value;
PerfSampleInfo[] infos = ((PerfEntityMetric)pemb).sampleInfo;
if (infos != null || infos.Length != 0)
{
Console.WriteLine("Sample time range: "
+ infos[0].timestamp.ToString()
+ " - "
+ infos[infos.Length - 1].timestamp.ToString() + "\n");
foreach (PerfMetricSeries pms in vals)
{
PerfCounterInfo pci = (PerfCounterInfo)counterInfoMap[pms.id.counterId];
Console.WriteLine("Counter Info: " + pci.groupInfo.key + "." + pci.nameInfo.key + "." + pci.rollupType + " - " + pci.unitInfo.key);
PerfMetricId perfMetricId = pms.id;
Console.WriteLine("Instance: " + perfMetricId.instance);
if (pms is PerfMetricIntSeries)
{
PerfMetricIntSeries val = (PerfMetricIntSeries)pms;
long[] longs = val.value;
foreach (long k in longs)
{
Console.WriteLine("Stats value: " + k + "\n");
}
Console.WriteLine();
}
}
}
else
Console.WriteLine("No Samples Found: ");
}
}
private static Dictionary<String, List<String>> getCountersMap(String[] perfCountersArr)
{
Dictionary<String, List<String>> countersMap = null;
int perfCountersArrLength = perfCountersArr.Length;
Console.WriteLine("PerfCountersArr length: " + perfCountersArrLength);
if (perfCountersArrLength == 0)
{
Console.WriteLine("!!! Performance counters are not specified !!!");
Environment.Exit(0);
}
else
{
countersMap = new Dictionary<String, List<String>>();
for (int i = 0; i < perfCountersArrLength; i++)
{
String perfCounter = perfCountersArr[i].Trim();
if (!perfCounter.Contains(DOT))
{
if (perfCounter.Equals(CPU))
{
perfCounter = CPU_DEFAULT;
}
else if (perfCounter.Equals(MEM))
{
perfCounter = MEM_DEFAULT;
}
else if (perfCounter.Equals(NET))
{
perfCounter = NET_DEFAULT;
}
else if (perfCounter.Equals(DISK))
{
perfCounter = DISK_DEFAULT;
}
}
if (perfCounter.StartsWith(CPU))
{
if (countersMap.ContainsKey(CPU))
{
List<String> cpuCountersList = countersMap[CPU];
cpuCountersList.Add(perfCounter);
}
else
{
List<String> cpuCountersList = new List<String>();
cpuCountersList.Add(perfCounter);
countersMap.Add(CPU, cpuCountersList);
}
}
else if (perfCounter.StartsWith(MEM))
{
if (countersMap.ContainsKey(MEM))
{
List<String> memCountersList = countersMap[MEM];
memCountersList.Add(perfCounter);
}
else
{
List<String> memCountersList = new List<String>();
memCountersList.Add(perfCounter);
countersMap.Add(MEM, memCountersList);
}
}
else if (perfCounter.StartsWith(NET))
{
if (countersMap.ContainsKey(NET))
{
List<String> netCountersList = countersMap[NET];
netCountersList.Add(perfCounter);
}
else
{
List<String> netCountersList = new List<String>();
netCountersList.Add(perfCounter);
countersMap.Add(NET, netCountersList);
}
}
else if (perfCounter.StartsWith(DISK))
{
if (countersMap.ContainsKey(DISK))
{
List<String> diskCountersList = countersMap[DISK];
diskCountersList.Add(perfCounter);
}
else
{
List<String> diskCountersList = new List<String>();
diskCountersList.Add(perfCounter);
countersMap.Add(DISK, diskCountersList);
}
}
else if (perfCounter.StartsWith(CLUSTER_SERVICES))
{
if (countersMap.ContainsKey(CLUSTER_SERVICES))
{
List<String> csCountersList = countersMap[CLUSTER_SERVICES];
csCountersList.Add(perfCounter);
}
else
{
List<String> csCountersList = new List<String>();
csCountersList.Add(perfCounter);
countersMap.Add(CLUSTER_SERVICES, csCountersList);
}
}
else if (perfCounter.StartsWith(MANAGEMENT_AGENT))
{
if (countersMap.ContainsKey(MANAGEMENT_AGENT))
{
List<String> agentCountersList = countersMap[MANAGEMENT_AGENT];
agentCountersList.Add(perfCounter);
}
else
{
List<String> agentCountersList = new List<String>();
agentCountersList.Add(perfCounter);
countersMap.Add(MANAGEMENT_AGENT, agentCountersList);
}
}
else if (perfCounter.StartsWith(RESOURCE_SCHEDULER))
{
if (countersMap.ContainsKey(RESOURCE_SCHEDULER))
{
List<String> resCountersList = countersMap[RESOURCE_SCHEDULER];
resCountersList.Add(perfCounter);
}
else
{
List<String> resCountersList = new List<String>();
resCountersList.Add(perfCounter);
countersMap.Add(RESOURCE_SCHEDULER, resCountersList);
}
}
else if (perfCounter.StartsWith(SYSTEM))
{
if (countersMap.ContainsKey(SYSTEM))
{
List<String> sysCountersList = countersMap[SYSTEM];
sysCountersList.Add(perfCounter);
}
else
{
List<String> sysCountersList = new List<String>();
sysCountersList.Add(perfCounter);
countersMap.Add(SYSTEM, sysCountersList);
}
}
else if (perfCounter.StartsWith(VM_OPERATIONS))
{
if (countersMap.ContainsKey(VM_OPERATIONS))
{
List<String> vmopCountersList = countersMap[VM_OPERATIONS];
vmopCountersList.Add(perfCounter);
}
else
{
List<String> vmopCountersList = new List<String>();
vmopCountersList.Add(perfCounter);
countersMap.Add(VM_OPERATIONS, vmopCountersList);
}
}
}
}
return countersMap;
}
public static bool TrustAllCertificateCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
private static void pause()
{
Console.WriteLine("Press any key to exit");
Console.Read();
}
/**
* This method prints the command line argument details for this program.
*/
private static void printUsage()
{
Console.WriteLine("GetPerfStats.exe <host> <username> <password> <Entity Name> <Entity Type vm | host | rp | ccr> <PerfGroup.PerfCounter.RollupType> [isCSVOutput]");
Console.WriteLine("The sixth argument where you specify the performance group, counter and rollup type ");
Console.WriteLine("can be multiple types with comma separation, and has defaults for cpu, mem, net, and disk");
Console.WriteLine("cpu default: cpu.usage.average");
Console.WriteLine("net default: net.usage.average");
Console.WriteLine("disk default: disk.usage.average");
Console.WriteLine("mem default: mem.consumed.average");
pause();
}
/**
* @param args
*/
public static void Main(String[] args)
{
// This is to accept all SSL certifcates by default.
System.Net.ServicePointManager.ServerCertificateValidationCallback = GetPerfStats.TrustAllCertificateCallback;
if (args.Length < 6)
{
printUsage();
}
else
{
try
{
vimHost = args[0];
userName = args[1];
password = args[2];
String entityName = args[3];
String entityType = args[4];
//String perfCounter = args[5];
int intervalId = 0;
if (entityType.Equals("vm"))
{
entityType = "VirtualMachine";
}
else if (entityType.Equals("host"))
{
entityType = "HostSystem";
}
else if (entityType.Equals("rp"))
{
entityType = "ResourcePool";
}
else if (entityType.Equals("ccr"))
{
entityType = "ClusterComputeResource";
}
initAll();
//To check if the output format is CSV
Boolean isCSV = false;
if (args.Length == 7)
{
if ((args[6] != null) && !(args[6].Trim().Equals("")))
{
isCSV = Boolean.Parse(args[6].Trim());
}
}
ManagedObjectReference mor = getEntityByName(entityName, entityType);
PerfProviderSummary pps = getPerfProviderSummary(mor);
if (pps != null)
{
//If the entity supports real time stats, retrieve real time stats,
//otherwise default to retrieving the 5 minute samples for the day.
if (pps.currentSupported)
{
intervalId = 20;
}
else
{
intervalId = 300;
}
if (intervalId != 0)
{
PerfMetricId[] pmArr = getAvailablePerfMetricIds(mor, intervalId);
String[] perfCountersArr = args[5].Split(COMMA.ToCharArray());
Dictionary<String, List<String>> countersMap = getCountersMap(perfCountersArr);
if (countersMap == null)
{
Console.WriteLine("!!! Performance counters are not specified !!!");
Environment.Exit(0);
}
else
{
//This list is used for all the PerfQuerySpec objects.
//Later this list is used to find the stats retrieval time
//when all these objects are used to retrieve the stats in one call.
List<PerfQuerySpec> allPQSpecList = new List<PerfQuerySpec>();
List<String> keySet = new List<String>(countersMap.Keys);
foreach (String counter in keySet)
{
List<String> countersList = (List<String>)countersMap[counter];
int countersListSize = countersList.Count;
//You can create PerfQuerySpec for more than one counter
//and retrieve the stats if the PerfQuerySpec objects
//are placed in this array.
//Create an array for counters of the same type.
PerfQuerySpec[] perfQuerySpecArr = new PerfQuerySpec[countersListSize];
for (int i = 0; i < countersListSize; i++)
{
String perfCounter = countersList[i];
PerfQuerySpec perfQuerySpec = getPerfQuerySpec(mor, perfCounter, intervalId, pmArr);
if (isCSV)
{
perfQuerySpec.format = CSV;
}
perfQuerySpecArr[i] = perfQuerySpec;
allPQSpecList.Add(perfQuerySpec);
}
//Retrieve Stats for same counter group in one call
//Each counter group will have one call
getPerfStats(perfQuerySpecArr, isCSV);
Console.WriteLine();
Console.WriteLine("\n-------------------------------------------------------------\n\n");
}
//Retrieve all the stats in one call with all different
//PerfQuerySpec objects related to different counters in the array.
PerfQuerySpec[] allPQSpecArr = new PerfQuerySpec[allPQSpecList.Count];
allPQSpecArr = allPQSpecList.ToArray();
Console.WriteLine("\n-------------------------------------------------------------\n\n");
Console.WriteLine("Cumulative time when all perf query specs are in one array");
getPerfStats(allPQSpecArr, isCSV);
pause();
}
}
else
{
Console.WriteLine("Sampling period to determine whether to retrieve real time stats or historical stats is null !!!");
pause();
Environment.Exit(0);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
pause();
}
finally
{
try
{
disconnect();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
pause();
}
}
}
}
}
}