-
Notifications
You must be signed in to change notification settings - Fork 0
/
newrelic_insights_beacon.js
79 lines (61 loc) · 2.36 KB
/
newrelic_insights_beacon.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var http, gu, setStatusBeacon
http = require('http');
gu = require('./gpio_utils')
var insightsApiKey, newrelicAccountId, insightsHost;
// node newrelic_insights_beacon.js HrurPdQHISZ4ESs8iydk34u7tKHv1zXU 429813 staging-insights-api.newrelic.com
if(!process.argv[2]){
throw ({'error':'Please supply an New Relic Insights query key'});
}
insightsApiKey = process.argv[2]; //ex.HrurPdQHISZ4ESs8iydk34u7tKHv1zXU
if(!process.argv[3]){
throw ({'error':'Please supply an New Relic account Id'});
}
newrelicAccountId = process.argv[3]; //ex.429813
insightsHost = !process.argv[4] ? "insights-api.newrelic.com" : process.argv[4]; //ex.staging-insights-api.newrelic.com
//RGB pins for a rev 2 raspberry pi (rev 1 are RED_PIN = 17, GREEN_PIN = 27, BLUE_PIN = 22;)
var RED_PIN = 11, GREEN_PIN = 13, BLUE_PIN = 15;
//Service polling ineterval in Milliseconds
var intervalInMills = 100000;
//NRQL query URL encoded from https://insights.newrelic.com/accounts/[Your_Account]/manage/api_keys
var query = "SELECT%20count(exception)%20FROM%20NewRelic_AzurePortal_APM_Requests%20WHERE%20application%20LIKE%20'NR-Stamp%25'%20AND%20exception%20IS%20NOT%20NULL%20SINCE%201%20hour%20ago%20limit%2050";
var fullPath = "/v1/accounts/" + newrelicAccountId + "/query?nrql=" + query
var insightsRequest = {
host: '' + insightsHost + '',
port: 80,
path: fullPath,
method: 'GET',
headers: {
'Accept:': 'application/json',
'X-Query-Key': '' + insightsApiKey + ''
}
};
setStatusBeacon = function() {
console.log('Updating...');
return http.get(insightsRequest, function(res){
console.log("Status code from response: " + res.statusCode);
var data, results;
res.on('data', function (chunk) {
if(chunk) {
data += chunk;
}
});
return res.on('end', function() {
//TODO: determine why undefined is in the data stream
data = data.replace("undefined","")
results = JSON.parse(data).results[0].count;
gu.clearPins();
if(results > 0){
gu.writePin(RED_PIN);
}
else{
gu.writePin(GREEN_PIN);
}
console.log("Results: " + results);
});
}).on('error', function(e) {
console.log("error: ", e);
gu.writePin(BLUE_PIN);
});
};
setStatusBeacon();
setInterval(setStatusBeacon, intervalInMills);