diff options
Diffstat (limited to 'iceaccess_server')
-rwxr-xr-x | iceaccess_server | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/iceaccess_server b/iceaccess_server new file mode 100755 index 0000000..bc98560 --- /dev/null +++ b/iceaccess_server @@ -0,0 +1,103 @@ +#!/usr/bin/perl -w +# +# Copyright (c) 2002 Ivan Kohler +# All rights reserved. +# This program is free software; you can redistribute it and/or modify it under +# the same terms as Perl itself. +# +# ivan-icelog@420.am + +use strict; +use vars qw( $Debug ); +use DBI; +use IO::Handle; +use Net::SSH qw(sshopen2); + +$Debug = 0; + +my $machine = shift or die &usage; +my $logfile = shift || '/var/log/icecast/access.log'; +my $pos = shift || 0; + +require "/etc/icelog.conf"; + +my $dbh = DBI->connect($dsn, $username, $password) + or die "Can't connect to $dsn: ". $DBI::errstr; + +my $iceaccessd = '/usr/local/bin/iceaccessd'; + +my $me = "[iceaccess_server]"; + +#my $pos = 0; + +while (1) { + my($reader, $writer) = (new IO::Handle, new IO::Handle); + $writer->autoflush(1); + warn "$me Connecting to $machine\n" if $Debug; + sshopen2($machine,$reader,$writer,$iceaccessd, $logfile, $pos); + warn "$me Entering main loop\n" if $Debug; + while (1) { + warn "$me Reading (waiting for) data\n" if $Debug; + my $line = scalar(<$reader>); + die "No response from remote iceaccessd process" unless defined($line); + chomp $line; + my %hash; + ( $pos, %hash ) = split(/\t/, $line); + if ( $pos eq 'EOF' ) { ; #re-open iceacceed process on new logfile + $pos = 0; + last; + } + + #write to db + + my $customer = $hash{mountpoint}; + $hash{mountpoint} =~ /^\/([^\/]*)/ + or die "weird mountpoint $hash{mountpoint}"; + $customer = $1; + + #$hash{mountpoint} =~ /\/|^([^\/]+)$/; + #my $file = $1; + + my $liveflag; + if ( $hash{mountpoint} =~ /\/0+(\.\w+)?$/ ) { + $liveflag = 'Y'; + } else { + $liveflag = 'N'; + } + + #switch to prepare_cached? + my $sth = $dbh->prepare(<<END) or die $dbh->errstr; + INSERT INTO icelog ( logdate, logmachine, logpos, customer, liveflag, + hostname, start, mountpoint, bytes, useragent, + seconds + ) VALUES ( NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) +END + + $sth->execute( + $machine, #logmachine + $pos, #logpos + $customer, #customer + $liveflag, #liveflag + $hash{hostname}, #hostname + $hash{start}, #start + $hash{mountpoint}, #mountpoint + $hash{bytes}, #bytes + $hash{useragent}, #useragent, + $hash{seconds}, #seconds + ) or die $sth->errstr; + $sth->finish; + + } + close $writer; + close $reader; + warn "connection to $machine lost!\n"; + sleep 5; + warn "reconnecting...\n"; +} + + + + +sub usage { + die "Usage:\n\n iceaccess_server machine logfile position\n"; +} |