Skip to content

Instantly share code, notes, and snippets.

@ltudury
Created January 14, 2012 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltudury/1609521 to your computer and use it in GitHub Desktop.
Save ltudury/1609521 to your computer and use it in GitHub Desktop.
LogglySender
#!/usr/local/bin/perl
#---------
# LogglySender v0.1
# Sends log lines from Apache to Loggly via an HTTP input
# http://wiki.loggly.com/
#---------
# IMPORTANT: Be sure to make this script executable!
# (e.g. "chmod +x logglysender.pl")
#---------
use LWP::UserAgent;
while ($log = <STDIN>) { my ($host,$date,$url_with_method,$status,$size,$referrer,$agent) = $log =~
m/^(\S+) - - \[(\S+ [\-|\+]\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) "(.*?)" "([^"]+)"$/;
my ($method, $url, $http) = split /\s+/, $url_with_method;
$url =~ s/\?(.*)//;
$referrer=~ s/\?(.*)//;
my $uri = '<< put your Loggly input HTTP POST url here! >>';
my $json = "{'host':'$host','date':'$date','url':'$url','status':'$status','size':'$size','referrer':'$referrer','agen
t':'$agent'}";
my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $lwp = LWP::UserAgent->new;
$lwp->request( $req );
}
#---------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment