protected override void ExcuteJob(IJobExecutionContext context, CancellationTokenSource cancellationSource) { if (EntityModel.ConfigModel.redis_configs != null) { var time = DateTime.Now; foreach (var item in EntityModel.ConfigModel.redis_configs) { string res = HttpHelper.HttpGet(item.url); if (string.IsNullOrWhiteSpace(res)) { return; } RedisTargetModel redis_target = new RedisTargetModel { redis_name = item.node_name }; Regex used_bytes_reg = new Regex("redis_memory_used_bytes .+"); MatchCollection matchs = used_bytes_reg.Matches(res); string used_bytes_val = matchs[2].Value; var redis_memory_used_bytes = UtilHelper.ChangeDataToD(used_bytes_val.Replace("redis_memory_used_bytes", "").Trim()); redis_target.used_bytes_val = redis_memory_used_bytes; Regex max_bytes_reg = new Regex("redis_memory_max_bytes .+"); matchs = max_bytes_reg.Matches(res); string max_bytes_val = matchs[2].Value; var redis_memory_max_bytes = UtilHelper.ChangeDataToD(max_bytes_val.Replace("redis_memory_max_bytes", "").Trim()); redis_target.max_bytes_val = redis_memory_max_bytes; Regex connected_clients_reg = new Regex("redis_connected_clients .+"); matchs = connected_clients_reg.Matches(res); string connected_clients_val = matchs[2].Value; var redis_connected_clients = int.Parse(connected_clients_val.Replace("redis_connected_clients", "").Trim()); redis_target.connected_clients_val = redis_connected_clients; Regex key_count_reg = new Regex("redis_db_keys{db=.+"); matchs = key_count_reg.Matches(res); int key_count = 0; foreach (var match in matchs) { Regex number_reg = new Regex(@" \d+"); var m = number_reg.Match(match.ToString()); if (int.TryParse(m.Value, out int tmp)) { key_count += tmp; } } redis_target.redis_db_keys = key_count; ServerTargetHelper.RedisTarget(time, redis_target); } } }
/// <summary> /// /// </summary> public static void RedisTarget(DateTime time, RedisTargetModel target) { StringBuilder sb = new StringBuilder(); sb.Append($"{time.ToString("yyyy-MM-dd HH:mm:ss.ffff")}#"); sb.Append("target_redis_node#"); string node_name = "_"; if (string.IsNullOrWhiteSpace(target.redis_name) == false) { node_name = target.redis_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.redis_node != null) { if (ConfigModel.rule_config.redis_node.mem_rate > 0 && ConfigModel.rule_config.redis_node.mem_rate <= target.mem_rate && target.max_bytes_val > 0) { is_warning = true; msg += $" 当前Redis内存使用占比{target.mem_rate}% 持续{ConfigModel.rule_config.redis_node.time_out}秒已超过预警值{ConfigModel.rule_config.redis_node.mem_rate}%"; } } } if (msg == null) { msg = "_"; } sb.Append($"{notice}#"); var now = DateTimeUtility.ConvertToTimeStamp(time); if (is_warning) { if (redis_time <= 0) { redis_time = now; is_warning = false; } else { if (now - redis_time >= ConfigModel.rule_config.redis_node.time_out) { redis_time = 0; } else { is_warning = false; } } } else { redis_time = 0; } if (is_warning) { sb.Append("1#"); } else { sb.Append("0#"); } sb.Append($"{msg}#"); Console.WriteLine(sb.ToString()); _log.Info(sb.ToString()); }