Ejemplo n.º 1
0
 private void M_shownet_Checked(object sender, RoutedEventArgs e)
 {
     lbl_download.Visibility = Visibility.Visible;
     lbl_upload.Visibility   = Visibility.Visible;
     netpanel.Visibility     = Visibility.Visible;
     mainpanel.Width        += 105;
     if (netlist.SelectedItem != null)
     {
         monitor.StartMonitoring(adapters[netlist.SelectedIndex]);
     }
 }
Ejemplo n.º 2
0
        private void GetNetworkUsage()
        {
            var monitor = new NetworkMonitor();

            NetworkAdapter[] adapters = monitor.Adapters;

            if (adapters.Length == 0)
            {
                throw new Exception("No network adapters found on this server");
            }

            monitor.StartMonitoring();

            for (int i = 0; i < 10; i++)
            {
                foreach (NetworkAdapter adapter in adapters)
                {
                    var down = adapter.DownloadSpeedKbps;
                    var up   = adapter.UploadSpeedKbps;

                    if (down > 0 || up > 0)
                    {
                        down = adapter.DownloadSpeedKbps;
                        up   = adapter.UploadSpeedKbps;

                        monitor.StopMonitoring();
                        Web.Write("{\"dl\": " + down + ", \"ul\": " + up + "}");
                        return;
                    }
                }

                System.Threading.Thread.Sleep(1000);
            }
        }
 private void lstCard_SelectedIndexChanged(object sender, EventArgs e)
 {
     // sự kiện chọn card mạng trong listbox
     monitor.StopMonitoring(); // ngưng không phân tích để phân tích card mạng đã chọn
     monitor.StartMonitoring(adapters[this.lstCard.SelectedIndex]);
     this.timer1.Start();      // start sự kiện bấm giờ
 }
Ejemplo n.º 4
0
        private void TimerCounter_Tick()
        {
            string down          = "kB/s";
            string up            = "KB/s";
            double downloadSpeed = 0;
            double uploadSpeed   = 0;

            for (int i = 0; i < adapters.Length; i++)
            {
                monitor.StartMonitoring();
                //Console.WriteLine(adapters[i].DownloadSpeedKbps);
                downloadSpeed += adapters[i].DownloadSpeedKbps;
                uploadSpeed   += adapters[i].UploadSpeedKbps;
            }
            //// 转换成KB
            //downloadSpeed /= 8;
            //uploadSpeed /= 8;
            // 转换成MB
            if (downloadSpeed > 1024)
            {
                downloadSpeed /= 1024;
                down           = "MB/s";
            }
            if (uploadSpeed > 1024)
            {
                uploadSpeed /= 1024;
                up           = "MB/s";
            }
            this.Download.Content = "↓ " + downloadSpeed.ToString("0.00") + down;
            this.Upload.Content   = "↑ " + uploadSpeed.ToString("0.00") + up;
            //this.LableDownloadValue.Text = String.Format("{0:n} kbps", adapter.DownloadSpeedKbps);
            //this.LabelUploadValue.Text = String.Format("{0:n} kbps", adapter.UploadSpeedKbps);
        }
Ejemplo n.º 5
0
 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     monitor.StopMonitoring();
     monitor.StartMonitoring(adapters[list.SelectedIndex]);
     register.CreateSubKey("KadirAydinli").SetValue("network", list.SelectedIndex);
     time.Start();
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 网络速度测试
        /// </summary>
        public void MonitorNetSpeed()
        {
            Double.TryParse(XMLconfigReader.getValbyName(_configPath, CInternetSpeed1), out _InnerSpeed1);
            Double.TryParse(XMLconfigReader.getValbyName(_configPath, CInternetSpeed2), out _InnerSpeed2);

            _monitor = new NetworkMonitor();
            //_monitor.NetworkSpeedChange += new NetworkMonitor.NetworkAdapterRefreshHandle(monitor_NetworkSpeedChange);
            string strAdapter = GetActivatedAdapter();

            if (strAdapter != null && strAdapter != String.Empty)
            {
                strAdapter = strAdapter.Substring(strAdapter.IndexOf(')') + 1, strAdapter.Length - strAdapter.IndexOf(')') - 1);
                strAdapter.Trim();
            }
            foreach (NetworkAdapter adapter in _monitor.Adapters)
            {
                string temp = adapter.Name;
                temp = temp.Substring(temp.IndexOf(']') + 1, temp.Length - temp.IndexOf(']') - 1);
                temp.Trim();
                if (temp.Equals(strAdapter))
                {
                    _curAdapter = adapter;//得到当前的网络Adapter
                    _monitor.StopMonitoring();
                    _monitor.StartMonitoring(adapter);
                }
            }
        }
Ejemplo n.º 7
0
        public MainViewModel()
        {
            _monitor  = new NetworkMonitor();
            _adapters = _monitor.Adapters;

            if (_adapters.Length == 0)
            {
                MessageBox.Show("No network adapters found on this computer.");
                return;
            }

            _monitor.StopMonitoring();
            _monitor.StartMonitoring(_adapters[1]);

            MyModel = new PlotModel {
                Title = "Up / Down (kbps)"
            };
            MyModel.Series.Add(new OxyPlot.Series.LineSeries());
            MyModel.Series.Add(new OxyPlot.Series.LineSeries());

            var dispatcherTimer = new DispatcherTimer {
                Interval = new TimeSpan(0, 0, 1)
            };

            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Start();
        }
Ejemplo n.º 8
0
        private void MainForm_Load(object sender, EventArgs e)
        {
            if (Configs.MonitorNetwork)
            {
                netMonitor = new NetworkMonitor();
                if (netMonitor.Adapters.Count() > 0)
                {
                    netMonitor.StartMonitoring();
                }
                timer2           = new System.Timers.Timer(1000);
                timer2.AutoReset = true;
                timer2.Elapsed  += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
                timer2.Start();
            }
            RefreshMsg("正在初始化系统...");
            ThreadPool.QueueUserWorkItem(delegate
            {
                int a = this.AdminId;//获取管理员ID缓存以便后面用...
            });
            LoginForm f8 = new LoginForm(this);

            if (f8.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.panel2.SendToBack();
                this.panel2.Hide();
            }
            else
            {
                notifyIcon1.Dispose();
                Environment.Exit(0);
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 初始化网卡检测器
 /// </summary>
 private void StateInit()
 {
     monitor.StopMonitoring();
     if (adapter == null)
     {
         foreach (var item in monitor.Adapters)
         {
             if (item.Name.StartsWith("Intel[R]"))
             {
                 monitor.StartMonitoring(item);
                 networkAdaptersList.Add(item);
             }
         }
     }
     else
     {
         monitor.StartMonitoring(adapter);
         networkAdaptersList.Add(adapter);
     }
 }
Ejemplo n.º 10
0
 static void Main(string[] args)
 {
     try
     {
         FirstInitial();
         var options = new Options();
         var parser  = new Parser();
         if (new Parser().ParseArguments(args, options))
         {
             NetworkMonitor monitor  = new NetworkMonitor();
             var            adapters = monitor.Adapters;
             if (adapters.Length == 0)
             {
                 Console.WriteLine("Network adapter interface not found, program terminated.");
                 return;
             }
             double dlLimit = options.Download;
             double ulLimit = options.Upload;
             int    i       = 0;
             Console.WriteLine($"Select 1 of {adapters.Count()} below network interfaces");
             monitor.StartMonitoring();
             adapters.ToList().ForEach(adapter =>
             {
                 Console.Write("Initializing...");
                 AdapterInitial(adapter, ref i);
                 Console.ForegroundColor = ConsoleColor.Gray;
             }); //load adapter list, also measure which adapter is active for network connection
             System.IO.File.Delete("temp");
             Console.Write("select : ");
             var selectedInterface = Convert.ToInt32(Console.ReadLine()) - 1;
             var selectedAdapter   = adapters[selectedInterface];
             Monitor(dlLimit, ulLimit, selectedAdapter, options.Force, options.Interval);
         }
         else
         {
             Console.WriteLine(options.GetUsage());
         }
     }
     catch (FormatException)
     {
         Console.WriteLine("You must choose one of the interfaces above.");
         Environment.Exit(0);
     }
     catch (IndexOutOfRangeException)
     {
         Console.WriteLine("You can't select non-exists network interface.");
         Environment.Exit(0);
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         Environment.Exit(0);
     }
 }
Ejemplo n.º 11
0
 void SetAdapterToMonitor()
 {
     _adapter = _adapters.FirstOrDefault(a => a.Name == ConfigHelper.AdapterName);
     try
     {
         monitor.StartMonitoring(_adapter);
         _timer.Elapsed += RefreshView;
         _timer.Start();
     }
     catch (Exception)
     {
         MessageBox.Show("Not selected adapter.");
     }
 }
Ejemplo n.º 12
0
        static int Run(RunActionOptions runActionOptions)
        {
            Console.WriteLine("Running...");
            var configuration = ConfigurationRepository.Load(runActionOptions.ConfigFilePath);

            Console.WriteLine("Triggers:");
            foreach (var trigger in configuration.Triggers)
            {
                Console.WriteLine($"{trigger.SourceMacAddress} => {trigger.ActionName}");
            }
            Console.WriteLine();

            var networkMonitor = new NetworkMonitor(configuration, new ActionExecutor());

            networkMonitor.StartMonitoring();
            return(0);
        }
Ejemplo n.º 13
0
        private void Index_Load(object sender, EventArgs e)
        {
            netMonitor = new NetworkMonitor();
            if (netMonitor.Adapters.Length > 0)
            {
                netMonitor.StartMonitoring();
            }
            timer2           = new System.Timers.Timer(1000);
            timer2.AutoReset = true;
            timer2.Elapsed  += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
            timer2.Start();
            ShowStatus("正在初始化系统...");
            Login login = new Login();

            if (login.ShowDialog() == DialogResult.OK)
            {
                Logs.LogLogin();
                ShowStatus("Ready...");
            }
            else
            {
                SureExit();
            }
        }
Ejemplo n.º 14
0
        private static Dictionary <string, double> NetworkUsage()
        {
            Dictionary <string, double> temp = new Dictionary <string, double>();

            double DownloadSpeed = 0;
            double UploadSpeed   = 0;

            NetworkMonitor monitor = new NetworkMonitor();

            NetworkAdapter[] adapters = monitor.Adapters;

            // Start a timer to obtain new performance counter sample every second.
            monitor.StartMonitoring();

            for (int i = 0; i < 10; i++)
            {
                foreach (NetworkAdapter adapter in adapters)
                {
                    // The DownloadSpeedKbps and UploadSpeedKbps are
                    // double values.

                    DownloadSpeed += adapter.DownloadSpeedKbps;
                    UploadSpeed   += adapter.UploadSpeedKbps;
                }
                System.Threading.Thread.Sleep(1000); // Sleeps for one second.
            }

            // Stop the timer. Properties of adapter become invalid.
            monitor.StopMonitoring();


            temp.Add("upload", (UploadSpeed / 8) * 1024);
            temp.Add("download", (DownloadSpeed / 8) * 1024);

            return(temp);
        }
Ejemplo n.º 15
0
 private void ListAdapters_SelectedIndexChanged(object sender, System.EventArgs e)
 {
     monitor.StopMonitoring();
     monitor.StartMonitoring(adapters[this.ListAdapters.SelectedIndex]);
     this.timer2.Start();
 }
Ejemplo n.º 16
0
        public void MonitorPerformance()
        {
            // 开启一个定时器用以每秒钟计算数据包
            _monitor.StartMonitoring();

            int interval = 10000;

            //此线程针对10秒一次查询
            threadSecond = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        //系统内存
                        MongoDBHelper.Insert <SystemMemories>(prohelper.GetSystemMemory(), MongoConnType.Project);
                        //系统Cpu
                        MongoDBHelper.Insert <SystemCpu>(prohelper.GetSystemCpu(), MongoConnType.Project);
                        //系统网络
                        MongoDBHelper.Insert <SystemInternet>(prohelper.GetSystemInternet(_adapters), MongoConnType.Project);
                        //进程内存
                        var processMemories = MongoDBHelper.Get <ActivityConfigures>(Query.EQ("ActivityName", BsonValue.Create("进程内存设置")), MongoConnType.Center);
                        if (processMemories != null)
                        {
                            var Model = prohelper.GetProcessMemories(processMemories.ProcessName);
                            if (!string.IsNullOrWhiteSpace(Model.ProcessName))
                            {
                                MongoDBHelper.Insert <ProcessMemories>(Model, MongoConnType.Project);
                            }
                        }
                        //进程网络
                        var processInternet = MongoDBHelper.Get <ActivityConfigures>(Query.EQ("ActivityName", BsonValue.Create("进程网络设置")), MongoConnType.Center);
                        if (processInternet != null)
                        {
                            var Model = prohelper.GetProcessInternet(processInternet.ProcessName, interval);
                            if (!string.IsNullOrWhiteSpace(Model.ProcessName))
                            {
                                MongoDBHelper.Insert <ProcessInternet>(Model, MongoConnType.Project);
                            }
                        }
                        Thread.Sleep(interval);
                    }
                    catch (Exception e)
                    {
                        logHelper.Write(new Msg(e.Message, MsgType.Error));
                        logHelper.Dispose();
                    }
                }
            });
            threadSecond.Start();

            //此线程针对一天一次查询
            threadDay = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        //系统硬盘
                        var systemHddList = prohelper.GetSystemHdd();
                        foreach (var item in systemHddList)
                        {
                            MongoDBHelper.Insert <SystemHdd>(item, MongoConnType.Project);
                        }
                        Thread.Sleep(86400000);
                    }
                    catch (Exception e)
                    {
                        logHelper.Write(new Msg(e.Message, MsgType.Error));
                        logHelper.Dispose();
                    }
                }
            });
            threadDay.Start();

            //进程Cpu需要对Cpu一段时间内的运行进行计算,需单独处理
            threadProcessCpu = new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        var processCpu = MongoDBHelper.Get <ActivityConfigures>(Query.EQ("ActivityName", BsonValue.Create("进程Cpu设置")), MongoConnType.Center);
                        if (processCpu != null)
                        {
                            if (Process.GetProcessesByName(processCpu.ProcessName).Length > 0)
                            {
                                using (var pro = Process.GetProcessesByName(processCpu.ProcessName)[0])
                                {
                                    //上次记录的CPU时间
                                    var prevCpuTime = TimeSpan.Zero;
                                    while (true)
                                    {
                                        //当前时间
                                        var curTime = pro.TotalProcessorTime;
                                        //间隔时间内的CPU运行时间除以逻辑CPU数量
                                        var value   = (curTime - prevCpuTime).TotalMilliseconds / interval / Environment.ProcessorCount * 100;
                                        prevCpuTime = curTime;
                                        MongoDBHelper.Insert <ProcessCpu>(new ProcessCpu
                                        {
                                            ProcessName      = processCpu.ProcessName,
                                            ProcessUsageRate = Math.Round(value, 1),
                                            DateTimeNow      = DateTime.Now
                                        }, MongoConnType.Project);
                                        Thread.Sleep(interval);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        logHelper.Write(new Msg(e.Message, MsgType.Error));
                        logHelper.Dispose();
                    }
                }
            });

            threadProcessCpu.Start();
            //进程Cpu
        }