Exemple #1
0
        private static void RecycleGuard()
        {
            while (Common.IsRun)
            {
                try
                {
                    //获取所有监控状态
                    List <MqCount> list = MSMQHelper.GetMqCount(_config);

                    if (list != null && list.Count > 0)
                    {
                        IStateCenterConnector connector = StateCenterConnectorFactory.GetAvailableConnector();
                        string msg = string.Empty;
                        foreach (MqCount mq in list)
                        {
                            msg += mq.Name + "@" + mq.Count + "@" + mq.Remark + "|";
                        }
                        connector.SendMsg("msmq", msg);
                    }
                }
                catch (Exception ex)
                {
                    CommonLog.InnerErrorLog.Error("mq监控出错:" + ex.ToString());
                }
                Thread.Sleep(Config.Internal);
            }
        }
        /// <summary>
        /// 寻找可用的状态中心
        /// </summary>
        /// <returns></returns>
        public static IStateCenterConnector GetAvailableConnector()
        {
            IStateCenterConnector connector = GetConnector(CommonConfig.ConnectorType);

            if (connector == null || connector.IsCenterAlived() == false)
            {
                connector = GetConnector(CommonConfig.BackupConnectorType);
            }
            return(connector);
        }
Exemple #3
0
        /// <summary>
        /// 内部循环
        /// </summary>
        private static void Recycle()
        {
            // 创建自己的日志
            IDLog log = new TinyLog();

            log.Init("beat", "beat/log");



            if (string.IsNullOrEmpty(AppName))
            {
                log.Error("节点BeatAppName 未配置,请配置");
                return;
            }
            string beatName = ComputerInfo.GetMacAddress() + "." + AppName;

            while (RecycleRunState)
            {
                log.Info("开始一次心跳");
                IStateCenterConnector connector = StateCenterConnectorFactory.GetAvailableConnector();
                if (connector == null)
                {
                    log.Error("没有找到可用的状态中心连接器");
                }
                else
                {
                    try
                    {
                        //不管有没有值,都发送到远端
                        log.Info("发送到远端" + beatName);
                        if (connector.Beat(beatName, LastBeatTime))
                        {
                            log.Info("发送成功");
                        }
                        else
                        {
                            log.Error("发送失败");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("心跳出错:" + ex.ToString());
                    }
                }
                Thread.Sleep(CommonConfig.BeatInternal);
            }
        }
Exemple #4
0
        /// <summary>
        /// 核心方法
        /// </summary>
        private static void RecycleGuard()
        {
            //加载本地配置
            var mqConfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mq.config");

            try
            {
                if (File.Exists(mqConfig))
                {
                    _config = XmlHelper.Xml2Entity(mqConfig, new MSMQConfig().GetType()) as MSMQConfig;
                }
            }
            catch (Exception ex)
            {
                CommonLog.InnerErrorLog.Error("读取本地mq.config出错:" + ex.ToString() + ",继续逻辑");
            }
            int count = 0;

            while (Common.IsRun)
            {
                Thread.Sleep(Config.Internal);

                try
                {
                    //获取网络连接器
                    IStateCenterConnector connector = StateCenterConnectorFactory.GetConnector("web");

                    string lastConfigTime = connector.Promise("getLastConfigTime", "", "");
                    if (string.IsNullOrEmpty(lastConfigTime))
                    {
                        if (count++ > 10000)
                        {
                            //一直连不上
                            return;
                        }
                        //无法连接的web服务器时,等待重试
                        continue;
                    }
                    if (string.IsNullOrEmpty(_lastConfigTime) || lastConfigTime != _lastConfigTime)
                    {
                        _lastConfigTime = lastConfigTime;
                        //进行下载云端配置
                        string remoteConfig = connector.Promise("getMSMQConfig", "", "");
                        //空时,认为远端未配置,不进行读取执行
                        if (string.IsNullOrEmpty(remoteConfig) == false)
                        {
                            //存储到本地
                            File.WriteAllText(mqConfig, remoteConfig);

                            //重新加载配置
                            try
                            {
                                _config = XmlHelper.Xml2Entity(mqConfig, new MSMQConfig().GetType()) as MSMQConfig;
                            }
                            catch (Exception ex)
                            {
                                CommonLog.InnerErrorLog.Error("读取Remote mq.config出错:" + ex.ToString() + ",中断运行");
                                return;
                            }
                        }
                    }

                    //判断是否成功读取配置文件
                    if (_config == null)
                    {
                        //如果为空继续抓取远端配置
                        continue;
                    }
                    //执行监控
                    List <MonitorResult> mrs = WinMonitorHelper.RunMonitor(_config);
                    //推送数据到服务器
                    string msg = string.Empty;
                    foreach (MonitorResult mr in mrs)
                    {
                        msg += (mr.Domain ?? "") + "~" + (mr.CategoryName ?? "") + "~" + (mr.CounterName ?? "") + "~"
                               + (mr.Instance ?? "") + "~" + (mr.Result ?? "") + "~" + (mr.Remark ?? "") + "|";
                    }
                    connector.Promise("sendMonitorMsg", "key", msg.TrimEnd(new char[] { '|' }));
                }
                catch (Exception ex)
                {
                    CommonLog.InnerErrorLog.Error("mq监控出错:" + ex.ToString());
                }
            }
        }
Exemple #5
0
        private void RecycleGuard()
        {
            // 创建自己的日志
            IDLog log = new TinyLog();

            log.Init(_config.ID + "_" + _config.AppName, _config.ID + "_" + _config.AppName + "/log");
            int count1 = 0;

            _appErrExecutor.Log = log;

            //监控间隔提醒
            if (_config.GuardInternal < 1500)
            {
                log.Fatal("注意: 配置的监控间隔小于1.5秒,监控非常容易出问题,监控停止!GuardInternal:" + _config.GuardInternal);
                return;
            }
            else if (_config.GuardInternal < 5000)
            {
                log.Warn("注意: 配置的监控间隔小于5秒,可能存在问题!GuardInternal:" + _config.GuardInternal);
            }


            while (Common.IsRun)
            {
                Thread.Sleep(_config.GuardInternal);
                log.Info("开始一次守护主流程+++++++++++++++++++++++++++++++++++++++++++++++");
                //进行进程守护 // 不管是哪种守护类型,都需要进行守护
                try
                {
                    if (_config.AppType == (int)Enums.EnumAppType.务)
                    {
                        ServiceManger sm = new ServiceManger();
                        if (sm.GetServiceValue(_config.AppName, "State").ToString().Equals(ServiceState.Stopped))
                        {
                            log.Error("服务状态已停止,开始处理错误");
                            _appErrExecutor.HandleError(Enums.EnumHandleCondition.务进程停止, _config, LastProcessTime);
                            continue;
                        }
                        else
                        {
                            log.Info("服务状态正在运行!");
                            LastProcessTime = DateTime.Now;
                        }
                    }
                    else
                    {
                        if (ProcessHelper.ExistProcess(_config.AppName) == false)
                        {
                            log.Error("进程已停止,开始处理错误");
                            _appErrExecutor.HandleError(Enums.EnumHandleCondition.务进程停止, _config, LastProcessTime);
                            continue;
                        }
                        else
                        {
                            log.Info("进程运行正常!");
                            LastProcessTime = DateTime.Now;
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(string.Format("进程守护 ID[{0}] 名称[{1}] 出错: ", _config.ID, _config.AppName) + ex.ToString());
                    continue;
                }

                if (_config.GuardType == (int)Enums.EnumGuardType.心跳守护)
                {
                    log.Info("开始心跳检测");
                    try
                    {
                        log.Info("开始检测状态中心健康情况");
                        IStateCenterConnector connector = StateCenterConnectorFactory.GetAvailableConnector();
                        if (connector == null || !connector.IsCenterAlived())
                        {
                            log.Error("状态中心无法连接,开始处理错误:");
                            _appErrExecutor.HandleError(Enums.EnumHandleCondition.务不可见, _config, LastStateCenterTime);
                            continue;
                        }
                        else
                        {
                            log.Info("状态中心连接正常");
                            LastStateCenterTime = DateTime.Now;
                        }
                        string beatName = ComputerInfo.GetMacAddress() + "." + _config.AppName;
                        log.Info(string.Format("开始获取心跳时间:[{0}]", beatName));
                        // 心跳检测
                        DateTime?lastBeatTime = connector.GetLastBeatTime(beatName);
                        if (lastBeatTime.HasValue == false)
                        {
                            log.Error("连接的服务未向中心发出心跳,首次连接不正常,不进行处理");
                            count1++;
                            if (count1 > 100)
                            {
                                log.Error("超过100次,无法抓取到首次心跳反应,监控终止");
                                return;
                            }
                        }
                        else
                        {
                            //首次心跳
                            if (LastBeatTime == null)
                            {
                                LastBeatTime = lastBeatTime;
                                log.Info("首次心跳时间:" + LastBeatTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                            }
                            else
                            {
                                log.Info("远程心跳时间: " + lastBeatTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                                //先判断是链路是否通着,只有通着的时候,才判断是否心跳
                                if (connector.IsClientAlived(beatName))
                                {
                                    if (lastBeatTime.Value == LastBeatTime.Value)
                                    {
                                        log.Error(string.Format("心跳未进行:处理问题  lastBeatTime:" + lastBeatTime.Value.ToString("yyyy-MM-dd HH:mm:ss")));
                                        _appErrExecutor.HandleError(Enums.EnumHandleCondition.务可见心脏停止跳动, _config, LastBeatTime);
                                    }
                                    else
                                    {
                                        LastBeatTime = lastBeatTime;
                                        log.Info("正常心跳");
                                        //重置状态
                                        _appErrExecutor.ResetState();
                                    }
                                }
                                else
                                {
                                    log.Error("客户端服务未能正常连接到状态中心,心跳监控失效");
                                    continue;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(string.Format("心跳 ID[{0}] 名称[{1}] 出错: ", _config.ID, _config.AppName) + ex.ToString());
                        continue;
                    }
                }
            }
        }