Пример #1
0
        /// <summary>
        /// Register a new pingable object.
        /// </summary>
        /// <param name="obj">The object to register.</param>
        /// <returns>True if the object was successfully registered, false if the object was already registered.</returns>
        public static bool RegisterService(IPingable obj)
        {
            lock (lockObj)
            {
                if (!feeds.Contains(obj))
                {
                    obj.PropertyChanged += OnFeedPropertyChanged;

                    feeds.Add(obj);
                }
                else
                {
                    return(false);
                }

                UpdateTriggerGroups();

                if (IntervalThread == null)
                {
                    cts = new CancellationTokenSource();

                    IntervalThread = new Thread(IntervalMethod);
                    IntervalThread.IsBackground = true;
                    IntervalThread.Start();
                }

                return(true);
            }
        }
 public void StartMonitoringNewNode(string name, IPingable node)
 {
     lock (monitoredNodes)
     {
         monitoredNodes[name] = new MonitoredNode(name, node);
     }
 }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Pinger"/> class.
        /// </summary>
        /// <param name="moduleName">
        /// The module name.
        /// </param>
        /// <param name="processId">
        /// The process id.
        /// </param>
        /// <param name="needToSetProcessId">
        /// The need to set process id.
        /// </param>
        public Pinger(string moduleName, int processId, bool needToSetProcessId)
        {
            ModuleName = moduleName;
            ProcessId  = processId;
            Uri                 tcpUri  = new Uri(string.Format(AddressResolver.PingWcfServer, processId));
            EndpointAddress     address = new EndpointAddress(tcpUri);
            NetNamedPipeBinding binding = new NetNamedPipeBinding();

            factory             = new ChannelFactory <IPingable>(binding, address);
            mPingService        = factory.CreateChannel();
            timerClock.Elapsed += OnPing;
            timerClock.Interval = 5000;
            if (needToSetProcessId)
            {
                AgentSingleton.Instance.SetProcessId(ModuleName, ProcessId);
            }
        }
Пример #4
0
        /// <summary>
        /// 异步Ping远程目标
        /// </summary>
        /// <param name="target">目标</param>
        /// <param name="timeout">超时时间</param>
        /// <param name="tryCount">ping的次数</param>
        /// <returns>方法执行成功返回非空且非空白字符串,否则统一返回空串</returns>
        public static async Task <string> PingRemoteTargetAsync(IPingable target, int timeout, int tryCount)
        {
            string result = string.Empty;

            try
            {
                using (Process process = new Process())
                {
                    ProcessStartInfo startInfo = process.StartInfo;
                    startInfo.FileName = _pingCmd.PingName;
                    //对目标执行ping操作四次,超时为1秒
                    //todao:这里的ping4次数跟超时预设,需要使用数据库配置
                    if (tryCount < 1)
                    {
                        tryCount = 4;
                    }
                    startInfo.Arguments = string.Format($" {target.IpAddresV4} {_pingCmd.RepeatParam} {tryCount} {_pingCmd.TimeoutParam} {timeout.ToString()}");


                    //重定向输出流,便于获取ping命令结果
                    startInfo.RedirectStandardOutput = true;
                    //startInfo.StandardOutputEncoding = Encoding.UTF8;
                    NLogMgr.DebugLog(_programLog, $"执行命令:{_pingCmd.PingName} {startInfo.Arguments}");

                    //开始执行命令
                    process.Start();
                    using (StreamReader reader = process.StandardOutput)
                    {
                        result = await reader.ReadToEndAsync();

                        //NLogMgr.DebugLog(_programLog, $"{target.Remarks}执行完成===={TimeMgr.GetLoaclDateTime().ToString("yyyy-MM-dd HH:mm:ss:ffff")}====");
                        //NLogMgr.DebugLog(_programLog, result);
                        //Console.WriteLine($"{target.Remarks}执行完成===={TimeMgr.GetLoaclDateTime().ToString("yyyy-MM-dd HH:mm:ss:ffff")}====");
                    }
                }
            }
            catch (Exception ex)
            {
                //System.Console.WriteLine(ex.Message);
                NLogMgr.ErrorExLog(_errorLog, "执行Ping操作异常", ex);
            }

            return(result);
        }
Пример #5
0
        public MainWindowViewModel(IPingable pingable = default)
        {
            _pingable = pingable;

            if (pingable == default)
            {
                _pingable = new Pingable();
            }

            Config          = new PingConfig();
            Config.Address  = "192.168.229.95"; // "192.168.229.86";
            Config.Interval = 2000;
            Config.Timeout  = 100;

            _pingable.PingStatsUpdated += pingable_PingStatsUpdated;
            _pingable.PingCompleted    += pingable_PingCompleted;

            StartCommand = new RelayCommand(StartAction, () => !PingServiceRunning);
            StopCommand  = new RelayCommand(StopAction, () => PingServiceRunning);

            SeriesCollection = new SeriesCollection {
                new ScatterSeries {
                    Title = "Successful RTT", Values = new ChartValues <DateTimePoint>()
                },
                new ScatterSeries {
                    Title = "Failures", Values = new ChartValues <DateTimePoint>()
                }
            };

            _success = new List <bool>();

            /*SeriesCollection[0].Values.Add(new DateTimePoint(DateTime.Now, 0));
             * _success.Add(true);
             * SeriesCollection[1].Values.Add(new DateTimePoint(DateTime.Now, 0));
             * _success.Add(false);*/
            AxisMin    = DateTime.Now.AddSeconds(-5).Ticks;
            AxisMax    = DateTime.Now.AddSeconds(1).Ticks;
            XFormatter = (val) => new DateTime((long)val).ToString("HH:mm:ss");
        }
Пример #6
0
        /// <summary>
        /// Unregister a pingable object.
        /// </summary>
        /// <param name="obj">The object to unregister.</param>
        /// <returns>True if the object was successfully unregistered, false if the object was not registered.</returns>
        public static bool UnregisterService(IPingable obj)
        {
            lock (lockObj)
            {
                if (feeds.Contains(obj))
                {
                    feeds.Remove(obj);
                }
                else
                {
                    return(false);
                }

                if (feeds.Count == 0)
                {
                    CancelIntervalThread();
                }
                else
                {
                    UpdateTriggerGroups();
                }
                return(true);
            }
        }
 public MonitoredNode(string name, IPingable pingable)
 {
     this.pingable = pingable;
     this.Name     = name;
 }
 public NodeFailedEventArgs(string name, IPingable failedNode)
 {
     this.FailedNodeName = name;
     this.FailedNode     = failedNode;
 }
Пример #9
0
 /// <summary>
 /// The start.
 /// </summary>
 public void Start()
 {
     timerClock.Enabled = true;
     mPingService       = factory.CreateChannel();
 }