backwards compatibility with 4.2
[nodeconfig.git] / PlanetLabConf / ntp.conf.php
1 <?php
2 //
3 // /etc/ntp.conf generator
4 //
5 // Marc Fiuczynski <mef@cs.princeton.edu>
6 // Copyright (C) 2006 The Trustees of Princeton University
7 // 
8 // $Id$
9 //
10
11 // Get admin API handle
12 require_once 'plc_api.php';
13 global $adm;
14
15 $config_directory = "/var/www/html/PlanetLabConf/ntp/";
16 $file_prefix = "ntp.conf.";
17 $default_name = "default";
18 $file_name = $config_directory . $file_prefix . $default_name;
19
20 // Look up the node
21
22 // backwards compatibility with the old 4.2 API
23 if ( ! method_exists ($adm,"GetInterfaces"))
24   $interfaces = $adm->GetNodeNetworks(array('ip' => $_SERVER['REMOTE_ADDR']));
25 else
26   $interfaces = $adm->GetInterfaces(array('ip' => $_SERVER['REMOTE_ADDR']));
27
28 if (!empty($interfaces)) {
29   $nodes = $adm->GetNodes(array($interfaces[0]['node_id']));
30   if (!empty($nodes)) {
31     $node = $nodes[0];
32   }
33 }
34
35 if (!isset($node)) {
36   readfile($file_name); 
37   exit();
38 }
39
40 $problem_models = array("Dell Precision 340", "Unknown");
41
42 $hostname= trim($node['hostname']);
43 $model= trim($node['model']);
44
45 /* pull LatLong data */
46 $sites = $adm->GetSites(array($node['site_id']));
47 if (!empty($sites)) {
48   $site_name= $sites[0]['name'];
49   $mylat= $sites[0]['latitude'];
50   $mylong= $sites[0]['longitude'];
51 } else {
52   $site_name= "Unknown";
53   $mylat= 0;
54   $mylong= 0;
55 }
56
57 /* typical NTP settings */
58
59 print( "# node $hostname site $site_name $mylat $mylong $model\n");
60 print( "driftfile /var/lib/ntp/ntp.drift\n" );
61 print( "statsdir /var/log/ntpstats/\n" );
62 if (is_numeric(array_search($model, $problem_models))) {
63   print( "tinker stepout 0\n" );
64 }
65
66 /* Look for config file */
67
68 $hostname_bits = explode('.', $hostname);
69 $chunk_counter = sizeof ($hostname_bits);
70 $compare_chunk = $hostname ;
71 $found_file = 0;
72
73 /* look for host specific overrides */
74 $file_name = $config_directory . "host/" . $file_prefix . $compare_chunk ;
75 if (is_file($file_name)) {
76   $chunk_counter = 0;
77   $found_file = 1;
78 }
79
80 /* look for domain specific overrides */
81 while ($chunk_counter > 0) {
82   $file_name = $config_directory . $file_prefix . $compare_chunk ;
83   if (is_file($file_name)) {
84     $chunk_counter = 0;
85     $found_file = 1;
86   }
87   else {
88     array_shift($hostname_bits);
89     $compare_chunk = implode('.',$hostname_bits);
90     $chunk_counter--;
91   }
92 }
93
94
95 if ($found_file and is_readable($file_name)) {
96   readfile($file_name);
97
98 else {
99   if (is_numeric(array_search($model,$problem_models))) {
100     $file_name = $config_directory . $file_prefix . "prob." . $default_name ;
101   }
102   else {
103     $file_name = $config_directory . $file_prefix . $default_name ;
104   }
105   readfile($file_name); 
106
107
108 ?>