Skip to content

Commit

Permalink
Merge pull request #252 from cddude229/microoptimize-xml-2
Browse files Browse the repository at this point in the history
Micro-optimize XML calls
  • Loading branch information
vvuksan committed Oct 2, 2014
2 parents 88cdaa9 + 52a02b3 commit c989e6d
Showing 1 changed file with 67 additions and 21 deletions.
88 changes: 67 additions & 21 deletions ganglia.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# information as we need to make the page.
#

error_reporting(E_ALL);

$gweb_root = dirname(__FILE__);

include_once($gweb_root . "/version.php");
Expand Down Expand Up @@ -77,14 +79,16 @@ function preamble($ganglia)
$version[$component] = $ganglia['VERSION'];
}

function debug_start_meta ($parser, $tagname, $attrs)
{
print "<br/>DEBUG: parser start meta [$tagname]\n";
start_meta($parser, $tagname, $attrs);
}

function start_meta ($parser, $tagname, $attrs)
{
global $metrics, $grid, $self, $debug;
static $sourcename, $metricname;

if ($debug) print "<br/>DEBUG: parser start meta [$tagname]\n";

switch ($tagname)
{
case "GANGLIA_XML":
Expand All @@ -93,7 +97,7 @@ function start_meta ($parser, $tagname, $attrs)

case "GRID":
case "CLUSTER":
if ($debug) print "<br/>DEBUG: parser start meta GRID|CLUSTER\n";
global $grid, $self;
# Our grid will be first.
if (!$sourcename) $self = $attrs['NAME'];

Expand All @@ -105,11 +109,13 @@ function start_meta ($parser, $tagname, $attrs)
break;

case "METRICS":
global $metrics;
$metricname = rawurlencode($attrs['NAME']);
$metrics[$sourcename][$metricname] = $attrs;
break;

case "HOSTS":
global $grid;
$grid[$sourcename]['HOSTS_UP'] = $attrs['UP'];
$grid[$sourcename]['HOSTS_DOWN'] = $attrs['DOWN'];
break;
Expand All @@ -119,28 +125,35 @@ function start_meta ($parser, $tagname, $attrs)
}
}

function debug_start_cluster ($parser, $tagname, $attrs)
{
print "<br/>DEBUG: parser start cluster [$tagname]\n";
start_cluster($parser, $tagname, $attrs);
}

function start_cluster ($parser, $tagname, $attrs)
{
global $metrics, $cluster, $self, $grid, $hosts_up, $hosts_down, $debug;
static $hostname;

if ($debug) print "<br/>DEBUG: parser start cluster [$tagname]\n";
switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;

case "GRID":
global $self, $grid;
$self = $attrs['NAME'];
$grid = $attrs;
break;

case "CLUSTER":
global $cluster;
$cluster = $attrs;
break;

case "HOST":
global $metrics, $cluster, $hosts_up, $hosts_down;
$hostname = $attrs['NAME'];

if (host_alive($attrs, $cluster))
Expand Down Expand Up @@ -176,6 +189,7 @@ function start_cluster ($parser, $tagname, $attrs)
break;

case "METRIC":
global $metrics;
$metricname = rawurlencode($attrs['NAME']);
$metrics[$hostname][$metricname] = $attrs;
break;
Expand All @@ -185,19 +199,23 @@ function start_cluster ($parser, $tagname, $attrs)
}
}

function debug_start_everything ($parser, $tagname, $attrs)
{
print "<br/>DEBUG: parser start everything [$tagname]\n";
start_everything($parser, $tagname, $attrs);
}

function start_everything ($parser, $tagname, $attrs)
{
global $index_array, $hosts, $metrics, $cluster, $self, $grid, $hosts_up, $hosts_down, $debug;
static $hostname, $cluster_name;

if ($debug) print "<br/>DEBUG: parser start everything [$tagname]\n";

switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;
case "GRID":
global $self, $grid;
$self = $attrs['NAME'];
$grid = $attrs;
break;
Expand All @@ -208,16 +226,18 @@ function start_everything ($parser, $tagname, $attrs)
break;

case "HOST":
global $index_array;
$hostname = $attrs['NAME'];
# For some reason this occasionally will end up marking live hosts not alive
# causing them to miss out from aggregate graphs
# if (host_alive($attrs, $cluster_name))
$index_array['cluster'][$hostname][] = $cluster_name;

case "METRIC":
global $index_array;
$metricname = rawurlencode($attrs['NAME']);
if ( $metricname != $hostname )
$index_array['metrics'][$metricname][] = $hostname;
if ( $metricname != $hostname )
$index_array['metrics'][$metricname][] = $hostname;
break;

default:
Expand All @@ -226,28 +246,35 @@ function start_everything ($parser, $tagname, $attrs)

}

function start_cluster_summary ($parser, $tagname, $attrs)
function debug_start_cluster_summary ($parser, $tagname, $attrs)
{
global $metrics, $cluster, $self, $grid;
start_cluster_summary($parser, $tagname, $attrs);
}

function start_cluster_summary ($parser, $tagname, $attrs)
{
switch ($tagname)
{
case "GANGLIA_XML":
preamble($attrs);
break;
case "GRID":
global $self, $grid;
$self = $attrs['NAME'];
$grid = $attrs;
case "CLUSTER":
global $cluster;
$cluster = $attrs;
break;

case "HOSTS":
global $cluster;
$cluster['HOSTS_UP'] = $attrs['UP'];
$cluster['HOSTS_DOWN'] = $attrs['DOWN'];
break;

case "METRICS":
global $metrics;
$metrics[$attrs['NAME']] = $attrs;
break;

Expand All @@ -257,9 +284,14 @@ function start_cluster_summary ($parser, $tagname, $attrs)
}


function debug_start_host ($parser, $tagname, $attrs)
{
start_host($parser, $tagname, $attrs);
}


function start_host ($parser, $tagname, $attrs)
{
global $metrics, $cluster, $hosts_up, $hosts_down, $self, $grid;
static $metricname;

switch ($tagname)
Expand All @@ -268,21 +300,25 @@ function start_host ($parser, $tagname, $attrs)
preamble($attrs);
break;
case "GRID":
global $self, $grid;
$self = $attrs['NAME'];
$grid = $attrs;
break;
case "CLUSTER":
global $cluster;
$cluster = $attrs;
break;

case "HOST":
global $cluster, $hosts_up, $hosts_down;
if (host_alive($attrs, $cluster))
$hosts_up = $attrs;
else
$hosts_down = $attrs;
break;

case "METRIC":
global $metrics;
$metricname = rawurlencode($attrs['NAME']);
$metrics[$metricname] = $attrs;
break;
Expand All @@ -291,6 +327,7 @@ function start_host ($parser, $tagname, $attrs)
break;

case "EXTRA_ELEMENT":
global $metrics;
if ( isset($attrs['NAME']) && isset($attrs['VAL']) && ($attrs['NAME'] == "GROUP")) {
if ( isset($metrics[$metricname]['GROUP']) ) {
$group_array = array_merge( (array)$attrs['VAL'], $metrics[$metricname]['GROUP'] );
Expand Down Expand Up @@ -340,7 +377,16 @@ function Gmetad ()
$ip = func_get_arg(0);
}

if ($debug) print "<br/>DEBUG: Creating parser\n";
if ($debug)
{
print "<br/>DEBUG: Creating parser\n";
$debug_prefix = "debug_";
}
else
{
$debug_prefix = "";
}

if ( $context == "compare_hosts" or $context == "views" or $context == "decompose_graph")
return TRUE;
$parser = xml_parser_create();
Expand All @@ -351,26 +397,26 @@ function Gmetad ()
case "control":
case "tree":
default:
xml_set_element_handler($parser, "start_meta", "end_all");
xml_set_element_handler($parser, $debug_prefix . "start_meta", "end_all");
$request = "/?filter=summary";
break;
case "physical":
case "cluster":
xml_set_element_handler($parser, "start_cluster", "end_all");
xml_set_element_handler($parser, $debug_prefix . "start_cluster", "end_all");
$request = "/$clustername";
break;
case "index_array":
case "views":
xml_set_element_handler($parser, "start_everything", "end_all");
xml_set_element_handler($parser, $debug_prefix . "start_everything", "end_all");
$request = "/";
break;
case "cluster-summary":
xml_set_element_handler($parser, "start_cluster_summary", "end_all");
xml_set_element_handler($parser, $debug_prefix . "start_cluster_summary", "end_all");
$request = "/$clustername?filter=summary";
break;
case "node":
case "host":
xml_set_element_handler($parser, "start_host", "end_all");
xml_set_element_handler($parser, $debug_prefix . "start_host", "end_all");
$request = "/$clustername/$hostname";
$strip_extra = false;
break;
Expand All @@ -387,7 +433,7 @@ function Gmetad ()
if ($port == 8649)
{
# We are connecting to a gmond. Non-interactive.
xml_set_element_handler($parser, "start_cluster", "end_all");
xml_set_element_handler($parser, $debug_prefix . "start_cluster", "end_all");
}
else
{
Expand Down

0 comments on commit c989e6d

Please sign in to comment.