Skip to content

Commit

Permalink
added massive memory improvements by not storing the log in the memor…
Browse files Browse the repository at this point in the history
…y and also gave new option for just influx checking
  • Loading branch information
geek-at committed May 7, 2016
1 parent fc3eeab commit f67a74e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
67 changes: 40 additions & 27 deletions analyze.php
Expand Up @@ -8,18 +8,29 @@

echo "[+] Started..\n";

$lines = file(LOG_FILE,FILE_SKIP_EMPTY_LINES);
if(file_exists('cache/lasttime.txt'))
$lasttime = trim(implode("",file('cache/lasttime.txt')));
else $lasttime=0;

//since we might want to check the logs and send them to influx more often
if(INFLUX_HOST)
{
$influxsent = 0;
if(file_exists('cache/lastinflux.txt'))
$lastinflux = trim(implode("",file('cache/lastinflux.txt')));
else $lastinflux=0;
}

if($dev) $lasttime=0;

if($lasstime)
if($lasttime)
echo "[#] Lasttime: ".date("d.m (H:i)",$lasttime)."\n";
$stats = array();
foreach($lines as $line)
{
$handle = fopen(LOG_FILE, "r");
if ($handle)
while (($line = fgets($handle)) !== false)
{
$line = trim($line);
$i++;
$type = false;
$hash = false;
Expand All @@ -32,8 +43,7 @@
$responsecode = $arr[8];
$referrer = str_replace('"', '', $arr[10]);
$arr2 = explode('/', $path);
if(count($arr2)<2){continue;}

if(count($arr2)<2){continue;}
$timestring = substr($time,1);
$tarr = explode('/',$timestring);
$day = $tarr[0];
Expand All @@ -42,33 +52,27 @@
$year = $tarr[0];
$hour = $tarr[1];
$min = $tarr[2];
$sec = $tarr[3];

$sec = $tarr[3];
$time = strtotime("$day-$month-$year $hour:$min:$sec");

$agent = str_replace('"', '', implode(' ',array_slice($arr, 11)));

$agent = str_replace('"', '', implode(' ',array_slice($arr, 11)));
if($time<$lasttime || $responsecode!=200)
{continue;}
else $lasttime = $time;

if($dev) echo "Analyzing $path. Is image? ".(isThatAnImage($path)?'Yes':'No')."\n";

{continue;}
else $lasttime = $time;
if($dev) echo "Analyzing $path. Is image? ".(isThatAnImage($path)?'Yes':'No')."\n";
if(isThatAnImage($path))
{
if(!$data[$path])
$data[$path] = getDataFromURL($path);

$data[$path] = getDataFromURL($path);
$hash = $data[$path]['hash'];
$size = $data[$path]['size'];

if(!$hash || !$size) continue;

$size = $data[$path]['size'];
if(!$hash || !$size) continue;
if(!$dev) echo "\rGot ".++$count.' requests';

if(INFLUX_HOST)
if(INFLUX_HOST && $time>$lastinflux)
{
$influxtime = $time.'000000000';
++$influxsent;
sendToInflux('hash='.$hash.',ip='.$ip.',referrer='.sanatizeStringForInflux(($referrer?$referrer:'0')).' value=1,size='.$size,$influxtime);
}

Expand All @@ -82,16 +86,14 @@
fwrite($fp,trim($referrer)."\n");
fclose($fp);
}



$stats[$hash]['count']++;
$stats[$hash]['traffic']+=$size;
$mosttraffic[$hash]+=$size;

$mosttraffic[$hash]+=$size;
$alltraffic+=$size;
$allhits++;
}
}
}

echo "\r[+] Done analyzing..\n\n---------------\n\n";
$lasttime = (time()-1);
Expand All @@ -100,6 +102,17 @@
fwrite($fh,$lasttime);
fclose($fh);

if(INFLUX_HOST)
{
echo "\r\n[+] Sent $influxsent packages to influx\n\n---------------\n\n";
$lastinflux = (time()-1);
$fh = fopen("cache/lastinflux.txt","w");
fwrite($fh,$lastinflux);
fclose($fh);
if($argv[1]=='onlyinflux')
die("[X] Stopping since cli arg told me to ;)\n");
}

if(is_array($mosttraffic))
{
arsort($mosttraffic);
Expand Down
2 changes: 1 addition & 1 deletion example.config.inc.php
@@ -1,5 +1,5 @@
<?php
//InfluxDB reporting
//InfluxDB reporting. If you don't need it, just leave the host field empty
define('INFLUX_HOST','');
define('INFLUX_HOST_UDP_PORT','');
define('INFLUX_HOST_MEASUREMENT','pictshare');
Expand Down

0 comments on commit f67a74e

Please sign in to comment.