diff --git a/app/Console/Commands/AppendLogs.php b/app/Console/Commands/AppendLogs.php new file mode 100644 index 0000000000000000000000000000000000000000..7049e06a6777901d7272abf7090394744667a413 --- /dev/null +++ b/app/Console/Commands/AppendLogs.php @@ -0,0 +1,72 @@ +<?php + +namespace App\Console\Commands; + +use Illuminate\Console\Command; +use Illuminate\Support\Facades\Redis; +use Log; +use Monospice\LaravelRedisSentinel\RedisSentinel; + +class AppendLogs extends Command +{ + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'logs:gather'; + const LOGKEY = "metager.logs"; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Retrieves all Log Entries from Redis and writes them to file'; + + /** + * Create a new command instance. + * + * @return void + */ + public function __construct() + { + parent::__construct(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + $redis = null; + + if (env("REDIS_CACHE_DRIVER", "redis") === "redis") { + $redis = Redis::connection('cache'); + } elseif (env("REDIS_CACHE_DRIVER", "redis") === "redis-sentinel") { + $redis = RedisSentinel::connection('cache'); + } + if ($redis === null) { + Log::error("No valid Redis Connection specified"); + return; + } + + $elements = []; + $reply = $redis->pipeline(function ($pipe) use ($elements) { + $pipe->lrange(\App\Console\Commands\AppendLogs::LOGKEY, 0, -1); + $pipe->del(\App\Console\Commands\AppendLogs::LOGKEY); + }); + $elements = $reply[0]; + if (!is_array($elements) || sizeof($elements) <= 0) { + return; + } + if (file_put_contents(\App\MetaGer::getMGLogFile(), implode(PHP_EOL, $elements) . PHP_EOL, FILE_APPEND) === false) { + Log::error("Konnte Log Zeile(n) nicht schreiben"); + $redis->lpush(array_reverse($elements)); + } else { + Log::info("Added " . sizeof($elements) . " lines to todays log!"); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index decf0ba0c2c94d37c3a61365b1236a7c5e7ec965..51233aaaee9bbf7bc976113e4e7bdbe32ca99a1b 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -27,6 +27,7 @@ class Kernel extends ConsoleKernel { $schedule->command('requests:gather')->everyFifteenMinutes(); $schedule->command('requests:useragents')->everyFiveMinutes(); + $schedule->command('logs:gather')->everyMinute(); $schedule->call(function () { DB::table('monthlyrequests')->truncate(); diff --git a/app/Http/Middleware/UserAgentMaster.php b/app/Http/Middleware/UserAgentMaster.php index 64b1c75e91a9361398907dc6e80f240d7783bf72..f19fdcd3a1091347e41e8cadeaa9e779b9151b6f 100644 --- a/app/Http/Middleware/UserAgentMaster.php +++ b/app/Http/Middleware/UserAgentMaster.php @@ -34,10 +34,13 @@ class UserAgentMaster } else if ($agent->isPhone()) { $device = "mobile"; } - // Push an entry to a list in Redis - // App\Console\Commands\SaveUseragents.php is called regulary to save the list into a sqlite database - Redis::rpush('useragents', json_encode(["platform" => $agent->platform(), "browser" => $agent->browser(), "device" => $device, "useragent" => $_SERVER['HTTP_USER_AGENT']])); - Redis::expire('useragents', 301); + + if (!empty($_SERVER['HTTP_USER_AGENT'])) { + // Push an entry to a list in Redis + // App\Console\Commands\SaveUseragents.php is called regulary to save the list into a sqlite database + Redis::rpush('useragents', json_encode(["platform" => $agent->platform(), "browser" => $agent->browser(), "device" => $device, "useragent" => $_SERVER['HTTP_USER_AGENT']])); + Redis::expire('useragents', 301); + } // Try to retrieve a random User-Agent of the same category from the sqlite database $newAgent = \App\UserAgent::where("platform", $agent->platform()) diff --git a/app/MetaGer.php b/app/MetaGer.php index 8a30f661a4578c28152055bf13fdb13268f51206..495ce417e5c820cd36a2978957d764bc623e369a 100644 --- a/app/MetaGer.php +++ b/app/MetaGer.php @@ -10,6 +10,7 @@ use Illuminate\Support\Facades\Redis; use Jenssegers\Agent\Agent; use LaravelLocalization; use Log; +use Monospice\LaravelRedisSentinel\RedisSentinel; use Predis\Connection\ConnectionException; class MetaGer @@ -1392,9 +1393,10 @@ class MetaGer $logEntry = preg_replace("/\n+/", " ", $logEntry); - $logpath = \App\MetaGer::getMGLogFile(); - if (file_put_contents($logpath, $logEntry . PHP_EOL, FILE_APPEND) === false) { - Log::error("Konnte Log Zeile nicht schreiben"); + if (env("REDIS_CACHE_DRIVER", "redis") === "redis") { + Redis::connection('cache')->rpush(\App\Console\Commands\AppendLogs::LOGKEY, $logEntry); + } elseif (env("REDIS_CACHE_DRIVER", "redis") === "redis-sentinel") { + RedisSentinel::connection('cache')->rpush(\App\Console\Commands\AppendLogs::LOGKEY, $logEntry); } } catch (\Exception $e) { Log::error($e->getMessage()); diff --git a/config/database.php b/config/database.php index b079591fcb79fc98a4d977cec168c40b0754718a..d01f9ab8ecf0e2cd33c1b12e4202e0837c1d32d6 100644 --- a/config/database.php +++ b/config/database.php @@ -121,6 +121,7 @@ return [ ], 'cache' => [ + 'driver' => env('REDIS_CACHE_DRIVER', 'redis'), 'host' => env('REDIS_CACHE_HOST', 'localhost'), 'password' => env('REDIS_CACHE_PASSWORD', null), 'port' => env('REDIS_CACHE_PORT', 6379), diff --git a/public/index.php b/public/index.php index bd755eded92e522891ab9649b1539aa687cbc72b..372a02060609375f84c60846112de78c7b54b093 100644 --- a/public/index.php +++ b/public/index.php @@ -16,7 +16,10 @@ if (isset($_SERVER["HTTP_FORWARDED"]) && isset($_SERVER["HTTP_X_FORWARDED_FOR"]) unset($_SERVER["HTTP_FORWARDED"]); } -$_SERVER["AGENT"] = $_SERVER["HTTP_USER_AGENT"]; +$_SERVER["AGENT"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"; +if (!empty($_SERVER["HTTP_USER_AGENT"])) { + $_SERVER["AGENT"] = $_SERVER["HTTP_USER_AGENT"]; +} /* |--------------------------------------------------------------------------