-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate monitor.sh #53
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fca00ad
refactor getMonitorUpdates to calculate updates from most recent mong…
haydenhw fa664aa
remove monitor.sh and all related code
haydenhw 348d8ab
replace aggregation with regualr mongo query in getMonitorUpdates; re…
haydenhw 78d8a09
rename updates -> routeCounts
haydenhw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,12 +55,28 @@ function MonitorApp ({ | |
return JSON.parse(value); | ||
} | ||
|
||
async function getMonitorUpdates() { | ||
return await Promise.all(exitnodeIPs.map(async ip => { | ||
let d = await getCacheData(`alive-${ip}`); | ||
d = d ? d : { error: util.noCheckInMessage(ip) }; | ||
d.ip = ip; | ||
return d; | ||
function countActiveRoutesByExitnode() { | ||
// Report a count of unique nodes and gateways connected to exitnodes | ||
// If an exitnode has not checked in in the last two minutes, report an error message | ||
return Promise.all(exitnodeIPs.map(async ip => { | ||
const twoMinutesAgo = new Date(new Date() - 2 * 60 * 1000); | ||
const [mostRecentLog] = await db.collection('routeLog') | ||
.find({ | ||
exitnodeIP: ip, | ||
timestamp: { $gt: twoMinutesAgo } | ||
}) | ||
.sort({ timestamp: -1 }) | ||
.limit(1) | ||
.toArray(); | ||
|
||
if (!mostRecentLog) | ||
return { error: util.noCheckInMessage(ip) }; | ||
|
||
return { | ||
exitnodeIP: ip, | ||
numberOfRoutes: _.unique(mostRecentLog.routes.map(r => r.nodeIP)).length, | ||
numberOfGateways: _.unique(mostRecentLog.routes.map(r => r.gatewayIP)).length | ||
}; | ||
})); | ||
} | ||
|
||
|
@@ -87,8 +103,8 @@ function MonitorApp ({ | |
|
||
// Home Page | ||
app.get('/', asyncMiddleware(async function(req, res, next) { | ||
let updates = await getMonitorUpdates(); | ||
let exitnodes = await getRoutingTableUpdates(); | ||
const routeCounts = await countActiveRoutesByExitnode(); | ||
const exitnodes = await getRoutingTableUpdates(); | ||
|
||
exitnodes.forEach(exitnode => { | ||
if (exitnode.routingTable) { | ||
|
@@ -105,7 +121,7 @@ function MonitorApp ({ | |
}); | ||
|
||
res.render('index', { | ||
updates: updates, | ||
routeCounts, | ||
nodes: exitnodes, | ||
timeAgo: util.timeAgo | ||
}); | ||
|
@@ -116,26 +132,9 @@ function MonitorApp ({ | |
// | ||
|
||
app.get('/api/v0/monitor', asyncMiddleware(async function(req, res, next) { | ||
res.json(await getMonitorUpdates()); | ||
res.json(await countActiveRoutesByExitnode()); | ||
})); | ||
|
||
app.post('/api/v0/monitor', ipAuthMiddleware(exitnodeIPs), function(req, res) { | ||
let ip = util.getRequestIP(req); | ||
const key = `alive-${ip}`; | ||
let handleErr = function(err) { | ||
if (err) { | ||
return res.status(502).json({ error: 'Could not set key, because of [' + err + '].' }); | ||
} | ||
return res.json({ message: 'Set attached values', result: processed }); | ||
}; | ||
const processed = util.processUpdate(req); | ||
if (processed.error) { | ||
return res.status(400).json(processed); | ||
} else { | ||
mjs.set(key, JSON.stringify(processed), {expires: 120}, handleErr); | ||
} | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yessssssssssss :-) |
||
app.get('/api/v0/nodes', asyncMiddleware(async function(req, res) { | ||
let data = await getRoutingTableUpdates(); | ||
res.json(data); | ||
|
@@ -214,7 +213,7 @@ function MonitorApp ({ | |
mjs.set(key, JSON.stringify(newRoutes), {}, handleErr); | ||
|
||
})); | ||
|
||
app.get('/api/v0/numNodesTimeseries', asyncMiddleware(async function(req, res) { | ||
let now = new Date(); | ||
let yesterday = new Date(now - 1000 * 60 * 60 * 24); | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,6 @@ if (require.main === module) { | |
|
||
async function main() { | ||
await Promise.all([ | ||
simulateMonitorRequest({"numberOfGateways": 15, "numberOfRoutes": 21}, exitnodeIPs[0]), | ||
simulateMonitorRequest({"numberOfGateways": 15, "numberOfRoutes": 29}, exitnodeIPs[1]), | ||
simulateRoutingTableRequest('134.0.0.0/26,134.0.0.1|132.0.0.0/26,132.0.0.1|131.0.0.0/26,131.0.0.1|129.0.0.0/26,129.0.0.1|127.0.0.0/26,127.0.0.1|128.0.0.0/26,128.0.0.1|127.0.0.10/26,127.0.0.1|', exitnodeIPs[0]), | ||
simulateRoutingTableRequest('134.0.0.0/26,134.0.0.1|132.0.0.0/26,132.0.0.1|131.0.0.0/26,131.0.0.1|125.0.0.0/26,124.0.0.1|121.0.0.0/26,121.0.0.1|123.0.0.0/26,123.0.0.1', exitnodeIPs[1]), | ||
simulateRoutingTableRequest('', exitnodeIPs[2]) // simulate an empty POST | ||
|
@@ -27,8 +25,6 @@ async function main() { | |
setTimeout(async () => { | ||
// omit one routing table request to simulate a delay between posts | ||
await Promise.all([ | ||
simulateMonitorRequest({"numberOfGateways": 15, "numberOfRoutes": 21}, exitnodeIPs[0]), | ||
simulateMonitorRequest({"numberOfGateways": 15, "numberOfRoutes": 29}, exitnodeIPs[1]), | ||
simulateRoutingTableRequest('134.0.0.0/26,134.0.0.1|132.0.0.0/26,132.0.0.1|131.0.0.0/26,131.0.0.1|129.0.0.0/26,129.0.0.1|127.0.0.0/26,127.0.0.1|128.0.0.0/26,128.0.0.1|127.0.0.10/26,127.0.0.1|', exitnodeIPs[0]), | ||
simulateRoutingTableRequest('', exitnodeIPs[2]) // simulate an empty POST | ||
]) | ||
|
@@ -37,36 +33,6 @@ async function main() { | |
}) | ||
} | ||
|
||
async function simulateMonitorRequest(data, exitnodeIP) { | ||
const response = await monitorRequest(data, exitnodeIP) | ||
switch (response.statusCode) { | ||
case 200: | ||
console.info(`Successful /monitor request`) | ||
break; | ||
default: | ||
throw new Error(`Unexpected statusCode from simulated monitor request: ${response.statusCode}`) | ||
} | ||
} | ||
|
||
async function monitorRequest(data, exitnodeIP) { | ||
return new Promise((resolve, reject) => { | ||
var options = { | ||
url: `http://localhost:${process.env.PORT}/api/v0/monitor`, | ||
// the server authenticates the request by looking at | ||
// its source ip or x-forwarded-for header | ||
headers: { | ||
'x-forwarded-for': exitnodeIP, | ||
'content-type': 'application/json' | ||
}, | ||
body: (typeof data === 'object') ? JSON.stringify(data) : data | ||
}; | ||
request.post(options, (error, response) => { | ||
if (error) return reject(error) | ||
return resolve(response) | ||
}) | ||
}) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yessssssssssss. |
||
async function simulateRoutingTableRequest(body, exitnodeIP) { | ||
const response = await routingTableRequest(body, exitnodeIP) | ||
switch (response.statusCode) { | ||
|
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yesssssssssssss