Пример #1
0
        private static Dictionary <string, long> pg_total_site = new Dictionary <string, long>(); //单位KB
        //#时间#ServerName#NodelName#预留字段#Json数据#预警邮箱#是否预警#预警内容#

        /// <summary>
        ///
        /// </summary>
        public static void ServerTarget(DateTime time, ServerTargetModel target)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"{time.ToString("yyyy-MM-dd HH:mm:ss.ffff")}#");
            sb.Append("target_server_node#");
            string node_name = "_";

            if (string.IsNullOrWhiteSpace(ConfigModel.node_name) == false)
            {
                node_name = ConfigModel.node_name;
            }
            sb.Append($"{node_name}#");
            sb.Append($"_#");
            sb.Append($"{Newtonsoft.Json.JsonConvert.SerializeObject(target)}#");
            bool   is_warning = false;
            string msg        = null;
            string notice     = "_";

            if (ConfigModel.rule_config != null)
            {
                notice = ConfigModel.rule_config.notice;
                if (ConfigModel.rule_config.server_node != null)
                {
                    if (ConfigModel.rule_config.server_node.cpu_rate > 0 && ConfigModel.rule_config.server_node.cpu_rate <= target.cpu_rate)
                    {
                        is_warning = true;
                        msg       += $"  当前CPU使用率{target.cpu_rate}% 持续{ConfigModel.rule_config.server_node.time_out}秒超过预警值{ConfigModel.rule_config.server_node.cpu_rate}%";
                    }
                    if (ConfigModel.rule_config.server_node.disk_rate > 0 && ConfigModel.rule_config.server_node.disk_rate <= target.disk_rate)
                    {
                        is_warning = true;
                        msg       += $"  当前磁盘占用率{target.disk_rate}% 持续{ConfigModel.rule_config.server_node.time_out}秒超过预警值{ConfigModel.rule_config.server_node.disk_rate}%";
                    }
                    if (ConfigModel.rule_config.server_node.mem_rate > 0 && ConfigModel.rule_config.server_node.mem_rate <= target.mem_rate)
                    {
                        is_warning = true;
                        msg       += $"  当前内存使用率{target.mem_rate}% 持续{ConfigModel.rule_config.server_node.time_out}秒超过预警值{ConfigModel.rule_config.server_node.mem_rate}%";
                    }
                }
            }
            if (msg == null)
            {
                msg = "_";
            }
            sb.Append($"{notice}#");
            var now = DateTimeUtility.ConvertToTimeStamp(time);

            if (is_warning)
            {
                if (server_time <= 0)
                {
                    server_time = now;
                    is_warning  = false;
                }
                else
                {
                    if (now - server_time >= ConfigModel.rule_config.server_node.time_out)
                    {
                        server_time = 0;
                    }
                    else
                    {
                        is_warning = false;
                    }
                }
            }
            else
            {
                server_time = 0;
            }

            if (is_warning)
            {
                sb.Append("1#");
            }
            else
            {
                sb.Append("0#");
            }

            sb.Append($"{msg}#");

            Console.WriteLine(sb.ToString());

            _log.Info(sb.ToString());
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        public static void MessageTarget(DateTime time, MessageTargetModel target)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append($"{time.ToString("yyyy-MM-dd HH:mm:ss.ffff")}#");
            sb.Append("target_message_node#");
            sb.Append("kafka#");
            sb.Append($"_#");
            sb.Append($"{Newtonsoft.Json.JsonConvert.SerializeObject(target)}#");
            bool   is_warning = false;
            string msg        = null;
            string notice     = "_";

            if (ConfigModel.rule_config != null && ConfigModel.rule_config.message_node != null)
            {
                if (ConfigModel.rule_config.message_node.task_count > 0 && ConfigModel.rule_config.message_node.task_count <= target.task_count)
                {
                    is_warning = true;
                    msg        = $"  当前任务堆积数{target.task_count} 持续{ConfigModel.rule_config.message_node.time_out}秒已超过预警值{ConfigModel.rule_config.message_node.task_count}";
                }
                notice = ConfigModel.rule_config.notice;
            }
            if (msg == null)
            {
                msg = "_";
            }
            sb.Append($"{notice}#");

            var now = DateTimeUtility.ConvertToTimeStamp(time);

            if (is_warning)
            {
                if (message_time <= 0)
                {
                    message_time = now;
                    is_warning   = false;
                }
                else
                {
                    if (now - message_time >= ConfigModel.rule_config.message_node.time_out)
                    {
                        message_time = 0;
                    }
                    else
                    {
                        is_warning = false;
                    }
                }
            }
            else
            {
                message_time = 0;
            }

            if (is_warning)
            {
                sb.Append("1#");
            }
            else
            {
                sb.Append("0#");
            }

            sb.Append($"{msg}#");

            Console.WriteLine(sb.ToString());
            _log.Info(sb.ToString());
        }