Skip to content

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
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
"bindAddress": "0.0.0.0",
"developerShare": 1,
"daemonAddress": "127.0.0.1:18081",
"httpEnable": false,
"httpAddress": "127.0.0.1",
"httpPort": "8080",
"coinSettings": {
"xmr":{
"minDiff": 100,
Expand Down
82 changes: 82 additions & 0 deletions index.html
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>
43 changes: 40 additions & 3 deletions proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -578,7 +579,13 @@ function balanceWorkers(){
}
}

function enumerateWorkerStats(){
function enumerateWorkerStats() {
// here we do a bit of a hack and "cache" the activeWorkers
Copy link

@Ethorsen Ethorsen Mar 9, 2018

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

Copy link
Author

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."

// 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)){
Expand Down Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also add this.login here?

};
};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1183,4 +1218,6 @@ if (cluster.isMaster) {
}, 10000);
setInterval(checkActivePools, 90000);
activatePorts();
if(global.config.httpEnable)
activateHTTP();
}