/** * Inserts the access log information in * the database * * @param array $alogs Access Log lines * @param $fromServer array The sender of these access logs * * @throws \Kassner\LogParser\FormatException */ public function insertAccessLogging(array $alogs, $fromServer, $parseString) { try { if (!(count($alogs) > 0)) { return; } $parser = new \Kassner\LogParser\LogParser(); $parser->setFormat($parseString); $toBeInserted = []; foreach ($alogs as $line) { $userSpec = $parser->parse($line); if ($userSpec->host === '::1') { continue; } $userSpec->device = parse_user_agent($userSpec->HeaderUserAgent); $city = new Reader(database_path() . '/GeoLite2-City.mmdb'); $geoRecord = $city->city($userSpec->host); $userSpec->fromServer = $fromServer; $userSpec->location = ['city' => $geoRecord->city->name, 'country' => $geoRecord->country->name]; $userSpec->createdAt = time(); $toBeInserted[] = $userSpec; } $this->mongoCollectionForAccess->batchInsert($toBeInserted); } catch (\Exception $e) { error_log(print_r($e, true)); } }
/** * Perform command. */ public function perform() { try { $entry = []; $parser = new \Kassner\LogParser\LogParser(); $parser->setFormat('%h %l %u %t "%r" %>s %O "%{Referer}i" \\"%{User-Agent}i"'); $lines = file('/var/log/apache2/access.log', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as &$line) { $userSpec = $parser->parse($line); $userSpec->device = parse_user_agent($userSpec->HeaderUserAgent); $city = new Reader(__DIR__ . '/../Database/GeoLite2-City.mmdb'); $country = new Reader(__DIR__ . '/../Database/GeoLite2-Country.mmdb'); $userSpec->location = ['city' => $city->city($userSpec->host)->city->name, 'country' => $country->country($userSpec->host)->country->name]; $entry[] = $userSpec; } file_put_contents('/var/log/apache2/access.log', ''); } catch (\Exception $e) { file_put_contents('/var/log/apache2/access.log', ''); } if (count($entry) > 0) { AccessReport::odmCollection()->batchInsert($entry); print "wrote " . count($entry) . " records"; } else { print 0; } }
/** * Insert Raw Varnish data into database * * @param $log * @throws \Kassner\LogParser\FormatException */ public function importDataFromLogFile($log) { $parser = new \Kassner\LogParser\LogParser(); $parser->setFormat('%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-agent}i\\"'); $lines = explode("\n", $log); foreach ($lines as $line) { if (strlen(trim($line)) > 0) { $entry = $parser->parse($line); $requestURIParts = explode(' ', $entry->request); $logLine = new VarnishLog(); $logLine->setHost($entry->host); $logLine->setReferer($entry->HeaderReferer); $logLine->setRequestUri($requestURIParts[1]); $logLine->setStatus($entry->status); $logLine->setUserAgent($entry->HeaderUseragent); $this->em->persist($logLine); $this->em->flush(); } } }