summaryrefslogtreecommitdiff
path: root/iceaccess_server
blob: 6d868fe2e45d4986a79b2aac66ec8525dcb31141 (plain)
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
#!/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 $dsn $username $password );
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";
}