Skip to content

Instantly share code, notes, and snippets.

@reklis
Last active December 22, 2015 11:29
Show Gist options
  • Save reklis/6466145 to your computer and use it in GitHub Desktop.
Save reklis/6466145 to your computer and use it in GitHub Desktop.
A simple node script to tail web logs into a BrightContext QuantChannel Input
# download and extract the gist, then from inside the folder
chmod +x ./tail-web-logs.js
# install dependencies
npm i
# tail the web logs into the input
./tail-web-logs.js --apikey="your api key" --project="www metrics" --channel="weblogs" --input="requests" --logfile=/var/log/httpd/access_log
{
"name": "node-w3c-log-input",
"version": "0.0.0",
"description": "parse w3c log files from a web server and send them into a brightcontext input",
"main": "tail-web-logs.js",
"license": "MIT",
"dependencies": {
"brightcontext": "~1.8.0",
"optimist": "~0.6.0",
"tail": "~0.3.1"
}
}
#!/usr/bin/env node
/*global require, console, process*/
(function () {
'use strict';
var
bcc = require('brightcontext'),
Tail = require('tail').Tail,
argv = require('optimist')
.demand('apikey')
.demand('project')
.demand('channel')
.demand('input')
.demand('logfile')
.argv,
re = /(OPTIONS|GET|HEAD|POST|PUT|DELETE|TRACE|CONNECT|PATCH)(?:\s)(.*)(?:\s)(?:HTTP\/\d\.\d)(?:"\s)(\d+)/
;
bcc.init(argv.apikey) // initialize the context with an api key
.project(argv.project) // open the project
.feed({ // open the input feed
channel: argv.channel,
name: argv.input,
onopen: function tailLogs (input_feed) {
var tail = new Tail(argv.logfile);
tail.on('line', function (data) {
var matches = data.match(re), msg;
if (matches && matches.length >= 4) {
// the first match is the GET, POST, etc
// the second match is the path like /some/document.foo
// the third is the server status like 200, 404...
msg = {
action: matches[1] || '',
path: matches[2] || '',
code: parseInt(matches[3] || 0, 10)
};
console.dir(msg);
// send the feed input
input_feed.send(msg);
}
});
},
onerror: function (error) {
console.log(error);
process.exit(13);
}
}
);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment