查询ES集群全部索引的数据量

工作需要,查询目前ES集群下各索引的数据量及日增量。用于资源管理统计。

查询url:

http://clusterIP:clusterPort/_cat/indices?v

示例:

部分列说明:

index:索引名

status:是否启用

rep:副本数

storesize:总大小(包含副本)

pristoresize:不包含副本的大小

 

C#代码:

 1 public Dictionary<string, ResourceEntity> GetESTableSizeInfo()
 2         {
 3             Dictionary<string, ResourceEntity> result = new Dictionary<string, ResourceEntity>();
 4             StreamReader reader = null ;
 5             WebResponse rs = null;
 6             string uri = string.Format("{0}/_cat/indices?v", AppConfigGetter.Get(ConfigConstants.KEY_ESurl));
 7             try
 8             {
 9                 HttpWebRequest req = WebRequest.Create(new Uri(uri)) as HttpWebRequest;
10                 req.Method = "GET";
11                 rs = req.GetResponse();
12                 Stream rss = rs.GetResponseStream();
13                 reader = new StreamReader(rss);
14                 
15                 reader.ReadLine();
16                 string data = reader.ReadLine();
17                 
18                 while (string.IsNullOrEmpty(data) == false)
19                 {
20                     string[] fields = System.Text.RegularExpressions.Regex.Split(data, "[ ]+");
21                     if (fields.Length == 10)//如果是close的,则fields.Length=5
22                     {
23                         string index = fields[2];
24                         int copiesNum = Convert.ToInt32(fields[5]);
25                         string size_ = fields[8];
26                         if (result.ContainsKey(index)==false)
27                         {
28                             result.Add(index, new ResourceEntity()
29                             {
30                                 Type = ResourceType.ES,
31                                 TableName = index,
32                                 CopiesNum = copiesNum,
33                                 Size_B = DataUtils.FormatData_GetB(size_)
34                             });
35                         }
36                     }
37                     data = reader.ReadLine();
38                 }
39             }
40             catch
41             {
42                 throw;
43             }
44             finally
45             {
46                 if (null != reader)
47                     reader.Close();
48                 if (null != rs)
49                     rs.Close();
50             }
51             return result;
52         }
53     }
C#获取ES索引数据量

 

posted @ 2017-06-11 17:54  闲汉  阅读(9855)  评论(0编辑  收藏  举报