///<summary> ///Take the ObjectContent[] from RetrieveProperties() ///and print it out. ///ObjectContent[] should have Network information ///</summary> private static void printNetworkInfo(ObjectContent[] ocs) { // Network MoRef -> Network Hashtable networksByNetwork = new Hashtable(); // Network MoRef -> Host Hashtable hostsByNetwork = new Hashtable(); // Network MoRef -> VirtualMachine Hashtable vmsByNetwork = new Hashtable(); // HostSystem MoRef -> Host Hashtable hostByHost = new Hashtable(); if (ocs != null) { for (int i = 0; i < ocs.Length; ++i) { ObjectContent oc = ocs[i]; String type = oc.obj.type; // Create our Network objects if ("Network".Equals(type)) { Network network = new Network(oc.obj); DynamicProperty[] dps = oc.propSet; if (dps != null) { for (int j = 0; j < dps.Length; ++j) { DynamicProperty dp = dps[j]; if ("name".Equals(dp.name)) { network.setName((String)dp.val); } } } // Put them in the Map networksByNetwork.Add(oc.obj.Value, network); // Create our Host objects } else if ("HostSystem".Equals(type)) { Host cHost = new Host(oc.obj); ManagedObjectReference[] network = null; DynamicProperty[] dps = oc.propSet; if (dps != null) { for (int j = 0; j < dps.Length; ++j) { String pName = dps[j].name; Object pVal = dps[j].val; if ("name".Equals(pName)) { cHost.setName((String)pVal); } else if ("network".Equals(pName)) { network = (ManagedObjectReference[])pVal; } else if ( "summary.hardware".Equals(pName)) { cHost.setHardware( (HostHardwareSummary)pVal); } else if ("runtime.connectionState" .Equals(pName)) { cHost.setConnectionState( (HostSystemConnectionState)pVal); } else if ("summary.overallStatus" .Equals(pName)) { cHost.setOverallStatus( (ManagedEntityStatus)pVal); } else if ("summary.quickStats" .Equals(pName)) { cHost.setQuickStats( (HostListSummaryQuickStats)pVal); } } } Host host = new Host( cHost.getMoRef(), cHost.getName(), cHost.getHardware(), cHost.getConnectionState(), cHost.getOverallStatus(), cHost.getQuickStats()); hostByHost.Add( host.getMoRef().Value, host); for (int n = 0; n < network.Length; ++n) { ArrayList hl = (ArrayList)hostsByNetwork[network[n].Value]; if (hl == null) { hl = new ArrayList(); hostsByNetwork.Add(network[n].Value, hl); } hl.Add(host); } // Create our VirtualMachine objects } else if ("VirtualMachine".Equals(type)) { VirtualMachine cVm = new VirtualMachine(oc.obj); ManagedObjectReference[] network = null; DynamicProperty[] dps = oc.propSet; if (dps != null) { for (int j = 0; j < dps.Length; ++j) { String pName = dps[j].name; Object pVal = dps[j].val; if ("name".Equals(pName)) { cVm.setName((String)pVal); } else if ("network".Equals(pName)) { network = (ManagedObjectReference[])pVal; } else if ("runtime.host".Equals(pName)) { cVm.setHost( (ManagedObjectReference)pVal); } else if ("runtime.powerState" .Equals(pName)) { cVm.setPowerState( (VirtualMachinePowerState)pVal); } else if ("summary.overallStatus" .Equals(pName)) { cVm.setOverallStatus( (ManagedEntityStatus)pVal); } else if ("summary.quickStats" .Equals(pName)) { cVm.setQuickStats( (VirtualMachineQuickStats)pVal); } } } VirtualMachine vm = new VirtualMachine( cVm.getMoRef(), cVm.getName(), cVm.getHost(), cVm.getPowerState(), cVm.getOverallStatus(), cVm.getQuickStats()); for (int n = 0; n < network.Length; ++n) { ArrayList vml = (ArrayList)vmsByNetwork[network[n].Value]; if (vml == null) { vml = new ArrayList(); vmsByNetwork.Add(network[n].Value, vml); } vml.Add(vm); } } } } // Now the Hashtables have all the information // Now populate our Network object with the Hosts // and VMs connected and print out the 'tables' for (IEnumerator nit = networksByNetwork.GetEnumerator(); nit.MoveNext(); ) { foreach (String key in networksByNetwork.Keys) { Network network = networksByNetwork[key] as Network; if (network != null) { ArrayList vms = (ArrayList) vmsByNetwork[network.getMoRef().Value]; ArrayList hosts = (ArrayList)hostsByNetwork[network.getMoRef().Value]; Console.WriteLine("Network: " + network.getName()); Console.WriteLine(" Virtual Machines:"); if (vms != null) { for (IEnumerator vmIt = vms.GetEnumerator(); vmIt.MoveNext(); ) { VirtualMachine vm = (VirtualMachine)vmIt.Current; Host host = (Host)hostByHost[vm.getHost().Value]; int cpuUsage = vm.getQuickStats().overallCpuUsage; int memUsage = vm.getQuickStats().hostMemoryUsage; StringBuilder sb = new StringBuilder(); sb .Append(" Name :") .Append(vm.getName()) .Append("\n") .Append(" State :") .Append(vm.getPowerState()) .Append("\n") .Append(" Status :") .Append(vm.getOverallStatus()) .Append("\n") .Append(" Host Name :") .Append(host != null ? host.getName() : "") .Append("\n") .Append(" Host CPU MHZ :") .Append(cpuUsage != null ? cpuUsage : 0) .Append("\n") .Append(" Host Mem = MB :") .Append(memUsage != null ? memUsage / 1024 / 1024 : 0) .Append("\n"); Console.WriteLine(sb.ToString()); } } Console.WriteLine(" Hosts:"); if (hosts != null) { for (IEnumerator hostIt = hosts.GetEnumerator(); hostIt.MoveNext(); ) { Host host = (Host)hostIt.Current; int cpuUsage = host.getQuickStats().overallCpuUsage; int memUsage = host.getQuickStats().overallMemoryUsage; StringBuilder sb = new StringBuilder(); sb .Append(" Name :") .Append(host.getName()) .Append("\n") .Append(" State :") .Append(host.getConnectionState()) .Append("\n") .Append(" Status :") .Append(host.getOverallStatus()) .Append("\n") .Append(" CPU % :") .Append(cpuUsage != null ? cpuUsage : 0) .Append("\n") .Append(" Mem MB :") .Append(memUsage != null ? memUsage : 0).Append("\n"); Console.WriteLine(sb.ToString()); } } } } } }