Search code examples
asp.net-mvcelasticsearchmetrics

How to check elasticksearch health with different metrics in single API call?


I need to read following elasticksearch metrics

  1. Version
  2. Up-time
  3. No. of Jobs
  4. Overall Health
  5. No. of Nodes
  6. Disk Available in %
  7. JVM Heap Size
  8. No. of Indices
  9. Primary Shards
  10. Replica Shards

in ASP.Net MVC application. My question :- Is it possible to read all above metrics with one API call in elasticsearch?

I have written following method

private static string CheckESHealth()
    {
        string esurl = "http://localhost:9200/_cluster/health";
        HttpClient httpClient = new HttpClient();
        string strReturnVal = string.Empty;
        try
        {
            var response = httpClient.GetAsync(new Uri(esurl)).Result;
            if (response.IsSuccessStatusCode)
            {
                var esdata = response.Content.ReadAsStringAsync().Result;

                if (!string.IsNullOrEmpty(esdata))
                {
                    JObject jobject = JObject.Parse(esdata);
        //as a example i have taken only status.. but i need all paramters mention above
                    strReturnVal = jobject["status"].ToString();
                }
            }
            else
            {
                strReturnVal = "Errored : Received status code : " + response.StatusCode;
            }
        }
        catch (Exception ex)
        {
            strReturnVal = "Errored : " + ex.Message;
        }

        return strReturnVal;
    }

in above example i am using :- GET _cluster/health command which give following result

enter image description here

but i am trying to read all above metrics in one API call


Solution

  • I didn't find a way to read above[in question] mentioned metrics in one query. so, i used following queries to get metrics.

    1. http://localhost:9200/_cat/health?h=cluster,status,node.total,shards,pri,relo&format=json
    2. http://localhost:9200/_cat/nodes?h=version,uptime,heap.percent&format=json
    3. http://localhost:9200/_cat/allocation?h=disk.percent&format=json