Add geo point to records
This commit is contained in:
parent
d222270fee
commit
3803f317e3
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,3 +1,7 @@
|
||||
.env
|
||||
vendor
|
||||
.idea
|
||||
|
||||
IP2LOCATION-LITE-DB5.BIN
|
||||
LICENSE_LITE.TXT
|
||||
README_LITE.TXT
|
||||
|
||||
@ -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": {
|
||||
|
||||
46
composer.lock
generated
46
composer.lock
generated
@ -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",
|
||||
|
||||
40
cpu-mem-json.sh
Executable file
40
cpu-mem-json.sh
Executable file
@ -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 '}'
|
||||
66
src/GeoPoint.php
Normal file
66
src/GeoPoint.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* GeoPoint.php
|
||||
*
|
||||
* @copyright 2024 Fairbanks Publishing LLC
|
||||
* @license Proprietary
|
||||
*/
|
||||
|
||||
namespace MakerDave\ElasticPush;
|
||||
|
||||
use IP2Location\Database as Ip2LocationDb;
|
||||
|
||||
/**
|
||||
* Class GeoPoint
|
||||
*
|
||||
* @author David Fairbanks <david@makerdave.com>
|
||||
* @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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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,
|
||||
@ -219,6 +227,10 @@ class LogSource
|
||||
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";
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user