-
Notifications
You must be signed in to change notification settings - Fork 4
/
librato-cli-metric-update-attr
59 lines (49 loc) · 1.96 KB
/
librato-cli-metric-update-attr
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/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');
program
.usage('<metric_name_pattern> <key=value...>')
.parse(process.argv);
var args = program.args;
if (args.length < 1) {
program.outputHelp();
flow.error('You must specify the metric name or pattern of the metric you wish to alter');
return;
}
if (args.length < 2) {
program.outputHelp();
flow.error('No attributes were specified to be updated, nothing has been done');
return;
}
var getMatchingMetrics = function(metricName, attributes) {
var endPoint = config.baseUrl + 'v1/metrics?name=' + metricName;
client.get(endPoint, function (data, response) {
for (var i = 0; i < data.metrics.length; i++) {
var metric = data.metrics[i];
console.log('Updating attributes for: ' + metric.name);
updateAttributes(metric.name, attributes);
}
});
};
var updateAttributes = function(metricName, attributes) {
var attributeData = '';
for (var i = 1; i < attributes.length; i++) {
if (attributeData.length > 0) {
attributeData += '&';
}
var attributeKeyValuePair = attributes[i].split('=');
attributeData += 'attributes[' + attributeKeyValuePair[0] + ']=' + attributeKeyValuePair[1];
}
var payload = {
data: attributeData,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
var endPoint = config.baseUrl + 'v1/metrics/' + metricName;
client.put(endPoint, payload, function (data, response) {
console.log(response.statusCode + ': ' + response.statusMessage);
console.log(JSON.stringify(data, null, 2));
});
};
getMatchingMetrics(args[0], args.slice(0));