Beispiel #1
0
        public void CheckStatus()
        {
            Task t = Task.Run(() =>
            { //Prevents Lock of Form
                try
                {
                    Parallel.ForEach(ips, (x) => //does parallel but only exit after all finished
                    {
                        if (x.Last_Checked == null || (x.Last_Checked + TimeSpan.FromSeconds(cycleTime) < DateTime.Now && x.RunningCheck == false))
                        {//Never checked or last Check older than cycleTime
                            x.RunningCheck  = true;
                            x.Last_Checked  = DateTime.Now;
                            PingReply reply = Pinger.PingIP(x.IP_Address.ToString(), timeout);  //new PingReply();

                            if (reply != null)
                            {//Ping is not null (means success or not)
                                string hostname = "";
                                if (cbHostnames.Checked == true && x.Hostname != null)
                                { //Resolve hostname is enabled
                                    hostname   = Hostname.Get(x.IP_Address.ToString());
                                    x.Hostname = hostname;
                                }
                                bool online = false;

                                if (reply.Status == IPStatus.Success)
                                {//Host is online
                                    online = true;
                                    if (x.First_Seen == null)
                                    {
                                        x.First_Seen = DateTime.Now;
                                    }
                                    x.Roundtrip = Convert.ToInt32(reply.RoundtripTime);
                                    x.Last_Seen = DateTime.Now;
                                    if (port != 0)
                                    {
                                        x.PortOpen = PortChecker.ScanPort(x.IP_Address, port);
                                    }
                                }
                                if (x.Active == false && online == true)
                                {//Host Status changed from offline to online
                                 //New Host
                                    if (port != 0)
                                    {
                                        messageBox.Push("NEW:                   " + x.IP_Address + ", " + x.Hostname + " First Seen: " + x.First_Seen + " Port " + port + ": " + x.PortOpen);
                                    }
                                    else
                                    {
                                        messageBox.Push("NEW:                   " + x.IP_Address + ", " + x.Hostname + " First Seen: " + x.First_Seen);
                                    }
                                }
                                else if (x.Active == true && online == false)
                                {//Host Status changed from online to offline
                                    if (port != 0)
                                    {
                                        messageBox.Push("DISAPPEARED: " + x.IP_Address + ", " + x.Hostname + " Last Seen: " + x.Last_Seen + " Port " + port + ": " + x.PortOpen);
                                    }
                                    else
                                    {
                                        messageBox.Push("DISAPPEARED: " + x.IP_Address + ", " + x.Hostname + " Last Seen: " + x.Last_Seen);
                                    }
                                }
                                x.Last_Checked = DateTime.Now;
                                x.Active       = online;
                                x.CountChecks++;
                                x.RunningCheck = false;
                            }
                        }
                    });
                }
                catch (System.AggregateException e)
                {
                    throw e;
                }
            });

            UpdateMessage();
        }