Wundagore
Legacy Member
Gegroet!
Ik heb momenteel een perl programma geschreven dat het volgende doet:
Het opens een pipe, krijgt daar input van. format die input en insert die daarna in een database.
het probleem is dat die normaal constant moet runnen aangezien er input gaat blijven komen van die pipe.
probleem is, ik heb dat volgens mij begrip er ingebouwd maar doet het niet. hij verwerkt alles wat reeds in de pipe zit en eindigt daarna. terwijl hij zou moeten wachten op verdere input.
Alle hulp is welkom
Ik heb momenteel een perl programma geschreven dat het volgende doet:
Het opens een pipe, krijgt daar input van. format die input en insert die daarna in een database.
het probleem is dat die normaal constant moet runnen aangezien er input gaat blijven komen van die pipe.
probleem is, ik heb dat volgens mij begrip er ingebouwd maar doet het niet. hij verwerkt alles wat reeds in de pipe zit en eindigt daarna. terwijl hij zou moeten wachten op verdere input.
Alle hulp is welkom

Code:
#!/usr/bin/perl -W
#
#This is the SQL Daemon that will read a pipe and
#import that data into a database
#
use DBI;
#The name of the pipe goes here.
$pipe_name= "/var/log/squid/access.log";
#This array is necessary to convert the proper date
@monthname =('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
#Opening the pipe for reading, disconnecting if it can't be read.
open (LOG, "<$pipe_name") or die "Couldn't open $pipe_name for reading:$! n";
#Endless loop to continue reading.
while (<LOG>) {
$number=0;
$number2=0;
#Splitting all input into an array
@sqlinput= split(/\s+/,$_);
#Converting the epoch time to localtime
($seconds,$minutes,$hours, $day_of_month,$month,$year) = localtime($sqlinput[0]);
#localtime returns the current year -1900. Corrected here.
$year=$year+1900;
#Formatting date and time for the database
$sqlinput[0]= "$day_of_month-$monthname[$month]-$year";
$sqlinput[1]= "$hours:$minutes:$seconds";
#Splitting up the http and squid code.
@codes=split(/\//,$sqlinput[3]);
$sqlinput[3]=$codes[0];
$sqlinput[4]=$codes[1];
#List of the array:
#$sqlinput[0]=Date $sqlinput[3]=Squid status code $sqlinput[6]=URL
#$sqlinput[1]=Time $sqlinput[4]=HTTP status code $sqlinput[7]=
#$sqlinput[2]=Network address $sqlinput[5]=Request method $sqlinput[8]=
#$sqlinput[9]=Mime type
#Database connectionstring
$db = DBI->connect( "dbi:Pg:dbname=wlan", undef, undef, {PrintError => 0} ) or die "Can't connect to PostgreSQL: $DBI::errstr ($DBI::err)\n";
#SQL query buildup
$insertion = $db->prepare("INSERT INTO squid VALUES ('$sqlinput[0]','$sqlinput[1]','$sqlinput[2]','$sqlinput[4]','$sqlinput[5]','$sqlinput[6]','$sqlinput[9]')");
#Execution of the query and disconnect from the database.
$insertion->execute();
$db->disconnect();
#Overview in the runnning terminal
print $_;
}
close (LOG);