Commit dac7026e authored by Ahmad Nemati's avatar Ahmad Nemati

jkjk

parent 32b57214
...@@ -5,7 +5,9 @@ const shell = require('shelljs') ...@@ -5,7 +5,9 @@ const shell = require('shelljs')
const cron = require('node-cron'); const cron = require('node-cron');
const cronRemover = require('node-cron'); const cronRemover = require('node-cron');
const fs = require('fs') const fs = require('fs')
const cronSpeed = require('node-cron');
const shellExec = require('shell-exec')
var decimalPointRegex = /\-?\d+\.\d+/g;
app.get('/', (req, res) => { app.get('/', (req, res) => {
res.send('Hello World!') res.send('Hello World!')
run() run()
...@@ -113,3 +115,53 @@ function getIp() { ...@@ -113,3 +115,53 @@ function getIp() {
}); });
} }
async function calculateSpeed() {
let d = await shellExec('vnstat -tr')
d = d.stdout
d = d.match(decimalPointRegex)
if (d.includes('kbit/s')) {
d[0] = 1
d[1] = 1
} else if (d.includes('Mbit/s')) {
} else if (d.includes('Gbit/s')) {
d[0] = d[0] * 1000
d[1] = d[1] * 1000
} else if (d.includes('bit/s')) {
d[0] = 1
d[1] = 1
}
await sendRate(d[0], d[1])
}
function sendRate(rx, tx) {
const request = require('request');
let url = 'http://admin.fcfglobal.co:3000/rate/' + rx + '/' + tx
return new Promise(function (resolve, reject) {
request(url, {timeout: 6000}, function (error, res, body) {
resolve(true)
});
});
}
cronSpeed.schedule(' */5 * * * * *', () => {
return calculateSpeed()
});
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"lodash": "^4.17.15", "lodash": "^4.17.15",
"node-cron": "^2.0.3", "node-cron": "^2.0.3",
"request": "^2.88.0", "request": "^2.88.0",
"shell-exec": "^1.0.2",
"shelljs": "^0.8.3" "shelljs": "^0.8.3"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment