Added support to look up statically NAT'd nodes by their public_ip_address InterfaceTag
[nodeconfig.git] / boot / upload-bmlog.php
1 <?php
2
3 // Thierry Parmentelat -- INRIA
4 // first draft for a revival of former (3.x?) alpina-logs in 5.0
5
6 // this needs be created with proper permissions at package install time
7 $logdir="/var/log/bm";
8
9 // limit: applies to uploads coming from an unrecognized IP
10 $limit_bytes=4*1024;
11
12 $default_hostname="unknown";
13
14 function mkdir_if_needed ($dirname) {
15   if (is_dir ($dirname))
16     return;
17   mkdir ($dirname) or die ("Cannot create dir " . $dirname);
18 }
19   
20 // Get admin API handle
21 require_once 'plc_api.php';
22 global $adm;
23
24 // find the node that these longs should belong to by looking for a node_id
25 // with an ip the same as the http requestor ip
26 $ip = $_SERVER['REMOTE_ADDR'];
27
28 $hostname=$default_hostname;
29 // locate hostname from DB based on this IP
30 $interfaces=$adm->GetInterfaces(array("ip"=>$ip));
31
32 if (empty($interfaces) ) {  // maybe its a node with a NAT'd IP addresses
33   // look up an interface tag using the public IP address value
34   $interfacetags=$adm->GetInterfaceTags(array("tagname"=>"public_ip_address","value"=>$ip));
35   if (! empty($interfacetags)) {
36     // using the interface_id now get the interface with the node_id information
37     $interface_id=$interfacetags[0]['interface_id'];
38     $interfaces=$adm->GetInterfaces($interface_id);
39     // fall thru to the conditional below ...
40   }
41  }
42
43 if (! empty($interfaces) ) {
44   $interface=$interfaces[0];
45   $node_id=$interface['node_id'];
46   $nodes=$adm->GetNodes($node_id,array("hostname"));
47   if (!empty($nodes)) {
48     $hostname=$nodes[0]['hostname'];
49   }
50  }
51
52 // store the actual data in /var/log/bm/raw/2008-11-31-20-02-onelab01.inria.fr-138.96.250.141.txt
53
54 $rawdir=$logdir . "/raw";
55 $date=strftime("%Y-%m-%d-%H-%M");
56 $log_name=$date . "-" . $hostname . "-" . $ip . ".txt";
57 $log_path=$rawdir . "/" . $log_name;
58 $month=strftime("%Y-%m");
59 $time=strftime("%d-%H-%M");
60
61 mkdir_if_needed ($rawdir);
62
63 ////////////////////////////////////////
64
65 $log=fopen($log_path,"w") or die ("Cannot open logfile "+$log_path);
66
67 $uploaded_name= $_FILES['log']['tmp_name'];
68 $uploaded_size=filesize($uploaded_name);
69
70 fprintf ($log, "BootManager log created on: %s-%s\n",$month,$time);
71 fprintf( $log, "From IP: %s\n",$ip);
72 fprintf( $log, "hostname: %s\n",$hostname);
73 fprintf ( $log, "uploaded file: %s (%d bytes)\n",$uploaded_name,$uploaded_size);
74 if ( ( strcmp($hostname,$default_hostname)==0) && ( $uploaded_size >= $limit_bytes) ) {
75   fprintf ( $log, "contents from an unrecognized IP address was truncated to %d bytes\n",$limit_bytes);
76   $truncated=TRUE;
77   $uploaded_size=$limit_bytes;
78  } else {
79   $truncated=FALSE;
80  }
81
82 fprintf( $log, "-----------------\n\n" );
83 $uploaded = fopen($uploaded_name,'r');
84 $contents = fread($uploaded, $uploaded_size);
85 fclose($uploaded);
86 fwrite($log,$contents);
87 if ($truncated)
88   fwrite ($log, " ..<" . "truncated" . ">..\n");
89 fclose($log);
90
91 ////////////////////////////////////////
92
93 // create symlinks for easy browsing
94
95 // /var/log/bm/per-month/2008-11/onelab1.inria.fr/31-20-02.bmlog
96 $linkdir=$logdir;
97 $linkdir=$linkdir . "/per-month";
98 mkdir_if_needed ($linkdir);
99 $linkdir=$linkdir . "/" . $month;
100 mkdir_if_needed ($linkdir);
101 $linkdir = $linkdir . "/" . $hostname;
102 mkdir_if_needed ($linkdir);
103 $link = $linkdir . "/" . $time ;
104 symlink ("../../../raw/".$log_name,$link);
105
106 # /var/log/bm/per-hostname/onelab1.inria.fr/2008-11-31-20-02.bmlog
107 $linkdir=$logdir;
108 $linkdir=$linkdir . "/per-hostname";
109 mkdir_if_needed ($linkdir);
110 $linkdir=$linkdir . "/" . $hostname;
111 mkdir_if_needed ($linkdir);
112 $link = $linkdir . "/" . $month . "-" . $time ;
113 symlink ("../../raw/".$log_name,$link);
114
115 # /var/log/bm/per-ip/138.96.250.141/2008-11-31-20-02.bmlog
116 $linkdir=$logdir;
117 $linkdir=$linkdir . "/per-ip";
118 mkdir_if_needed ($linkdir);
119 $linkdir=$linkdir . "/" . $ip;
120 mkdir_if_needed ($linkdir);
121 $link = $linkdir . "/" . $month . "-" . $time ;
122 symlink ("../../raw/".$log_name,$link);
123
124 ?>