-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdm_data.js
67 lines (61 loc) · 1.7 KB
/
hdm_data.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
module.exports = {
get_hdm_data: function(callback) {
var oauth = require('./oauth.js');
var https = require('https');
var nbi_label = "nbi";
var nbi_host = process.env[nbi_label+"_IP_ADDRESS"];
var nbi_port = process.env[nbi_label+"_TCP_9999_PORT"];
var product_id = process.env.CAF_SYSTEM_PRODUCT_ID;
var serial_id = process.env.CAF_SYSTEM_SERIAL_ID;
var device_id = product_id+":"+serial_id;
oauth.get_oauth(function(oauth_token) {
console.log('oauth in getdata:');
console.log(oauth_token);
var hdm_data_type = ["sh proc cpu sorted", "sh memory statistics"];
for(var i=0; i<hdm_data_type.length; i++) {
var payload = hdm_data_type[i];
var header = {
"Content-Type": "text/plain",
"Authorization": "Bearer "+oauth_token,
'Content-Length': Buffer.byteLength(payload)
};
var options = {
host: nbi_host,
port: nbi_port,
path: '/api/v1/mw/hdmrpc/showcmd',
method: 'POST',
rejectUnauthorized: false,
headers: header
};
var req = https.request(options, (res) => {
console.log('Get data statusCode:', res.statusCode);
var body = '';
res.on('data', (d) => {
body += d.toString('utf8');
});
res.on('end', () => {
console.log('No more data in postdata-api response.');
if(res.statusCode===200) {
body = JSON.parse(body)['output'].split('\r\n');
if(body[1].startsWith('CPU')) {
body = body.slice(1,2);
}
else {
body = body.slice(1,4);
}
}
else {
body = JSON.parse(body);
}
callback(body);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(payload);
req.end();
}
});
}
}