-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (64 loc) · 2.84 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
#!/usr/bin/env node
var dgram = require('dgram');
var getAuth = require('./getAuth');
var argv = require('minimist')(process.argv.slice(2));
var timeOut = argv.timeOut || 120 ;
argv.apiUrl = argv.apiUrl || 'http://127.0.0.1/api';
argv.apiUser = argv.apiUser || 'username';
argv.apiPass = argv.apiPass || 'password';
argv.apiType = argv.apiType || 'local';
argv.url = argv.apiUrl + '/v2/statistic/data';
argv.apiSess = argv.apiSess || argv.apiUrl + '/v2/session';
argv.type = argv.type || [ "total_pps","calls_pps","regs_pps" ];
argv.udp = argv.udp || false;
argv.backend = argv.backend || 'cassandra';
var request = require('request');
var homercookie = request.cookie("PCAPTURESESSION="+Math.random().toString(36).slice(2)+";path=/");
argv.jar = request.jar();
argv.jar.setCookie(homercookie, argv.apiSess, function(error, cookie) {});
argv.auth = JSON.stringify({ "username": argv.apiUser, "password": argv.apiPass, auth_type: argv.apiType });
var scheduleAuth = function(){
getAuth(argv);
setInterval(function() {
getAuth(argv);
}, timeOut*1000 );
}
if(argv.udp) {
var UDP_HOST = argv.udp.split(':')[0];
var UDP_PORT = argv.udp.split(':')[1];
var client = dgram.createSocket('udp4');
}
var sendUDP = function (message) {
if (argv.debug) console.log('Sending Message',message,HOST,PORT);
client.send(message, 0, message.length, UDP_PORT, UDP_HOST, function(err, bytes) {
if (err) { throw err; client.close(); }
});
}
var fromts,tots,params;
if (argv.backend == 'cassandra'){
params = JSON.stringify( {"timestamp":{"from":fromts,"to":tots},"param":{"filter":["@filters"],"limit":500,"total":argv.total||false,"query":[{"main":"hepic_statistics_all","database":"hepic","type":argv.type,"tag":[]}],"precision":60,"bfrom":fromts}} );
} else {
params = JSON.stringify({"timestamp":{"from":fromts,"to":tots},"param":{"filter":["@filters"],"limit":500,"total":argv.total||false,"query":[{"main":"proto","type":["hepall"],"tag":[]}],"precision":60}});
}
scheduleAuth();
setInterval(function () {
tots = new Date().getTime() - (argv.interval || 60000);
fromts = tots - (argv.interval || 60000);
params = JSON.stringify( {"timestamp":{"from":fromts,"to":tots},"param":{"filter":["@filters"],"limit":500,"total":argv.total||false,"query":[{"main":"hepic_statistics_all","database":"hepic","type":argv.type,"tag":[]}],"precision":60,"bfrom":fromts}} );
console.log('GET:',argv.url, fromts, tots);
request({
uri: argv.url,
method: "POST",
headers: { "Content-Type": "application/json" },
form: params,
jar: argv.jar
}, function(error, response, body) {
console.log(body);
var data = JSON.parse(body).data[0];
data.forEach(function(row){
if (argv.debug) console.log(row);
if (argv.udp) { sendUDP(JSON.stringify(row)); return; }
});
});
}, argv.interval || 60000)
console.warn('PPS/CPU Counter started...', argv)