/// <summary> /// 获取子项 /// </summary> /// <param name="redisData"></param> /// <returns></returns> public static object GetItems(int offset, RedisData redisData) { object result = new object(); if (_redisClients.ContainsKey(redisData.Name)) { var redisClient = _redisClients[redisData.Name]; if (redisClient.IsConnected) { if (redisData.Key == null) { redisData.Key = "*"; } switch (redisData.Type) { case 2: var hresult = redisClient.GetDataBase(redisData.DBIndex).HScan(redisData.ID, offset, redisData.Key, 10); if (hresult.Data == null && hresult.Offset > 0) { hresult = redisClient.GetDataBase(redisData.DBIndex).HScan(redisData.ID, hresult.Offset, redisData.Key, 1000000); } result = hresult.Data; break; case 3: var sresult = redisClient.GetDataBase(redisData.DBIndex).SScan(redisData.ID, offset, redisData.Key, 10); if (sresult.Data == null && sresult.Offset > 0) { sresult = redisClient.GetDataBase(redisData.DBIndex).SScan(redisData.ID, offset, redisData.Key, 1000000); } result = sresult.Data; break; case 4: var zresult = redisClient.GetDataBase(redisData.DBIndex).ZScan(redisData.ID, offset, redisData.Key, 10); if (zresult.Data == null && zresult.Offset > 0) { zresult = redisClient.GetDataBase(redisData.DBIndex).ZScan(redisData.ID, offset, redisData.Key, 1000000); } result = zresult.Data; break; case 5: result = redisClient.GetDataBase(redisData.DBIndex).LRang(redisData.ID, offset, 10); break; } } } return(result); }
public ActionResult GetCount(RedisData redisData) { var result = new JsonResult <int>() { Code = 3, Message = "操作失败" }; try { if (redisData != null) { switch (redisData.Type) { case 1: break; case 2: result.Data = CurrentRedisClient.HashSetCount(redisData.Name, redisData.DBIndex, redisData.ID); break; case 3: result.Data = CurrentRedisClient.SAddCount(redisData.Name, redisData.DBIndex, redisData.ID); break; case 4: result.Data = CurrentRedisClient.ZAddCount(redisData.Name, redisData.DBIndex, redisData.ID); break; case 5: result.Data = CurrentRedisClient.LLen(redisData.Name, redisData.DBIndex, redisData.ID); break; default: break; } } result.Code = 1; result.Message = "ok"; } catch (Exception ex) { LogHelper.Error($"RedisController.GetCount redisData:{SerializeHelper.Serialize(redisData)}", ex); return(Json(new JsonResult <string>() { Code = 2, Message = ex.Message })); } return(Json(result)); }
public ActionResult Set(RedisData redisData) { var result = new JsonResult <string>() { Code = 3, Message = "操作失败" }; try { if (redisData != null) { switch (redisData.Type) { case 1: CurrentRedisClient.StringSet(redisData.Name, redisData.DBIndex, redisData.ID, redisData.Value); break; case 2: CurrentRedisClient.HashSet(redisData.Name, redisData.DBIndex, redisData.ID, redisData.Key, redisData.Value); break; case 3: CurrentRedisClient.SAdd(redisData.Name, redisData.DBIndex, redisData.ID, redisData.Value); break; case 4: CurrentRedisClient.ZAdd(redisData.Name, redisData.DBIndex, redisData.ID, redisData.Key, redisData.Value); break; case 5: CurrentRedisClient.LPush(redisData.Name, redisData.DBIndex, redisData.ID, redisData.Value); break; default: break; } } result.Code = 1; result.Message = "ok"; } catch (Exception ex) { return(Json(new JsonResult <string>() { Code = 2, Message = ex.Message })); } return(Json(result)); }
private static String GetMarkdown(RedisNode node, RedisData data, String title, IList <Action <StringBuilder> > actions) { var sb = new StringBuilder(); if (!title.IsNullOrEmpty()) { sb.AppendLine($"### [{node}]{title}"); } sb.AppendLine($">**分类:**<font color=\"info\">{node.Category}</font>"); sb.AppendLine($">**版本:**<font color=\"info\">{node.Version}</font>"); sb.AppendLine($">**已用内存:**<font color=\"info\">{data.UsedMemory:n0}</font>"); sb.AppendLine($">**内存容量:**<font color=\"info\">{node.MaxMemory:n0}</font>"); sb.AppendLine($">**连接数:**<font color=\"info\">{data.ConnectedClients:n0}</font>"); sb.AppendLine($">**服务器:**<font color=\"info\">{node.Server}</font>"); //var rate = node.MaxMemory == 0 ? 0 : (data.UsedMemory * 100 / node.MaxMemory); //if (rate >= node.AlarmMemoryRate && node.AlarmMemoryRate > 0) //{ // sb.AppendLine($">**内存告警:**<font color=\"info\">{data.UsedMemory}/{node.MaxMemory} >= {node.AlarmMemoryRate:p0}</font>"); //} //if (node.AlarmConnections > 0 && data.ConnectedClients >= node.AlarmConnections) //{ // sb.AppendLine($">**连接告警:**<font color=\"info\">{data.ConnectedClients:n0} >= {node.AlarmConnections:n0}</font>"); //} foreach (var item in actions) { item(sb); } var str = sb.ToString(); if (str.Length > 2000) { str = str.Substring(0, 2000); } // 构造网址 var url = Setting.Current.WebUrl; if (!url.IsNullOrEmpty()) { url = url.EnsureEnd("/") + "Nodes/RedisNode?id=" + node.Id; str += Environment.NewLine + $"[更多信息]({url})"; } return(str); }
public static int GetItemsCount(int offset, RedisData redisData) { lock (_locker) { var result = 0; if (_redisClients.ContainsKey(redisData.Name)) { var redisClient = _redisClients[redisData.Name]; if (redisClient.IsConnected) { if (redisData.Key == null) { redisData.Key = "*"; } switch (redisData.Type) { case 2: var hdata = redisClient.GetDataBase(redisData.DBIndex).HScan(redisData.ID, offset, redisData.Key, 10000).Data; result = hdata != null ? hdata.Count : 0; break; case 3: var sdata = redisClient.GetDataBase(redisData.DBIndex).SScan(redisData.ID, offset, redisData.Key, 10000).Data; result = sdata != null ? sdata.Count : 0; break; case 4: var zdata = redisClient.GetDataBase(redisData.DBIndex).ZScan(redisData.ID, offset, redisData.Key, 10000).Data; result = zdata != null ? zdata.Count : 0; break; case 5: var ldata = redisClient.GetDataBase(redisData.DBIndex).LRang(redisData.ID, offset, 10000); result = ldata != null ? ldata.Count : 0; break; } } } return(result); } }
public ActionResult DelItem(RedisData redisData) { var result = new JsonResult <string>() { Code = 3, Message = "操作失败" }; try { object data = string.Empty; if (redisData != null) { redisData.ID = SAEA.Http.HttpUtility.UrlDecode(redisData.ID); if (!string.IsNullOrEmpty(redisData.Key)) { redisData.Key = SAEA.Http.HttpUtility.UrlDecode(redisData.Key); } if (!string.IsNullOrEmpty(redisData.Value)) { redisData.Value = SAEA.Http.HttpUtility.UrlDecode(redisData.Value); } CurrentRedisClient.DelItem(redisData); } result.Code = 1; result.Message = "ok"; } catch (Exception ex) { LogHelper.Error($"RedisController.DelItem redisData:{SerializeHelper.Serialize(redisData)}", ex); return(Json(new JsonResult <string>() { Code = 2, Message = ex.Message })); } return(Json(result)); }
/// <summary> /// 获取子项 /// </summary> /// <param name="redisData"></param> /// <returns></returns> public static object GetItems(RedisData redisData) { object result = new object(); if (_redisClients.ContainsKey(redisData.Name)) { var redisClient = _redisClients[redisData.Name]; if (redisClient.IsConnected) { if (redisData.Key == null) { redisData.Key = "*"; } switch (redisData.Type) { case 2: result = redisClient.GetDataBase(redisData.DBIndex).HScan(redisData.ID, 0, redisData.Key, 50).Data; break; case 3: result = redisClient.GetDataBase(redisData.DBIndex).SScan(redisData.ID, 0, redisData.Key, 50).Data; break; case 4: result = redisClient.GetDataBase(redisData.DBIndex).ZScan(redisData.ID, 0, redisData.Key, 50).Data; break; case 5: result = redisClient.GetDataBase(redisData.DBIndex).LRang(redisData.ID, 0, 50); break; } } } return(result); }
private void Update(RedisNativeClient client, string ip) { if (client == null) { Console.WriteLine("client == null"); return; } RedisData reply = null; try { reply = client.RawCommand("CLUSTER", "NODES"); } catch (RedisException e) { if (e.Message.Contains("cluster support disabled")) { Global.Info(ip, "Switch to non-cluster mode"); for (int i = 0; i < Global.HashSlotSize; i++) { this.slots[i] = client; } } else { Global.Error(ip, e.Message); } return; } if (reply != null) { this.ParseClusterNodes(reply.ToRedisText().Text, ip); } }
public ActionResult Set(RedisData redisData) { return(Json(new RedisService().Set(redisData))); }
public ActionResult DelItem(RedisData redisData) { return(Json(new RedisService().DelItem(redisData))); }
public ActionResult Rename(RedisData redisData, string newID) { return(Json(new RedisService().Rename(redisData, newID))); }
/// <summary> /// 获取子项 /// </summary> /// <param name="offset"></param> /// <param name="redisData"></param> /// <returns></returns> public ActionResult GetItems(int offset, RedisData redisData) { return(Json(new RedisService().GetItems(offset, redisData))); }
/// <summary> /// 获取数据 /// </summary> /// <param name="redisData"></param> /// <returns></returns> public ActionResult GetCount(RedisData redisData) { return(Json(new RedisService().GetCount(redisData))); }
private void ProcessRedisData(RedisNode node) { if (!RobotHelper.CanAlarm(node.Category, node.WebHook)) { return; } if (node.AlarmMemoryRate <= 0 || node.AlarmConnections == 0) { return; } // 最新数据 var data = RedisData.FindLast(node.Id); if (data == null) { return; } using var span = _tracer?.NewSpan($"Alarm:{nameof(RedisNode)}"); var actions = new List <Action <StringBuilder> >(); // 内存告警 var rate = data.UsedMemory * 100 / node.MaxMemory; if (rate >= node.AlarmMemoryRate) { // 一定时间内不要重复报错,除非错误翻倍 var error2 = _cache.Get <Int32>("alarm:RedisMemory:" + node.Id); if (error2 == 0 || rate > error2 * 2) { _cache.Set("alarm:RedisMemory:" + node.Id, rate, 5 * 60); actions.Add(sb => sb.AppendLine($">**内存告警:**<font color=\"info\">{rate / 100:p0} >= {node.AlarmMemoryRate / 100:p0}</font>")); } } // 连接数告警 var cs = data.ConnectedClients; if (node.AlarmConnections > 0 && cs >= node.AlarmConnections) { // 一定时间内不要重复报错,除非错误翻倍 var error2 = _cache.Get <Int32>("alarm:RedisConnections:" + node.Id); if (error2 == 0 || cs > error2 * 2) { _cache.Set("alarm:RedisConnections:" + node.Id, cs, 5 * 60); actions.Add(sb => sb.AppendLine($">**连接数告警:**<font color=\"info\">{cs:n0} >= {node.AlarmConnections:n0}</font>")); } } // 速度告警 var speed = data.Speed; if (node.AlarmSpeed > 0 && speed >= node.AlarmSpeed) { // 一定时间内不要重复报错,除非错误翻倍 var error2 = _cache.Get <Int32>("alarm:RedisSpeed:" + node.Id); if (error2 == 0 || speed > error2 * 2) { _cache.Set("alarm:RedisSpeed:" + node.Id, speed, 5 * 60); actions.Add(sb => sb.AppendLine($">**速度告警:**<font color=\"info\">{speed:n0} >= {node.AlarmSpeed:n0}</font>")); } } // 入流量告警 var input = data.InputKbps; if (node.AlarmInputKbps > 0 && input >= node.AlarmInputKbps) { // 一定时间内不要重复报错,除非错误翻倍 var error2 = _cache.Get <Int32>("alarm:RedisInputKbps:" + node.Id); if (error2 == 0 || input > error2 * 2) { _cache.Set("alarm:RedisInputKbps:" + node.Id, input, 5 * 60); actions.Add(sb => sb.AppendLine($">**入流量告警:**<font color=\"info\">{input:n0} >= {node.AlarmInputKbps:n0}</font>")); } } // 出流量告警 var output = data.OutputKbps; if (node.AlarmOutputKbps > 0 && output >= node.AlarmOutputKbps) { // 一定时间内不要重复报错,除非错误翻倍 var error2 = _cache.Get <Int32>("alarm:RedisOutputKbps:" + node.Id); if (error2 == 0 || output > error2 * 2) { _cache.Set("alarm:RedisOutputKbps:" + node.Id, output, 5 * 60); actions.Add(sb => sb.AppendLine($">**出流量告警:**<font color=\"info\">{output:n0} >= {node.AlarmOutputKbps:n0}</font>")); } } if (actions.Count > 0) { var msg = GetMarkdown(node, data, "Redis告警", actions); RobotHelper.SendAlarm(node.Category, node.WebHook, "Redis告警", msg); } }
// 读取集群上的节点信息 static IList <InternalClusterNode> ReadClusterNodes(IEnumerable <ClusterNode> source) { RedisClient c = null; StringReader reader = null; IList <InternalClusterNode> result = null; int index = 0; int rowCount = source.Count(); foreach (var node in source) { try { // 从当前节点读取REDIS集群节点信息 index += 1; c = new RedisClient(node.Host, node.Port, node.Password); RedisData data = c.RawCommand("CLUSTER".ToUtf8Bytes(), "NODES".ToUtf8Bytes()); string info = Encoding.UTF8.GetString(data.Data); // 将读回的字符文本转成强类型节点实体 reader = new StringReader(info); string line = reader.ReadLine(); while (line != null) { if (result == null) { result = new List <InternalClusterNode>(); } InternalClusterNode n = InternalClusterNode.Parse(line); n.Password = node.Password; result.Add(n); line = reader.ReadLine(); } // 只要任意一个节点拿到集群信息,直接退出 if (result != null && result.Count > 0) { break; } } catch (Exception ex) { // 出现异常,如果还没到最后一个节点,则继续使用下一下节点读取集群信息 // 否则抛出异常 if (index < rowCount) { Thread.Sleep(100); } else { throw new RedisClusterException(ex.Message, c != null ? c.GetHostString() : string.Empty, ex); } } finally { if (reader != null) { reader.Dispose(); } if (c != null) { c.Dispose(); } } } if (result == null) { result = new List <InternalClusterNode>(0); } return(result); }