예제 #1
0
        protected void TimerGet_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                _timerGet.Enabled = false;

                _dto = GetInfo();
                if (_islog)
                {
                    LogInfo("TimerGet: " + Newtonsoft.Json.JsonConvert.SerializeObject(_dto));
                }

                if (!string.IsNullOrEmpty(_rabbitHost) && _rabbitPort > 0 && !string.IsNullOrEmpty(_rabbitPerformanceHighKey))
                {
                    if (_dto != null && (_dto.CPUHigh || _dto.RAMHigh || _dto.HDDHigh))
                    {
                        if (_dtHighNext != null && DateTime.Now.CompareTo(_dtHighNext.Value) > 0)
                        {
                            var factory = new ConnectionFactory()
                            {
                                HostName = _rabbitHost, Port = _rabbitPort, UserName = _rabbitUserName, Password = _rabbitPassword
                            };
                            using (var connection = factory.CreateConnection())
                                using (var channel = connection.CreateModel())
                                {
                                    channel.QueueDeclare(queue: _rabbitPerformanceHighKey, durable: false, exclusive: false, autoDelete: false, arguments: null);
                                    string str = Newtonsoft.Json.JsonConvert.SerializeObject(_dto);
                                    channel.BasicPublish("", _rabbitPerformanceHighKey, null, Encoding.Unicode.GetBytes(str));

                                    if (_islog)
                                    {
                                        LogInfo("TimerGet send rabbit");
                                    }
                                }

                            _dtHighNext = DateTime.Now.AddSeconds(_secondHighCheck);
                        }
                    }
                }



                _timerGet.Enabled = true;
            }
            catch (Exception ex)
            {
                LogError(ex);
                _timerGetReset.Enabled = true;
            }
        }
예제 #2
0
        protected override void OnStart(string[] args)
        {
            try
            {
                var timersend = System.Configuration.ConfigurationManager.AppSettings.Get("TimerSend");
                var logdata   = System.Configuration.ConfigurationManager.AppSettings.Get("LogData");
                _servername = System.Configuration.ConfigurationManager.AppSettings.Get("ServerName");

                var rabbitHost               = System.Configuration.ConfigurationManager.AppSettings.Get("RabbitHost");
                var rabbitPort               = System.Configuration.ConfigurationManager.AppSettings.Get("RabbitPort");
                var rabbitUserName           = System.Configuration.ConfigurationManager.AppSettings.Get("RabbitUserName");
                var rabbitPassword           = System.Configuration.ConfigurationManager.AppSettings.Get("RabbitPassword");
                var rabbitPerformanceDataKey = System.Configuration.ConfigurationManager.AppSettings.Get("RabbitPerformanceDataKey");
                var rabbitPerformanceHighKey = System.Configuration.ConfigurationManager.AppSettings.Get("RabbitPerformanceHighKey");

                var cpuHighPercent  = System.Configuration.ConfigurationManager.AppSettings.Get("CPUHighPercent");
                var ramHighPercent  = System.Configuration.ConfigurationManager.AppSettings.Get("RAMHighPercent");
                var hddHighPercent  = System.Configuration.ConfigurationManager.AppSettings.Get("HDDHighPercent");
                var sendHigh        = System.Configuration.ConfigurationManager.AppSettings.Get("SendHigh");
                var secondHighCheck = System.Configuration.ConfigurationManager.AppSettings.Get("SecondHighCheck");
                var secondCheck     = System.Configuration.ConfigurationManager.AppSettings.Get("SecondCheck");
                _batchFileInit  = System.Configuration.ConfigurationManager.AppSettings.Get("BatchInit");
                _batchFileCheck = System.Configuration.ConfigurationManager.AppSettings.Get("BatchCheck");
                _batchFileClear = System.Configuration.ConfigurationManager.AppSettings.Get("BatchClear");

                if (!string.IsNullOrEmpty(timersend) && !string.IsNullOrEmpty(logdata) && !string.IsNullOrEmpty(_servername) && !string.IsNullOrEmpty(sendHigh) && !string.IsNullOrEmpty(secondCheck))
                {
                    int i = Convert.ToInt32(timersend);
                    _secondCheck     = Convert.ToInt32(secondCheck);
                    _secondHighCheck = Convert.ToDouble(secondHighCheck);
                    if (i > 0 && _secondCheck > 5)
                    {
                        _lstDTO     = new List <InfoData>();
                        _islog      = logdata == "true";
                        _isSendHigh = sendHigh == "true";
                        if (!string.IsNullOrEmpty(rabbitPort))
                        {
                            _rabbitPort = Convert.ToInt32(rabbitPort);
                        }
                        _rabbitHost               = rabbitHost;
                        _rabbitUserName           = rabbitUserName;
                        _rabbitPassword           = rabbitPassword;
                        _rabbitPerformanceDataKey = rabbitPerformanceDataKey;
                        _rabbitPerformanceHighKey = rabbitPerformanceHighKey;

                        _batchClear    = false;
                        _batchRun      = false;
                        _batchComplete = false;

                        if (!string.IsNullOrEmpty(cpuHighPercent))
                        {
                            _cpuHighPercent = Convert.ToSingle(cpuHighPercent);
                        }
                        if (!string.IsNullOrEmpty(ramHighPercent))
                        {
                            _ramHighPercent = Convert.ToSingle(ramHighPercent);
                        }
                        if (!string.IsNullOrEmpty(hddHighPercent))
                        {
                            _hddHighPercent = Convert.ToSingle(hddHighPercent);
                        }
                        if (_cpuHighPercent < 1 || _ramHighPercent < 1 || _hddHighPercent < 1)
                        {
                            throw new Exception("HighPercent fail");
                        }

                        HelperProcess.Init();
                        Microsoft.VisualBasic.Devices.ComputerInfo ci = new Microsoft.VisualBasic.Devices.ComputerInfo();
                        _totalPhysicalMemory = (ci.TotalPhysicalMemory / 1024) * 0.001;
                        _cpuUsage            = new PerformanceCounter("Processor", "% Processor Time", "_Total");
                        _dto = GetInfo();

                        LogInfo("Start service (" + _servername + ")");
                        _timerGet               = new System.Timers.Timer(1000);//1s
                        _timerGet.Elapsed      += TimerGet_Elapsed;
                        _timerGet.Enabled       = true;
                        _timerGetReset          = new System.Timers.Timer(600000);//10p reset
                        _timerGetReset.Enabled  = false;
                        _timerGetReset.Elapsed += TimerGetReset_Elapsed;

                        _timerSend               = new System.Timers.Timer(i);
                        _timerSend.Elapsed      += TimerSend_Elapsed;
                        _timerSend.Enabled       = true;
                        _timerSendReset          = new System.Timers.Timer(600000);//10p reset
                        _timerSendReset.Enabled  = false;
                        _timerSendReset.Elapsed += TimerSendReset_Elapsed;
                    }
                    else
                    {
                        throw new Exception("TimerSend fail");
                    }
                }
                else
                {
                    throw new Exception("Config fail");
                }
            }
            catch (Exception ex)
            {
                LogError(ex);
            }
        }
예제 #3
0
        private InfoData GetInfo()
        {
            var result      = new InfoData();
            var ramMBUsage  = new PerformanceCounter("Memory", "Available MBytes");
            var hddPerUsage = new PerformanceCounter("LogicalDisk", "% Free Space", "_Total", true);
            var hddMBUsage  = new PerformanceCounter("LogicalDisk", "Free Megabytes", "_Total", true);

            result.ServerName      = _servername;
            result.RabbitDate      = DateTime.Now;
            result.CPUUsagePercent = _cpuUsage.NextValue();
            result.RAMFreeMB       = ramMBUsage.NextValue();
            result.HDDFreePercent  = hddPerUsage.NextValue();
            result.HDDFreeMB       = hddMBUsage.NextValue();
            result.HDDHigh         = false;
            result.RAMHigh         = false;
            result.CPUHigh         = false;
            PerformanceCounter.CloseSharedResources();
            result.ListDetails = HelperProcess.GetProcessTop();

            if (_lstDTO != null)
            {
                if (_lstDTO.Count >= _secondCheck)
                {
                    int cpu = 0;
                    int ram = 0;
                    int hdd = 0;

                    foreach (var item in _lstDTO)
                    {
                        if (item.CPUUsagePercent > _cpuHighPercent)
                        {
                            cpu += 2;
                        }
                        else if (item.CPUUsagePercent > _cpuHighPercent - 5)
                        {
                            cpu++;
                        }
                        if (item.RAMFreeMB / (_totalPhysicalMemory / 100) < _ramHighPercent)
                        {
                            ram += 2;
                        }
                        else if (item.RAMFreeMB / (_totalPhysicalMemory / 100) < _ramHighPercent + 5)
                        {
                            ram++;
                        }
                        if (item.HDDFreePercent < _hddHighPercent)
                        {
                            hdd += 2;
                        }
                        else if (item.HDDFreePercent < _hddHighPercent + 5)
                        {
                            hdd++;
                        }
                    }
                    if (result.CPUUsagePercent > _cpuHighPercent)
                    {
                        cpu += 2;
                    }
                    else if (result.CPUUsagePercent > _cpuHighPercent - 5)
                    {
                        cpu++;
                    }
                    if (result.RAMFreeMB / (_totalPhysicalMemory / 100) < _ramHighPercent)
                    {
                        ram += 2;
                    }
                    else if (result.RAMFreeMB / (_totalPhysicalMemory / 100) < _ramHighPercent + 5)
                    {
                        ram++;
                    }
                    if (result.HDDFreePercent < _hddHighPercent)
                    {
                        hdd += 2;
                    }
                    else if (result.HDDFreePercent < _hddHighPercent + 5)
                    {
                        hdd++;
                    }

                    var check = ((_secondCheck + 1) * 2) - ((_secondCheck + 1) / 4);
                    result.CPUHigh = cpu >= check;
                    result.RAMHigh = ram >= check;
                    result.HDDHigh = hdd >= check;

                    if (_islog)
                    {
                        LogInfo(string.Format("CPU:{1}>={0} RAM:{2}>={0} HDD:{3}>={0}", check, cpu, ram, hdd));
                    }

                    _lstDTO.RemoveAt(_lstDTO.Count - 1);
                }

                _lstDTO.Insert(0, new InfoData
                {
                    ServerName      = result.ServerName,
                    RabbitDate      = result.RabbitDate,
                    CPUUsagePercent = result.CPUUsagePercent,
                    RAMFreeMB       = result.RAMFreeMB,
                    HDDFreePercent  = result.HDDFreePercent,
                    HDDFreeMB       = result.HDDFreeMB,
                    HDDHigh         = result.HDDHigh,
                    RAMHigh         = result.RAMHigh,
                    CPUHigh         = result.CPUHigh
                });
            }

            return(result);
        }