<?php

require_once "WEB-INF/php/inc.php";

if (! admin_init()) {
  return;
}

if (! require_professional()) {
  return;
}

enable_graph_controls();

echo "<h1>";
echo gettext('Health');
echo print_help("Health");
echo "</h1>\n";

echo "<div id='health-tabs'>\n";
echo "<ul>";
echo "<li><a href='#health'>" . gettext('Health') . "</a></li>\n";
echo "<li><a href='#health-graph'>" . gettext('Graphs') . "</a></li>\n";
echo "<li><a href='#heartbeat'>" . gettext('Heartbeat') . "</a></li>\n";
echo "<li><a href='#health-logs'>" . gettext('Health Logs') . "</a></li>\n";
echo "</ul>";

echo "<div id='health'>\n";
display_health_tab();
echo "</div>\n";

echo "<div id='health-graph'>\n";
display_health_graph_tab();
echo "</div>\n";

echo "<div id='heartbeat'>\n";
display_heartbeat();
echo "</div>\n";

echo "<div id='health-logs'>\n";
display_health_logs();
echo "</div>\n";

echo "</div>\n";

function display_health_tab()
{
  global $g_server;

  foreach ($g_server->SelfServer->Cluster->Servers as $s) {
    display_health_status($s);
  }
}

function display_health_graph_tab()
{
  global $g_server;

  foreach ($g_server->SelfServer->Cluster->Servers as $s) {
    display_health_status_graph($s);
  }
}

function display_health_status_graph($s)
{
  $si = sprintf("%02d", $s->ClusterIndex);
  $server_id = $s->Name;
  if ($server_id == "")
  	$server_id = "default";

  $mbean_server = new MBeanServer($server_id);

  $label = "$si - $server_id";
	
  display_health_graph($mbean_server, $s, $si, $label);
}

function display_health_graph($mbean_server, $s, $si, $label)
{
  $params = new GraphParams("health_${si}", 600, 200);
  $params->title = gettext('Server') . "${label}";

  $params->alt
    = gettext("A line graph representing the status of each health check performed on server $label over the last 24 hours.");
  $params->alt = preg_replace('/\$label/', $label, $params->alt);

  $params->labels_mbean = "resin:type=HealthSystem";
  $params->mbean_server = $mbean_server;

  stat_graph_regexp($params, "/^${si}\|Resin\|Health/");
}

function display_heartbeat()
{
  global $g_server;

  $resin = $g_server->Cluster->Resin;
  $clusters = $resin->Clusters;

  echo "<table cellspacing='5'>";

  echo "<tr>";

  foreach ($clusters as $c) {
    echo "<th>" . $c->Name . "</th>\n";
  }

  echo "</tr>";

  for ($i = 0; $i < 64; $i++) {
    if (! has_server($clusters, $i))
      continue;

    echo "<tr>";

    foreach ($clusters as $c) {
      echo "<td>";
    
      $triad_a = $c->Servers[0];
      if ($triad_a) {
        $mbean_server_a = new MBeanServer($triad_a->Name);
      }
    
      $triad_b = $c->Servers[1];
      if ($triad_b) {
        $mbean_server_b = new MBeanServer($triad_b->Name);
      }

      $triad_c = $c->Servers[2];
      if ($triad_c) {    
        $mbean_server_c = new MBeanServer($triad_c->Name);
      }

      $s = $c->Servers[$i];

      if ($s) {
        $style = "border-width : 1px";

        $s_mbean_server = new MBeanServer($s->Name);
        $s_server = $s_mbean_server->lookup("resin:type=Server");

        if (! $s_server) {
          $style .= "; border-color : #cc0000";
        }
        else if ($s->Name === $g_server->SelfServer->Name) {
          $style .= "; border-color : #e5a122";
        }
      
        if ($s->isTriadServer()) {
          $style .= "; border-style : solid";
        }
        else {
          $style .= "; border-style : dashed";
        }

        echo "<table style='$style' width='100%'>\n";

        echo "<tr>";
        echo "<td>";
      
        $is_health = $s_server;
      
        print_check_or_x($is_health);
      
        echo "</td>";
        echo "<td width='5em'>";
  
      $name = $s->Name;
      if (! $name)
        $name = "default";
      printf("%02d - %s", $s->ClusterIndex, $name);

      echo "</td>\n";
      echo "<td>";

      do_triad($s, $triad_a, $mbean_server_a);

      echo "</td></tr>\n";
      echo "<tr><td>&nbsp;</td><td>";

      printf("%s:%d", $s->Address, $s->Port);

      echo "</td>\n<td>";

      do_triad($s, $triad_b, $mbean_server_b);

      echo "</td></tr>\n";

      //
      // third row - health and triad C
      // 

      $s_mbean_server = new MBeanServer($s->Name);
      $s_server = $s_mbean_server->lookup("resin:type=Server");
      $s_health = $s_mbean_server->lookup("resin:type=HealthCheck,name=Resin");
      
      $is_health = $s_server;
      
      echo "<tr><td></td><td nowrap='nowrap'>";

      if (! $s_health) {
        echo "n/a";
      }
      else {
      	print_health($s_health->Status, "Health " . $s_health->Status);
      }
      
      echo "</td><td>";
      do_triad($s, $triad_c, $mbean_server_c);
      echo "</td>";
      
      echo "</tr>\n";
      echo "</table>\n";
    }

    echo "</td>";
  }

  echo "</tr>";
  }

  echo "</table>";
}

function has_server($clusters, $i)
{
  return true;
}  

function do_triad($s, $triad, $triad_mbean_server)
{
  if (! $triad) {
    echo "&nbsp;&nbsp;\n";
    return;
  }

  echo "<span style='font-size=1.2em'>";
  
  $s_mbean_server = new MBeanServer($s->Name);
  $s_server = $s_mbean_server->lookup("resin:type=Server");
  $s_triad_server = $s_server->SelfServer->Cluster->Servers[$triad->ClusterIndex];

  $triad_server = $triad_mbean_server->lookup("resin:type=Server");
  $triad_cluster = $triad_server->SelfServer->Cluster;
  $triad_cluster_server = $triad_cluster->Servers[$s->ClusterIndex];

  if (! $s_triad_server) {
    echo "<span>&ndash;</span>";
  }
  else if ($s_triad_server->isHeartbeatActive()) {
    echo "<span style='color:#00c000'>&larr;</span>";
  }
  else {
    echo "<span style='color:#c00000'>&#x2717;</span>";
  }

  if (! $triad_cluster_server) {
    echo "<span>&ndash;</span>";
  }
  else if ($triad_cluster_server->isHeartbeatActive()) {
    echo "<span style='color:#00c000'>&rarr;</span>";
  }
  else {
    echo "<span style='color:#c00000'>&#x2717;</span>";
  }
  
  echo "</span>\n";
}

function display_health_logs()
{
  global $mbean_server;
  global $g_mbean_server;

  if ($g_mbean_server) {
    $mbean = $g_mbean_server->lookup("resin:type=LogService");
  }

  //
  // recent messages
  //

  if ($mbean) {
    $now = time();

    $messages = $mbean->findMessagesByName("com.caucho.env.health",
                                           "warning",
                                           ($now - 24 * 3600) * 1000,
                                           $now * 1000);
    echo "<table class='data'>\n";
    echo "<thead>\n";
    echo "<tr>\n";
    echo "  <th scope='col' class='date'>" . gettext('Date') . "</th>\n";
    echo "  <th scope='col' class='level'>" . gettext('Level') . "</th>\n";
    echo "  <th scope='col' class='message'>" . gettext('Message') . "</th>\n";
    echo "</tr>\n";
    echo "</thead>\n";
                                           
    if (! empty($messages)) {
    	
      $messages = array_reverse($messages);

      echo "<tbody class='scroll'>\n";
      foreach ($messages as $message) {
        echo "<tr class='{$message->level}'>";
        echo "  <td class='date'>";
        echo strftime("%Y-%m-%d&nbsp;%H:%M:%S", $message->timestamp / 1000);
        echo " </td>";
        echo "  <td class='level'>{$message->level}</td>";
        echo "  <td class='message'>" . htmlspecialchars(wordwrap($message->message, 90));
        echo "  </td>";
        echo "</tr>";
      }

      echo "</tbody>\n";
    } else {
      echo "<tbody>\n";
      echo "<tr><td colspan='3'>";
      echo gettext('No recent health logs');
      echo "</td></tr>\n";
      echo "</tbody>\n";
    }
    
    echo "</table>\n";
  }
}

display_footer($g_page, javascript_create_tab("health-tabs"));

?>
