HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
manager.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
15 //Set default timezone if not set in host environment
16 @date_default_timezone_set(@date_default_timezone_get());
17 
18 require_once '../inc/manager.inc.php';
19 
20 if(php_sapi_name()!=='cli' || !defined('STDIN')){
21  echo 'Only cli execution mode supported'.PHP_EOL;
22  exit(1);
23 }
24 
26 
27 if(!isset($args['host']) || empty($args['host'])){
28  $args['host']='localhost';
29 }else{
30  //Re-build hosts lists to remove empty values
31  $h=explode(',', $args['host']);
32  foreach($h as $key=>$hs){
33  if(trim($hs)==''){
34  unset($h[$key]);
35  }
36  }
37  $args['host']=implode(',', $h);
38 }
39 
40 if(!isset($args['port']) || empty($args['port'])){
41  $args['port']=HCE_CLUSTER_SCHEMA_PORTS_DEFAULT;
42 }else{
43  //Re-build ports lists to remove empty values
44  $h=explode(',', $args['port']);
45  foreach($h as $key=>$hs){
46  $hs=trim($hs);
47  if($hs=='' || !is_numeric($hs) || ($hs+0)==0){
48  unset($h[$key]);
49  }
50  }
51  $args['port']=implode(',', $h);
52 }
53 
54 if(!isset($args['timeout']) || empty($args['timeout'])){
55  $args['timeout']=HCE_CLUSTER_CMD_TIMEOUT;
56 }
57 
58 if(!isset($args['log']) || $args['log']===''){
59  $args['log']=0; //Silent default
60 }
61 
62 if(!isset($args['scan']) || empty($args['scan'])){
63  $args['scan']=0;
64 }
65 
66 if(!isset($args['ignore']) || empty($args['ignore'])){
67  $args['ignore']=0;
68 }
69 
70 if(!isset($args['tformat']) || empty($args['tformat'])){
71  $args['tformat']=null;
72 }
73 
74 if(!isset($args['port'], $args['command']) || empty($args['command']) || empty($args['port'])){
75  echo hce_manage_get_help();
76  exit(1);
77 }
78 
79 $aborted='';
81 $command_category=0; //0 - Sphinx admin, 1 - cluster structure
82 switch($args['command']){
83  case HCE_SPHINX_CMD_INDEX_CONNECT :
84  case HCE_SPHINX_CMD_INDEX_DISCONNECT :
85  case HCE_SPHINX_CMD_INDEX_DATA_LIST :
86  case HCE_SPHINX_CMD_INDEX_STATUS_SEARCHD :
87  case HCE_SPHINX_CMD_INDEX_STATUS :
88  case HCE_SPHINX_CMD_INDEX_MAX_DOC_ID :
89  case HCE_SPHINX_CMD_INDEX_REMOVE :
90  case HCE_SPHINX_CMD_INDEX_DELETE_DOC_NUMBER :
91  case HCE_SPHINX_CMD_INDEX_CHECK :
92  case HCE_SPHINX_CMD_INDEX_START :
93  case HCE_SPHINX_CMD_INDEX_STOP :
94  case HCE_SPHINX_CMD_INDEX_DELETE_SCHEMA_FILE:
95  case HCE_SPHINX_CMD_INDEX_CREATE : {
96  if(!isset($args['name'])){
97  $aborted='--name=<index_name> ';
98  }else{
99  $options_array['index']=$args['name'];
100  }
101  break;
102  }
103  case HCE_SPHINX_CMD_INDEX_APPEND_DATA_FILE :
104  case HCE_SPHINX_CMD_INDEX_STORE_DATA_FILE : {
105  if(!isset($args['name'])){
106  $aborted='--name=<index_name> ';
107  }else{
108  $options_array['index']=$args['name'];
109  }
110  if(!isset($args['branch'])){
111  $aborted.='--branch=<branch_name> ';
112  }else{
113  $options_array['branch']=$args['branch'];
114  }
115  if(!isset($args['data'])){
116  $aborted.='--data=<data_file> ';
117  }else{
118  if(!file_exists($args['data'])){
119  $aborted.='file '.$args['data'].' ';
120  }else{
121  if(SPHINX_JSON_USE_BASE64){
122  $options_array['data']=base64_encode(file_get_contents($args['data']));
123  }else{
124  $options_array['data']=file_get_contents($args['data']);
125  }
126  }
127  }
128  break;
129  }
130  case HCE_SPHINX_CMD_INDEX_STORE_SCHEMA_FILE : {
131  if(!isset($args['name'])){
132  $aborted='--name=<index_name> ';
133  }else{
134  $options_array['index']=$args['name'];
135  }
136  if(!isset($args['data'])){
137  $aborted.='--data=<data_file> ';
138  }else{
139  if(!file_exists($args['data'])){
140  $aborted.='file '.$args['data'].' ';
141  }else{
142  if(SPHINX_JSON_USE_BASE64){
143  $options_array['data']=base64_encode(file_get_contents($args['data']));
144  }else{
145  $options_array['data']=file_get_contents($args['data']);
146  }
147  }
148  }
149  break;
150  }
151  case HCE_SPHINX_CMD_INDEX_PACK_DOC_DATA :
152  case HCE_SPHINX_CMD_INDEX_MERGE :
153  case HCE_SPHINX_CMD_INDEX_DELETE_DATA_FILE :
154  case HCE_SPHINX_CMD_INDEX_REBUILD : {
155  if(!isset($args['name'])){
156  $aborted='--name=<index_name> ';
157  }else{
158  $options_array['index']=$args['name'];
159  }
160  if(!isset($args['branches'])){
161  $aborted.='--branches=<branches_list_csv> ';
162  }else{
163  $options_array['branches']=explode(',', $args['branches']);
164  }
165  break;
166  }
167  case HCE_SPHINX_CMD_INDEX_DELETE_DOC : {
168  if(!isset($args['name'])){
169  $aborted='--name=<index_name> ';
170  }else{
171  $options_array['index']=$args['name'];
172  }
173  if(!isset($args['documents'])){
174  $aborted.='--documents=<documents_list_csv> ';
175  }else{
176  $options_array['documents']=explode(',', $args['documents']);
177  }
178  break;
179  }
180  case HCE_SPHINX_CMD_INDEX_RENAME :
181  case HCE_SPHINX_CMD_INDEX_COPY : {
182  if(!isset($args['from'])){
183  $aborted='--from=<index_from> ';
184  }else{
185  $options_array['index_from']=$args['from'];
186  }
187  if(!isset($args['to'])){
188  $aborted.='--to=<index_to> ';
189  }else{
190  $options_array['index_to']=$args['to'];
191  }
192  break;
193  }
194  case HCE_SPHINX_CMD_INDEX_SET_CONFIG_VAR : {
195  if(!isset($args['index'])){
196  $aborted='--index=<index_name> ';
197  }else{
198  $options_array['index_name']=$args['index'];
199  }
200  if(!isset($args['type'])){
201  $aborted.='--type=<config_type> ';
202  }else{
203  $options_array['type']=$args['type'];
204  }
205  if(!isset($args['section'])){
206  $aborted='--section=<section_name> ';
207  }else{
208  $options_array['section']=$args['section'];
209  }
210  if(!isset($args['parameter'])){
211  $aborted.='--parameter=<parameter_name> ';
212  }else{
213  $options_array['parameter']=$args['parameter'];
214  }
215  if(!isset($args['value'])){
216  $aborted.='--value=<parameter_value> ';
217  }else{
218  $options_array['value']=$args['value'];
219  }
220  break;
221  }
222  case HCE_CLUSTER_CMD_STRUCTURE_CHECK : {
223  if($args['scan']==1 && count($hosts)!=count($ports)){
224  $aborted='--host and --port must to list the same number of items if --scan=1';
225  }else{
226  if(!isset($args['name'])){
227  $args['name']='';
228  }
230  if($r['error_code']==0){
231  echo $r['data'];
232  }else{
233  if($args['log'] & 1 > 0){
234  echo $r['error_message'];
235  }
236  }
237  }
238 
240  break;
241  }
242  case HCE_CLUSTER_CMD_NODE_SHUTDOWN : {
243  if($args['scan']==1 && count($hosts)!=count($ports)){
244  $aborted='--host and --port must to list the same number of items if --scan=1';
245  }else{
246  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_ADMIN, HCE_CMD_SHUTDOWN);
247  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
248  }
250  break;
251  }
252  case HCE_CLUSTER_CMD_NODE_MMGET : {
253  if($args['scan']==1 && count($hosts)!=count($ports)){
254  $aborted='--host and --port must to list the same number of items if --scan=1';
255  }else{
256  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_REDUCER_PROXY, HCE_CMD_MMGET);
257  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
258  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_SERVER_PROXY, HCE_CMD_MMGET);
259  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
260  }
262  break;
263  }
264  case HCE_CLUSTER_CMD_NODE_MMSET : {
265  $modes=array('smanager'=>0, 'rmanager'=>1, 'rmanager-rnd'=>4, 'rmanager-resources-usage'=>5);
266  if($args['scan']==1 && count($hosts)!=count($ports)){
267  $aborted='--host and --port must to list the same number of items if --scan=1';
268  }else if(!isset($args['mode']) || !array_key_exists($args['mode'], $modes)){
269  $aborted='--mode must be set as "smanager", "rmanager", "rmanager-rnd" or "rmanager-resources-usage"';
270  }else{
271  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_REDUCER_PROXY, HCE_CMD_MMSET, $modes[$args['mode']]);
272  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
273  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_SERVER_PROXY, HCE_CMD_MMSET, $modes[$args['mode']]);
274  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
275  }
277  break;
278  }
279  case HCE_CLUSTER_CMD_NODE_MPMGET : {
280  if($args['scan']==1 && count($hosts)!=count($ports)){
281  $aborted='--host and --port must to list the same number of items if --scan=1';
282  }else{
283  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_REDUCER_PROXY, HCE_CMD_MPMGET);
284  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
285  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_SERVER_PROXY, HCE_CMD_MPMGET);
286  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
287  }
289  break;
290  }
291  case HCE_CLUSTER_CMD_NODE_MPMSET : {
292  if($args['scan']==1 && count($hosts)!=count($ports)){
293  $aborted='--host and --port must to list the same number of items if --scan=1';
294  }else if(!isset($args['purge'])) {
295  $aborted='--purge must be set as "1" or "0"';
296  }else{
297  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_REDUCER_PROXY, HCE_CMD_MPMSET, $args['purge']);
298  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
299  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_SERVER_PROXY, HCE_CMD_MPMSET, $args['purge']);
300  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
301  }
303  break;
304  }
305  case HCE_CLUSTER_CMD_NODE_MRCSGET : {
306  if($args['scan']==1 && count($hosts)!=count($ports)){
307  $aborted='--host and --port must to list the same number of items if --scan=1';
308  }else{
309  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_SERVER_PROXY, HCE_CMD_MRCSGET);
310  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
311  }
313  break;
314  }
315  case HCE_CLUSTER_CMD_NODE_MRCSSET : {
316  if($args['scan']==1 && count($hosts)!=count($ports)){
317  $aborted='--host and --port must to list the same number of items if --scan=1';
318  }else if(!isset($args['csize'])) {
319  $aborted='--csize must be set as numeric value, "0" - unlimited size';
320  }else{
321  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_SERVER_PROXY, HCE_CMD_MRCSSET, $args['csize']);
322  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
323  }
325  break;
326  }
327  case HCE_CLUSTER_CMD_NODE_TIME :
328  case HCE_CLUSTER_CMD_NODE_ECHO : {
329  //Map manager command to ACN node API command
330  $manager_to_hce_commands_map=array(HCE_CLUSTER_CMD_NODE_TIME=>HCE_CMD_TIME, HCE_CLUSTER_CMD_NODE_ECHO=>HCE_CMD_ECHO);
331 
332  if($args['scan']==1 && count($hosts)!=count($ports)){
333  $aborted='--host and --port must to list the same number of items if --scan=1';
334  }else{
335  $nodes=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_ADMIN, $manager_to_hce_commands_map[$args['command']]);
336  foreach($nodes as $node_key=>$node_data){
337  if(isset($node_data[HCE_HANDLER_ADMIN])){
338  $properties=hce_manage_node_get_handler_properties($node_data[HCE_HANDLER_ADMIN]);
339  if($properties!==false){
340  if($args['command']==HCE_CLUSTER_CMD_NODE_PROPERTIES){
341  $properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_TIME]=date(HCE_CLUSTER_CMD_DATE_FORMAT, round($properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_TIME]/1000));
342  $properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_NAME]=$properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CIDENTITY];
343  unset($properties[HCE_CLUSTER_CMD_PROPERTIES_FIELD_CIDENTITY]);
344  }
345  $nodes[$node_key]=$properties;
346  }
347  }
348  }
349  echo cli_prettyPrintJson(json_encode($nodes), ' ').PHP_EOL;
350  }
352  break;
353  }
354  case HCE_CLUSTER_CMD_NODE_LLGET :
355  case HCE_CLUSTER_CMD_NODE_LLSET :
356  case HCE_CLUSTER_CMD_NODE_PROPERTIES :
357  case HCE_CMD_NODE_RESOURCE_USAGE :
358  case HCE_CLUSTER_CMD_NODE_HB_DELAY_GET :
359  case HCE_CLUSTER_CMD_NODE_HB_DELAY_SET :
360  case HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_GET :
361  case HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_SET :
362  case HCE_CLUSTER_CMD_NODE_HB_MODE_GET :
363  case HCE_CLUSTER_CMD_NODE_HB_MODE_SET :
364  case HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_GET:
365  case HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_SET:
366  case HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_GET:
367  case HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_SET:
368  case HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_GET:
369  case HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_SET: {
370  if($args['scan']==1 && count($hosts)!=count($ports)){
371  $aborted='--host and --port must to list the same number of items if --scan=1 ';
372  break;
373  }
374  if(!isset($args['handler'])){
375  $aborted='--handler=<handler_name{ADMIN,ROUTERSERVER_PROXY,DATASERVER_PROXY,DATA_CLIENT_PROXY,DATAPROCESSOR_DATA,DATACLIENT_DATA,DATAREDUCER_PROXY,*}> ';
376  break;
377  }
378  if($args['command']==HCE_CLUSTER_CMD_NODE_LLSET && !isset($args['level'])){
379  $aborted='--level=<level(s)_value_csv> ';
380  break;
381  }
382  //List of all supported handler names
383  $handlers=array(HCE_HANDLER_ADMIN, HCE_HANDLER_ROUTER_SERVER_PROXY, HCE_HANDLER_DATA_SERVER_PROXY, HCE_HANDLER_DATA_CLIENT_PROXY, HCE_HANDLER_DATA_PROCESSOR_DATA,
384  HCE_HANDLER_DATA_CLIENT_DATA, HCE_HANDLER_DATA_REDUCER_PROXY);
385  $handlers_list=explode(',', $args['handler']);
386  //Extend all handlers value "*" to exact handlers list
387  if($handlers_list[0]=='*'){
388  $handlers_list=$handlers;
389  }
390  //Set levels list
391  if($args['command']==HCE_CLUSTER_CMD_NODE_LLSET){
392  $levels_list=explode(',', $args['level']);
393  if((count($levels_list)>count($handlers_list)) ||
394  ((count($handlers_list)>1) && (count($levels_list)>1) && (count($levels_list)!=count($handlers_list)))
395  ){
396  $aborted='handler and level value mismatch by items number, --handler and --level > ';
397  break;
398  }
399  //Extend levels list to have the same items as handlers
400  if(count($levels_list)==1 && count($handlers_list)>1){
401  for($i=0; $i<count($handlers_list)-1; $i++){
402  $levels_list[]=$levels_list[0];
403  }
404  }
405  }else{
406  $levels_list=array();
407  }
408  foreach($handlers_list as $handler_item){
409  if(!in_array($handler_item, $handlers)){
410  $aborted='Handler name "'.$handler_item.'" not supported, --handler ';
411  break 2;
412  }
413  }
414  //Map manager command to ACN node API command
415  $mgr_to_hce_cmd_map=array(HCE_CLUSTER_CMD_NODE_LLSET=>HCE_CMD_LLSET, HCE_CLUSTER_CMD_NODE_LLGET=>HCE_CMD_LLGET,
416  HCE_CLUSTER_CMD_NODE_PROPERTIES=>HCE_CMD_PROPERTIES, HCE_CLUSTER_CMD_NODE_RESOURCE_USAGE=>HCE_CMD_NODE_RESOURCE_USAGE,
417  HCE_CLUSTER_CMD_NODE_HB_DELAY_SET=>HCE_CMD_HEARTBEAT_DELAY_SET, HCE_CLUSTER_CMD_NODE_HB_DELAY_GET=>HCE_CMD_HEARTBEAT_DELAY_GET,
418  HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_SET=>HCE_CMD_HEARTBEAT_TIMEOUT_SET, HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_GET=>HCE_CMD_HEARTBEAT_TIMEOUT_GET,
419  HCE_CLUSTER_CMD_NODE_HB_MODE_SET=>HCE_CMD_HEARTBEAT_MODE_SET, HCE_CLUSTER_CMD_NODE_HB_MODE_GET=>HCE_CMD_HEARTBEAT_MODE_GET,
420  HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_SET=>HCE_CMD_POLL_TIMEOUT_SET, HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_GET=>HCE_CMD_POLL_TIMEOUT_GET,
421  HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_SET=>HCE_CMD_PROPERTY_INTERVAL_SET, HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_GET=>HCE_CMD_PROPERTY_INTERVAL_GET,
422  HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_SET=>HCE_CMD_DUMP_INTERVAL_SET, HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_GET=>HCE_CMD_DUMP_INTERVAL_GET);
423  //Get nodes info
424  $node_info=hce_manage_nodes_get_info($args);
425 
426  $ret=array();
427  //For each detected node
428  foreach($node_info as $node=>$node_data){
429  //For each of possible handlers
430  for($i=0; $i<count($handlers_list); $i++){
431  if($node_data['error']==0 && isset($node_data['data'][$handlers_list[$i]])){
432  $properties=hce_manage_node_get_handler_properties($node_data['data'][$handlers_list[$i]]);
433 
434  if($properties!==false){
435  //Init params value
436  $param_value='';
437  switch ($args['command']){
438  case HCE_CLUSTER_CMD_NODE_LLSET:
439  $param_value=$levels_list[$i];
440  break;
441  case HCE_CLUSTER_CMD_NODE_HB_DELAY_SET:{
442  if($args['scan']==1 && count($hosts)!=count($ports)){
443  $aborted='--host and --port must to list the same number of items if --scan=1';
444  }else if(!isset($args['hbdelay'])) {
445  $aborted='--hbdelay must be set im sec';
446  }else{
447  $param_value=$args['hbdelay'];
448  }
449  }
450  break;
451  case HCE_CLUSTER_CMD_NODE_HB_TIMEOUT_SET:{
452  if($args['scan']==1 && count($hosts)!=count($ports)){
453  $aborted='--host and --port must to list the same number of items if --scan=1';
454  }else if(!isset($args['hbtimeout'])) {
455  $aborted='--hbtimeout must be set in sec';
456  }else{
457  $param_value=$args['hbtimeout'];
458  }
459  }
460  break;
461  case HCE_CLUSTER_CMD_NODE_HB_MODE_SET:{
462  if($args['scan']==1 && count($hosts)!=count($ports)){
463  $aborted='--host and --port must to list the same number of items if --scan=1';
464  }else if(!isset($args['hbmode'])) {
465  $aborted='--hbmode must be set in numeric value';
466  }else{
467  $param_value=$args['hbmode'];
468  }
469  }
470  break;
471  case HCE_CLUSTER_CMD_NODE_POLL_TIMEOUT_SET:{
472  if($args['scan']==1 && count($hosts)!=count($ports)){
473  $aborted='--host and --port must to list the same number of items if --scan=1';
474  }else if(!isset($args['ptimeout'])) {
475  $aborted='--ptimeout must be set in numeric value';
476  }else{
477  $param_value=$args['ptimeout'];
478  }
479  }
480  break;
481  case HCE_CLUSTER_CMD_NODE_PROPERTY_INTERVAL_SET:{
482  if($args['scan']==1 && count($hosts)!=count($ports)){
483  $aborted='--host and --port must to list the same number of items if --scan=1';
484  }else if(!isset($args['interval'])) {
485  $aborted='--interval must be set in numeric value';
486  }else{
487  $param_value=$args['interval'];
488  }
489  }
490  break;
491  case HCE_CLUSTER_CMD_NODE_DUMP_INTERVAL_SET:{
492  if($args['scan']==1 && count($hosts)!=count($ports)){
493  $aborted='--host and --port must to list the same number of items if --scan=1';
494  }else if(!isset($args['interval'])) {
495  $aborted='--interval must be set in numeric value';
496  }else{
497  $param_value=$args['interval'];
498  }
499  }
500  break;
501  case HCE_CLUSTER_CMD_NODE_PROPERTIES:{
502  if($args['scan']==1 && count($hosts)!=count($ports)){
503  $aborted='--host and --port must to list the same number of items if --scan=1';
504  }else{
505  if(isset($args['realtime'])) {
506  $param_value=$args['realtime'];
507  }else{
508  $param_value='';
509  }
510  }
511  }
512  break;
513  }
514 
515  //Execution command with parameters
516  $ret[$node][$handlers_list[$i]]=hce_manage_node_handler_command($node_data['host'], $node_data['port'], $handlers_list[$i], $mgr_to_hce_cmd_map[$args['command']],
517  $param_value, $args['timeout']);
518  if($ret[$node][$handlers_list[$i]]['error']==0){
519  $properties=hce_manage_node_get_handler_properties($ret[$node][$handlers_list[$i]]['data'][$handlers_list[$i]], HCE_ADMIN_CMD_DELIMITER, $args['tformat']);
520 
521  if(isset($properties['clients'])) {
522  $properties['clients'] = json_decode($properties['clients'], true);
523 
524  if(is_array($properties['clients']['clients']) && $args['tformat']!==null){
525  //$properties['clients']['expiry']=date($args['tformat'], ceil($properties['clients']['expiry']/1000));
526  foreach($properties['clients']['clients'] as $key=>$client){
527  if(is_array($client)){
528  $properties['clients']['clients'][$key]['expiry']=date($args['tformat'], ceil($client['expiry']/1000));
529  }
530  }
531  }
532  }
533 
534  if($properties!==false){
535  if($args['command']==HCE_CLUSTER_CMD_NODE_PROPERTIES){
536  //Extended format of Sphinx FO properties as key-value array
537  if(isset($args['sphinx_properties_formatted']) && $args['sphinx_properties_formatted']=='yes' && isset($properties['sphinxDataStorageProperties'])){
538  $props=explode(',', $properties['sphinxDataStorageProperties']);
539  $properties['sphinxDataStorageProperties']=array();
540  foreach($props as $value){
541  $props2=explode(':', $value);
542  //$properties['sphinxDataStorageProperties'][$props2[0]]=$props2[1];
543  if($props2[0]=='ranker_expression'){
544  $properties['sphinxDataStorageProperties'][$props2[0]]=rawurldecode($props2[1]);
545  }else{
546  $properties['sphinxDataStorageProperties'][$props2[0]]=$props2[1];
547  }
548  }
549  }
550  //Extended format of DRCE FO properties as key-value array
551  if(isset($args['sphinx_properties_formatted']) && $args['sphinx_properties_formatted']=='yes' && isset($properties['drceDataStorageProperties'])){
552  $props=explode(',', $properties['drceDataStorageProperties']);
553  $properties['drceDataStorageProperties']=array();
554  foreach($props as $value){
555  $props2=explode(':', $value);
556  $properties['drceDataStorageProperties'][$props2[0]]=$props2[1];
557  }
558  }
559  $ret[$node][$handlers_list[$i]]=$properties;
560  }else{
561  if(isset($properties[HCE_CLUSTER_CMD_PROPERTY_NAME_LOG])){
562  $ret[$node][$handlers_list[$i]]=$properties[HCE_CLUSTER_CMD_PROPERTY_NAME_LOG];
563  }else{
564 
565  }
566  }
567  }else{
568 
569  }
570 
571  }else{
572  $ret[$node][$handlers_list[$i]]='Error : '.$ret[$node][$handlers_list[$i]]['error'];
573  }
574  }else{
575 
576  }
577  }else{
578  $ret[$node][$handlers_list[$i]]='Error : Node not found at '.$node.' or connection timeout.';
579  }
580  }
581  }
582  echo cli_prettyPrintJson(json_encode($ret), ' ').PHP_EOL;
583 
585  break;
586  }
587  case HCE_CLUSTER_CMD_NODE_DRCE : {
588  require_once '../inc/hce_drce_api.inc.php';
589  if($args['scan']==1 && count($hosts)!=count($ports)){
590  $aborted='--host and --port must to list the same number of items if --scan=1';
591  }else{
592  if(!isset($args['request']) || !isset($args['id']) || !isset($args['json'])){
593  $aborted='--request, --json and --id is mandatory options for this command!';
594  break;
595  }
596  $REQUEST_TYPE=isset($args['request']) ? $args['request'] : 'CHECK';
597  $requests_names=array('SET'=>HCE_DRCE_REQUEST_TYPE_SET, 'CHECK'=>HCE_DRCE_REQUEST_TYPE_CHECK, 'TERMINATE'=>HCE_DRCE_REQUEST_TYPE_TERMINATE, 'GET'=>HCE_DRCE_REQUEST_TYPE_GET, 'DELETE'=>HCE_DRCE_REQUEST_TYPE_DELETE);
598  if(!in_array($REQUEST_TYPE, $requests_names)){
599  echo 'Error: --request not supported "'.$REQUEST_TYPE.'"'.PHP_EOL;
600  exit(1);
601  }else{
603  }
604  $REQUEST_ID=isset($args['id']) ? $args['id'] : 0;
605  if($REQUEST_ID==0 && $REQUEST_TYPE!=0){
606  echo 'Error: this --request required --id value greater than zero'.PHP_EOL;
607  exit(1);
608  }else{
609  //Generate request Id
610  if($REQUEST_ID==0 && $REQUEST_TYPE==0){
611  $REQUEST_ID=crc32(hce_unique_message_id(1, microtime(true)));
612  }
613  }
614  $input_json=trim(file_get_contents($args['json']));
615  if($args['log']>2){
616  echo 'Input file json: "'.$input_json.'"'.PHP_EOL;
617  }
618  $params_array=hce_drce_exec_create_parameters_array($input_json, $REQUEST_TYPE);
619  if($args['log']>2){
620  echo 'Parameters array: "'.var_export($params_array, TRUE).'"'.PHP_EOL;
621  }
622  $parameters=hce_drce_exec_prepare_request_admin($params_array, $REQUEST_TYPE, $REQUEST_ID);
623  if($args['log']>2){
624  echo 'DRCE reques json: "'.$parameters.'"'.PHP_EOL.PHP_EOL;
625  }
626  $nodes=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CLUSTER_CMD_NODE_DRCE, $parameters);
627  //Parse responses jsons
628  foreach($nodes as $node_key=>$node_data){
629  if(isset($node_data[HCE_HANDLER_DATA_PROCESSOR_DATA])){
630  $node_data[HCE_HANDLER_DATA_PROCESSOR_DATA]=explode(HCE_ADMIN_CMD_DELIMITER, $node_data[HCE_HANDLER_DATA_PROCESSOR_DATA]);
631  if(isset($node_data[HCE_HANDLER_DATA_PROCESSOR_DATA][1])){
632  $node_data[HCE_HANDLER_DATA_PROCESSOR_DATA][1]=json_decode($node_data[HCE_HANDLER_DATA_PROCESSOR_DATA][1], true);
633  $node_data[HCE_HANDLER_DATA_PROCESSOR_DATA][1][0]['stdout']=base64_decode($node_data[HCE_HANDLER_DATA_PROCESSOR_DATA][1][0]['stdout']);
634  $node_data[HCE_HANDLER_DATA_PROCESSOR_DATA][1][0]['stderror']=base64_decode($node_data[HCE_HANDLER_DATA_PROCESSOR_DATA][1][0]['stderror']);
635  }
636  $nodes[$node_key]=$node_data[HCE_HANDLER_DATA_PROCESSOR_DATA];
637  }
638  }
639 
640  echo cli_prettyPrintJson(json_encode($nodes), ' ').PHP_EOL;
641  }
643  break;
644  }
645  case HCE_CMD_DRCE_SET_HOST : {
646  if($args['scan']==1 && count($hosts)!=count($ports)){
647  $aborted='--host and --port must to list the same number of items if --scan=1';
648  }else if(!isset($args['host'])){
649  $aborted='--host must be set value different from zero';
650  }else{
651  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CMD_DRCE_SET_HOST, $args['host']);
652  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
653  }
655  break;
656  }
657  case HCE_CMD_DRCE_GET_HOST : {
658  if($args['scan']==1 && count($hosts)!=count($ports)){
659  $aborted='--host and --port must to list the same number of items if --scan=1';
660  }else{
661  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CMD_DRCE_GET_HOST);
662  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
663  }
665  break;
666  }
667  case HCE_CMD_DRCE_SET_PORT : {
668  if($args['scan']==1 && count($hosts)!=count($ports)){
669  $aborted='--host and --port must to list the same number of items if --scan=1';
670  }else if(!isset($args['port'])){
671  $aborted='--port must be set value different from zero';
672  }else{
673  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CMD_DRCE_SET_PORT, $args['port']);
674  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
675  }
677  break;
678  }
679  case HCE_CMD_DRCE_GET_PORT : {
680  if($args['scan']==1 && count($hosts)!=count($ports)){
681  $aborted='--host and --port must to list the same number of items if --scan=1';
682  }else{
683  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CMD_DRCE_GET_PORT);
684  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
685  }
687  break;
688  }
689  case HCE_CMD_DRCE_GET_TASKS : {
690  if($args['scan']==1 && count($hosts)!=count($ports)){
691  $aborted='--host and --port must to list the same number of items if --scan=1';
692  }else{
693  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CMD_DRCE_GET_TASKS);
694  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
695  }
697  break;
698  }
699  case HCE_CMD_DRCE_GET_TASKS_INFO : {
700  if($args['scan']==1 && count($hosts)!=count($ports)){
701  $aborted='--host and --port must to list the same number of items if --scan=1';
702  }else{
703  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CMD_DRCE_GET_TASKS_INFO);
704  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
705  }
707  break;
708  }
709  case HCE_CMD_NODE_RECOVER_NOTIFICATION_CONNECTION : {
710  if($args['scan']==1 && count($hosts)!=count($ports)){
711  $aborted='--host and --port must to list the same number of items if --scan=1';
712  }else{
713  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_PROCESSOR_DATA, HCE_CMD_NODE_RECOVER_NOTIFICATION_CONNECTION);
714  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
715  }
717  break;
718  }
719  case HCE_CMD_NODE_ROUTES : {
720  if($args['scan']==1 && count($hosts)!=count($ports)){
721  $aborted='--host and --port must to list the same number of items if --scan=1';
722  }else{
723  $r=hce_manage_command_cluster_node_handler_command($args, HCE_HANDLER_DATA_SERVER_PROXY, HCE_CMD_NODE_ROUTES);
724  echo cli_prettyPrintJson(json_encode($r), ' ').PHP_EOL;
725  }
727  break;
728  }
729  default : {
730  $aborted='command "'.$args['command'].'" not supported';
731  break;
732  }
733 }
734 
735 if($aborted=='' && $command_category==0){
736  $connection_array=hce_connection_create(array('host'=>$args['host'], 'port'=>$args['port'], 'type'=>HCE_CONNECTION_TYPE_ADMIN, 'identity'=>hce_unique_client_id()));
737  if($connection_array['error']==0){
738  $ret=hce_sphinx_exec($args['command'], $options_array, $connection_array, $args['timeout']);
739  if(!is_array($ret)){
740  echo 'Error of command execution '.$ret.PHP_EOL;
741  switch($ret){
742  case HCE_PROTOCOL_ERROR_TIMEOUT : {
743  echo 'Timeout '.$args['timeout'].' ms, or connection error!'.PHP_EOL;
744  break;
745  }
746  case HCE_SPHINX_ERROR_PARSE_RESPONSE : {
747  echo 'Wrong response format!'.PHP_EOL;
748  break;
749  }
750  }
751  }else{
752  echo 'Command executed '.PHP_EOL;
753  if($ret['error_code']!=HCE_SPHINX_ERROR_OK){
754  echo 'Returned error '.$ret['error_code'].' : '.$ret['error_message'].PHP_EOL;
755  if(!empty($ret['data'])){
756  if(SPHINX_JSON_USE_BASE64){
757  echo 'data: '.base64_decode($ret['data']).PHP_EOL;
758  }else{
759  echo 'data: '.$ret['data'].PHP_EOL;
760  }
761  }
762  }else{
763  switch($args['command']){
764  case HCE_SPHINX_CMD_INDEX_DATA_LIST :
765  case HCE_SPHINX_CMD_INDEX_MERGE :
766  case HCE_SPHINX_CMD_INDEX_PACK_DOC_DATA :
767  case HCE_SPHINX_CMD_INDEX_DELETE_DOC_NUMBER :
768  case HCE_SPHINX_CMD_INDEX_STATUS_SEARCHD :
769  case HCE_SPHINX_CMD_INDEX_STATUS :
770  case HCE_SPHINX_CMD_INDEX_MAX_DOC_ID :
771  case HCE_SPHINX_CMD_INDEX_CHECK : {
772  if(SPHINX_JSON_USE_BASE64){
773  echo 'data: '.base64_decode($ret['data']).PHP_EOL;
774  }else{
775  echo 'data: '.$ret['data'].PHP_EOL;
776  }
777  }
778  }
779  }
780  echo 'time: '.$ret['time'].' ms'.PHP_EOL;
781  }
782  hce_connection_delete($connection_array);
783  }else{
784  echo 'Error create acn connection '.$connection_array['error'].$ret.PHP_EOL;
785  }
786 }else if($aborted!==''){
787  echo $aborted.' required'.PHP_EOL;
788 }
789 
790 exit();
791 
792 ?>