Skip to content

Instantly share code, notes, and snippets.

@guizmaii
Created January 27, 2017 08:56
Show Gist options
  • Save guizmaii/0a2ea403dbdcd9734a65fe031413a44b to your computer and use it in GitHub Desktop.
Save guizmaii/0a2ea403dbdcd9734a65fe031413a44b to your computer and use it in GitHub Desktop.
Export events from Logmatic.io output API

Installation

Download the index.js and the package.json files.

Then:

  • run npm install
  • edit and set the index.js with your query and your API_KEY
  • run node index.js and wait for the result

Questions & Feedbacks > @gpolaert

const fetch = require('node-fetch');
const fs = require('fs');
// Global args
const api_key = "<API_KEY>";
const filename = "events.log";
const rate = 1200; // 50 calls/hour
//const rate = 120; // 500 calls a hour
var query = {
"time": {
"to": "now",
"from": "now - 3h"
},
"filters": [
{
"search": {
"query": "*"
}
},
{
"field": {
"id": "syslog_hostname",
"values": [
"prod-data1",
"prod-data2"
],
"exclude": false
}
}
],
"event": true,
"limit": 1000
};
function saveToFile(json) {
if (!json.result.events || json.result.events.length == 0) {
return
}
var stream = fs.createWriteStream(filename, {flags: 'a'});
stream.once('open', function(fd) {
json.result.events.map(item => stream.write(JSON.stringify(item)+ "\n"));
stream.end();
const number_items = json.result.events.length
const last_item_id = json.result.events[number_items - 1].id
console.log(number_items + " events extracted (last_item: " + last_item_id + ")")
console.log("Events saved to " + filename);
query.afterEvent = last_item_id
setTimeout(fetchEvents, rate)
});
}
function fetchEvents() {
fetch('https://app.logmatic.io/beta/events/list?api_key=' + api_key, {
method: 'POST',
body: JSON.stringify(query),
headers: {'Content-Type': 'application/json'},
})
.then(res => res.json())
.then(json => saveToFile(json))
}
fetchEvents(query)
{
"name": "export_events",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^1.6.3",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment