Compressing Apache logs

Discussion in 'General' started by brent.stephens, Oct 20, 2006.

  1. brent.stephens

    brent.stephens New Member

    The apache logs that build up with ISPC are inordinately large, so I'd like to gzip the old ones. I'd run a script to do so every couple of weeks. My script-fu, however, is not strong enough to figure out how to find exactly what files I'm looking for, pipe them to gzip properly, and still be confident that I won't break it.

    Does anyone have a script for this that they could share?
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    Which log are you talking about? The log in /var/log/httpd/ ?
     
  3. brent.stephens

    brent.stephens New Member

    I mean the web.log files that accumulate in the directories of my clients.
     
  4. falko

    falko Super Moderator Howtoforge Staff

    Have a look at this PHP code at the end of /root/ispconfig/scripts/shell/webalizer.php:

    PHP:
    ////////////// LOGSIZE //////////////////
    function dir_size($dir) {
      
    $totalsize=0;
      if (
    $dirstream = @opendir($dir)) {
        while (
    false !== ($filename readdir($dirstream))) {
          if (
    $filename!="." && $filename!=".."){
            if (
    is_file($dir."/".$filename) && !is_link($dir."/".$filename)){
              
    $totalsize+=filesize($dir."/".$filename);
            }
            if (
    is_dir($dir."/".$filename)) $totalsize+=dir_size($dir."/".$filename);
          }
        }
      }
      
    closedir($dirstream);
      
    clearstatcache();
      return 
    $totalsize;
    }

    function 
    dir_array($dir){
      
    $directory_array = array();
      if (
    $dirstream = @opendir($dir)) {
        while (
    false !== ($filename readdir($dirstream))) {
          if (
    $filename!="." && $filename!=".."){
            if (
    is_file($dir."/".$filename) && !is_link($dir."/".$filename)){
              
    $directory_array[$dir."/".$filename] = filemtime($dir."/".$filename);
            }
            if (
    is_dir($dir."/".$filename)) $directory_array array_merge($directory_arraydir_array($dir."/".$filename));
          }
        }
      }
      
    closedir($dirstream);
      
    clearstatcache();
      return 
    $directory_array;
    }

    $webs $mod->db->queryAllRecords("SELECT * FROM isp_isp_web");
    if(!empty(
    $webs)){
      foreach(
    $webs as $web){
        
    $log_dir $path_httpd_root."/web".$web["doc_id"]."/log";
        if(
    is_dir($log_dir)){
          
    $max_directory_size str_replace(",""."trim($web["optionen_logsize"]));
          if(
    strstr($max_directory_size'%')){
            if(
    $web["web_speicher"] == -1){
              
    $log_check false;
            } else {
              
    $parts explode('%'$max_directory_size);
              if(
    is_numeric(trim($parts[0])) && trim($parts[0]) >= 0){
                
    $max_directory_size str_replace(",""."$web["web_speicher"]) * 1048576 floatval($max_directory_size) / 100;
                
    $log_check true;
              } else {
                
    $log_check false;
              }
              
    $parts NULL;
            }
          } else {
            if(
    is_numeric($max_directory_size) && $max_directory_size >= 0){
              
    $max_directory_size $max_directory_size 1048576;
              
    $log_check true;
            } else {
              
    $log_check false;
            }
          }
          
    $directory_size dir_size($log_dir);

          if(
    $log_check){
            while(
    $directory_size >= $max_directory_size){
              
    $files dir_array($log_dir);
              if(!empty(
    $files)){
                
    asort($files);
                
    $files array_slice ($files01);
                foreach(
    $files as $key => $val){
                  if(
    is_file($key)) unlink($key);
                }
              } else {
                break;
              }
              unset(
    $files);
              
    $directory_size dir_size($log_dir);
            }
          }
        }
      }
    }
    //////////////// LOGSIZE ENDE ////////////////
     

Share This Page