a3121a7a8ae422476f7fdfb223d9a8da1bb5c490
[plewww.git] / planetlab / nodes / nodes.php
1 <?php
2
3 // $Id$
4
5 // Require login
6 require_once 'plc_login.php';
7
8 // Get session and API handles
9 require_once 'plc_session.php';
10 global $plc, $api;
11
12 // Print header
13 require_once 'plc_drupal.php';
14 include 'plc_header.php';
15
16 // Common functions
17 require_once 'plc_functions.php';
18 require_once 'plc_objects.php';
19 require_once 'plc_peers.php';
20 require_once 'plc_visibletags2.php';
21 require_once 'linetabs.php';
22 require_once 'table2.php';
23 require_once 'nifty.php';
24 require_once 'toggle.php';
25 require_once 'columns.php';
26
27 // keep css separate for now
28 drupal_set_html_head('
29 <link href="/planetlab/css/my_slice.css" rel="stylesheet" type="text/css" />
30 ');
31
32
33 ini_set("memory_limit","64M");
34
35 // -------------------- 
36 // recognized URL arguments
37 $peerscope=$_GET['peerscope'];
38 $pattern=$_GET['pattern'];
39 $site_id=intval($_GET['site_id']);
40 $slice_id=intval($_GET['slice_id']);
41 $person_id=intval($_GET['person_id']);
42
43 // --- decoration
44 $title="Nodes";
45 $tabs=array();
46 $tabs []= tab_nodes();
47 if (count (plc_my_site_ids()) == 1) {
48     $tabs []= tab_nodes_mysite();
49 } else {
50     $tabs []= tab_nodes_all_mysite();
51 }
52 $tabs []= tab_nodes_local();
53
54 // -------------------- 
55 $node_filter=array();
56
57 //////////////////
58 // performs sanity check and summarize the result in a single column
59 function node_status ($node) {
60
61   $messages=array();
62   if ($node['node_type'] != 'regular' && $node['node_type'] != 'reservable' ) 
63     $messages []= $node['node_type'];
64
65   // checks on local nodes only
66   if ( ( ! $node['peer_id']) ) {
67     // has it got interfaces 
68     if (count($node['interface_ids']) == 0) 
69       $messages []= "No interface";
70   }
71   return plc_vertical_table($messages,'plc-warning');
72 }
73
74
75 $first_time_configuration = false;
76
77 if (plc_is_admin()) 
78         $default_configuration = "ID:f|hostname:f|ST:f|AU:f|RES:f";
79 else
80         $default_configuration = "hostname:f|ST:f|AU:f|RES:f";
81
82 //$extra_default = "LCN|DN|R|L|OS|MS|SN";
83 $column_configuration = "";
84 $slice_column_configuration = "";
85 $show_configuration = "";
86 $show_columns_message = '1';
87
88
89 $PersonTags=$api->GetPersonTags (array('person_id'=>$plc->person['person_id']));
90 //print_r($PersonTags);
91 foreach ($PersonTags as $ptag) {
92         if ($ptag['tagname'] == 'columnconf')
93         {
94                 $column_configuration = $ptag['value'];
95                 $conf_tag_id = $ptag['person_tag_id'];
96         }
97         if ($ptag['tagname'] == 'showconf')
98         {
99                 $show_configuration = $ptag['value'];
100                 $show_tag_id = $ptag['person_tag_id'];
101         }
102 }
103
104 //print("column configuration = ".$column_configuration);
105
106 $nodesconf_exists = false;
107 if ($column_configuration == "")
108 {
109         $first_time_configuration = true;
110         $column_configuration = "nodes;default";
111         $nodesconf_exists = true;
112 }
113 else {
114         $slice_conf = explode(";",$column_configuration);
115         for ($i=0; $i<count($slice_conf); $i++ ) {
116                 if ($slice_conf[$i] == "nodes")
117                 {
118                         $i++;
119                         $slice_column_configuration = $slice_conf[$i];
120                         $nodesconf_exists = true;
121                         break;
122                 }
123                 else
124                 {
125                         $i++;
126                         $slice_column_configuration = $slice_conf[$i];
127                 }
128         }
129 }
130
131 if ($nodesconf_exists == false)
132         $column_configuration = $column_configuration.";nodes;default";
133 //panos: need to define an "empty" configuration here (for the moment A column
134 //will be added by default the first time
135
136
137 if ($slice_column_configuration == "" || $slice_column_configuration == "default")
138         $full_configuration = $default_configuration;
139         
140 else
141         $full_configuration = $default_configuration."|".$slice_column_configuration;
142
143 //print("full configuration = ".$full_configuration);
144
145 // fetch nodes 
146 $node_fixed_columns=array('node_type','site_id','boot_state','last_contact','interface_ids','peer_id', 'slice_ids');
147
148 $fix_columns = array();
149 if (plc_is_admin()) 
150 $fix_columns[]=array('tagname'=>'node_id', 'header'=>'ID', 'type'=>'string', 'title'=>'The ID the node');
151 $fix_columns[]=array('tagname'=>'hostname', 'header'=>'hostname', 'type'=>'string', 'title'=>'The name of the node');
152 $fix_columns[]=array('tagname'=>'peer_id', 'header'=>'AU', 'type'=>'string', 'title'=>'Authority');
153 $fix_columns[]=array('tagname'=>'run_level', 'header'=>'ST', 'type'=>'string', 'title'=>'Status');
154 $fix_columns[]=array('tagname'=>'node_type', 'header'=>'RES', 'type'=>'string', 'title'=>'Reservable');
155
156
157 $visibletags = new VisibleTags ($api, 'node');
158 $visibletags->columns();
159 $tag_columns = $visibletags->headers();
160
161 //columns that are not defined as extra myslice tags
162 $extra_columns = array();
163 //MyPLC columns
164 $extra_columns[]=array('tagname'=>'sitename', 'header'=>'SN', 'type'=>'string', 'title'=>'Site name', 'fetched'=>true, 'source'=>'myplc');
165 $extra_columns[]=array('tagname'=>'domain', 'header'=>'DN', 'type'=>'string', 'title'=>'Toplevel domain name', 'fetched'=>true, 'source'=>'myplc');
166 $extra_columns[]=array('tagname'=>'ipaddress', 'header'=>'IP', 'type'=>'string', 'title'=>'IP Address', 'fetched'=>true, 'source'=>'myplc');
167 $extra_columns[]=array('tagname'=>'fcdistro', 'header'=>'OS', 'type'=>'string', 'title'=>'Operating system', 'fetched'=>false, 'source'=>'myplc');
168 $extra_columns[]=array('tagname'=>'date_created', 'header'=>'DA', 'source'=>'myplc', 'type'=>'date', 'title'=>'Date added', 'fetched'=>false);
169 $extra_columns[]=array('tagname'=>'arch', 'header'=>'A', 'source'=>'myplc', 'type'=>'string', 'title'=>'Architecture', 'fetched'=>false);
170 if (plc_is_admin()) { 
171 $extra_columns[]=array('tagname'=>'deployment', 'header'=>'DL', 'source'=>'myplc', 'type'=>'string', 'title'=>'Deployment', 'fetched'=>false);
172 }
173
174 //CoMon Live data
175 $extra_columns[]=array('tagname'=>'bwlimit', 'header'=>'BW', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'Bandwidth limit', 'fetched'=>false);
176 $extra_columns[]=array('tagname'=>'numcores', 'header'=>'CC', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'Number of CPU Cores', 'fetched'=>false);
177 $extra_columns[]=array('tagname'=>'cpuspeed', 'header'=>'CR', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'CPU clock rate', 'fetched'=>false);
178 $extra_columns[]=array('tagname'=>'disksize', 'header'=>'DS', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'Disk size', 'fetched'=>false);
179 $extra_columns[]=array('tagname'=>'gbfree', 'header'=>'DF', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'Currently available disk space', 'fetched'=>false);
180 $extra_columns[]=array('tagname'=>'memsize', 'header'=>'MS', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'Memory size', 'fetched'=>false);
181 $extra_columns[]=array('tagname'=>'numslices', 'header'=>'SM', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'Number of slices in memory', 'fetched'=>false);
182 $extra_columns[]=array('tagname'=>'uptime', 'header'=>'UT', 'source'=>'comon', 'type'=>'sortAlphaNumericTop', 'title'=>'Continuous uptime until now', 'fetched'=>false);
183
184 //TopHat Live data
185 //$extra_columns[]=array('tagname'=>'hopcount', 'header'=>'HC', 'source'=>'tophat', 'type'=>'sortAlphaNumericTop', 'title'=>'Hop count from reference node', 'fetched'=>false);
186
187
188 $ConfigureColumns =new PlekitColumns($full_configuration, $fix_columns, $tag_columns, $extra_columns);
189
190 $visiblecolumns = $ConfigureColumns->node_tags();
191
192 $node_columns=array_merge($node_fixed_columns,$visiblecolumns);
193
194 //$visibletags = new VisibleTags ($api, 'node');
195 //$visiblecolumns = $visibletags->column_names();
196 //print("<p>OLD");
197 //print_r($visiblecolumns);
198 //$node_columns=array_merge($node_fixed_columns,$visiblecolumns);
199
200
201 // server-side filtering - set pattern in $_GET for filtering on hostname
202 if ($pattern) {
203   $node_filter['hostname']=$pattern;
204   $title .= " matching " . $pattern;
205  } else {
206   $node_filter['hostname']="*";
207  }
208
209 // server-side selection on peerscope
210 $peerscope=new PeerScope($api,$_GET['peerscope']);
211 $node_filter=array_merge($node_filter,$peerscope->filter());
212 $title .= ' - ' . $peerscope->label();
213
214 if ($site_id) {
215   $sites=$api->GetSites(array($site_id));
216   $site=$sites[0];
217   $name=$site['name'];
218   $login_base=$site['login_base'];
219   $title .= t_site($site);
220   $tabs []= tab_site($site);
221   $node_filter['site_id']=array($site_id);
222 }
223
224 if ($slice_id) {
225   $slices=$api->GetSlices(array($slice_id),array('node_ids','name'));
226   $slice=$slices[0];
227   $title .= t_slice($slice);
228   $tabs []= tab_slice($slice);
229   $node_filter['node_id'] = $slice['node_ids'];
230  }
231
232 // person_id is set : this is mostly oriented towards people managing several sites
233 if ($person_id) {
234   // avoid doing a useless call to GetPersons if the person_id is already known though $plc,
235   // as this is mostly done for the 'all my sites nodes' link
236   if ($person_id == plc_my_person_id()) { 
237     $person=plc_my_person();
238     $site_ids = plc_my_site_ids();
239   } else {
240     // fetch the person's site_ids
241     $persons = $api->GetPersons(array('person_id'=>$person_id),array('person_id','email','site_ids'));
242     $person=$persons[0];
243     $site_ids=$person['site_ids'];
244   }
245   $title .= t_person($person);
246   $node_filter['site_id']=$site_ids;
247  }
248
249 // go
250 //print("getting nodes ".$node_columns);
251 //print_r($node_columns);
252 $nodes=$api->GetNodes($node_filter,$node_columns);
253
254 //print("<p> GOT NODES </p>");
255 //print_r($nodes);
256
257 $ConfigureColumns->fetch_live_data($nodes);
258
259 $show_conf = explode(";",$show_configuration);
260 foreach ($show_conf as $ss) {
261         if ($ss =="columns")
262                 $show_columns_message = '0';
263 }
264
265
266
267 // build site_ids - interface_ids
268 $site_ids=array();
269 $interface_ids=array();
270 if ($nodes) foreach ($nodes as $node) {
271   $site_ids []= $node['site_id'];
272   $interface_ids = array_merge ($interface_ids,$node['interface_ids']);
273 }
274
275 // fetch related interfaces
276 $interface_columns=array('ip','node_id','interface_id');
277 $interface_filter=array('is_primary'=>TRUE,'interface_id'=>$interface_ids);
278 $interfaces=$api->GetInterfaces($interface_filter,$interface_columns);
279
280 $interface_hash=array();
281 foreach ($interfaces as $interface) $interface_hash[$interface['node_id']]=$interface;
282
283 // fetch related sites
284 $site_columns=array('site_id','login_base');
285 $site_filter=array('site_id'=>$site_ids);
286 $sites=$api->GetSites($site_filter,$site_columns);
287
288 $site_hash=array();
289 foreach ($sites as $site) $site_hash[$site['site_id']]=$site;
290
291 // --------------------
292 drupal_set_title($title);
293
294 plekit_linetabs($tabs);
295
296 if ( ! $nodes ) {
297   drupal_set_message ('No node found');
298   return;
299  }
300   
301 $nifty=new PlekitNifty ('','objects-list','big');
302 $nifty->start();
303 $headers = array (); $offset=0;
304 $notes=array();
305 $notes [] = "For information about the different columns please see the <b>node table layout</b> tab above or <b>mouse over</b> the column headers";
306
307
308 /*
309 // fixed columns
310 if (plc_is_admin()) { 
311   $short="I"; $long="node_id"; $type='int'; 
312         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
313   $offset=1; 
314  }
315 $short="P"; $long="Peer"; $type='string'; 
316         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
317 $short="D"; $long="toplevel domain name"; $type='string'; 
318         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
319 $headers["Site"]="string";
320 $headers["Hostname"]="string";
321 $short="IP"; $long="IP Address"; $type='sortIPAddress'; 
322         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
323 $short="ST"; $long=Node::status_footnote(); $type='string'; 
324         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
325 $short="SL"; $long="Number of slivers"; $type='int'; 
326         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
327
328 $headers=array_merge($headers,$visibletags->headers());
329 $notes=array_merge($notes,$visibletags->notes());
330 $short="?"; $long="extra status info"; $type='string'; 
331         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
332 */
333
334 $info_header = array();
335 $short="?"; $long="extra status info"; $type='string'; 
336 $info_header[$short]=array('type'=>$type,'title'=>$long, 'label'=>'?', 'header'=>'?', 'visible'=>true); 
337 //$notes []= "$short = $long";
338 //$info_header["?"] = "none";
339 $headers = array_merge($ConfigureColumns->get_headers(),$info_header);
340
341 if ($first_time_configuration)
342 $column_conf_visible = '1';
343 else
344 $column_conf_visible = '0';
345
346 $toggle_nodes=new PlekitToggle('nodes-column-configuration',
347                                "Node table layout",
348                                array('visible'=>$column_conf_visible, 'info_div'=>'note_columns_div'));
349 $toggle_nodes->start();
350 print("<div id='debug'></div>");
351 print("<input type='hidden' id='slice_id' value='nodes' />");
352 print("<input type='hidden' id='person_id' value='".$plc->person['person_id']."' />");
353 print("<input type='hidden' id='conf_tag_id' value='".$conf_tag_id."' />");
354 print("<input type='hidden' id='show_tag_id' value='".$show_tag_id."' />");
355 print("<input type='hidden' id='show_configuration' value='".$show_configuration."' />");
356 print("<input type='hidden' id='column_configuration' value='".$slice_column_configuration."' />");
357 print("<br><input type='hidden' size=80 id='full_column_configuration' value='".$column_configuration."' />");
358 print("<input type='hidden' id='defaultConf' value='".$default_configuration."'></input>");
359
360 if ($show_columns_message == '0')
361 $note_display = "display:none;";
362 else
363 $note_display = "";
364
365
366 print <<<EOF
367 <div id='note_columns_div' style="align:center; background-color:#CAE8EA; padding:4px; width:800px; $note_display">
368 <table align=center><tr><td valign=top>
369 This tab allows you to customize the columns in the node tables, below. Information on the nodes comes from a variety of monitoring sources. If you, as either a user or a provider of monitoring data, would like to see additional columns made available, please send us your request in mail to <a href="mailto:support@myslice.info">support@myslice.info</a>. You can find more information about the MySlice project at <a href="http://trac.myslice.info">http://trac.myslice.info</a>.
370 </td><td valign=top><span onClick=closeMessage('columns')><img class='reset' src="/planetlab/icons/clear.png" alt="hide message permanently"></span>
371 </td></tr></table>
372 </div>
373 EOF;
374
375 $ConfigureColumns->configuration_panel_html(true);
376 $ConfigureColumns->javascript_init();
377 $toggle_nodes->end();
378
379 $table_options = array('notes'=>$notes,
380                        'search_width'=>15,
381                        'pagesize'=>20,
382                         'configurable'=>true);
383
384 # initial sort on hostnames
385 $table=new PlekitTable ("nodes",$headers,3+$offset, $table_options);
386 $table->start();
387
388 $peers = new Peers ($api);
389 // write rows
390 foreach ($nodes as $node) {
391   //$node_obj = new Node ($node);
392   $hostname=$node['hostname'];
393   $node_id=$node['node_id'];
394   $site_id=$node['site_id'];
395   $site=$site_hash[$site_id];
396   $login_base = $site['login_base'];
397   $ip=$interface_hash[$node['node_id']]['ip'];
398   $interface_id=$interface_hash[$node['node_id']]['interface_id'];
399   $peer_id=$node['peer_id'];
400   
401   $table->row_start();
402   $table->cell($node['node_id'], array('display'=>'none'));
403   if (plc_is_admin()) $table->cell(l_node_t($node_id,$node_id));
404   $table->cell (l_node_t($node_id,$hostname));
405   $peers->cell ($table,$peer_id);
406
407   //prefetch some columns
408   $node['domain'] = topdomain($hostname);
409   $node['sitename'] = l_site_t($site_id,$login_base);
410   if ($interface_id)
411         $node['ipaddress'] = l_interface_t($interface_id,$ip);
412   else
413         $node['ipaddress'] = "n/a";
414
415   list($label,$class) = Node::status_label_class_($node);
416   $table->cell ($label,array('class'=>$class));
417   $table->cell( ($node['node_type']=='reservable')?reservable_mark():"" );
418   //$table->cell (count($node['slice_ids']));
419   //foreach ($visiblecolumns as $tagname) $table->cell($node[$tagname]);
420   $ConfigureColumns->cells($table, $node);
421   $table->cell (node_status($node));
422   $table->row_end();
423   
424 }
425
426 $table->end();
427 $nifty->end();
428
429 //plekit_linetabs ($tabs,"bottom");
430
431 // Print footer
432 include 'plc_footer.php';
433
434 ?>
435