forked from delanohelio/autoscaling-rancher-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
230 lines (198 loc) · 6.54 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var grafana = require('./grafana')
var express = require('express');
const request = require('superagent');
var app = express();
app.use(express.json())
const URL_API_RANCHER = "http://192.168.99.100:8080/";
const URL_API_RANCHER_V1 = URL_API_RANCHER + "v1/";
const URL_API_RANCHER_V2 = URL_API_RANCHER + "v2-beta/";
const API_RANCHER = URL_API_RANCHER_V1;
const API_RANCHER_WEBHOOK = URL_API_RANCHER + "v1-webhooks/";
const PREFIX_LABEL = "scale.";
var APIKEYS = {"username": "1C68819471683113BCDD", "password": "ngGEjcTVtwbBrYzUgrH7wSREoXaYXRrLqYsC3Kws"}
var CONFIG = {"domain": "http://10.0.2.2:3000"}
var HOSTS = {};
var SERVICES = {};
var WEBHOOKS = {};
var ALERTS = [];
var STACKS = {}
grafana.setWebhookURL(CONFIG["domain"] + "/webhook");
app.post('/apikeys', function (req, res) {
APIKEYS = {"username": req.body.username, "password": req.body.password};
res.send(APIKEYS)
});
app.post('/config', function (req, res) {
CONFIG = req.body
grafana.setWebhookURL(CONFIG["domain"] + "/webhook");
res.send(CONFIG)
});
app.get('/', function (req, res) {
res.send({"stacks": STACKS, "services": SERVICES, "webhooks": WEBHOOKS})
});
app.get('/update', function (req, res) {
stackUpdate((r1) => {
serviceUpdate((r2) => {
webHookUpdate();
alertsUpdate();
})
})
res.send()
});
app.get('/webhook', function (req, res) {
res.send(WEBHOOKS)
});
app.post('/webhook', function (req, res) {
console.log(req.body)
var ruleName = req.body.ruleName.split("/")
var stack_name = ruleName[0]
var service_name = ruleName[1]
for(var s in SERVICES) {
var service = SERVICES[s]
if (service["name"] == service_name && STACKS[service["stack"]] == stack_name) {
request.post(WEBHOOKS[s]).then((result) => {
res.send()
})
}
}
});
app.get('/webhook/update', function (req, res) {
webHookUpdate()
res.send()
});
/*
app.get('/host', function (req, res) {
request.get(API_RANCHER + "hosts").then((result) => {
var hosts_data = {};
for(var i in result.body.data) {
var host = result.body.data[i];
var host_data = {};
for (label in host.data.fields.labels) {
if (label.startsWith(PREFIX_LABEL)) {
host_data[label] = host.data.fields.labels[label]
}
}
if(Object.keys(host_data).length !== 0) {
host_data["name"] = host.data.fields.hostname
host_data["accountId"] = host.accountId
hosts_data[host.id] = host_data
}
}
HOSTS = hosts_data;
res.send(HOSTS)
})
});
*/
app.get('/service', function (req, res) {
var response = JSON.parse(JSON.stringify(SERVICES))
for(var s in SERVICES) {
response[s]["stack"] = STACKS[response[s]["stack"]]
response[s]["url"] = WEBHOOKS[s]
}
res.send(response)
});
app.get('/service/update', function (req, res) {
serviceUpdate((result => {
res.send(result)
}))
});
app.get('/stack', function (req, res) {
res.send(STACKS)
});
app.get('/stack/update', function (req, res) {
stackUpdate((result) => {
res.send(result)
})
});
function serviceUpdate(callback) {
request.get(API_RANCHER + "services").then((result) => {
var services_data = {}
for(var i in result.body.data) {
var service = result.body.data[i]
var name = service.name
if(service.description != null) {
try {
service_data = JSON.parse(service.description)
service_data["name"] = name
service_data["accountId"] = service.accountId
service_data["stack"] = service.environmentId
services_data[service.id] = service_data
} catch (e) {
console.log(e)
}
}
}
SERVICES = services_data;
if(callback){callback(SERVICES)}
})
}
function stackUpdate(callback) {
request.get(API_RANCHER + "environment").then((result) => {
var stacks_data = {}
for(var i in result.body.data) {
var stack = result.body.data[i]
stacks_data[stack.id] = stack.name
}
STACKS = stacks_data;
if(callback){callback(STACKS)}
})
}
function webHookUpdate() {
request.get(API_RANCHER_WEBHOOK + "receivers").query({"projectId": "1a5"}).then((result) => {
//console.log(result.body.data)
WEBHOOKS = {}
for(var i in result.body.data) {
var webhook = result.body.data[i]
if(webhook.driver == "scaleService"){
WEBHOOKS[webhook.scaleServiceConfig.serviceId] = webhook.url
}
}
for (var s in SERVICES) {
var service = SERVICES[s]
if(!(s in WEBHOOKS)) {
// CREATE
var data = {"driver": "scaleService", "name": "scale.service."+service["name"], "scaleServiceConfig": {
"action": "up",
"amount": service["scale.up"],
"max": 100,
"min": 1,
"serviceId": s,
"type": "scaleService",
}}
request.post(API_RANCHER_WEBHOOK + "receivers").query({"projectId": "1a5"}).send(data).then((result) => {
WEBHOOKS[result.body.scaleServiceConfig.serviceId] = result.body.url
});
}
}
});
}
function alertsUpdate(){
grafana.getAlerts((result) => {
ALERTS = result
var new_alerts = []
for(var s in SERVICES) {
var service = SERVICES[s]
var stack_service_name = STACKS[service["stack"]] + "/" + service["name"]
var create = true
for(var i in ALERTS) {
var alert = ALERTS[i]
if(alert.service == stack_service_name) {
create = false;
break;
}
}
if(create) {
new_alerts.push({"service": stack_service_name, "metric":"cpu_usage_total" , "threshould": service["scale.up.cpu"]})
}
}
if(new_alerts.length > 0) {
grafana.createAlerts(new_alerts, (result) => {
console.log(result)
}, error => {
console.log(error)
})
}
});
}
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});