\n"; } return $hidden; } #------------------------------------------------------------------------------- # Gives an Uptime string, which we can understand more easily than 'boottime'. # Returns the time a host has been running. function uptime($host, $cluster) { global $clusters, $metrics; $clustertime=$clusters[$cluster][LOCALTIME]; $boottime=$metrics[$cluster][boottime][$host][VAL]; $uptimeS=$clustertime - $boottime; $uptimeD=intval($uptimeS/86400); $uptimeS=$uptimeD ? $uptimeS % ($uptimeD*86400) : $uptimeS; $uptimeH=intval($uptimeS/3600); $uptimeS=$uptimeH ? $uptimeS % ($uptimeH*3600) : $uptimeS; $uptimeM=intval($uptimeS/60); $s = ($uptimeD>1) ? "s" : ""; return "$uptimeD day$s, $uptimeH:$uptimeM"; } #------------------------------------------------------------------------------- # Try to determine a nodes location in the cluster. Attempts to find the # LOCATION attribute first. Requires the host attribute array from # $hosts[$cluster][$name], where $name is the hostname. # Returns [-1,-1,-1] if we could not determine location. # function findlocation($attrs) { $rack=$rank=$plane=-1; $loc=$attrs[LOCATION]; if ($loc) { sscanf($loc, "%d,%d,%d", $rack, $rank, $plane); #echo "Found LOCATION: $rack, $rank, $plane.
"; } if ($rack<0 or $rank<0) { # Try to parse the host name. Assumes a compute-- # naming scheme. $n=sscanf($attrs[NAME], "compute-%d-%d", $rack, $rank); $plane=0; } return array($rack,$rank,$plane); } #------------------------------------------------------------------------------- # # __sort_asc() and __sort_des() are helpers for # __sort() # function __sort_asc ($a, $b) { return ($a[VAL] > $b[VAL]); } function __sort_des ($a, $b) { return ($a[VAL] < $b[VAL]); } function __sort_name ($metric, $cluster) { global $metrics; ksort($metrics[$cluster][$metric]); } function __sort ($metric, $function, $cluster) { global $metrics; uasort($metrics[$cluster][$metric], $function); } #------------------------------------------------------------------------------- /* * ganglia_sum() ganglia_max() and work with ganglia_cluster_reduce() to sum * values for a particular metric */ function __sum ( $v, $i ) { $v += $i; return $v; } function __max ( $v, $i ) { if ($i > $v) { return $i; } return $v; } function __min ($v, $i ) { if (! isset($v) ) { $v = 99999999999999999999999; } if ($i < $v) { return $i; } return $v; } #------------------------------------------------------------------------------- # # Does a reduce function (either min, max, sum, etc) accross a # single cluster or all clusters. # # Takes a variable number of arguments: 1 - metric name, # 2 - reduction function, 3 - cluster name. # # Returns a scalar value for __sum, and a two-element array for # __min and __max: [reduction_value, index], where index is the specific # host that has the min or max value. # function __reduce () { global $clusters, $metrics; $metric = func_get_arg(0); $function = func_get_arg(1); $cluster2reduce = array(); if( func_num_args() == 3 ) { $arg = func_get_arg(2); array_push($cluster2reduce, $arg); } else { foreach($clusters as $k => $val) { array_push($cluster2reduce, $k); } } if ($function=="__min" or $function=="__max") { $do_index="yes"; } foreach($cluster2reduce as $name) { if(is_array($metrics[$name][$metric])) { foreach($metrics[$name][$metric] as $host => $v ) { if ($do_index) { $new_val = $function($reduction_val, $v[VAL]); if ($new_val != $reduction_val) { $index = $host; } $reduction_val = $new_val; } else { $reduction_val = $function($reduction_val, $v[VAL]); } } } } if ($do_index) { return array($reduction_val, $index); } else { return $reduction_val; } } #------------------------------------------------------------------------------- # # A useful function for giving the correct picture for a given # load. Scope is "node | cluster | grid". Value is 0 <= v <= 1. function load_image ($scope, $value) { if ($value>1.00) { $image = template("images/${scope}_overloaded.jpg"); } else if ($value>=0.75) { $image = template("images/${scope}_75-100.jpg"); } else if ($value >= 0.50) { $image = template("images/${scope}_50-74.jpg"); } else if ($value>=0.25) { $image = template("images/${scope}_25-49.jpg"); } else { $image = template("images/${scope}_0-24.jpg"); } return $image; } #------------------------------------------------------------------------------- # # Just a useful function to print the HTML for # the load/death of a cluster node function node_image ($cluster, $host) { global $hosts_down, $metrics; global $get_metric_string; global $template_name; $cpu_num = $metrics[$cluster]["cpu_num"][$host][VAL]; if(!$cpu_num || $cpu_num == 0) { $cpu_num = 1; } $load_one = $metrics[$cluster]["load_one"][$host][VAL]; $value = $load_one/ $cpu_num; # Check if the host is down if( $hosts_down[$cluster][$host] ) { $image = template("images/node_dead.jpg"); } else { $image = load_image("node", $value); } $cluster_url = rawurlencode($cluster); $host_url = rawurlencode($host); $str = ""; $str.= "\"$host\""; return $str; } ?>