-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlibrato-cli-metric-import
46 lines (39 loc) · 1.39 KB
/
librato-cli-metric-import
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env node
var config = require('./modules/librato-cli-config');
var client = require('./modules/librato-cli-client');
var flow = require('./modules/librato-cli-flow');
var program = require('commander');
var fs = require('fs');
program
.usage('[options] [metrics_definition]')
.option('-f, --file [metric_file]', 'Import a metric file of one or more metric definitions')
.parse(process.argv);
var args = program.args;
if (!program.file && args.length < 1) {
program.outputHelp();
flow.error('You must specify a metric definition to import, either as a string argument or by loading from a file using --file/-f');
return;
}
var importMetric = function(metricDefinitions) {
metricDefinitions.forEach(function(metric) {
var payload = {
data: metric,
headers: { 'Content-Type': 'application/json' }
};
var endPoint = config.baseUrl + 'v1/metrics/' + metric.name;
client.put(endPoint, payload, function (data, response) {
console.log(response.statusCode + ': ' + response.statusMessage);
console.log(JSON.stringify(data, null, 2));
});
});
};
if (program.file) {
fs.readFile(program.file, 'utf8', function (err, data) {
if (err) {
return flow.error(err);
}
importMetric(JSON.parse(data));
});
} else {
importMetric(JSON.parse(args[0]));
}