-
Notifications
You must be signed in to change notification settings - Fork 213
Added a simple http+json server option for monitoring purposes #111
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
Open
mike-teehan
wants to merge
3
commits into
Snipa22:master
Choose a base branch
from
mike-teehan:http+json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>XNP Hashrate Monitor</title> | ||
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css"> | ||
</head> | ||
<body> | ||
<table id="displaytable" width="100%" class="display" cellspacing="0"><tfoot><td></td><td></td><td></td></tfoot><table> | ||
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | ||
<script src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script> | ||
<script> | ||
(($) => { | ||
|
||
var $disp = $('#displaytable').DataTable({ | ||
pageLength: 50, | ||
ajax: { | ||
url: "/json", | ||
type: "GET", | ||
dataSrc: parseJSON | ||
}, | ||
columns: [ | ||
{ title: "Name", data: "name" }, | ||
{ title: "Hashrate", data: "hashrate" }, | ||
{ title: "Difficulty", data: "diff" } | ||
], | ||
footerCallback: function(row, data, start, end, display) { | ||
var api = this.api(), data; | ||
|
||
// converting to interger to find total | ||
var intVal = function (i) { | ||
return typeof i === 'string' ? | ||
i.replace(/[\$,]/g, '')*1 : | ||
typeof i === 'number' ? | ||
i : 0; | ||
}; | ||
|
||
var minerCount = api.column(0).data().length; | ||
|
||
var hashTotal = api | ||
.column(1) | ||
.data() | ||
.reduce( function (a, b) { | ||
return intVal(a) + intVal(b); | ||
}, 0); | ||
|
||
var totDiff = api | ||
.column(2) | ||
.data() | ||
.reduce( function (a, b) { | ||
return intVal(a) + intVal(b); | ||
}, 0); | ||
var avgDiff = Math.floor(totDiff / api.column(2).data().length); | ||
|
||
// Update footer by showing the total with the reference of the column index | ||
$(api.column(0).footer()).html("Count: " + minerCount); | ||
$(api.column(1).footer()).html("Total: " + hashTotal); | ||
$(api.column(2).footer()).html("Avg: " + avgDiff); | ||
} | ||
}) | ||
setInterval(() => { $disp.ajax.reload(); }, 15000); | ||
|
||
function parseJSON(json) { | ||
var res = []; | ||
var worker, miner, row, name; | ||
for(var workerid in json) { | ||
worker = json[workerid]; | ||
if(worker.length == 0) continue; | ||
for(var minerid in worker) { | ||
miner = worker[minerid]; | ||
if(miner.length == 0) continue; | ||
name = (miner["password"].length > 0) ? miner["password"] : miner["id"]; | ||
row = { name: name, hashrate: miner["avgSpeed"], diff: miner["diff"] } | ||
res.push(row); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
})(jQuery) | ||
</script> | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
const cluster = require('cluster'); | ||
const net = require('net'); | ||
const tls = require('tls'); | ||
const http = require('http'); | ||
const fs = require('fs'); | ||
const async = require('async'); | ||
const uuidV4 = require('uuid/v4'); | ||
|
@@ -578,7 +579,13 @@ function balanceWorkers(){ | |
} | ||
} | ||
|
||
function enumerateWorkerStats(){ | ||
function enumerateWorkerStats() { | ||
// here we do a bit of a hack and "cache" the activeWorkers | ||
// this file is parsed for the http://host/json endpoint | ||
fs.writeFile("workers.json", JSON.stringify(activeWorkers), function(err) { | ||
if(err) | ||
return console.log(err); | ||
}); | ||
let stats, global_stats = {miners: 0, hashes: 0, hashRate: 0, diff: 0}; | ||
for (let poolID in activeWorkers){ | ||
if (activeWorkers.hasOwnProperty(poolID)){ | ||
|
@@ -803,7 +810,8 @@ function Miner(id, params, ip, pushMessage, portData, minerSocket) { | |
lastShare: this.lastShareTime, | ||
coin: this.coin, | ||
pool: this.pool, | ||
id: this.id | ||
id: this.id, | ||
password: this.password | ||
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. Should also add this.login here? |
||
}; | ||
}; | ||
|
||
|
@@ -952,6 +960,33 @@ function handleMinerData(method, params, ip, portData, sendReply, pushMessage, m | |
} | ||
} | ||
|
||
function activateHTTP() { | ||
var jsonServer = http.createServer((req, res) => { | ||
if(req.url == "/") { | ||
res.writeHead(200, {'Content-type':'text/html'}); | ||
fs.readFile('index.html', 'utf8', function(err, contents) { | ||
res.write(contents); | ||
res.end(); | ||
}) | ||
} else if(req.url.substring(0, 5) == "/json") { | ||
fs.readFile('workers.json', 'utf8', (err, data) => { | ||
if(err) { | ||
res.writeHead(503); | ||
} else { | ||
res.writeHead(200, {'Content-type':'application/json'}); | ||
res.write(data + "\r\n"); | ||
} | ||
res.end(); | ||
}); | ||
} else { | ||
res.writeHead(404); | ||
res.end(); | ||
} | ||
}); | ||
|
||
jsonServer.listen(global.config.httpPort || "8080", global.config.httpAddress || "localhost") | ||
} | ||
|
||
function activatePorts() { | ||
/* | ||
Reads the current open ports, and then activates any that aren't active yet | ||
|
@@ -990,7 +1025,7 @@ function activatePorts() { | |
socket.write(sendData); | ||
}; | ||
handleMinerData(jsonData.method, jsonData.params, socket.remoteAddress, portData, sendReply, pushMessage, minerSocket); | ||
}; | ||
}; | ||
|
||
function socketConn(socket) { | ||
socket.setKeepAlive(true); | ||
|
@@ -1183,4 +1218,6 @@ if (cluster.isMaster) { | |
}, 10000); | ||
setInterval(checkActivePools, 90000); | ||
activatePorts(); | ||
if(global.config.httpEnable) | ||
activateHTTP(); | ||
} |
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.
Should check for global.config.httpEnable before dumping to disk. Maybe even start a different timeout function and not re-use enumerateWorkerStats
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.
I don't know where else in the app loop "activeWorkers" is properly defined, and I haven't tried too hard to figure it out, hence the "hack."