示例#1
0
        private void OnTimerElapsed(Object state)
        {
            // get next workstation to probe
            Workstation workstation = null;

            {
                lock (_guard)
                {
                    if (_workstations.Count > 0)
                    {
                        // get one
                        workstation = _workstations[0];

                        // and pop it
                        _workstations.RemoveAt(0);
                    }
                }
            }

            // see if we were able to get something
            if (workstation != null)
            {
                log.DebugFormat("Probing workstation {0}.", workstation.DnsHostName);

                // ok we have a workstation to probe, do it
                List <Address> addresses = WorkstationProber.Probe(workstation);
                foreach (var address in addresses)
                {
                    log.DebugFormat("Workstation {0} will be added with address {1}.", workstation.DnsHostName, address.AsString);

                    lock (_guard)
                    {
                        _storage.Insert(address);
                    }
                }
            }
            else
            {
                log.DebugFormat("No workstations to probe remaining, getting the list of existing workstations from LDAP server...");

                // we do not have any workstations left, issue a call to LDAP server
                List <Workstation> workstations = WorkstationHarvester.Harvest();

                log.DebugFormat("Got {0} workstations from LDAP server, adding them all to probe queue.", workstations.Count);

                // and update the list in class
                lock (_guard)
                {
                    _workstations = workstations;
                }
            }
        }
示例#2
0
        public static List <Address> Probe(Workstation workstation)
        {
            // this is the result
            List <Address> result = new List <Address>();

            try
            {
                log.DebugFormat("Resolving workstation {0}", workstation.DnsHostName);

                // get host entry (this clears DNS cache in .net too)
                var hostenry = Dns.GetHostEntry(workstation.DnsHostName);

                // get addresses
                var addresses = hostenry.AddressList;
                {
                    log.DebugFormat("Workstation {0} was resolved into {1} IP addresses", workstation.DnsHostName, addresses.Length);

                    foreach (IPAddress ip in addresses)
                    {
                        log.DebugFormat("Workstation {0} has address {1}", workstation.DnsHostName, ip.ToString());
                    }
                }

                // resolve the workstation name, get all addresses from it
                foreach (IPAddress ip in addresses)
                {
                    Address address = Prober.Probe(ip);
                    if (address.Users.Count == 0)
                    {
                        // log it
                        log.DebugFormat("Probing of address {0} for workstation {1} indicated the number of logged in users are 0, skipping it.", ip.ToString(), workstation.DnsHostName);

                        // there are no one logged on on that IP, skip it then
                        continue;;
                    }

                    // debug check we have filled in the address itself
                    Debug.Assert(address.IP == ip);
                    Debug.Assert(address.Users.Count > 0);

                    // assign some other fields for reference
                    address.DistinguishedName = workstation.DistinguishedName;
                    address.CommonName        = workstation.CommonName;
                    address.DnsHostName       = workstation.DnsHostName;
                    address.LastLogon         = workstation.LastLogon;
                    address.Name = workstation.Name;

                    // log it
                    log.DebugFormat("Address {0} was probed successfully, {1} users found. Adding it to the storage.", ip.ToString(), address.Users.Count);

                    // and add this address
                    result.Add(address);
                }
            }
            catch (Exception e)
            {
                log.WarnFormat("Probe failed for workstation {0}. Error: {1}", workstation.DnsHostName, e.Message);
            }

            // and return (possibly empty) list
            return(result);
        }
示例#3
0
        public static List <Workstation> Harvest()
        {
            // trace it
            log.Debug("Starting LDAP search for existing workstations in the Active Directory...");

            // this is the value to return
            List <Workstation> result = new List <Workstation>();

            // connect to LDAP
            using (DirectoryEntry root = new DirectoryEntry())
            {
                using (DirectorySearcher searcher = new DirectorySearcher(root))
                {
                    searcher.PropertiesToLoad.AddRange(new[] { "cn", "distinguishedName", "dNSHostName", "lastLogon", "name" });
                    searcher.PageSize      = 1000;
                    searcher.ClientTimeout = new TimeSpan(0, 0, 20);
                    searcher.Filter        = "(objectCategory=computer)";

                    var entries = searcher.FindAll();
                    foreach (SearchResult entry in entries)
                    {
                        Workstation w = new Workstation();
                        {
                            if (entry.Properties["cn"].Count > 0)
                            {
                                w.CommonName = (string)entry.Properties["cn"][0];
                            }

                            if (entry.Properties["distinguishedName"].Count > 0)
                            {
                                w.DistinguishedName = (string)entry.Properties["distinguishedName"][0];
                            }

                            if (entry.Properties["dNSHostName"].Count > 0)
                            {
                                w.DnsHostName = (string)entry.Properties["dNSHostName"][0];
                            }

                            if (entry.Properties["lastLogon"].Count > 0)
                            {
                                w.LastLogon = (Int64)entry.Properties["lastLogon"][0];
                            }

                            if (entry.Properties["name"].Count > 0)
                            {
                                w.Name = (string)entry.Properties["name"][0];
                            }

                            log.DebugFormat("Found workstation {0}", w.DnsHostName);
                        }
                        result.Add(w);
                    }
                }
            }

            // trace it
            log.DebugFormat("LDAP search for workstations finished, got {0} workstations.", result.Count);

            // and return
            return(result);
        }