forked from trojanc/node-influx-uptimerobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (148 loc) · 5.59 KB
/
index.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const Influx = require('@influxdata/influxdb-client');
const http = require('https');
const moment = require('moment');
const fs = require('fs');
// Input parameters
let configFile = "./config.json";
let config = {};
// Check if a config file was specified
if (process.argv.length > 2) {
configFile = process.argv[2];
}
// Read the config file if it exists
if (fs.existsSync(configFile)) {
console.log("Loading config file: " + configFile);
config = require(configFile);
if (config.uptimerobot && config.uptimerobot.apiKey !== undefined) {
console.warn("uptimerobot.apiKey is deprecated, use uptimerobot.api_key instead");
config.uptimerobot.api_key = config.uptimerobot.apiKey;
}
} else {
console.log("Config file not found, depending on environment variables");
}
// Read environment variables
// Poor man's config setup
config.application = config.application || {};
config.uptimerobot = config.uptimerobot || {};
config.influx = config.influx || {};
if (process.env.APPLICATION_INTERVAL !== undefined) {
config.application.interval = process.env.APPLICATION_INTERVAL;
}
if (process.env.UPTIMEROBOT_API_KEY !== undefined) {
config.uptimerobot.api_key = process.env.UPTIMEROBOT_API_KEY;
}
if (process.env.UPTIMEROBOT_LOGS_LIMIT !== undefined) {
config.uptimerobot.logs_limit = process.env.UPTIMEROBOT_LOGS_LIMIT;
}
if (process.env.UPTIMEROBOT_RESPONSE_TIMES_LIMIT !== undefined) {
config.uptimerobot.response_times_limit = process.env.UPTIMEROBOT_RESPONSE_TIMES_LIMIT;
}
if (process.env.INFLUX_URL !== undefined) {
config.influx.url = process.env.INFLUX_URL;
}
if (process.env.INFLUX_ORG !== undefined) {
config.influx.org = process.env.INFLUX_ORG;
}
if (process.env.INFLUX_TOKEN !== undefined) {
config.influx.token = process.env.INFLUX_TOKEN;
}
if (process.env.INFLUX_BUCKET !== undefined) {
config.influx.bucket = process.env.INFLUX_BUCKET;
}
const url = config.influx.url
const token = config.influx.token
const influxdb = new Influx.InfluxDB({url, token}).getWriteApi(config.influx.org, config.influx.bucket, 'ms');
/**
* Gets the monitor data
* @returns {Promise<any>}
*/
function getMonitors() {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.uptimerobot.com',
port: 443,
path: '/v2/getMonitors',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const postData = {
response_times: "1",
response_times_limit: config.uptimerobot.response_times_limit,
response_times_average: "5",
timezone: "1",
format: "json",
logs: "1",
logs_limit: config.uptimerobot.logs_limit,
api_key: config.uptimerobot.api_key
};
const req = http.request(options, (response) => {
let objectString = "";
response.on('data', (chunk) => {
objectString += chunk;
});
response.on('end', () => {
const responseData = JSON.parse(objectString);
resolve(responseData.monitors);
});
});
req.write(JSON.stringify(postData));
req.end();
});
}
function processMonitors(monitors) {
console.log("Sending data to Influx")
if (monitors === undefined) {
console.warn("Could not find any monitors");
return;
}
monitors.forEach((monitor) => {
/*********************************************************************
* Response times
********************************************************************/
const responseTimes = monitor.response_times;
const responseTimePoints = [];
responseTimes.forEach(function (rt) {
const timestamp = moment.unix(rt.datetime);
responseTimePoints.push(
new Influx.Point()
.measurement('response_times')
.tag('monitorName', monitor.friendly_name)
.floatField('value', rt.value)
.timestamp(timestamp.valueOf())
);
});
influxdb.writePoints(responseTimePoints)
/*********************************************************************
* Monitor logs
********************************************************************/
const logs = monitor.logs;
const logTimePoints = [];
logs.forEach((log) => {
const timestamp = moment.unix(log.datetime);
logTimePoints.push(
new Influx.Point()
.measurement('logs')
.tag('monitorName', monitor.friendly_name)
.intField('logDatetime', timestamp.valueOf())
.stringField('type', log.type)
.stringField('reason', (log.reason.code === undefined || log.reason.code == null) ? "" : "" + log.reason.code)
.stringField('reason_detail', (log.reason.detail === undefined || log.reason.detail == null) ? "" : log.reason.detail)
.timestamp(Date.now())
);
});
influxdb.writePoints(logTimePoints)
});
}
getMonitors()
.then(processMonitors);
// If there is an interval configured, keep running at the interval
if (config.application.interval !== undefined && config.application.interval > 0) {
setInterval(() => {
getMonitors()
.then(processMonitors);
console.log("Waiting for " + config.application.interval + " seconds");
}, config.application.interval * 1000);
}