Skip to content

Commit

Permalink
Add ip to country extension
Browse files Browse the repository at this point in the history
So all writes are now logged according to the country the user
connected from.
  • Loading branch information
jorianvo committed Jun 17, 2015
1 parent 940affc commit 7d0b873
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions lib/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Liuggio\StatsdClient\Factory\StatsdDataFactory,
Liuggio\StatsdClient\Sender\SocketSender,
Liuggio\StatsdClient\Service\StatsdService;
use GeoIp2\Database\Reader;

class OC_esLog {

Expand All @@ -15,7 +16,7 @@ public function __construct(){
// The path is the path of the file that is read or written
// currently this is not used. The action is either 'File read'
// or 'File write' and determines what value to send to statsd
public static function sendToStatsd($path,$action){
public static function sendToStatsd($path,$action){
// Create the udp socket to send the data to statsd
$sender = new SocketSender('localhost', 8125, 'udp');

Expand All @@ -24,15 +25,40 @@ public static function sendToStatsd($path,$action){
$factory = new StatsdDataFactory('\Liuggio\StatsdClient\Entity\StatsdData');
$service = new StatsdService($client, $factory);

// Get ip and city of origin
//if(isset($_SERVER["REMOTE_ADDR"])) {
// $ip = $_SERVER["REMOTE_ADDR"];
//}

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = "none";
}

$country = self::IpToCountry($ip);

//throw new \Exception("country of origin = $country");

// Check if file is read or written
if ($action == "File read"){
$service->increment('read');
} elseif ($action == "File write"){
throw new \Exception("\$action = $action");
$service->increment('write');
$service->increment("files.writes.".$country);
}

// Send the data over the socket to statsd
$service->flush();
}

// This function will return the corresponding country given a city
public static function IpToCountry($ip) {
$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-Country.mmdb');

// Get the record of the corrsponding ip
$record = $reader->country($ip);

// Return ip
return $record->country->name;
}
}

0 comments on commit 7d0b873

Please sign in to comment.