You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.4 KiB

4 years ago
function getArgs () {
const args = {};
process.argv
.slice(2, process.argv.length)
.forEach( arg => {
// long arg
if (arg.slice(0,2) === '--') {
const longArg = arg.split('=');
const longArgFlag = longArg[0].slice(2,longArg[0].length);
const longArgValue = longArg.length > 1 ? longArg[1] : true;
args[longArgFlag] = longArgValue;
}
// flags
else if (arg[0] === '-') {
const flags = arg.slice(1,arg.length).split('');
flags.forEach(flag => {
args[flag] = true;
});
}
});
return args;
}
const args = getArgs();
4 years ago
function mysearch(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].name === nameKey) {
return myArray[i];
}
}
}
4 years ago
const { spawn, exec, execSync } = require('child_process')
4 years ago
//console.log(args)
4 years ago
// #!/bin/bash
//logs=$("/usr/local/bin/wg-json")
//echo $logs
4 years ago
//node ./venus-stats.js --insert=yes --port=30001 --ip='127.0.0.1' --user=swarmlab --password=swarmlab --db=venus --coll=logs --json='{ "name" : "Hello", "log" : "World" }'
if (args["insert"] == 'yes'){
try{
var dbuser = args["user"]
var dbpassword = args["password"]
var dbName = args["db"]
var collName = args["coll"]
var dbNamePort = args["port"]
var dbNameIP = args["ip"]
4 years ago
var dbdate = args["json"]
4 years ago
var stackid = args["stackid"]
var stackinterface = args["stackinterface"]
var stackpeer = args["stackpeer"]
var devname = args["devname"]
4 years ago
var showexec ='/usr/local/bin/wg-json';
try {
const stdout = execSync(showexec, {
env: {
NODE_ENV: "production",
},
});
4 years ago
var datajsontmp = stdout.toString()
4 years ago
var datajson = mysearch(devname, Object.values(datajsontmp));
4 years ago
console.error('exec ' + JSON.stringify(datajson));
} catch (error) {
var datajson = error.toString()
console.error('error ' + JSON.stringify(datajson));
}
var RESlog = new Object();
RESlog.date = dbdate
RESlog.log = datajson
4 years ago
RESlog.stackid = stackid
RESlog.stackinterface = stackinterface
RESlog.stackpeer = stackpeer
RESlog.devname = devname
4 years ago
4 years ago
var MongoClient = require('mongodb').MongoClient;
var mongourl = `mongodb://${dbuser}:${dbpassword}@${dbNameIP}:${dbNamePort}`;
const OPTS = {
useNewUrlParser: true,
useUnifiedTopology: true
};
MongoClient.connect(mongourl, OPTS, function(err, db) {
if(err){
console.log(err);
} else {
var dbo = db.db(dbName);
dbo.listCollections({name: collName})
.next(function(err, collinfo) {
if (collinfo) {
}else{
dbo.createCollection(collName, {
capped: true,
size: 100000
});
}
});
4 years ago
dbo.collection(collName).insertOne(RESlog, function(err, res) {
4 years ago
if(err){
console.log(err);
} else {
console.log("ok");
db.close();
}
});
}
});
} catch (err) {
console.error(err);
}
}