Skip to main content

Geo database

Ip lookup

https://whitespace.analys.co/index.php?module=API&method=UserCountry.getLocationFromIP&ip=194.57.91.215&format=xml

Whitespace database

https://composer.analys.cloud/Whitespace-Geo-City.mmdb.gz

Scripts

List Top 20 IP Addresses

Analyzes the Matomo Nginx access log and displays the top 20 IP addresses by number of requests, including their percentage of total traffic.

Create a file called check-ips.sh and make it executable:

#!/bin/bash

# Step 1: Get total number of requests
total=$(awk '{print $1}' /srv/www/matomo.riksarkivet.se/logs/access.log | wc -l)

# Step 2: Print IPs with counts and percentage
awk '{print $1}' /srv/www/matomo.riksarkivet.se/logs/access.log | sort | uniq -c | sort -nr | head -n 20 |
awk -v total="$total" '{
percent = ($1 / total) * 100;
printf "%-20s %8d (%.2f%%)\n", $2, $1, percent
}'

Lookup Geolocation for a Single IP

Looks up geolocation information (country, city, region, coordinates) for a given IP address using the local DBIP-City.mmdb file.

php geoip-test.php x.x.x.x

Create a file called geoip-test.php:

<?php
require 'vendor/autoload.php';

use GeoIp2\Database\Reader;

$ip = $argv[1] ?? null;

if (!$ip) {
echo "Usage: php geoip-test.php <IP>\n";
exit(1);
}

$dbPath = __DIR__ . '/misc/DBIP-City.mmdb';

if (!file_exists($dbPath)) {
echo "❌ MMDB file not found at $dbPath\n";
exit(1);
}

try {
$reader = new Reader($dbPath);
$record = $reader->city($ip);

echo "IP: $ip\n";
echo "Country: " . $record->country->name . " (" . $record->country->isoCode . ")\n";
echo "City: " . $record->city->name . "\n";
echo "Region: " . ($record->mostSpecificSubdivision->name ?? '-') . "\n";
echo "Latitude: " . $record->location->latitude . "\n";
echo "Longitude: " . $record->location->longitude . "\n";

} catch (Exception $e) {
echo "❌ Error: " . $e->getMessage() . "\n";
}