/
kubernetes_scheduler.js
261 lines (246 loc) · 7.29 KB
/
kubernetes_scheduler.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* This driver extract kubernetes Scheduler metrics
* Communication protocol is http
* dynamically create metrics for Kubernetes Scheduler depending from response of the service
* communicate with http api using a jwt token generated with this command:
* # kubectl -n kube-system create token default --duration=8760h
* this command returns a jwt token valid for a year for kube-system user
* tested with minikube version: v1.28.0 under Ubuntu 22.04.1 LTS
*/
var _var = D.device.createVariable;
var token = D.device.password();
var port = 80;
/**
*
* @returns promise for http response body containig Kubernetes API metrics in PROMETHEUS format
*/
function getMetrics() {
var d = D.q.defer();
D.device.http.get({
url: "/metrics",
port: port,
headers: {
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
}
}, function (err, response, body) {
if (err) {
console.error(err);
D.failure(D.errorType.GENERIC_ERROR);
}
if(response.statusCode == 404){
D.failure(D.errorType.RESOURCE_UNAVAILABLE);
}
if(response.statusCode == 401){
D.failure(D.errorType.AUTHENTICATION_ERROR);
}
if(response.statusCode != 200){
D.failure(D.errorType.GENERIC_ERROR);
}
d.resolve(body);
});
return d.promise;
}
/**
*
* @param {string} body containing metrics api response in PROMETHEUS format
* @returns convert PROMETHEUS format to Javascript object
*/
function convert(body) {
var lines = body.split("\n");
var result = lines
.filter(function (line) { return line.indexOf("#") != 0 && line; })
.map(function (line) {
var matches = line.match(/^([^\{]*)(\{(.*)\})? (.*)$/);
var key = matches[1];
var desc = matches[3] ? matches[3].split(",").reduce(function (a, b) {
var keyValue = b.split("=");
a[keyValue[0]] = keyValue[1].substring(1, keyValue[1].length - 1);
return a;
}, {}) : null;
var value = matches[4];
return { key: key, desc: desc, value: value };
});
return result;
}
/**
*
* @param {string} key parameter key
* @param {string} statusFirstNumber http status prefix
* @returns value
*/
function statusFilter(key, statusFirstNumber) {
return function (data) {
return data.filter(function (d) {
return d.key == key && d.desc != null && d.desc.code && d.desc.code.indexOf(statusFirstNumber) == 0;
}).map(function (d) { return parseFloat(d.value); });
};
}
/**
*
* @param {[number]} data
* @returns sum of array
*/
function sum(data) {
if (!data.length) return null;
return data.reduce(function (a, b) { return a + b; }, 0);
}
/**
*
* @param {string} key parameter key
* @param {function} filterFn filter function
* @returns sum of returned data based on the key and the filter function
*/
function getValueSum(key, filterFn){
return function(data){
return data
.filter(function(d){return d.key == key && (filterFn ? filterFn(d) : true);})
.map(function(d){return parseFloat(d.value);})
.reduce(function(a,b){return a + b;}, 0);
};
}
// config variable containing the list of monitoring parameters
var config = [
{
uid: "rest_client_requests_total_200",
label: "HTTP requests: 2xx",
execute: [statusFilter("rest_client_requests_total","2"), sum],
type: D.valueType.RATE
},
{
uid: "rest_client_requests_total_300",
label: "HTTP requests: 3xx",
execute: [statusFilter("rest_client_requests_total","3"), sum],
type: D.valueType.RATE
},
{
uid: "rest_client_requests_total_400",
label: "HTTP requests: 4xx",
execute: [statusFilter("rest_client_requests_total","4"), sum],
type: D.valueType.RATE
},
{
uid: "rest_client_requests_total_500",
label: "HTTP requests: 5xx",
execute: [statusFilter("rest_client_requests_total","5"), sum],
type: D.valueType.RATE
},
{
uid: "cpu",
label: "CPU",
execute: [getValueSum("process_cpu_seconds_total"), multiplier(100)],
type: D.valueType.RATE
},
{
uid: "max_fds",
label: "Fds max",
execute: getValueSum("process_max_fds")
},
{
uid: "open_fds",
label: "Fds open",
execute: getValueSum("process_open_fds")
},
{
uid: "go_goroutines",
label: "Goroutines",
execute: getValueSum("go_goroutines")
},
{
uid: "go_threads",
label: "Go threads",
execute: getValueSum("go_threads")
},
{
uid: "process_resident_memory_bytes",
label: "Resident memory",
execute: getValueSum("process_resident_memory_bytes"),
unit: "B"
},
{
uid: "process_virtual_memory_bytes",
label: "Virtual memory,",
execute: getValueSum("process_virtual_memory_bytes"),
type: D.valueType.RATE,
unit: "B"
},
{
uid: "scheduler_schedule_attempts_error",
label: "Schedule attempts: error",
execute: getValueSum("scheduler_schedule_attempts_total", function(d){return d.desc.result == "error";}),
type: D.valueType.RATE,
},
{
uid: "scheduler_schedule_attempts_scheduled",
label: "Schedule attempts: scheduled",
execute: getValueSum("scheduler_schedule_attempts_total", function(d){return d.desc.result == "scheduled";}),
type: D.valueType.RATE,
},
{
uid: "scheduler_schedule_attempts_unschedulable",
label: "Schedule attempts: unschedulable",
execute: getValueSum("scheduler_schedule_attempts_total", function(d){return d.desc.result == "unschedulable";}),
type: D.valueType.RATE,
}
];
/**
*
* @param {number} number multiplier
* @returns the value muliplied by the number
*/
function multiplier(number){
return function(value){
return value*number;
};
}
/**
*
* @param {[object]} data array of objects
* @returns list of domotz variables
*/
function extract(data) {
return config.map(function (c) {
var result;
if (Array.isArray(c.execute)) {
result = c.execute.reduce(function (a, b) { return b(a); }, data);
} else if (typeof (c.execute) == "function") {
result = c.execute(data);
}
if(result){
return _var(c.uid, c.label, result, c.unit, c.type);
}else{
return null;
}
}).filter(function(v){
return v != null;
});
}
/**
* @remote_procedure
* @label Validate Association
* @documentation This procedure is used to validate if the metrics api is accessible
*/
function validate() {
getMetrics()
.then(function(){
D.success();
});
}
function success(vars){
D.success(vars);
}
/**
* @remote_procedure
* @label Get Device Variables
* @documentation This procedure is used for retrieving device * variables data
*/
function get_status() {
getMetrics()
.then(convert)
.then(extract)
.then(success)
.catch(function(err){
console.error(err);
D.failure(D.errorType.GENERIC_ERROR);
});
}