Помогите написать парсер лога!

Sandr

Guest
Помогите написать парсер лога!

Есть ФТП сервер ServU
Пишет все в лог (пример ниже)
Очень хотелось-бы заставить парсер заносить в MySQL всю инфу по сессиям
и отдельно какой юзверь какой файл скачал - какой закачал
Вот пробую но не получается :(
для начала хотяб разобрать лог и вывести в виде таблицы ( чтоб посмотреть)



Вот пример лога
[5] Mon 20Jan03 21:49:33 - (000106) Connected to 10.0.0.18 (Local address 10.0.0.99)
[5] Mon 20Jan03 21:49:33 - (000106) IP-Name: DOBERMAN
[5] Mon 20Jan03 21:49:33 - (000106) User DOBERMAN logged in
[5] Mon 20Jan03 21:50:11 - (000106) Closing connection for user DOBERMAN (00:00:38 connected)
[5] Mon 20Jan03 21:50:16 - (000107) Connected to 10.0.0.18 (Local address 10.0.0.99)
[5] Mon 20Jan03 21:50:16 - (000107) IP-Name: DOBERMAN
[5] Mon 20Jan03 21:50:16 - (000107) User DOBERMAN logged in
[5] Mon 20Jan03 21:51:11 - (000107) Closing connection for user DOBERMAN (00:00:55 connected)
[5] Mon 20Jan03 21:52:16 - (000110) Connected to 10.0.0.18 (Local address 10.0.0.99)
[5] Mon 20Jan03 21:52:16 - (000110) IP-Name: DOBERMAN
[5] Mon 20Jan03 21:52:16 - (000110) User DOBERMAN logged in
[5] Mon 20Jan03 21:52:47 - (000110) Closing connection for user DOBERMAN (00:00:31 connected)
[5] Mon 20Jan03 21:55:19 - (000111) Connected to 10.0.0.18 (Local address 10.0.0.99)
[5] Mon 20Jan03 21:55:19 - (000111) IP-Name: DOBERMAN
[5] Mon 20Jan03 21:55:20 - (000111) User DOBERMAN logged in
[5] Mon 20Jan03 21:55:50 - (000111) Closing connection for user DOBERMAN (00:00:31 connected)
[5] Mon 20Jan03 21:56:36 - (000112) Connected to 10.0.0.18 (Local address 10.0.0.99)
[5] Mon 20Jan03 21:56:36 - (000112) IP-Name: DOBERMAN
[5] Mon 20Jan03 21:56:36 - (000112) User DOBERMAN logged in
[4] Mon 20Jan03 21:56:36 - (000112) Receiving file f:\other\upload\_остолом.avi
[4] Mon 20Jan03 21:57:43 - (000112) Error receiving file f:\other\upload\_остолом.avi, aborting (163 Kb/sec - 11173888 bytes, command connection closed)
[5] Mon 20Jan03 21:57:43 - (000112) Closing connection for user DOBERMAN (00:01:07 connected)
[5] Mon 20Jan03 21:57:49 - (000113) Connected to 10.0.0.18 (Local address 10.0.0.99)
[5] Mon 20Jan03 21:57:49 - (000113) IP-Name: DOBERMAN
[5] Mon 20Jan03 21:57:50 - (000113) User DOBERMAN logged in
[4] Mon 20Jan03 21:58:16 - (000113) Receiving file f:\other\upload\_остолом.avi
[5] Mon 20Jan03 22:00:39 - (000114) Connected to 10.0.0.17 (Local address 10.0.0.99)
[5] Mon 20Jan03 22:00:39 - (000114) User GUEST logged in
[5] Mon 20Jan03 22:00:39 - (000114) IP-Name: P@RTIZAN
[5] Mon 20Jan03 22:00:49 - (000114) Closing connection for user GUEST (00:00:10 connected)
PHP:
#*************************************************************************
# Параметры программы (файл статистики)
#*************************************************************************
$statfile = 'c:/ftp.log';

#*************************************************************************
# Поля таблицы вывода
#*************************************************************************
$field = array ('num','day','Date','Time','tire','Session ID','Action');

#*************************************************************************
# Чтение из файла статистики в массив stat_src
#*************************************************************************
$stat_src = file($statfile);
	$num= count($stat_src);
	echo $num;
	for ($i=1; $i<count($stat_src); $i++) {
	$stat_src[$i] = eregi_replace("\[([0-9])\]"," ",$stat_src[$i]);
	$stat_src[$i] = eregi_replace("(\()([0-9]{6})(\)(.*)", "", $stat_src[$i]);
	$stat_ar = split(" ",$stat_src[$i]);
	print '<table width=100% border=0 cellpadding=2 cellspacing=2>';
	print $stat_ar;
			for ($m=0; $m<count($stat_ar); $m++) {
					print '<tr>';
					print '<td width=50% align=right bgcolor=#cccccc>';
					print $field[$m];
					print '</td>';
					print '<td width=50% align=left bgcolor=#eeeeee>';
					print $stat_ar[$m];
					print '</td>';
					print '</tr>';
				}
			print '</table>';
			print '</tr>';
			print '</table>';
		}
#*************************************************************
 

Sandr

Guest
Primer 1
PHP:
$statfile = file ('c:/apache/apache/htdocs/sandr/ftp.log');
foreach ($statfile as $line_num => $line) {
    ereg ('(\(([0-9]{6})\)) (Connected to) ((10.0.0.)+([0-9]{2})) (.*)', $line, $regs);
    echo "Session# - $regs[2]<br>From - $regs[4]<br>";
}
 

Sandr

Guest
Вот что я сам написал
(конечно можно сказать - не оптимизированн - да и вообще... - Но работает!)
PHP:
foreach ($statfile as $line_num => $line) {
    if ( ereg ('(\(([0-9]{6})\)) (Connected to) ((10.0.0.)([0-9]{2})) (.*)', $line, $regs)) {
    	echo "Session# - $regs[2], IP - $regs[4], Connect<br><br>";
    	} elseif ( ereg ('(\(([0-9]{6})\)) (Closing connection for user) ([^0-9{2}]{1,})([^\con]{8})', $line, $regs)) {
    	echo "Session# - $regs[2], Disconnect, время online- $regs[5]<br><br>";
    	} elseif ( ereg ('(\(([0-9]{6})\)) (Receiving file) (.*)', $line, $regs)) {
    	echo "Session# - $regs[2], UL(part) - $regs[4]<br><br>";
    	} elseif ( ereg ('(\(([0-9]{6})\)) (Error receiving file) ([^,]*)(.*)', $line, $regs)) {
    	echo "Session# - $regs[2], UL(err) - $regs[4]<br><br>";
    	} elseif ( ereg ('(\(([0-9]{6})\)) (Receiv file) ([^(]*)(.*)', $line, $regs)) {
    	echo "Session# - $regs[2], UL(ok) - $regs[4]<br><br>";
    	} elseif ( ereg ('(\(([0-9]{6})\)) (Sending file) (.*)', $line, $regs)) {
    	echo "Session# - $regs[2], DL(part) - $regs[4]<br><br>";
    	} elseif ( ereg ('(\(([0-9]{6})\)) (Error sending file) ([^,]*) (.*)', $line, $regs)) {
    	echo "Session# - $regs[2], DL(err) - $regs[4]<br><br>";
    	} elseif ( ereg ('(\(([0-9]{6})\)) (Sent file) ([^(]*)(.*)', $line, $regs)) {
    	echo "Session# - $regs[2], DL(ok) - $regs[4]<br><br>";
    	} elseif ( ereg ('()(\(([0-9]{6})\)) (IP-Name:) (.*)', $line, $regs)) {
    	echo "First - $regs[1], Session# - $regs[2], DL(ok) - $regs[4]<br><br>";
    	} else {
		}
}
А вот что получаетс - осталось только приписать часть для работы с MySQL
и все будет работать :)

Session# - 000116, IP - 10.0.0.17, Connect
Session# - 000116, DL(part) - f:\music\queen\queen - who wants to live forever.mp3
Session# - 000116, DL(err) - f:\music\queen\queen - who wants to live
Session# - 000117, IP - 10.0.0.17, Connect
Session# - 000117, DL(part) - f:\music\queen\queen - who wants to live forever.mp3
Session# - 000113, UL(err) - f:\other\upload\Костолом.avi
Session# - 000113, Disconnect, времЯ online- 01:03:17
Session# - 000117, DL(ok) - f:\music\queen\queen - who wants to live forever.mp3 successfully
Session# - 000105, Disconnect, времЯ online- 00:12:45
Session# - 000117, Disconnect, времЯ online- 00:00:12
 
Сверху