| 1 |
#!/usr/bin/php -q |
|---|
| 2 |
<?php |
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
require("/var/alternc/bureau/class/local.php"); |
|---|
| 33 |
$FILES_OWNER = 33; |
|---|
| 34 |
$APACHE_LOG = '/var/log/apache/access.log.1'; |
|---|
| 35 |
umask(0177); |
|---|
| 36 |
$hosts = array(); |
|---|
| 37 |
$nolog_hosts = array(); |
|---|
| 38 |
$LOGS_SUFIX='.log'; |
|---|
| 39 |
|
|---|
| 40 |
if (!mysql_connect($L_MYSQL_HOST,$L_MYSQL_LOGIN,$L_MYSQL_PWD)) { |
|---|
| 41 |
echo "Cannot connect to Mysql !\n"; |
|---|
| 42 |
return 1; |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
if (!mysql_select_db($L_MYSQL_DATABASE)) { |
|---|
| 46 |
echo "Cannot connect to Mysql database $L_MYSQL_DATABASE !\n"; |
|---|
| 47 |
return 1; |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
$query = "SELECT mid,folder,hostname FROM stats2 ORDER BY CHAR_LENGTH(hostname) ASC"; |
|---|
| 51 |
$result = mysql_query($query); |
|---|
| 52 |
if (mysql_errno()) { |
|---|
| 53 |
echo 'MySQL error: '.mysql_error()."\n"; |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
while ($row = mysql_fetch_assoc($result)) { |
|---|
| 58 |
$hosts[$row['hostname']] = array( |
|---|
| 59 |
'mid'=>$row['mid'], |
|---|
| 60 |
'log_files'=>array( |
|---|
| 61 |
$row['folder'].'/'.$row['hostname'].$LOGS_SUFIX =>'' |
|---|
| 62 |
) |
|---|
| 63 |
); |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
$parent_host = $row['hostname']; |
|---|
| 67 |
while ($pos = strpos($parent_host, '.')) { |
|---|
| 68 |
$parent_host = substr($parent_host, $pos+1); |
|---|
| 69 |
if (!isset($hosts[$parent_host])) { |
|---|
| 70 |
continue; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
$parent_log_files = array_keys($hosts[$parent_host]['log_files']); |
|---|
| 74 |
foreach($parent_log_files as $parent_log_file) { |
|---|
| 75 |
$hosts[$row['hostname']]['log_files'][$parent_log_file] = &$hosts[$parent_host]['log_files'][$parent_log_file]; |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
break; |
|---|
| 79 |
} |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
if (!$apache_log_file = fopen($APACHE_LOG, 'r')) { |
|---|
| 84 |
return 1; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
while ($line = fgets($apache_log_file)) { |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
//assume that hostname is at end of line and separated with a space |
|---|
| 92 |
$host = substr($line, strrpos($line, ' ')+1, -1); |
|---|
| 93 |
|
|---|
| 94 |
if (is_null($hosts[$host])) { |
|---|
| 95 |
unset($hosts[$host]); |
|---|
| 96 |
$nolog_hosts[$host]=''; |
|---|
| 97 |
continue; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
if (isset($nolog_hosts[$host])) { |
|---|
| 101 |
continue; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
//Processed only one time by not listed hostname |
|---|
| 106 |
if (!isset($hosts[$host])) { |
|---|
| 107 |
$parent_host = $host; |
|---|
| 108 |
while ($pos = strpos($parent_host, '.')) { |
|---|
| 109 |
$parent_host = substr($parent_host, $pos+1); |
|---|
| 110 |
if (isset($nolog_hosts[$parent_host])) { |
|---|
| 111 |
$pos = false; |
|---|
| 112 |
break; |
|---|
| 113 |
} |
|---|
| 114 |
if (is_null($hosts[$parent_host])) { |
|---|
| 115 |
unset($hosts[$parent_host]); |
|---|
| 116 |
$nolog_hosts[$parent_host]=''; |
|---|
| 117 |
$pos = false; |
|---|
| 118 |
break; |
|---|
| 119 |
} |
|---|
| 120 |
if (isset($hosts[$parent_host])) { |
|---|
| 121 |
|
|---|
| 122 |
$hosts[$host] = &$hosts[$parent_host]; |
|---|
| 123 |
break; |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|
| 126 |
if ($pos === false) { |
|---|
| 127 |
|
|---|
| 128 |
$nolog_hosts[$host]=''; |
|---|
| 129 |
continue; |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
$log_files = array_keys($hosts[$host]['log_files']); |
|---|
| 134 |
if (count($log_files) < 1) { |
|---|
| 135 |
$hosts[$host] = null; |
|---|
| 136 |
unset($hosts[$host]); |
|---|
| 137 |
$nolog_hosts[$host]=''; |
|---|
| 138 |
continue; |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
foreach($log_files as $log_file) { |
|---|
| 142 |
|
|---|
| 143 |
if (is_null($hosts[$host]['log_files'][$log_file])) { |
|---|
| 144 |
|
|---|
| 145 |
unset($hosts[$host]['log_files'][$log_file]); |
|---|
| 146 |
continue; |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
if (!is_resource($hosts[$host]['log_files'][$log_file])) { |
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
if (!file_exists($log_file)) { |
|---|
| 154 |
if (!touch($log_file)) { |
|---|
| 155 |
$hosts[$host]['log_files'][$log_file] = null; |
|---|
| 156 |
unset($hosts[$host]['log_files'][$log_file]); |
|---|
| 157 |
continue; |
|---|
| 158 |
} |
|---|
| 159 |
if (!chgrp($log_file, (int)$hosts[$host]['mid'])) { |
|---|
| 160 |
unlink($log_file); |
|---|
| 161 |
$hosts[$host]['log_files'][$log_file] = null; |
|---|
| 162 |
unset($hosts[$host]['log_files'][$log_file]); |
|---|
| 163 |
continue; |
|---|
| 164 |
} |
|---|
| 165 |
if (!chown($log_file, (int)$FILES_OWNER)) { |
|---|
| 166 |
unlink($log_file); |
|---|
| 167 |
$hosts[$host]['log_files'][$log_file] = null; |
|---|
| 168 |
unset($hosts[$host]['log_files'][$log_file]); |
|---|
| 169 |
continue; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
if (!$hosts[$host]['log_files'][$log_file] = fopen($log_file, 'a')) { |
|---|
| 175 |
$hosts[$host]['log_files'][$log_file] = null; |
|---|
| 176 |
unset($hosts[$host]['log_files'][$log_file]); |
|---|
| 177 |
continue; |
|---|
| 178 |
} |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
if (!fwrite($hosts[$host]['log_files'][$log_file], $line)) { |
|---|
| 183 |
fclose($hosts[$host]['log_files'][$log_file]); |
|---|
| 184 |
$hosts[$host]['log_files'][$log_file] = null; |
|---|
| 185 |
unset($hosts[$host]['log_files'][$log_file]); |
|---|
| 186 |
} |
|---|
| 187 |
} |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
$hostnames = array_keys($hosts); |
|---|
| 192 |
while ($host = array_pop($hostnames)) { |
|---|
| 193 |
if (is_null($hosts[$hostname]['log_files'])) { |
|---|
| 194 |
unset($hosts[$hostname]['log_files'][$log_file]); |
|---|
| 195 |
continue; |
|---|
| 196 |
} |
|---|
| 197 |
$log_files = array_keys($hosts[$hostname]['log_files']); |
|---|
| 198 |
while ($log_file = array_pop($log_files)) { |
|---|
| 199 |
if (is_null($hosts[$hostname]['log_files'][$log_file])) { |
|---|
| 200 |
unset($hosts[$hostname]['log_files'][$log_file]); |
|---|
| 201 |
continue; |
|---|
| 202 |
} |
|---|
| 203 |
if (is_resource($hosts[$hostname]['log_files'][$log_file])) { |
|---|
| 204 |
fclose($hosts[$hostname]['log_files'][$log_file]); |
|---|
| 205 |
$hosts[$hostname]['log_files'][$log_file] = null; |
|---|
| 206 |
unset($hosts[$hostname]['log_files'][$log_file]); |
|---|
| 207 |
} |
|---|
| 208 |
} |
|---|
| 209 |
$hosts[$hostname] = null; |
|---|
| 210 |
unset($hosts[$hostname]); |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
fclose($apache_log_file); |
|---|
| 214 |
|
|---|
| 215 |
return 0; |
|---|
| 216 |
?> |
|---|
| 217 |
|
|---|