attempt to solve PHP Notice: Undefined variable: truncated in /var/www/html/boot...
[nodeconfig.git] / boot / upload-bmlog.php
index 4584111..6c7934b 100755 (executable)
@@ -1,11 +1,15 @@
 <?php
 
-  // Thierry Parmentelat -- INRIA
-  // first draft for a revival of former (3.x?) alpina-logs in 5.0
+// Thierry Parmentelat -- INRIA
+// first draft for a revival of former (3.x?) alpina-logs in 5.0
 
-  // this needs be created with proper permissions at package install time
+// this needs be created with proper permissions at package install time
 $logdir="/var/log/bm";
-$limit_bytes=16*1024;
+
+// limit: applies to uploads coming from an unrecognized IP
+$limit_bytes=4*1024;
+
+$default_hostname="unknown";
 
 function mkdir_if_needed ($dirname) {
   if (is_dir ($dirname))
@@ -21,7 +25,7 @@ global $adm;
 // with an ip the same as the http requestor ip
 $ip = $_SERVER['REMOTE_ADDR'];
 
-$hostname="unknown";
+$hostname=$default_hostname;
 // locate hostname from DB based on this IP
 $interfaces=$adm->GetInterfaces(array("ip"=>$ip));
 if (! empty($interfaces) ) {
@@ -37,7 +41,8 @@ if (! empty($interfaces) ) {
 
 $rawdir=$logdir . "/raw";
 $date=strftime("%Y-%m-%d-%H-%M");
-$log_name=$rawdir . "/" . $date . "-" . $hostname . "-" . $ip . ".txt";
+$log_name=$date . "-" . $hostname . "-" . $ip . ".txt";
+$log_path=$rawdir . "/" . $log_name;
 $month=strftime("%Y-%m");
 $time=strftime("%d-%H-%M");
 
@@ -45,7 +50,7 @@ mkdir_if_needed ($rawdir);
 
 ////////////////////////////////////////
 
-$log=fopen($log_name,"w") or die ("Cannot open logfile "+$log_name);
+$log=fopen($log_path,"w") or die ("Cannot open logfile "+$log_path);
 
 $uploaded_name= $_FILES['log']['tmp_name'];
 $uploaded_size=filesize($uploaded_name);
@@ -54,8 +59,8 @@ fprintf ($log, "BootManager log created on: %s-%s\n",$month,$time);
 fprintf( $log, "From IP: %s\n",$ip);
 fprintf( $log, "hostname: %s\n",$hostname);
 fprintf ( $log, "uploaded file: %s (%d bytes)\n",$uploaded_name,$uploaded_size);
-if ( $uploaded_size >= $limit_bytes) {
-  fprintf ( $log, "contents truncated to %d bytes\n",$limit_bytes);
+if ( ( strcmp($hostname,$default_hostname)==0) && ( $uploaded_size >= $limit_bytes) ) {
+  fprintf ( $log, "contents from an unrecognized IP address was truncated to %d bytes\n",$limit_bytes);
   $truncated=TRUE;
   $uploaded_size=$limit_bytes;
  }
@@ -66,7 +71,7 @@ $contents = fread($uploaded, $uploaded_size);
 fclose($uploaded);
 fwrite($log,$contents);
 if ($truncated)
-  fwrite ($log, " ..<truncated>..\n");
+  fwrite ($log, " ..<" . "truncated" . ">..\n");
 fclose($log);
 
 ////////////////////////////////////////
@@ -82,7 +87,7 @@ mkdir_if_needed ($linkdir);
 $linkdir = $linkdir . "/" . $hostname;
 mkdir_if_needed ($linkdir);
 $link = $linkdir . "/" . $time ;
-symlink ($log_name,$link);
+symlink ("../../../raw/".$log_name,$link);
 
 # /var/log/bm/per-hostname/onelab1.inria.fr/2008-11-31-20-02.bmlog
 $linkdir=$logdir;
@@ -91,7 +96,7 @@ mkdir_if_needed ($linkdir);
 $linkdir=$linkdir . "/" . $hostname;
 mkdir_if_needed ($linkdir);
 $link = $linkdir . "/" . $month . "-" . $time ;
-symlink ($log_name,$link);
+symlink ("../../raw/".$log_name,$link);
 
 # /var/log/bm/per-ip/138.96.250.141/2008-11-31-20-02.bmlog
 $linkdir=$logdir;
@@ -100,6 +105,6 @@ mkdir_if_needed ($linkdir);
 $linkdir=$linkdir . "/" . $ip;
 mkdir_if_needed ($linkdir);
 $link = $linkdir . "/" . $month . "-" . $time ;
-symlink ($log_name,$link);
+symlink ("../../raw/".$log_name,$link);
 
 ?>