initial commit
[map-websocket.git] / client-syslogpipe-injext.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use Parse::Syslog;
5 use Socket;
6 use IO::Socket;
7
8 use Geo::IP;
9 my $gi = Geo::IP->open("GeoLiteCity.dat", GEOIP_STANDARD);
10
11 my $server = IO::Socket::INET->new(
12   Proto     => 'tcp',
13   LocalPort => 10000,
14   Listen    => 5,
15   Reuse     => 1 )
16 or die "can't setup server";
17
18 my $num_of_client = -1;
19
20 while (1) {
21   my $client;
22   do {
23     $client = $server->accept;
24   } until ( defined($client) );
25   my $peerhost = $client->peerhost();
26   print "Accepted a client $client, $peerhost, id = ", ++$num_of_client, "\n";
27   $client->autoflush(1);
28   open (SYSLOG, "/tmp/perl.pipe");
29   while(<SYSLOG>) {
30     my @messages = split(",", $_);
31   
32     my $host = substr($messages[0],1);
33     my $facility = substr($messages[1],1);
34     my $priority = substr($messages[2],1);
35     my $level = substr($messages[3],1);
36     my $tag = substr($messages[4],1);
37     my $YMD = substr($messages[5],1);
38     my $HMS = substr($messages[6],1);
39     my $program = substr($messages[7],1);
40     my $msg = substr($messages[8],1);
41     if($msg =~/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ ){
42       my $ip = $1;
43       if (unpack('N',inet_aton($ip)) > 0) {
44         my $record = $gi->record_by_addr($ip);
45         if ($record) {
46           print "Sending $ip ".$record->latitude.":".$record->longitude."\n";
47           print $client $record->latitude.":".$record->longitude."\n";
48         }
49       } else {
50         print "$msg $ip Hrrm no country for old men.\n";
51       }
52     } else {
53       print "No ip in $msg";
54     }
55   }
56 }