diff --git a/.gitignore b/.gitignore index 676c9a0..9dab66a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ .env vendor .idea + +IP2LOCATION-LITE-DB5.BIN +LICENSE_LITE.TXT +README_LITE.TXT diff --git a/composer.json b/composer.json index 1ce4001..f908ea1 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "php": "^8.1", "ext-curl": "*", "aws/aws-sdk-php": "^3.314", - "vlucas/phpdotenv": "^5.6" + "vlucas/phpdotenv": "^5.6", + "ip2location/ip2location-php": "^8.3" }, "license": "private", "autoload": { diff --git a/composer.lock b/composer.lock index ca388c0..0498200 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "04526e671e37e821079169443d15dc59", + "content-hash": "c356f4d6e2b0283facee8041cbc68f4c", "packages": [ { "name": "aws/aws-crt-php", @@ -542,6 +542,50 @@ ], "time": "2023-12-03T20:05:35+00:00" }, + { + "name": "ip2location/ip2location-php", + "version": "8.3.0", + "source": { + "type": "git", + "url": "https://github.com/chrislim2888/IP2Location-PHP-Module.git", + "reference": "4c501aa1f666ae85eab84d4df9d19399f2e6ce15" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/chrislim2888/IP2Location-PHP-Module/zipball/4c501aa1f666ae85eab84d4df9d19399f2e6ce15", + "reference": "4c501aa1f666ae85eab84d4df9d19399f2e6ce15", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "IP2Location.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "IP2Location", + "email": "support@ip2location.com", + "homepage": "http://www.ip2location.com" + } + ], + "description": "[Official Release] IP2Location PHP API to get location info from IPv4 and IPv6 address.", + "homepage": "http://www.ip2location.com", + "keywords": [ + "geolocation", + "ip2location", + "ip2locationlite" + ], + "support": { + "issues": "https://github.com/chrislim2888/IP2Location-PHP-Module/issues", + "source": "https://github.com/chrislim2888/IP2Location-PHP-Module/tree/8.3.0" + }, + "time": "2020-11-23T04:30:39+00:00" + }, { "name": "mtdowling/jmespath.php", "version": "2.7.0", diff --git a/cpu-mem-json.sh b/cpu-mem-json.sh new file mode 100755 index 0000000..2948268 --- /dev/null +++ b/cpu-mem-json.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +printf '{\"time\":\"%(%Y-%m-%dT%H:%M:%S%z)T\",' -1 +grep 'cpu ' /proc/stat | awk '{usage=($2+$4)/($2+$4+$5)} END {printf("\"cpuPercent\":%f,", usage)}' +awk '{load=$1} END {printf("\"load\":%f,", load)}' /proc/loadavg +awk ' +/^MemTotal:/ { + mem_total=$2 +} +/^MemFree:/ { + mem_free=$2 +} +/^Buffers:/ { + mem_free+=$2 +} +/^Cached:/ { + mem_free+=$2 +} +/^SwapTotal:/ { + swap_total=$2 +} +/^SwapFree:/ { + swap_free=$2 +} +END { + used=(mem_total-mem_free) + total=mem_total + + pct=0 + if (total > 0) { + pct=used/total + } + + # full text + printf("\"memUsed\":%d,\"memTotal\":%d,\"memPercent\":%f,", used, total, pct) + } + ' /proc/meminfo +df | grep '/dev/nvme1n1p3' | awk '{usage=(1-($4/$2))} END {printf("\"disk01\":%f,", usage)}' +awk '{printf("\"secondsUp\":%d", $1)}' /proc/uptime +echo '}' diff --git a/src/GeoPoint.php b/src/GeoPoint.php new file mode 100644 index 0000000..3adee5f --- /dev/null +++ b/src/GeoPoint.php @@ -0,0 +1,66 @@ + + * @version 1.0 + */ +class GeoPoint +{ + /** + * @var Ip2LocationDb|bool|null + */ + protected static Ip2LocationDb|bool|null $ip2Location = null; + + protected static array $cache = []; + + public static function fromIp(string $ip): array|null + { + if (isset(self::$cache[$ip])) { + return self::$cache[$ip]; + } + + self::ensureDb(); + if (! self::$ip2Location instanceof Ip2LocationDb) { + self::$cache[$ip] = null; + + return null; + } + + $location = self::$ip2Location->lookup($ip, [Ip2LocationDb::LATITUDE, Ip2LocationDb::LONGITUDE]); + if ($location === false || empty($location['latitude']) || empty($location['longitude'])) { + self::$cache[$ip] = null; + + return null; + } + + $result = ['lat' => $location['latitude'], 'lon' => $location['longitude']]; + self::$cache[$ip] = $result; + + return $result; + } + + protected static function ensureDb(): void + { + if (self::$ip2Location !== null) { + return; + } + + try { + self::$ip2Location = new Ip2LocationDb(__DIR__ . '/../IP2LOCATION-LITE-DB5.BIN'); + } catch (\Exception $e) { + + } + } +} \ No newline at end of file diff --git a/src/LogSource.php b/src/LogSource.php index 494f685..56d25ef 100644 --- a/src/LogSource.php +++ b/src/LogSource.php @@ -177,6 +177,14 @@ class LogSource $data['@timestamp'] = date_format($date, 'c'); $data['time'] = date_format($date, 'c'); + + if (! empty($data['remoteIp'])) { + $location = GeoPoint::fromIp($data['remoteIp']); + if ($location) { + $data['location'] = $location; + } + } + $this->events[] = ['hash' => $hash, 'date' => $data['time'], 'line' => json_encode($data)]; $this->sendCount ++; @@ -204,7 +212,7 @@ class LogSource CURLOPT_URL => $_ENV['ELASTICSEARCH_HOST'] . '/_bulk', CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', - 'Authorization: ApiKey ' . $_ENV['ELASTICSEARCH_API_KEY'], + //'Authorization: ApiKey ' . $_ENV['ELASTICSEARCH_API_KEY'], ], CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, @@ -215,10 +223,14 @@ class LogSource //CURLOPT_VERBOSE => true, ]); - $resp= curl_exec($curl); + $resp = curl_exec($curl); curl_close($curl); $response = json_decode($resp, true); + if (json_last_error() !== JSON_ERROR_NONE) { + echo "Error decoding response:\n$resp\n"; + exit(0); + } if ($response['errors']) { echo $resp . "\n"; }