1<?php
2$tpl = new Dwoo_Template_File( template("meta_view.tpl") );
3$data = new Dwoo_Data();
4
5discover_filters();
6if ( !empty($filter_defs) ) {
7   $data->assign("filters", $filter_defs);
8   $data->assign("choose_filter", $choose_filter);
9 }
10
11$source_names = array_keys($grid);
12
13# Build a list of cluster names and randomly pick a smaller subset to
14# display for control room mode.  This allows a dedicated host to
15# eventually cycle through all the graphs w/o scrolling the mouse.  A bunch
16# of these stations could monitor a large grid.
17#
18# For the standard meta view still display all the hosts.
19
20if ( $context == "control" ) {
21       srand((double)microtime()*1000000);
22       shuffle($source_names);
23       $subset = array_slice($source_names, 0, abs($controlroom));
24       $source_names = $subset;
25}
26
27foreach( $source_names as $c)
28   {
29      $cpucount = $metrics[$c]["cpu_num"]['SUM'];
30      if (!$cpucount) $cpucount=1;
31      $load_one = $metrics[$c]["load_one"]['SUM'];
32      $value = (double) $load_one / $cpucount;
33      $sorted_sources[$c] = $value;
34      $values[$c] = $value;
35      isset($total_load) or $total_load = 0;
36      $total_load += $value;
37   }
38
39if ($sort == "descending") {
40      $sorted_sources[$self] = 999999999;
41      arsort($sorted_sources);
42} else if ($sort == "by name") { # SORT HACK to keep $self first; see below:
43      $sorted_sources["AAAAA.$self"] = $sorted_sources[$self];
44      unset($sorted_sources[$self]);
45      ksort($sorted_sources);
46} else if ($sort == "by hosts up") {
47      foreach ($sorted_sources as $source => $val) {
48            $sorted_sources[$source] = intval($grid[$source]['HOSTS_UP']);
49      }
50      $sorted_sources[$self] = 999999999;
51      arsort($sorted_sources);
52} else if ($sort == "by hosts down") {
53      foreach ($sorted_sources as $source => $val) {
54            $sorted_sources[$source] = intval($grid[$source]['HOSTS_DOWN']);
55      }
56      $sorted_sources[$self] = 999999999;
57      arsort($sorted_sources);
58} else {
59      $sorted_sources[$self] = -1;
60      asort($sorted_sources);
61}
62
63$sources = array();
64
65# Display the sources. The first is ourself, the rest are our children.
66foreach ( $sorted_sources as $source => $val )
67   {
68      # XXX: SORT HACK to keep $self first; see above
69      if ($source == "AAAAA.$self") {
70            $source = $self;
71      }
72
73      # Make sure the source is permitted by the filters, if any
74      if(!filter_permit($source))
75         continue;
76
77      $m = $metrics[$source];
78      $sourceurl = rawurlencode($source);
79      if (isset($grid[$source]['GRID']) and $grid[$source]['GRID'])
80         {
81            $localtime = $grid[$source]['LOCALTIME'];
82            # Is this the our own grid?
83            if ($source==$self)
84               {
85                  # Negative control room values means dont display grid summary.
86                  if ($controlroom < 0) continue;
87                  $num_sources = count($sorted_sources) - 1;
88                  $name = "$self ${conf['meta_designator']} ($num_sources sources)";
89                  $graph_url = "me=$sourceurl&amp;$get_metric_string";
90                  $url = "./?$get_metric_string";
91               }
92            else
93               {
94                  # Set grid context.
95                  $name = "$source ${conf['meta_designator']}";
96                  $graph_url = "G=$sourceurl&amp;$get_metric_string&amp;st=$localtime";
97                  $authority = $grid[$source]['AUTHORITY'];
98                  $url = "$authority?gw=fwd&amp;gs=$gridstack_url&amp;$get_metric_string";
99               }
100            $alt_url = "<a href=\"./?t=yes&amp;$get_metric_string\">(tree view)</a>";
101            $class = "grid";
102         }
103      else
104         {
105            # Set cluster context.
106            $name = $source;
107            $localtime = $grid[$source]['LOCALTIME'];
108            $graph_url = "c=$sourceurl&amp;$get_metric_string&amp;st=$localtime";
109            $url = "./?c=$sourceurl&amp;$get_metric_string";
110            $alt_url = "<a href=\"./?p=2&amp;$graph_url\">(physical view)</a>";
111            $class = "cluster";
112         }
113
114      $cpu_num = $m["cpu_num"]['SUM'] ? $m["cpu_num"]['SUM'] : 1;
115      $cluster_load15 = sprintf("%.0f", ((double) $m["load_fifteen"]['SUM'] / $cpu_num) * 100);
116      $cluster_load5 = sprintf("%.0f", ((double) $m["load_five"]['SUM'] / $cpu_num) * 100);
117      $cluster_load1 = sprintf("%.0f", ((double) $m["load_one"]['SUM'] / $cpu_num) * 100);
118      $cluster_load = "$cluster_load15%, $cluster_load5%, $cluster_load1%";
119
120      $clusname = $source == $self ? '' : $source;
121      $avg_cpu_num = find_avg($clusname, "", "cpu_num");
122      if ($avg_cpu_num == 0) $avg_cpu_num = 1;
123      $cluster_util = sprintf("%.0f", ((double) find_avg($clusname, "", "load_one") / $avg_cpu_num ) * 100);
124
125      $sources[$source]["name"] = $name;
126      $sources[$source]["cpu_num"] = $m["cpu_num"]['SUM'];
127      $sources[$source]["url"] = $url;
128      $sources[$source]["class"] = $class;
129
130      # Initialize some variables for the $sources array to be past to the template since private sources
131      # may not have these defined
132      $sources[$source]["alt_view"] = "";
133      $sources[$source]["cluster_load"] = "";
134      $sources[$source]["cluster_util"] = "";
135      $sources[$source]["num_dead_nodes"] = "";
136      $sources[$source]["range"] = "";
137      $sources[$source]["graph_url"] = "";
138      $sources[$source]["base64img"] = "";
139
140      if ($localtime)
141         $sources[$source]["localtime"] = "<font size=\"-1\">Localtime:</font><br>&nbsp;&nbsp;"
142            . date("Y-m-d H:i", $localtime);
143
144      # I dont like this either, but we need to have private clusters because some
145      # users are skittish about publishing the load info.
146      if(checkAccess($source, GangliaAcl::VIEW, $conf))
147         {
148            $sources[$source]["alt_view"] = "<FONT SIZE=\"-2\">$alt_url</FONT>";
149            $sources[$source]["public"] = 1;
150            if ($cluster_load)
151               $sources[$source]["cluster_load"] = "<font size=\"-1\">Current Load Avg (15, 5, 1m):</font>"
152                  ."<br>&nbsp;&nbsp;<b>$cluster_load</b>";
153            if (isset($cluster_util))
154               $sources[$source]["cluster_util"] = "<font size=\"-1\">Avg Utilization (last $range):</font>"
155                  ."<br>&nbsp;&nbsp;<b>$cluster_util%</b>";
156            $sources[$source]["num_nodes"] = $grid[$source]["HOSTS_UP"];
157            $sources[$source]["num_dead_nodes"] = $grid[$source]["HOSTS_DOWN"];
158            $sources[$source]["range"] = $range;
159            $sources[$source]["graph_url"] = $graph_url;
160	    if(isset($base64img)) {
161                $sources[$source]["base64img"] = $base64img;
162	    }
163            if ( $source == $self ) {
164               $sources[$source]["self_summary_graphs"] = 1;
165            } else {
166               $sources[$source]["summary_graphs"] = 1;
167            }
168         }
169      else
170         {
171            $sources[$source]["private"] = 1;
172            $sources[$source]["num_nodes"] = $grid[$source]["HOSTS_UP"] + $grid[$source]["HOSTS_DOWN"];
173         }
174   }
175
176$data->assign("sources", $sources);
177$snap_rows = array();
178
179# Show load images.
180if ($conf['show_meta_snapshot']=="yes") {
181   $data->assign("show_snapshot", 1);
182   $data->assign("self", "$self ${conf['meta_designator']}");
183
184   foreach ($sorted_sources as $c=>$value) {
185      if ($c==$self) continue;
186      if ($c=="AAAAA.$self") continue;  # SORT HACK; see above
187      if (! checkAccess($c, GangliaAcl::VIEW, $conf)) {
188         $Private[$c] = template("images/cluster_private.jpg");
189         continue;
190      }
191      $names[]=$c;
192
193      if (isset($grid[$c]['GRID']) and $grid[$c]['GRID'])
194         $image = load_image("grid", $values[$c]);
195      else
196         $image = load_image("cluster", $values[$c]);
197      $Images[]=$image;
198   }
199   # Add private cluster pictures to the end.
200   if (isset($Private) and is_array($Private)) {
201      foreach ($Private as $c=>$image) {
202         $names[]=$c;
203         $Images[]=$image;
204      }
205   }
206
207   # All this fancyness is to get the Cluster names
208   # above the image. Not easy with template blocks.
209   $cols=5;
210   $i = 0;
211   $count=count($names);
212   while ($i < $count)
213      {
214         $snapnames = "";
215         $snapimgs = "";
216         foreach(range(0, $cols-1) as $j)
217            {
218               $k = $i + $j;
219               if ($k >= $count) break;
220               $n = $names[$k];
221               $snapnames .= "<td valign=bottom align=center><b>$n</b></td>\n";
222               $snapimgs .= "<td valign=top align=center>";
223               if (isset($grid[$n]['GRID']) and $grid[$n]['GRID'])
224                  $snapimgs .= "<a href=\"" . $grid[$n]['AUTHORITY'] ."?gw=fwd&amp;gs=$gridstack_url\">";
225               else
226                  {
227                     $nameurl = rawurlencode($n);
228                     $snapimgs .= "<a href=\"./?c=$nameurl&amp;$get_metric_string\">";
229                  }
230               $snapimgs .= "<img src=\"$Images[$k]\" alt=\"$names[$k]\" border=0 align=top></a></td>\n";
231            }
232         $snap_rows[$i]["names"] = $snapnames;
233         $snap_rows[$i]["images"] = $snapimgs;
234         $i += $cols;
235      }
236   $data->assign("snap_rows", $snap_rows);
237}
238
239$dwoo->output($tpl, $data);
240?>
241