Example #1
0
        /// <summary>

        /// Establishe a connection using an existing cookie

        /// </summary>

        /// <param name="url">Server Url</param>

        /// <param name="cookie">Cookie used to connect to the server</param>

        public void Connect(string url, Cookie cookie)

        {
            if (_service != null)

            {
                Disconnect();
            }



            _service = GetVimService(url);

            _sic = _service.RetrieveServiceContent(_svcRef);



            // Add the cookie to the cookie manager

            var cookieManager = ((IContextChannel)_service).GetProperty <IHttpCookieContainerManager>();

            cookieManager.CookieContainer.SetCookies(new Uri(url), cookie.ToString());



            _state = ConnectionState.Connected;

            if (AfterConnect != null)

            {
                AfterConnect(this, new ConnectionEventArgs());
            }
        }
Example #2
0
        public void SSOConnect(RequestSecurityTokenResponseType token, string url)

        {
            if (_service != null)

            {
                Disconnect();
            }



            _service = GetVimService(url, null, null, null, token.RequestedSecurityToken);

            _sic = _service.RetrieveServiceContent(this._svcRef);

            if (ServiceContent.sessionManager != null)

            {
                var userSession = _service.LoginByToken(ServiceContent.sessionManager, null);

                VimUserSession = userSession;
            }



            _state = ConnectionState.Connected;

            if (AfterConnect != null)

            {
                AfterConnect(this, new ConnectionEventArgs());
            }
        }
Example #3
0
        /// <summary>

        /// Creates an instance of the VMA proxy and establishes a connection

        /// </summary>

        /// <param name="url"></param>

        /// <param name="username"></param>

        /// <param name="password"></param>

        public void Connect(string url, string username, string password)

        {
            if (_service != null)

            {
                Disconnect();
            }



            _service = GetVimService(url, username, password);

            _sic = _service.RetrieveServiceContent(_svcRef);



            if (_sic.sessionManager != null)

            {
                _service.Login(_sic.sessionManager, username, password, null);
            }



            _state = ConnectionState.Connected;

            if (AfterConnect != null)

            {
                AfterConnect(this, new ConnectionEventArgs());
            }
        }
 public ServiceContent Connect()
 {
     if (string.IsNullOrEmpty(this._serviceUrl))
     {
         throw new InvalidOperationException(Resources.InvalidServiceUrl);
     }
     try
     {
         this._vimService = this.CreateClientChannel();
         ServiceContent content = new ServiceInstance((VimClient)this, new ManagedObjectReference()
         {
             Type  = "ServiceInstance",
             Value = "ServiceInstance"
         }).RetrieveServiceContent();
         this._version        = VersionUtils.GetVIServerVersion(content);
         this._serviceContent = content;
         return(this._serviceContent);
     }
     catch (ProtocolException ex)
     {
         throw new VimProtocolException(ex.Message, (Exception)ex);
     }
     catch (EndpointNotFoundException ex)
     {
         throw new VimEndpointNotFoundException(ex.Message, (Exception)ex);
     }
     catch (SecurityNegotiationException ex)
     {
         throw new VimSecurityNegotiationException(ex.Message, (Exception)ex);
     }
     catch (CommunicationException ex)
     {
         throw new VimException(ex.Message, (Exception)ex);
     }
 }
 protected virtual void Dispose(bool disposing)
 {
     if (disposing) //free managed resources if any
     {
         //if (_vimService != null) { _vimService.Dispose(); }
     }
     // free native resources if there are any.
     //_certificateError = null;
     _serviceContent = null;
     _serviceUrl     = null;
     _vimService     = null;
     //_serializerCache.Clear();
 }
Example #6
0
        private static ObjectContent[] GetPropertyContent(VimPortType service, ServiceContent serviceContent, string propertyType, string path, ManagedObjectReference reference)
        {
            var propertySpecs = new PropertySpec[] { new PropertySpec()
                                                     {
                                                         type = propertyType, pathSet = new string[] { path }
                                                     } };
            var objectSpecs = new ObjectSpec[] { new ObjectSpec()
                                                 {
                                                     obj = reference
                                                 } };
            var propertyFilterSpecs = new PropertyFilterSpec[] { new PropertyFilterSpec()
                                                                 {
                                                                     propSet = propertySpecs, objectSet = objectSpecs
                                                                 } };

            return(service.RetrieveProperties(new RetrievePropertiesRequest(serviceContent.propertyCollector, propertyFilterSpecs)).returnval);
        }
Example #7
0
        /// <summary>

        /// Disconnects the Connection

        /// </summary>

        public void Disconnect()

        {
            if (_service != null)

            {
                if (BeforeDisconnect != null)

                {
                    BeforeDisconnect(this, new ConnectionEventArgs());
                }



                if (_sic != null)
                {
                    _service.Logout(_sic.sessionManager);
                }



                _service = null;

                _sic = null;



                _state = ConnectionState.Disconnected;

                if (AfterDisconnect != null)

                {
                    AfterDisconnect(this, new ConnectionEventArgs());
                }
            }
        }
Example #8
0
        public static VMwareHostSystemInformation Retrieve(VMwareHostConnectionInfo hostConnectionInfo)
        {
            try {
                var sysinfo = new VMwareHostSystemInformation();

#warning comments
#warning some system so the database don't get hammered too much

                sysinfo.timeStampInSecondsSinceEpochUtc = (long)(DateTime.UtcNow - _epochUtc).TotalSeconds;
                sysinfo.responsive   = 1;
                sysinfo.comments     = "";
                sysinfo.ipOrHostname = hostConnectionInfo.ipOrHostname;
                sysinfo.vmHostnames  = hostConnectionInfo.vmHostnames;

                VimPortType    service        = null;
                ServiceContent serviceContent = null;

                //Connect
                string url = "https://" + hostConnectionInfo.ipOrHostname + "/sdk";
                ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

                service = GetVimService(url, hostConnectionInfo.username, hostConnectionInfo.password);

                var svcRef = new ManagedObjectReference();
                svcRef.type    = "ServiceInstance";
                svcRef.Value   = "ServiceInstance";
                serviceContent = service.RetrieveServiceContent(svcRef);


                //Finally connect, we do not need the user session later on.
                UserSession session = service.Login(serviceContent.sessionManager, hostConnectionInfo.username, hostConnectionInfo.password, null);

                //Get the host ref by IP or by host name.
                IPAddress address;
                ManagedObjectReference reference = IPAddress.TryParse(hostConnectionInfo.ipOrHostname, out address) ?
                                                   service.FindByIp(serviceContent.searchIndex, null, hostConnectionInfo.ipOrHostname, false) :
                                                   service.FindByDnsName(serviceContent.searchIndex, null, Dns.GetHostEntry(hostConnectionInfo.ipOrHostname).HostName, false);


                var systemInfo = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.systemInfo", reference)[0].propSet[0].val as HostSystemInfo;
                sysinfo.system = systemInfo.vendor + " " + systemInfo.model;

                sysinfo.os = GetPropertyContent(service, serviceContent, "HostSystem", "summary.config.product.fullName", reference)[0].propSet[0].val.ToString();

                var biosInfo = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.biosInfo", reference)[0].propSet[0].val as HostBIOSInfo;
                sysinfo.bios = biosInfo.vendor + " " + biosInfo.biosVersion;

                var cpuPkgs = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.cpuPkg", reference)[0].propSet[0].val as HostCpuPackage[];
                var cpuDict = new SortedDictionary <string, int>();
                foreach (var cpuPkg in cpuPkgs)
                {
                    string[] candidateArr = cpuPkg.description.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    string   candidate    = string.Join(" ", candidateArr);
                    if (cpuDict.ContainsKey(candidate))
                    {
                        ++cpuDict[candidate];
                    }
                    else
                    {
                        cpuDict.Add(candidate, 1);
                    }
                }
                sysinfo.processors = ComponentDictToString(cpuDict);

                var cpuInfo = GetPropertyContent(service, serviceContent, "HostSystem", "hardware.cpuInfo", reference)[0].propSet[0].val as HostCpuInfo;
                sysinfo.numCpuCores   = cpuInfo.numCpuCores;
                sysinfo.numCpuThreads = cpuInfo.numCpuThreads;

                long memorySize = (long)GetPropertyContent(service, serviceContent, "HostSystem", "hardware.memorySize", reference)[0].propSet[0].val;
                sysinfo.memoryInGB = Convert.ToInt32(Math.Round(Convert.ToDouble(memorySize) / (1024 * 1024 * 1024), MidpointRounding.AwayFromZero));

                //----

                //First ask the childentity from the rootfolder (datacenter)
                ObjectContent[] oCont = GetPropertyContent(service, serviceContent, "Folder", "childEntity", serviceContent.rootFolder);

                ManagedObjectReference datacenter = (oCont[0].propSet[0].val as ManagedObjectReference[])[0];

                //Then ask the datastore from the datacenter
                var datastoreRefs = GetPropertyContent(service, serviceContent, "Datacenter", "datastore", datacenter)[0].propSet[0].val as ManagedObjectReference[];

                var hostMultipathInfo = GetPropertyContent(service, serviceContent, "HostSystem", "config.storageDevice.multipathInfo", reference)[0].propSet[0].val as HostMultipathInfo;
                var scsiLuns          = GetPropertyContent(service, serviceContent, "HostSystem", "config.storageDevice.scsiLun", reference)[0].propSet[0].val as ScsiLun[];

                string[] datastoreArr = new string[datastoreRefs.Length];
                for (int i = 0; i != datastoreRefs.Length; i++)
                {
                    var    candidate = datastoreRefs[i];
                    var    dsInfo = GetPropertyContent(service, serviceContent, "Datastore", "info", candidate)[0].propSet[0].val;
                    string dataStoreName = null, diskName = null;

                    if (dsInfo is VmfsDatastoreInfo)
                    {
                        dataStoreName = (dsInfo as VmfsDatastoreInfo).name;
                        diskName      = (dsInfo as VmfsDatastoreInfo).vmfs.extent[0].diskName;
                    }
                    else if (dsInfo is NasDatastoreInfo)
                    {
                        dataStoreName = (dsInfo as NasDatastoreInfo).name;
                    }
                    if (diskName == null)
                    {
                        diskName = "unknown";
                    }
                    else
                    {
                        foreach (ScsiLun lun in scsiLuns)
                        {
                            if (lun.canonicalName == diskName)
                            {
                                diskName = lun.displayName;
                                break;
                            }
                        }
                    }

                    if (dataStoreName == null)
                    {
                        dataStoreName = "Unknown";
                    }

                    datastoreArr[i] = dataStoreName + " disk " + diskName;
                }
                sysinfo.datastores = string.Join("\t", datastoreArr);

                //Then ask the vm folder from the datacenter
                var vmFolder = GetPropertyContent(service, serviceContent, "Datacenter", "vmFolder", datacenter)[0].propSet[0].val as ManagedObjectReference;
                //finally get the list of the managed object from the vms.
                var vmRefs = GetPropertyContent(service, serviceContent, "Folder", "childEntity", vmFolder)[0].propSet[0].val as ManagedObjectReference[];

                var vDiskPathsHs = new HashSet <string>();
                foreach (var vmRef in vmRefs)
                {
                    foreach (var dev in (GetPropertyContent(service, serviceContent, "VirtualMachine", "config.hardware", vmRef)[0].propSet[0].val as VirtualHardware).device)
                    {
                        if (dev is VirtualDisk)
                        {
                            if (dev.backing is VirtualDiskFlatVer2BackingInfo)
                            {
                                vDiskPathsHs.Add((dev.backing as VirtualDiskFlatVer2BackingInfo).fileName);
                            }
                            else if (dev.backing is VirtualDiskFlatVer1BackingInfo)
                            {
                                vDiskPathsHs.Add((dev.backing as VirtualDiskFlatVer1BackingInfo).fileName);
                            }
                        }
                    }
                }

                sysinfo.vDiskPaths = string.Join("\t", vDiskPathsHs);

                //---

                var      physicalNics = GetPropertyContent(service, serviceContent, "HostSystem", "config.network.pnic", reference)[0].propSet[0].val as PhysicalNic[];
                string[] pNicsArr     = new string[physicalNics.Length];
                for (int i = 0; i != physicalNics.Length; i++)
                {
                    var candidate = physicalNics[i];
                    pNicsArr[i] = candidate.device + " " + candidate.driver + " driver (" + (candidate.linkSpeed == null ? "not connected)" : "connected)");
                }
                sysinfo.nics = string.Join("\t", pNicsArr);

                var ipmiPropset = GetPropertyContent(service, serviceContent, "HostSystem", "config.ipmi", reference)[0].propSet;
                sysinfo.bmcIp = ipmiPropset == null ? "Unable to detect" : ipmiPropset[0].val.ToString();

                return(sysinfo);
            }
            catch (Exception ex) {
                //Let IIS handle the errors, but using own logging.
                Loggers.Log(Level.Error, "Failed retrieving vhost system info", ex, new object[] { hostConnectionInfo });
                throw;
            }
        }
 public void Disconnect()
 {
     this.Logout();
     this._vimService     = (VimPortType)null;
     this._serviceContent = (ServiceContent)null;
 }