public function execute() { $logfile = $this->arguments[0]; if (!is_file($logfile) || !is_readable($logfile)) { cli_error("File '{$logfile}' does not exist or not readable."); } require_once $this->mooshDir . '/includes/ApacheLogParser/Parser.class.php'; /* LogFormat "H: %v U: %{MOODLEUSER}n T: %Ts / %Dus | %{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" moodle_log H: www,example.com U: 10164391 T: 0s / 20500µs | 192.198.151.44 - - [22/Dec/2013:08:20:35 +0000] "GET /login/index.php?testsession=1164 HTTP/1.1" 303 904 "http://www,example.com/login/index.php" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" */ $elements = array('server_name', array('element' => 'note', 'name' => 'moodle_user'), 'serving_time', 'serving_time_microseconds', array('element' => 'request_header_line', 'name' => 'forwarded_for'), 'remote_logname', 'remote_user', 'time', 'request_first_line', 'status', 'bytes_sent', array('element' => 'request_header_line', 'name' => 'referer'), array('element' => 'request_header_line', 'name' => 'user-agent')); $re = 'H: %s U: %s T: %ss \\/ %sµs \\| %s %s %s %s "%s" %s %s "%s" "%s"'; // $parser = Parser::createFormat(Parser::$FORMAT_COMBINED); $parser = new Parser($re, $elements); $parser->setFile($logfile); while (($line = $parser->next()) != -1) { var_dump($line); if (!count($line)) { die; } } }
public function execute() { $logfile = $this->arguments[0]; $options = $this->expandedOptions; $this->checkFileArg($logfile); require_once $this->mooshDir . '/includes/ApacheLogParser/Parser.class.php'; $after = false; if ($options['after']) { $after = strtotime($options['after']); } $parser = Parser::createFormat(Parser::$FORMAT_COMBINED); $parser->setFile($logfile); $i = 0; $list = array(); while (($line = $parser->next()) != -1) { $i++; if ($line['status'] != 404) { continue; } // Possibly check a date if ($after && $line['time'] <= $after) { continue; } // Check for 'GET /file.php/1234/filename HTTP/' $matches = NULL; if (!preg_match('|GET /file.php/(.*) HTTP/|', $line['request_first_line'], $matches)) { continue; } //echo $matches[1]; if (!isset($list[$matches[1]])) { $list[$matches[1]] = 0; } $list[$matches[1]]++; if (!count($line)) { echo "Line {$i} could not be parsed!\n"; var_dump($line); die; } } foreach ($list as $k => $n) { echo "{$n},{$k}\n"; } }