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