Last active
July 22, 2018 23:12
-
-
Save BrychanOdlum/8a459e0e59869204242432dfa789a60b to your computer and use it in GitHub Desktop.
Speedtester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const speedTest = require('speedtest-net'); | |
const mysql = require('mysql'); | |
const cron = require('node-cron'); | |
const ping = require('ping'); | |
const dns = require('dns'); | |
var pool = mysql.createPool({ | |
host: 'localhost', | |
user: 'USER', | |
password: 'PASSWORD', | |
database: 'speedtester' | |
}); | |
var cachedTarget = 'speedtest.net' | |
function testSpeeds() { | |
var test = speedTest({maxTime: 5000}); | |
test.on('data', data => { | |
cachedTarget = data.server.host | |
pool.getConnection(function(err, connection) { | |
if (err) throw err; | |
var values = {download: data.speeds.originalDownload, upload: data.speeds.originalUpload} | |
var sql = 'INSERT INTO speeds SET ?'; | |
connection.query(sql, values, function (err, res) { | |
connection.release(); | |
if (err) throw err; | |
console.log(`Download: ${values.download}, upload: ${values.upload}`) | |
}); | |
}); | |
}); | |
test.on('error', err => { | |
console.error(err); | |
pool.getConnection(function(err, connection) { | |
if (err) throw err; | |
var values = {download: null, upload: null} | |
var sql = 'INSERT INTO speeds SET ?'; | |
connection.query(sql, values, function (err, res) { | |
connection.release(); | |
if (err) throw err; | |
console.log(`Download: ${values.download}, upload: ${values.upload}`) | |
}); | |
}); | |
}); | |
} | |
function testPing() { | |
ping.promise.probe(cachedTarget, { | |
timeout: 10, | |
extra: ['-c', '2'], | |
}).then(function (pingRes) { | |
console.log(pingRes); | |
pool.getConnection(function(err, connection) { | |
if (err) throw err; | |
var values = {result: pingRes.min} | |
var sql = 'INSERT INTO pings SET ?'; | |
connection.query(sql, values, function (err, res) { | |
connection.release(); | |
if (err) throw err; | |
console.log(`Ping: ${values.result}`) | |
}); | |
}); | |
}); | |
} | |
function getPing(host, callback) { | |
ping.sys.probe(host, function(isAlive){ | |
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead'; | |
console.log(msg); | |
}, { timeout: 10 }); | |
} | |
cron.schedule('*/30 */1 * * * *', function(){ | |
console.log('running test'); | |
testPing(); | |
}); | |
cron.schedule('0 */5 * * * *', function(){ | |
console.log('running test'); | |
testSpeeds(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment