Info.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Elastica\Node;
  3. use Elastica\Node as BaseNode;
  4. /**
  5. * Elastica cluster node object.
  6. *
  7. * @author Nicolas Ruflin <spam@ruflin.com>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-status.html
  10. */
  11. class Info
  12. {
  13. /**
  14. * Response.
  15. *
  16. * @var \Elastica\Response Response object
  17. */
  18. protected $_response;
  19. /**
  20. * Stats data.
  21. *
  22. * @var array stats data
  23. */
  24. protected $_data = [];
  25. /**
  26. * Node.
  27. *
  28. * @var \Elastica\Node Node object
  29. */
  30. protected $_node;
  31. /**
  32. * Query parameters.
  33. *
  34. * @var array
  35. */
  36. protected $_params = [];
  37. /**
  38. * Unique node id.
  39. *
  40. * @var string
  41. */
  42. protected $_id;
  43. /**
  44. * Create new info object for node.
  45. *
  46. * @param \Elastica\Node $node Node object
  47. * @param array $params List of params to return. Can be: settings, os, process, jvm, thread_pool, network, transport, http
  48. */
  49. public function __construct(BaseNode $node, array $params = [])
  50. {
  51. $this->_node = $node;
  52. $this->refresh($params);
  53. }
  54. /**
  55. * Returns the entry in the data array based on the params.
  56. * Several params possible.
  57. *
  58. * Example 1: get('os', 'mem', 'total') returns total memory of the system the
  59. * node is running on
  60. * Example 2: get('os', 'mem') returns an array with all mem infos
  61. *
  62. * @return mixed Data array entry or null if not found
  63. */
  64. public function get()
  65. {
  66. $data = $this->getData();
  67. foreach (func_get_args() as $arg) {
  68. if (isset($data[$arg])) {
  69. $data = $data[$arg];
  70. } else {
  71. return;
  72. }
  73. }
  74. return $data;
  75. }
  76. /**
  77. * Return port of the node.
  78. *
  79. * @return string Returns Node port
  80. */
  81. public function getPort()
  82. {
  83. // Returns string in format: inet[/192.168.1.115:9201]
  84. $data = $this->get('http_address');
  85. $data = substr($data, 6, strlen($data) - 7);
  86. $data = explode(':', $data);
  87. return $data[1];
  88. }
  89. /**
  90. * Return IP of the node.
  91. *
  92. * @return string Returns Node ip address
  93. */
  94. public function getIp()
  95. {
  96. // Returns string in format: inet[/192.168.1.115:9201]
  97. $data = $this->get('http_address');
  98. $data = substr($data, 6, strlen($data) - 7);
  99. $data = explode(':', $data);
  100. return $data[0];
  101. }
  102. /**
  103. * Return data regarding plugins installed on this node.
  104. *
  105. * @return array plugin data
  106. *
  107. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
  108. */
  109. public function getPlugins()
  110. {
  111. if (!in_array('plugins', $this->_params)) {
  112. //Plugin data was not retrieved when refresh() was called last. Get it now.
  113. $this->_params[] = 'plugins';
  114. $this->refresh($this->_params);
  115. }
  116. return $this->get('plugins');
  117. }
  118. /**
  119. * Check if the given plugin is installed on this node.
  120. *
  121. * @param string $name plugin name
  122. *
  123. * @return bool true if the plugin is installed, false otherwise
  124. */
  125. public function hasPlugin($name)
  126. {
  127. foreach ($this->getPlugins() as $plugin) {
  128. if ($plugin['name'] == $name) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. /**
  135. * Return all info data.
  136. *
  137. * @return array Data array
  138. */
  139. public function getData()
  140. {
  141. return $this->_data;
  142. }
  143. /**
  144. * Return node object.
  145. *
  146. * @return \Elastica\Node Node object
  147. */
  148. public function getNode()
  149. {
  150. return $this->_node;
  151. }
  152. /**
  153. * @return string Unique node id
  154. */
  155. public function getId()
  156. {
  157. return $this->_id;
  158. }
  159. /**
  160. * @return string Node name
  161. */
  162. public function getName()
  163. {
  164. return $this->_data['name'];
  165. }
  166. /**
  167. * Returns response object.
  168. *
  169. * @return \Elastica\Response Response object
  170. */
  171. public function getResponse()
  172. {
  173. return $this->_response;
  174. }
  175. /**
  176. * Reloads all nodes information. Has to be called if informations changed.
  177. *
  178. * @param array $params Params to return (default none). Possible options: settings, os, process, jvm, thread_pool, network, transport, http, plugin
  179. *
  180. * @return \Elastica\Response Response object
  181. */
  182. public function refresh(array $params = [])
  183. {
  184. $this->_params = $params;
  185. $endpoint = new \Elasticsearch\Endpoints\Cluster\Nodes\Info();
  186. $endpoint->setNodeID($this->getNode()->getId());
  187. if (!empty($params)) {
  188. $endpoint->setMetric($params);
  189. }
  190. $this->_response = $this->getNode()->getClient()->requestEndpoint($endpoint);
  191. $data = $this->getResponse()->getData();
  192. $this->_data = reset($data['nodes']);
  193. $this->_id = key($data['nodes']);
  194. $this->getNode()->setId($this->getId());
  195. }
  196. }