Skip to content

Commit

Permalink
Add ip->city and country->locator function
Browse files Browse the repository at this point in the history
Instead of just the country, the country + city are used. This enable a
better overview of how the service is being used.
  • Loading branch information
jorianvo committed Jun 19, 2015
1 parent 1906483 commit cb25d36
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions lib/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public static function sendToStatsd($path,$action){
// Ip can be invalid or a local address, if so set country to unknown
// Otherwise we can go ahead and resolv country
if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
$country = "Unknown";
$locator = "Unknown";
} else {
// As filtering the private range works only for ipv4 we can still get no
// location from the db, this case is handled by the IpToCountry method
$country = self::IpToCountry($ip);
$locator = self::IpToLocator($ip);
}

//throw new \Exception("country of origin = $country");
Expand All @@ -56,28 +56,53 @@ public static function sendToStatsd($path,$action){
if ($action == "File read"){
$service->increment('read');
} elseif ($action == "File write"){
$service->increment("files.writes.".$country);
$service->increment("files.writes.".$locator);
}

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

// This function will return the corresponding country given a city
private static function IpToCountry($ip) {
// This function will return the locator of a given (valid, non-private)
// ip address in the form <country>.<city> or just Unknown if the country
// cannot be determined
private static function IpToLocator($ip) {
$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-Country.mmdb');

// Get the record of the corrsponding ip
// The country can still be unknown e.g. if the $ip is a link local ipv6
// address, so if the $record throws a 'GeoIp2\Exception\AddressNotFoundException'
// Set the country to Unknown.
try {
$record = $reader->country($ip);
$country = $record->country->name;
$record = $reader->country($ip);
$country = $record->country->name;

// Country does exists, so now find the city
$city = self::IpToCity($ip);

// Only in this case we can return a country and city
return $country.".".$city;
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
// No country found, hence city makes no sense either
return $country = "Unknown";
}
}

// This function will return the corresponding city given an ip address
private static function IpToCity($ip) {
$reader = new Reader('/usr/local/share/GeoIP/GeoLite2-City.mmdb');

// Just to be safe, also catch the 'GeoIp2\Exception\AddressNotFoundException'
// if for some reason the city is unknown (we know for sure the country exists)
try {
$record = $reader->city($ip);

// Country does exists, so now find the city
$city = $record->city->name;
} catch (GeoIp2\Exception\AddressNotFoundException $e) {
$country = "Unknown";
$city = "Unknown";
}

return $country;
return $city;
}
}

0 comments on commit cb25d36

Please sign in to comment.