monitor.cgi 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Carp;
  4. use MyCGI;
  5. use XML::RPC;
  6. use MonitorGUI::View;
  7. use vars qw ($THIS_URL $GRID_SERVER_URL $DEFAULT_PROXY_PORT);
  8. $THIS_URL = "http://10.8.1.165/monitorgui/monitor.cgi";
  9. $GRID_SERVER_URL = "http://10.8.1.165/opensim/grid.cgi";
  10. $DEFAULT_PROXY_PORT = 9000;
  11. my %ACTIONS = (
  12. # Region commands
  13. move => \&move_command,
  14. split => \&split_command,
  15. merge => \&merge_command,
  16. # display commands
  17. default => \&main_screen,
  18. refresh => \&refresh,
  19. );
  20. # ##################
  21. # main
  22. my $param = &MyCGI::getParam;
  23. my $act = $param->{A} || "default";
  24. my $contents = "";
  25. if (!$ACTIONS{$act}) {
  26. &gui_error("404 NOT FOUND");
  27. } else {
  28. eval {
  29. $ACTIONS{$act}->($param);
  30. };
  31. if ($@) {
  32. &gui_error($@);
  33. }
  34. }
  35. # #################
  36. # Region Commands
  37. sub move_command {
  38. my $param = shift;
  39. # from
  40. my $from_ip = $param->{from_ip} || Carp::croak("not enough params (from_ip)");
  41. my $from_port = $param->{from_port} || Carp::croak("not enough params (from_port)");
  42. my $from_url = "http://" . $param->{from_ip} . ":" . $DEFAULT_PROXY_PORT;
  43. # to
  44. my $to_ip = $param->{to_ip} || Carp::croak("not enough params (to_ip)");
  45. my $to_port = $param->{to_port} || Carp::croak("not enough params (to_port)");
  46. my $to_url = "http://" . $param->{to_ip} . ":" . $DEFAULT_PROXY_PORT;
  47. # commands
  48. eval {
  49. &OpenSim::Utility::XMLRPCCall_array($from_url, "SerializeRegion", [$from_ip, $from_port]);
  50. &OpenSim::Utility::XMLRPCCall_array($to_url, "DeserializeRegion_Move", [$from_ip, $from_port, $to_ip, $to_port]);
  51. &OpenSim::Utility::XMLRPCCall_array($from_url, "TerminateRegion", [$from_port]);
  52. };
  53. if ($@) {
  54. print STDERR "Get Status Error: $@\n";
  55. }
  56. # client refresh
  57. &redirect_refresh({wait=>5, force=>"$from_url|$to_url", msg=>"Move region $from_ip:$from_port from $from_url to $to_url"});
  58. }
  59. sub split_command {
  60. my $param = shift;
  61. # from
  62. my $from_ip = $param->{from_ip} || Carp::croak("not enough params (from_ip)");
  63. my $from_port = $param->{from_port} || Carp::croak("not enough params (from_port)");
  64. my $from_url = "http://" . $param->{from_ip} . ":" . $DEFAULT_PROXY_PORT;
  65. # to
  66. my $to_ip = $param->{to_ip} || Carp::croak("not enough params (to_ip)");
  67. my $to_port = $param->{to_port} || Carp::croak("not enough params (to_port)");
  68. my $to_url = "http://" . $param->{to_ip} . ":" . $DEFAULT_PROXY_PORT;
  69. # commands
  70. eval {
  71. &OpenSim::Utility::XMLRPCCall_array($from_url, "SerializeRegion", [$from_ip, $from_port]);
  72. &OpenSim::Utility::XMLRPCCall_array($to_url, "DeserializeRegion_Clone", [$from_ip, $from_port, $to_ip, $to_port]);
  73. };
  74. if ($@) {
  75. print STDERR "Get Status Error: $@\n";
  76. }
  77. &redirect_refresh({wait=>5, force=>"$from_url", msg=>"Split region $from_ip:$from_port"});
  78. }
  79. sub merge_command {
  80. my $param = shift;
  81. # from
  82. my $from_ip = $param->{from_ip} || Carp::croak("not enough params (from_ip)");
  83. my $url = "http://" . $param->{from_ip} . ":" . $DEFAULT_PROXY_PORT;
  84. # ports
  85. my $master_port = $param->{master_port} || Carp::croak("not enough params (master_port)");
  86. my $slave_ip = $param->{slave_ip} || Carp::croak("not enough params (slave_ip)");
  87. my $slave_port = $param->{slave_port} || Carp::croak("not enough params (slave_port)");
  88. my $slave_url = "http://" . $param->{slave_ip} . ":" . $DEFAULT_PROXY_PORT;
  89. # commands
  90. eval {
  91. &XMLRPCCall_array($url, "MergeRegions", [$from_ip, $master_port]);
  92. &XMLRPCCall_array($slave_url, "TerminateRegion", [$slave_port]);
  93. };
  94. if ($@) {
  95. print STDERR "Get Status Error: $@\n";
  96. }
  97. &redirect_refresh({wait=>5, force=>"$url", msg=>"Merge region $from_ip:$master_port, $slave_port"});
  98. }
  99. # #################
  100. # Display
  101. sub main_screen {
  102. my %xml_rpc_param = (
  103. # TODO: should be 0 - 65535 ?
  104. xmin => 999, ymin => 999, xmax => 1010, ymax => 1010,
  105. );
  106. my $res_obj = undef;
  107. eval {
  108. $res_obj = &XMLRPCCall($GRID_SERVER_URL, "map_block", \%xml_rpc_param);
  109. };
  110. if ($@) {
  111. &gui_error("map_block Error: " . $@);
  112. }
  113. my %copy_obj = %$res_obj;
  114. my $getstatus_failed = "<font color=\"red\">GetStatus Failed</font>";
  115. my $regions_list = $res_obj->{"sim-profiles"};
  116. foreach(@$regions_list) {
  117. if ($_->{sim_ip} && $_->{sim_port}) {
  118. my $url = "http://" . $_->{sim_ip} . ":" . $DEFAULT_PROXY_PORT;
  119. my $port = $_->{sim_port};
  120. my $res = undef;
  121. eval {
  122. $res = &XMLRPCCall_array($url, "GetStatus", [$port]);
  123. };
  124. if ($@) {
  125. print STDERR "Get Status Error: $@\n";
  126. }
  127. $_->{get_scene_presence_filter} = $res ? $res->{get_scene_presence_filter} : $getstatus_failed;
  128. $_->{get_scene_presence} = $res ? $res->{get_scene_presence} : $getstatus_failed;
  129. $_->{get_avatar_filter} = $res ? $res->{get_avatar_filter} : $getstatus_failed;
  130. $_->{get_avatar} = $res ? $res->{get_avatar} : $getstatus_failed;
  131. $_->{avatar_names} = $res ? $res->{avatar_names} : "NO USER";
  132. }
  133. }
  134. my $html = &MonitorGUI::View::html(\%copy_obj);
  135. &MyCGI::outputHtml("UTF-8", &MonitorGUI::View::screen_header . $html . &MonitorGUI::View::screen_footer);
  136. }
  137. sub gui_error {
  138. my $msg = shift;
  139. &MyCGI::outputHtml("UTF-8", "<h1>ERROR</h1><hr />$msg");
  140. }
  141. sub redirect_refresh {
  142. my $args = shift;
  143. my $wait = $args->{wait};
  144. my $force = $args->{force} || "";
  145. my $msg = $args->{msg} || "";
  146. my $param = "A=refresh&wait=$wait&ip=$force&msg=$msg";
  147. my $dist_url = $THIS_URL . "?" . $param;
  148. &MyCGI::redirect($dist_url);
  149. }
  150. sub refresh {
  151. my $param = shift;
  152. my $msg = $param->{msg} || "";
  153. my $wait = $param->{wait} || 0;
  154. my $force = $param->{ip} || "";
  155. #my $jump_url = $force ? "$THIS_URL?A=force&ip=$force" : $THIS_URL;
  156. my $jump_url = $THIS_URL;
  157. my $html =<< "HTML";
  158. <html>
  159. <head>
  160. <meta http-equiv="Refresh" content="$wait;URL=$jump_url" />
  161. <title>Region Monitor GUI REFRESH</title>
  162. </head>
  163. <body>
  164. <h3>$msg</h3>
  165. <br>
  166. wait <font color="red"><b>$wait</b></font> sec for server to take effect ... <br>
  167. (* The page will jump to "Monitor Screen" automatically)
  168. </body>
  169. </html>
  170. HTML
  171. &MyCGI::outputHtml("UTF-8", $html);
  172. }
  173. # ##################
  174. # Utility
  175. sub XMLRPCCall {
  176. my ($url, $methodname, $param) = @_;
  177. my $xmlrpc = new XML::RPC($url);
  178. my $result = $xmlrpc->call($methodname, $param);
  179. return $result;
  180. }
  181. sub XMLRPCCall_array {
  182. my ($url, $methodname, $param) = @_;
  183. my $xmlrpc = new XML::RPC($url);
  184. my $result = $xmlrpc->call($methodname, @$param);
  185. return $result;
  186. }