Health.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace Elastica\Cluster;
  3. use Elastica\Client;
  4. use Elastica\Cluster\Health\Index;
  5. /**
  6. * Elastic cluster health.
  7. *
  8. * @author Ray Ward <ray.ward@bigcommerce.com>
  9. *
  10. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
  11. */
  12. class Health
  13. {
  14. /**
  15. * @var \Elastica\Client client object
  16. */
  17. protected $_client;
  18. /**
  19. * @var array the cluster health data
  20. */
  21. protected $_data;
  22. /**
  23. * @param \Elastica\Client $client the Elastica client
  24. */
  25. public function __construct(Client $client)
  26. {
  27. $this->_client = $client;
  28. $this->refresh();
  29. }
  30. /**
  31. * Retrieves the health data from the cluster.
  32. *
  33. * @return array
  34. */
  35. protected function _retrieveHealthData()
  36. {
  37. $endpoint = new \Elasticsearch\Endpoints\Cluster\Health();
  38. $endpoint->setParams(['level' => 'shards']);
  39. $response = $this->_client->requestEndpoint($endpoint);
  40. return $response->getData();
  41. }
  42. /**
  43. * Gets the health data.
  44. *
  45. * @return array
  46. */
  47. public function getData()
  48. {
  49. return $this->_data;
  50. }
  51. /**
  52. * Refreshes the health data for the cluster.
  53. *
  54. * @return $this
  55. */
  56. public function refresh()
  57. {
  58. $this->_data = $this->_retrieveHealthData();
  59. return $this;
  60. }
  61. /**
  62. * Gets the name of the cluster.
  63. *
  64. * @return string
  65. */
  66. public function getClusterName()
  67. {
  68. return $this->_data['cluster_name'];
  69. }
  70. /**
  71. * Gets the status of the cluster.
  72. *
  73. * @return string green, yellow or red
  74. */
  75. public function getStatus()
  76. {
  77. return $this->_data['status'];
  78. }
  79. /**
  80. * TODO determine the purpose of this.
  81. *
  82. * @return bool
  83. */
  84. public function getTimedOut()
  85. {
  86. return $this->_data['timed_out'];
  87. }
  88. /**
  89. * Gets the number of nodes in the cluster.
  90. *
  91. * @return int
  92. */
  93. public function getNumberOfNodes()
  94. {
  95. return $this->_data['number_of_nodes'];
  96. }
  97. /**
  98. * Gets the number of data nodes in the cluster.
  99. *
  100. * @return int
  101. */
  102. public function getNumberOfDataNodes()
  103. {
  104. return $this->_data['number_of_data_nodes'];
  105. }
  106. /**
  107. * Gets the number of active primary shards.
  108. *
  109. * @return int
  110. */
  111. public function getActivePrimaryShards()
  112. {
  113. return $this->_data['active_primary_shards'];
  114. }
  115. /**
  116. * Gets the number of active shards.
  117. *
  118. * @return int
  119. */
  120. public function getActiveShards()
  121. {
  122. return $this->_data['active_shards'];
  123. }
  124. /**
  125. * Gets the number of relocating shards.
  126. *
  127. * @return int
  128. */
  129. public function getRelocatingShards()
  130. {
  131. return $this->_data['relocating_shards'];
  132. }
  133. /**
  134. * Gets the number of initializing shards.
  135. *
  136. * @return int
  137. */
  138. public function getInitializingShards()
  139. {
  140. return $this->_data['initializing_shards'];
  141. }
  142. /**
  143. * Gets the number of unassigned shards.
  144. *
  145. * @return int
  146. */
  147. public function getUnassignedShards()
  148. {
  149. return $this->_data['unassigned_shards'];
  150. }
  151. /**
  152. * get the number of delayed unassined shards.
  153. *
  154. * @return int
  155. */
  156. public function getDelayedUnassignedShards()
  157. {
  158. return $this->_data['delayed_unassigned_shards'];
  159. }
  160. /**
  161. * @return int
  162. */
  163. public function getNumberOfPendingTasks()
  164. {
  165. return $this->_data['number_of_pending_tasks'];
  166. }
  167. /**
  168. * @return int
  169. */
  170. public function getNumberOfInFlightFetch()
  171. {
  172. return $this->_data['number_of_in_flight_fetch'];
  173. }
  174. /**
  175. * @return int
  176. */
  177. public function getTaskMaxWaitingInQueueMillis()
  178. {
  179. return $this->_data['task_max_waiting_in_queue_millis'];
  180. }
  181. /**
  182. * @return int
  183. */
  184. public function getActiveShardsPercentAsNumber()
  185. {
  186. return $this->_data['active_shards_percent_as_number'];
  187. }
  188. /**
  189. * Gets the status of the indices.
  190. *
  191. * @return \Elastica\Cluster\Health\Index[]
  192. */
  193. public function getIndices()
  194. {
  195. $indices = [];
  196. foreach ($this->_data['indices'] as $indexName => $index) {
  197. $indices[$indexName] = new Index($indexName, $index);
  198. }
  199. return $indices;
  200. }
  201. }