This file is indexed.

/usr/share/pgfouine/include/GenericLogReader.class.php is in pgfouine 1.2-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php

/*
 * This file is part of pgFouine.
 * 
 * pgFouine - a PostgreSQL log analyzer
 * Copyright (c) 2005-2008 Guillaume Smet
 *
 * pgFouine is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * pgFouine is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with pgFouine; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

require_once('lib/common.lib.php');
require_once('base.lib.php');

class GenericLogReader {
	var $displayHelp = true;
	
	var $fileName;
	var $lineParserName;
	var $accumulatorName;
	
	var $lineParsedCounter = 0;
	var $timeToParse;
	
	var $firstLineTimestamp;
	var $lastLineTimestamp;
	
	var $reportAggregators = array();
	var $listeners = array();
	
	function GenericLogReader($fileName, $lineParserName, $accumulatorName, $displayHelp = true) {
		$this->fileName = $fileName;
		$this->lineParserName = $lineParserName;
		$this->accumulatorName = $accumulatorName;
		
		$this->displayHelp = $displayHelp;
	}
	
	function addReportAggregator(& $reportAggregator) {
		$this->reportAggregators[] =& $reportAggregator;
	}
	
	function & getReportAggregators() {
		return $this->reportAggregators;
	}

	function parse() {
		global $lineParsedCounter;
		
		$this->prepare();
		
		$startTimestamp = time();
		
		$accumulator = new $this->accumulatorName;
		$lineParser = new $this->lineParserName;
		
		foreach(array_keys($this->listeners) AS $listenerName) {
			$listener =& $this->listeners[$listenerName];
			foreach($listener->getSubscriptions() AS $eventType) {
				$accumulator->addListener($eventType, $listener);
			}
		}
		
		if(DEBUG) {
			debug('Using parser: '.$this->lineParserName);
			debug('Using accumulator: '.$this->accumulatorName);
			debug('Using listeners: '.implode(', ', array_keys($this->listeners)));
		}
		
		$filePointer = @fopen($this->fileName, 'r');
		if(!$filePointer) {
			trigger_error('File '.$this->fileName.' is not readable.', E_USER_ERROR);
		}
		
		$lineParsedCounter = 0;
		$lineDetected = false;
		
		if(DEBUG) debug(getMemoryUsage());
		if(PROFILE) {
			$GLOBALS['profiler'] = new Profiler();
			$GLOBALS['profiler']->start();
		}
		
		$this->readFile($accumulator, $filePointer, $lineParser, $lineParsedCounter, $lineDetected);
		
		DEBUG && debug('Before close - '.getMemoryUsage());
		$accumulator->close();
		DEBUG && debug('After close - '.getMemoryUsage());
		
		fclose($filePointer);
		
		$this->timeToParse = time() - $startTimestamp;
		$this->lineParsedCounter = $lineParsedCounter;
		
		DEBUG && debug("\nParsed ".$lineParsedCounter.' lines in '.$this->timeToParse.' s');
		
		if(PROFILE) {
			$GLOBALS['profiler']->end();
			$GLOBALS['profiler']->displayProfile();
		}
		
		if(!$lineParsedCounter) {
			stderr('Log file is empty.');
			exit(0);
		}
		
		if(!$lineDetected && $this->displayHelp) {
			stderr('pgFouine did not find any valid PostgreSQL log line in your log file:');
			stderr('* check that PostgreSQL uses an english locale for logging (lc_messages in your postgresql.conf),');
			stderr('* check that you use the -logtype option (syslog, stderr) according to your log file,');
			stderr('* if you use syslog and log_line_prefix, check that your log_line_prefix has a trailing space,');
			stderr('* if you use stderr, check that your log_line_prefix is of the form \'%t [%p]: [%l-1] \'.');
			stderr('If you think your log file and your options are correct, please contact the author (gsmet on #postgresql@freenode or guillaume-pg at smet dot org).');
			exit(1);
		}
	}
	
	function readFile(& $accumulator, & $filePointer, &$lineParser, &$lineParsedCounter, &$lineDetected) {
		$currentTimestamp = time();
		
		while (!feof($filePointer)) {
			$text = rtrim(fgets($filePointer), "\r\n");
			if(empty($text)) {
				continue;
			}
			$lineParsedCounter ++;
			
			$line =& $lineParser->parse($text);
			if($line) {
				if(!isset($this->firstLineTimestamp)) {
					$this->firstLineTimestamp = $line->getTimestamp();
				}
				$this->lastLineTimestamp = $line->getTimestamp();
				$accumulator->append($line);
				
				if(!is_a($line, 'PostgreSQLContinuationLine')) {
					$lineDetected = true;
				}
			}
			if($lineParsedCounter % 20000 == 0 && isset($this->lastLineTimestamp)) {
				if(DEBUG) {
					debug('    Garbage collector:');
					debug('         before: '.getMemoryUsage());
				}
				$accumulator->garbageCollect($this->lastLineTimestamp);
				if(DEBUG) {
					debug('         after: '.getMemoryUsage());
				}
			}
			if($lineParsedCounter % 100000 == 0) {
				stderr('parsed '.$lineParsedCounter.' lines');
				if(DEBUG) {
					$currentTime = time() - $currentTimestamp;
					$currentTimestamp = time();
					debug('    '.getMemoryUsage());
					debug('    Time: '.$currentTime.' s');
				}
			}
		}
	}
	
	function output() {
		for($i = 0; $i < count($this->reportAggregators); $i++) {
			$this->reportAggregators[$i]->output();
		}
	}
	
	function prepare() {
		$needs = array();
		
		for($i = 0; $i < count($this->reportAggregators); $i++) {
			$needs = array_merge($needs, $this->reportAggregators[$i]->getNeeds());
		}
		$needs = array_unique($needs);
		foreach($needs AS $need) {
			$this->addListener($need);
		}
	}
	
	function getLineParsedCounter() {
		return $this->lineParsedCounter;
	}
	
	function addListener($listenerName) {
		$listener = new $listenerName();
		$this->listeners[$listenerName] =& $listener;
	}
	
	function & getListener($listenerName) {
		if(isset($this->listeners[$listenerName])) {
			$listener =& $this->listeners[$listenerName];
		} else {
			$listener = false;
		}
		return $listener;
	}
	
	function getFileName() {
		return $this->fileName;
	}
	
	function getTimeToParse() {
		return $this->timeToParse;
	}
	
	function getLineParsedCount() {
		return $this->lineParsedCounter;	
	}
	
	function getFirstLineTimestamp() {
		return $this->firstLineTimestamp;
	}
	
	function getLastLineTimestamp() {
		return $this->lastLineTimestamp;
	}
}

?>