Skip to content

Instantly share code, notes, and snippets.

@kylewest
Created February 22, 2012 22:00
Show Gist options
  • Save kylewest/1887709 to your computer and use it in GitHub Desktop.
Save kylewest/1887709 to your computer and use it in GitHub Desktop.
#!/usr/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','agent':'$agent'}";
my $req = HTTP::Request->new('POST', $uri);
$req->header('Content-Type' => 'application/json');
$req->content($json);
my $lwp = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
$lwp->request($req);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment