Commit e8b7723b authored by Leonardo Menezes's avatar Leonardo Menezes
Browse files

support aws clusters with no ip/host info

closes #64
parent bd6650f4
Loading
Loading
Loading
Loading
+28 −17
Original line number Diff line number Diff line
@@ -6,10 +6,11 @@ object ClusterOverview {

  def apply(clusterState: JsValue, nodesStats: JsValue, indicesStats: JsValue,
            clusterSettings: JsValue, aliases: JsValue, clusterHealth: JsValue,
            nodes: JsValue, main: JsValue): JsValue = {
            nodesInfo: JsValue, main: JsValue): JsValue = {

    val indices = Indices(clusterState, indicesStats, aliases)
    val clusterNodes = Nodes(clusterState, nodesStats, nodes)

    val masterNodeId = (clusterState \ "master_node").as[String]

    val persistentAllocation = (clusterSettings \ "persistent" \ "cluster" \ "routing" \ "allocation" \ "enable").asOpt[String].getOrElse("all")
    val transientAllocation = (clusterSettings \ "transient" \ "cluster" \ "routing" \ "allocation" \ "enable").asOpt[String]
@@ -32,8 +33,18 @@ object ClusterOverview {
      "closed_indices" -> JsNumber(indices.count { idx => (idx \ "closed").as[Boolean] }),
      "special_indices" -> JsNumber(indices.count { idx => (idx \ "special").as[Boolean] }),
      "indices" -> JsArray(indices),
      "nodes"                 -> JsArray(clusterNodes),
      "nodes" -> buildNodes(masterNodeId, nodesInfo, nodesStats),
      "shard_allocation" -> JsBoolean(shardAllocation)
    ))
  }

  def buildNodes(masterNodeId: String, nodesInfo: JsValue, nodesStats: JsValue): JsArray =
    JsArray(
      (nodesInfo \ "nodes").as[JsObject].value.map {
        case (id, info) =>
          val stats = (nodesStats \ "nodes" \ id).as[JsObject]
          Node(id, info, stats, masterNodeId)
      }.toSeq
    )

}
+65 −0
Original line number Diff line number Diff line
package models.overview

import play.api.libs.json._

object Node {

  def apply(id: String, info: JsValue, stats: JsValue, masterNodeId: String) = {
    val nodeRoles = NodeRoles(info)


    // AWS nodes return no host/ip info
    val host = (info \ "host").asOpt[JsString].getOrElse(JsNull)
    val ip = (info \ "ip").asOpt[JsString].getOrElse(JsNull)

    Json.obj(
      "id" -> JsString(id),
      "current_master" -> JsBoolean(id.equals(masterNodeId)),
      "name" -> (info \ "name").as[JsString],
      "host" -> host,
      "ip" -> ip,
      "es_version" -> (info \ "version").as[JsString],
      "jvm_version" -> (info \ "jvm" \ "version").as[JsString],
      "load_average" -> loadAverage(stats),
      "available_processors" -> (info \ "os" \ "available_processors").as[JsNumber],
      "cpu_percent" -> cpuPercent(stats),
      "master" -> JsBoolean(nodeRoles.master),
      "data" -> JsBoolean(nodeRoles.data),
      "client" -> JsBoolean(nodeRoles.client),
      "ingest" -> JsBoolean(nodeRoles.ingest),
      "heap" -> Json.obj(
        "used" -> (stats \ "jvm" \ "mem" \ "heap_used_in_bytes").as[JsNumber],
        "committed" -> (stats \ "jvm" \ "mem" \ "heap_committed_in_bytes").as[JsNumber],
        "used_percent" -> (stats \ "jvm" \ "mem" \ "heap_used_percent").as[JsNumber],
        "max" -> (stats \ "jvm" \ "mem" \ "heap_max_in_bytes").as[JsNumber]
      ),
      "disk" -> disk(stats)
    )
  }

  def disk(stats: JsValue): JsObject = {
    val totalInBytes = (stats \ "fs" \ "total" \ "total_in_bytes").asOpt[Long].getOrElse(0l)
    val freeInBytes = (stats \ "fs" \ "total" \ "free_in_bytes").asOpt[Long].getOrElse(0l)
    val usedPercent = 100 - (100 * (freeInBytes.toFloat / totalInBytes.toFloat)).toInt
    Json.obj(
      "total_in_bytes" -> JsNumber(totalInBytes),
      "disk_free_in_bytes" -> JsNumber(freeInBytes),
      "used_percent" -> JsNumber(usedPercent)
    )
  }

  def loadAverage(nodeStats: JsValue): JsNumber = {
    val load = (nodeStats \ "os" \ "cpu" \ "load_average" \ "1m").asOpt[Float].getOrElse(// 5.X
      (nodeStats \ "os" \ "load_average").asOpt[Float].getOrElse(0f) // FIXME: 2.X
    )
    JsNumber(BigDecimal(load.toDouble))
  }

  def cpuPercent(nodeStats: JsValue): JsNumber = {
    val cpu = (nodeStats \ "os" \ "cpu" \ "percent").asOpt[Int].getOrElse(// 5.X
      (nodeStats \ "os" \ "cpu_percent").asOpt[Int].getOrElse(0) // FIXME 2.X
    )
    JsNumber(BigDecimal(cpu))
  }

}

app/models/overview/Nodes.scala

deleted100644 → 0
+0 −59
Original line number Diff line number Diff line
package models.overview

import play.api.libs.json._

object Nodes {

  def apply(clusterState: JsValue, nodesStats: JsValue, nodes: JsValue): Seq[JsValue] = {
    val currentMaster = (clusterState \ "master_node").as[String]
    (nodes \ "nodes").as[JsObject].value.map { case (nodeId, info) =>

      val nodeRoles = NodeRoles(info)
      val stats = (nodesStats \ "nodes" \ nodeId).as[JsObject]
      val totalInBytes = (stats \ "fs" \ "total" \ "total_in_bytes").asOpt[Long].getOrElse(0l)
      val diskFreeInBytes = (stats \ "fs" \ "total" \ "free_in_bytes").asOpt[Long].getOrElse(0l)
      Json.obj(
        "id"             -> JsString(nodeId),
        "current_master" -> JsBoolean(nodeId.equals(currentMaster)),
        "name"           -> (info \ "name").as[JsString],
        "host"           -> (info \ "host").as[JsString],
        "ip"             -> (info \ "ip").as[JsString],
        "es_version"     -> (info \ "version").as[JsString],
        "jvm_version"    -> (info \ "jvm" \ "version").as[JsString],
        "load_average"         -> loadAverage(stats),
        "available_processors" -> (info \ "os" \ "available_processors").as[JsNumber],
        "cpu_percent"    -> cpuPercent(stats),
        "master"         -> JsBoolean(nodeRoles.master),
        "data"           -> JsBoolean(nodeRoles.data),
        "client"         -> JsBoolean(nodeRoles.client),
        "ingest"         -> JsBoolean(nodeRoles.ingest),
        "heap"           -> Json.obj(
          "used"          -> (stats \ "jvm" \ "mem" \ "heap_used_in_bytes").as[JsNumber],
          "committed"     -> (stats \ "jvm" \ "mem" \ "heap_committed_in_bytes").as[JsNumber],
          "used_percent"  -> (stats \ "jvm" \ "mem" \ "heap_used_percent").as[JsNumber],
          "max"           -> (stats \ "jvm" \ "mem" \ "heap_max_in_bytes").as[JsNumber]
        ),
        "disk"            -> Json.obj(
          "total_in_bytes"     -> JsNumber(totalInBytes),
          "disk_free_in_bytes" -> JsNumber(diskFreeInBytes),
          "used_percent"       -> JsNumber(100 - (100 * (diskFreeInBytes.toFloat / totalInBytes.toFloat)).toInt)
        )
      )
    }.toSeq
  }

  def loadAverage(nodeStats: JsValue): JsNumber = {
    val load = (nodeStats \ "os" \ "cpu" \ "load_average" \ "1m").asOpt[Float].getOrElse( // 5.X
      (nodeStats \ "os" \ "load_average").asOpt[Float].getOrElse(0f) // FIXME: 2.X
    )
    JsNumber(BigDecimal(load.toDouble))
  }

  def cpuPercent(nodeStats: JsValue): JsNumber = {
    val cpu = (nodeStats \ "os" \ "cpu" \ "percent").asOpt[Int].getOrElse( // 5.X
      (nodeStats \ "os" \ "cpu_percent").asOpt[Int].getOrElse(0) // FIXME 2.X
    )
    JsNumber(BigDecimal(cpu))
  }

}
+88 −0
Original line number Diff line number Diff line
package models.overview

import org.specs2.Specification
import play.api.libs.json.Json

object NodeSpec extends Specification {

  def is =
    s2"""
    Node should

      parse a >= 5.0 ES node $nodeInfo5
      parse a AWS nods       $awsNode
      """

  def nodeInfo5 = {
    val expected = Json.parse(
      """
        |{
        |  "id": "nodeId",
        |  "current_master": false,
        |  "name": "Solara",
        |  "host": "127.0.0.1",
        |  "ip": "127.0.0.1",
        |  "es_version": "2.1.0",
        |  "jvm_version": "1.8.0_72",
        |  "load_average": 3.17138671875,
        |  "available_processors": 8,
        |  "cpu_percent": 0,
        |  "master": true,
        |  "data": true,
        |  "client": false,
        |  "ingest": false,
        |  "heap": {
        |    "used": 28420720,
        |    "committed": 259522560,
        |    "used_percent": 2,
        |    "max": 1037959168
        |  },
        |  "disk": {
        |    "total_in_bytes": 249804886016,
        |    "disk_free_in_bytes": 41567444992,
        |    "used_percent": 84
        |  }
        |}
      """.stripMargin
    )
    val node = Node("nodeId", NodesInfo.nodeInfo5, NodeStats.nodeStats5, "otherId")
    node mustEqual expected
  }

  def awsNode = {
    val expected = Json.parse(
      """
        |{
        |  "id": "nodeId",
        |  "current_master": false,
        |  "name": "Solara",
        |  "host": null,
        |  "ip": null,
        |  "es_version": "2.1.0",
        |  "jvm_version": "1.8.0_72",
        |  "load_average": 3.17138671875,
        |  "available_processors": 8,
        |  "cpu_percent": 0,
        |  "master": true,
        |  "data": true,
        |  "client": false,
        |  "ingest": false,
        |  "heap": {
        |    "used": 28420720,
        |    "committed": 259522560,
        |    "used_percent": 2,
        |    "max": 1037959168
        |  },
        |  "disk": {
        |    "total_in_bytes": 249804886016,
        |    "disk_free_in_bytes": 41567444992,
        |    "used_percent": 84
        |  }
        |}
      """.stripMargin
    )
    val node = Node("nodeId", NodesInfo.awsInfo, NodeStats.awsNodeStats5, "otherId")
    node mustEqual expected
  }

}
+243 −0
Original line number Diff line number Diff line
package models.overview

import play.api.libs.json.Json

object NodeStats {

  val nodeStats5 = Json.parse(
    """
      |{
      |  "timestamp":1458346589015,
      |  "name":"Solara",
      |  "transport_address":"127.0.0.1:9301",
      |  "host":"127.0.0.1",
      |  "ip":[
      |    "127.0.0.1:9301",
      |    "NONE"
      |  ],
      |  "os":{
      |    "timestamp":1458346589015,
      |    "load_average":3.17138671875,
      |    "mem":{
      |      "total_in_bytes":8589934592,
      |      "free_in_bytes":101085184,
      |      "used_in_bytes":8488849408,
      |      "free_percent":1,
      |      "used_percent":99
      |    },
      |    "swap":{
      |      "total_in_bytes":2147483648,
      |      "free_in_bytes":1736966144,
      |      "used_in_bytes":410517504
      |    }
      |  },
      |  "process":{
      |    "timestamp":1458346589015,
      |    "open_file_descriptors":257,
      |    "max_file_descriptors":10240,
      |    "cpu":{
      |      "percent":0,
      |      "total_in_millis":24084
      |    },
      |    "mem":{
      |      "total_virtual_in_bytes":5274689536
      |    }
      |  },
      |  "jvm":{
      |    "timestamp":1458346589015,
      |    "uptime_in_millis":1106048,
      |    "mem":{
      |      "heap_used_in_bytes":28420720,
      |      "heap_used_percent":2,
      |      "heap_committed_in_bytes":259522560,
      |      "heap_max_in_bytes":1037959168,
      |      "non_heap_used_in_bytes":50725848,
      |      "non_heap_committed_in_bytes":51486720,
      |      "pools":{
      |        "young":{
      |          "used_in_bytes":8929536,
      |          "max_in_bytes":286326784,
      |          "peak_used_in_bytes":71630848,
      |          "peak_max_in_bytes":286326784
      |        },
      |        "survivor":{
      |          "used_in_bytes":6942440,
      |          "max_in_bytes":35782656,
      |          "peak_used_in_bytes":8912888,
      |          "peak_max_in_bytes":35782656
      |        },
      |        "old":{
      |          "used_in_bytes":12548744,
      |          "max_in_bytes":715849728,
      |          "peak_used_in_bytes":12548744,
      |          "peak_max_in_bytes":715849728
      |        }
      |      }
      |    },
      |    "threads":{
      |      "count":67,
      |      "peak_count":87
      |    },
      |    "gc":{
      |      "collectors":{
      |        "young":{
      |          "collection_count":5,
      |          "collection_time_in_millis":76
      |        },
      |        "old":{
      |          "collection_count":1,
      |          "collection_time_in_millis":18
      |        }
      |      }
      |    },
      |    "buffer_pools":{
      |      "direct":{
      |        "count":71,
      |        "used_in_bytes":13640062,
      |        "total_capacity_in_bytes":13640062
      |      },
      |      "mapped":{
      |        "count":0,
      |        "used_in_bytes":0,
      |        "total_capacity_in_bytes":0
      |      }
      |    }
      |  },
      |  "fs":{
      |    "timestamp":1458346589015,
      |    "total":{
      |      "total_in_bytes":249804886016,
      |      "free_in_bytes":41567444992,
      |      "available_in_bytes":41305300992
      |    },
      |    "data":[
      |      {
      |        "path":"/Users/leonardo.menezes/Downloads/elasticsearch-2.1.0/data/elasticsearch/nodes/1",
      |        "mount":"/ (/dev/disk1)",
      |        "type":"hfs",
      |        "total_in_bytes":249804886016,
      |        "free_in_bytes":41567444992,
      |        "available_in_bytes":41305300992
      |      }
      |    ]
      |  }
      |}
    """.stripMargin
  )

  val awsNodeStats5 = Json.parse(
    """
      |{
      |  "timestamp":1458346589015,
      |  "name":"Solara",
      |  "os":{
      |    "timestamp":1458346589015,
      |    "load_average":3.17138671875,
      |    "mem":{
      |      "total_in_bytes":8589934592,
      |      "free_in_bytes":101085184,
      |      "used_in_bytes":8488849408,
      |      "free_percent":1,
      |      "used_percent":99
      |    },
      |    "swap":{
      |      "total_in_bytes":2147483648,
      |      "free_in_bytes":1736966144,
      |      "used_in_bytes":410517504
      |    }
      |  },
      |  "process":{
      |    "timestamp":1458346589015,
      |    "open_file_descriptors":257,
      |    "max_file_descriptors":10240,
      |    "cpu":{
      |      "percent":0,
      |      "total_in_millis":24084
      |    },
      |    "mem":{
      |      "total_virtual_in_bytes":5274689536
      |    }
      |  },
      |  "jvm":{
      |    "timestamp":1458346589015,
      |    "uptime_in_millis":1106048,
      |    "mem":{
      |      "heap_used_in_bytes":28420720,
      |      "heap_used_percent":2,
      |      "heap_committed_in_bytes":259522560,
      |      "heap_max_in_bytes":1037959168,
      |      "non_heap_used_in_bytes":50725848,
      |      "non_heap_committed_in_bytes":51486720,
      |      "pools":{
      |        "young":{
      |          "used_in_bytes":8929536,
      |          "max_in_bytes":286326784,
      |          "peak_used_in_bytes":71630848,
      |          "peak_max_in_bytes":286326784
      |        },
      |        "survivor":{
      |          "used_in_bytes":6942440,
      |          "max_in_bytes":35782656,
      |          "peak_used_in_bytes":8912888,
      |          "peak_max_in_bytes":35782656
      |        },
      |        "old":{
      |          "used_in_bytes":12548744,
      |          "max_in_bytes":715849728,
      |          "peak_used_in_bytes":12548744,
      |          "peak_max_in_bytes":715849728
      |        }
      |      }
      |    },
      |    "threads":{
      |      "count":67,
      |      "peak_count":87
      |    },
      |    "gc":{
      |      "collectors":{
      |        "young":{
      |          "collection_count":5,
      |          "collection_time_in_millis":76
      |        },
      |        "old":{
      |          "collection_count":1,
      |          "collection_time_in_millis":18
      |        }
      |      }
      |    },
      |    "buffer_pools":{
      |      "direct":{
      |        "count":71,
      |        "used_in_bytes":13640062,
      |        "total_capacity_in_bytes":13640062
      |      },
      |      "mapped":{
      |        "count":0,
      |        "used_in_bytes":0,
      |        "total_capacity_in_bytes":0
      |      }
      |    }
      |  },
      |  "fs":{
      |    "timestamp":1458346589015,
      |    "total":{
      |      "total_in_bytes":249804886016,
      |      "free_in_bytes":41567444992,
      |      "available_in_bytes":41305300992
      |    },
      |    "data":[
      |      {
      |        "path":"/Users/leonardo.menezes/Downloads/elasticsearch-2.1.0/data/elasticsearch/nodes/1",
      |        "mount":"/ (/dev/disk1)",
      |        "type":"hfs",
      |        "total_in_bytes":249804886016,
      |        "free_in_bytes":41567444992,
      |        "available_in_bytes":41305300992
      |      }
      |    ]
      |  }
      |}
    """.stripMargin
  )

}
Loading