show extra tags in the nodes page as well
[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_peers.php';
19 require_once 'plc_visibletags.php';
20 require_once 'linetabs.php';
21 require_once 'table.php';
22 require_once 'nifty.php';
23
24 ini_set("memory_limit","64M");
25
26 // -------------------- 
27 // recognized URL arguments
28 $peerscope=$_GET['peerscope'];
29 $pattern=$_GET['pattern'];
30 $site_id=intval($_GET['site_id']);
31 $slice_id=intval($_GET['slice_id']);
32 $person_id=intval($_GET['person_id']);
33
34 // --- decoration
35 $title="Nodes";
36 $tabs=array();
37 $tabs []= tab_nodes();
38 if (count (plc_my_site_ids()) == 1) {
39     $tabs []= tab_nodes_mysite();
40 } else {
41     $tabs []= tab_nodes_all_mysite();
42 }
43 $tabs []= tab_nodes_local();
44
45 // -------------------- 
46 $node_filter=array();
47
48 //////////////////
49 // performs sanity check and summarize the result in a single column
50 function node_status ($node) {
51
52   // do all this stuff on local nodes only
53   if ( $node['peer_id'] )
54     return "n/a";
55
56   $messages=array();
57   // check that the node has interfaces
58   if (count($node['interface_ids']) == 0) {
59     $messages [] = "No interface";
60   }
61   return plc_vertical_table($messages,'plc-warning');
62 }
63
64 // fetch nodes 
65 $node_fixed_columns=array('hostname','node_type','site_id','node_id','boot_state','run_level',
66                           'interface_ids','peer_id', 'slice_ids');
67 $visibletags = new VisibleTags ($api, 'node');
68 $visiblecolumns = $visibletags->column_names();
69 $node_columns=array_merge($node_fixed_columns,$visiblecolumns);
70
71 // server-side filtering - set pattern in $_GET for filtering on hostname
72 if ($pattern) {
73   $node_filter['hostname']=$pattern;
74   $title .= " matching " . $pattern;
75  } else {
76   $node_filter['hostname']="*";
77  }
78
79 // server-side selection on peerscope
80 $peerscope=new PeerScope($api,$_GET['peerscope']);
81 $node_filter=array_merge($node_filter,$peerscope->filter());
82 $title .= ' - ' . $peerscope->label();
83
84 if ($site_id) {
85   $sites=$api->GetSites(array($site_id));
86   $site=$sites[0];
87   $name=$site['name'];
88   $login_base=$site['login_base'];
89   $title .= t_site($site);
90   $tabs []= tab_site($site);
91   $node_filter['site_id']=array($site_id);
92 }
93
94 if ($slice_id) {
95   $slices=$api->GetSlices(array($slice_id),array('node_ids','name'));
96   $slice=$slices[0];
97   $title .= t_slice($slice);
98   $tabs []= tab_slice($slice);
99   $node_filter['node_id'] = $slice['node_ids'];
100  }
101
102 // person_id is set : this is mostly oriented towards people managing several sites
103 if ($person_id) {
104   // avoid doing a useless call to GetPersons if the person_id is already known though $plc,
105   // as this is mostly done for the 'all my sites nodes' link
106   if ($person_id == plc_my_person_id()) { 
107     $person=plc_my_person();
108     $site_ids = plc_my_site_ids();
109   } else {
110     // fetch the person's site_ids
111     $persons = $api->GetPersons(array('person_id'=>$person_id),array('person_id','email','site_ids'));
112     $person=$persons[0];
113     $site_ids=$person['site_ids'];
114   }
115   $title .= t_person($person);
116   $node_filter['site_id']=$site_ids;
117  }
118
119 // go
120 $nodes=$api->GetNodes($node_filter,$node_columns);
121
122 // build site_ids - interface_ids
123 $site_ids=array();
124 $interface_ids=array();
125 if ($nodes) foreach ($nodes as $node) {
126   $site_ids []= $node['site_id'];
127   $interface_ids = array_merge ($interface_ids,$node['interface_ids']);
128 }
129
130 // fetch related interfaces
131 $interface_columns=array('ip','node_id','interface_id');
132 $interface_filter=array('is_primary'=>TRUE,'interface_id'=>$interface_ids);
133 $interfaces=$api->GetInterfaces($interface_filter,$interface_columns);
134
135 $interface_hash=array();
136 foreach ($interfaces as $interface) $interface_hash[$interface['node_id']]=$interface;
137
138 // fetch related sites
139 $site_columns=array('site_id','login_base');
140 $site_filter=array('site_id'=>$site_ids);
141 $sites=$api->GetSites($site_filter,$site_columns);
142
143 $site_hash=array();
144 foreach ($sites as $site) $site_hash[$site['site_id']]=$site;
145
146 // --------------------
147 drupal_set_title($title);
148
149 plekit_linetabs($tabs);
150
151 if ( ! $nodes ) {
152   drupal_set_message ('No node found');
153   return;
154  }
155   
156 $nifty=new PlekitNifty ('','objects-list','big');
157 $nifty->start();
158 $headers = array (); $offset=0;
159 $notes=array();
160
161 // fixed columns
162 if (plc_is_admin()) { 
163   $short="I"; $long="node_id"; $type='int'; 
164         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
165   $offset=1; 
166  }
167 $short="P"; $long="Peer"; $type='string'; 
168         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
169 $short="D"; $long="toplevel domain name"; $type='string'; 
170         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
171 $headers["Site"]="string";
172 $headers["State"]="string";
173                 $notes []= "state* = node doesn't have an observed state, preferred state is displayed";
174 $headers["Hostname"]="string";
175 $headers["Type"]="string";
176 $short="IP"; $long="IP Address"; $type='sortIPAddress'; 
177         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
178 $short="SL"; $long="Number of slivers"; $type='int'; 
179         $headers[$short]=array('type'=>$type,'title'=>$long); $notes []= "$short = $long";
180
181 $headers=array_merge($headers,$visibletags->headers());
182 $notes=array_merge($notes,$visibletags->notes());
183 $headers["?"]="string";         $notes []= "? = extra status info";
184
185 # initial sort on hostnames
186 $table=new PlekitTable ("nodes",$headers,4+$offset);
187 $table->start();
188
189 $peers = new Peers ($api);
190 // write rows
191 foreach ($nodes as $node) {
192   $hostname=$node['hostname'];
193   $node_id=$node['node_id'];
194   $site_id=$node['site_id'];
195   $site=$site_hash[$site_id];
196   $login_base = $site['login_base'];
197   $ip=$interface_hash[$node['node_id']]['ip'];
198   $interface_id=$interface_hash[$node['node_id']]['interface_id'];
199   $peer_id=$node['peer_id'];
200   $node_type = $node['node_type'];
201   
202   $table->row_start();
203   if (plc_is_admin()) $table->cell(l_node_t($node_id,$node_id));
204   $peers->cell ($table,$peer_id);
205   $table->cell (topdomain($hostname));
206   $table->cell (l_site_t($site_id,$login_base));
207   if ($node['run_level']) {
208       $table->cell($node['run_level']);
209   } else {
210       $table->cell ($node['boot_state'] . '*');
211   }
212   $table->cell (l_node_t($node_id,$hostname));
213   $table->cell ($node_type);
214   $table->cell (l_interface_t($interface_id,$ip),array('only-if'=> !$peer_id));
215   $table->cell (count($node['slice_ids']));
216   foreach ($visiblecolumns as $tagname) $table->cell($node[$tagname]);
217   $table->cell (node_status($node));
218   $table->row_end();
219   
220 }
221
222 $table->end(array('notes'=>$notes));
223 $nifty->end();
224
225 //plekit_linetabs ($tabs,"bottom");
226
227 // Print footer
228 include 'plc_footer.php';
229
230 ?>