Пример #1
0
		public ICMP_PingResult Ping(String host, Int32 count = 4, Int32 timeout = 1000) 
		{
			bool infinite = false;
			ICMP_PingResult Result = new ICMP_PingResult();

			// Resolve the specified IP address
			IPAddress hostIP;
			try 
			{
				hostIP = Dns.Resolve(host).AddressList[0];
			} 
			catch (Exception e)
			{
				throw new Exception("ICMP_Ping: Unable to resolve the specified hostname.",e);
			}

			Result.hostIP = hostIP;

			// Start pinging
			try {
				Icmp icmp = new Icmp(hostIP);
				TimeSpan ret;
				while(infinite || count > 0) 
				{
					try 
					{
						ret = icmp.Ping(timeout);
						if (ret.Equals(TimeSpan.MaxValue))
							Result.Status = ICMP_Status.TimeOut;
						else
						{
							Result.Status = ICMP_Status.Success;
							Result.RoundtripMS.Add(ret.TotalMilliseconds);
						}
						if (1000 - ret.TotalMilliseconds > 0)
							Thread.Sleep(1000 - (int)ret.TotalMilliseconds);
					} 
					catch (Exception e) 
					{
						throw new Exception("Network Error",e);
					}
					if (!infinite)
						count--;
				}
			} catch {
				throw new Exception("Error while pinging the specified host.");
			}

			return Result;
		}
Пример #2
0
		// this is the network monitoring script
		public void Run()
        {
			ConsoleOutputLogger.WriteLine("Starting up the Network Monitoring...");
			ICMP pinger = new ICMP();

			while(running)
			{
				foreach(NetworkMonitoringHost Host in NetworkMonitorConfiguration.NetworkHosts)
				{
					// start pinging around
					ICMP_PingResult result = null;
					try
					{
						result = pinger.Ping(Host.IPAdressOrHostname,1,500);
					}
					catch(Exception)
					{
						//ConsoleOutputLogger.WriteLine("NetworkMonitor Exception: "+e.Message);
                        result = new ICMP_PingResult();
                        result.Status = ICMP_Status.TimeOut;
                        result.hostIP = null;
                    }

					// we got a result...
					if (result != null)
					{
						// it's already in the list
						if (OnOfflineList.ContainsKey(Host.IPAdressOrHostname))
						{
							if (OnOfflineList[Host.IPAdressOrHostname] == true) // this one was online before...
							{
								if (result.Status == ICMP_Status.TimeOut)
								{
									// it's now offline...
									OnOfflineList[Host.IPAdressOrHostname] = false;

									NetworkMonitoringDataSet ds_result = new NetworkMonitoringDataSet();
									
									ds_result.AverageRoundtripMS = result.AverageRoundtripMS;
									ds_result.Descriptor = Host.Descriptor;
									ds_result.HostnameIP = Host.IPAdressOrHostname;
									ds_result.Status = result.Status;
									ds_result.TimeOfMeasurement = result.TimeOfMeasurement;
									
									iQueue.Enqueue(ds_result);
								}
							}
							else
							{
								if (result.Status == ICMP_Status.Success)
								{
									// it's now online...
									OnOfflineList[Host.IPAdressOrHostname] = true;
									
									NetworkMonitoringDataSet ds_result = new NetworkMonitoringDataSet();
									
									ds_result.AverageRoundtripMS = result.AverageRoundtripMS;
									ds_result.Descriptor = Host.Descriptor;
									ds_result.HostnameIP = Host.IPAdressOrHostname;
									ds_result.Status = result.Status;
									ds_result.TimeOfMeasurement = result.TimeOfMeasurement;
									
									iQueue.Enqueue(ds_result);
								}
							}
						}
						else
						{
							if (result.Status == ICMP_Status.Success)
								OnOfflineList.Add(Host.IPAdressOrHostname,true);
							else
								OnOfflineList.Add(Host.IPAdressOrHostname,false);

							// enqueue
							NetworkMonitoringDataSet ds_result = new NetworkMonitoringDataSet();
							
							ds_result.AverageRoundtripMS = result.AverageRoundtripMS;
							ds_result.Descriptor = Host.Descriptor;
							ds_result.HostnameIP = Host.IPAdressOrHostname;
							ds_result.Status = result.Status;
							ds_result.TimeOfMeasurement = result.TimeOfMeasurement;
							
							iQueue.Enqueue(ds_result);
						}

					}
				}

				Thread.Sleep (NetworkMonitorUpdateTime);
			}
		}