forked from BlockchainWorks/ethmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
245 lines (210 loc) · 7.16 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var express_logger = require('morgan');
var routes = require('./routes/index');
var miners = require('./routes/miners');
var app = express();
// View engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// Uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(express_logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
// Make miner data accessible to the router
app.use(function(req, res, next) {
req.json = {
"title" : config.title,
"animation" : config.animation,
"header" : config.header ? config.header : config.title,
"miners" : miners.json,
"refresh" : config.web_refresh,
"tolerance" : config.tolerance,
"temperature" : config.temperature,
"hashrates" : config.hashrates,
"updated" : moment().format("YYYY-MM-DD HH:mm:ss")
};
next();
});
app.use('/', routes);
app.use('/miners', miners);
// Catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// Error handlers
// Development error handler will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// Production error handler, no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
// --------------- BOOT ---------------
var config = require('./config.json');
var log4js = require('log4js');
var logger = log4js.getLogger();
logger.setLevel(config.log_level ? config.log_level : 'INFO');
logger.warn('app: booting');
// --------------- /BOOT ---------------
// --------------- REQUESTER ---------------
var net = require('net');
var moment = require('moment');
require("moment-duration-format");
var miners = [];
miners.json = [];
logger.info('config: ' + config.miners.length + ' rig(s) configured');
config.miners.forEach(function(item, i, arr) {
logger.trace(item.name + ': config[' + i + ']');
// settings
var m = miners[i] = {};
var c = config.miners[i];
var j = miners.json[i];
m.name = c.name;
m.host = c.host;
m.port = c.port;
m.poll = (typeof c.poll !== 'undefined') ? c.poll : config.miner_poll;
m.timeout = (typeof c.timeout !== 'undefined') ? c.timeout : config.miner_timeout;
function hostname() {
return c.hostname ? c.hostname : (m.host + ':' + m.port);
}
// stats
m.reqCnt = 0;
m.rspCnt = 0;
// it was never seen and never found good yet
c.last_seen = null;
c.last_good = null;
// socket
m.socket = new net.Socket()
.on('connect', function() {
logger.info(m.name + ': connected to ' + m.socket.remoteAddress + ':' + m.socket.remotePort);
var req = '{"id":0,"jsonrpc":"2.0","method":"miner_getstat1"}';
++m.reqCnt;
logger.trace(m.name + ': req[' + m.reqCnt + ']: ' + req);
m.socket.write(req + '\n');
m.socket.setTimeout(m.timeout);
})
.on('timeout', function() {
logger.warn(m.name + ': response timeout');
m.socket.destroy();
miners.json[i] = {
"name" : m.name,
"host" : hostname(),
"uptime" : "",
"eth" : "",
"dcr" : "",
"eth_hr" : "",
"dcr_hr" : "",
"temps" : "",
"pools" : "",
"ver" : "",
"target_eth" : "",
"target_dcr" : "",
"comments" : c.comments,
"offline" : c.offline,
"warning" : null,
"error" : 'Error: no response',
"last_seen" : c.last_seen ? c.last_seen : 'never'
};
})
.on('data', function(data) {
++m.rspCnt;
logger.trace(m.name + ': rsp[' + m.rspCnt + ']: ' + data.toString().trim());
c.last_seen = moment().format("YYYY-MM-DD HH:mm:ss");
m.socket.setTimeout(0);
var d = JSON.parse(data);
miners.json[i] = {
"name" : m.name,
"host" : hostname(),
"uptime" : moment.duration(parseInt(d.result[1]), 'minutes').format('d [days,] hh:mm'),
"eth" : d.result[2],
"dcr" : d.result[4],
"eth_hr" : d.result[3],
"dcr_hr" : d.result[5],
"temps" : d.result[6],
"pools" : d.result[7],
"ver" : d.result[0],
"target_eth" : c.target_eth,
"target_dcr" : c.target_dcr,
"comments" : c.comments,
"offline" : c.offline,
"ti" : c.ti ? c.ti : null,
"error" : null
};
if (c.target_eth && config.tolerance) {
if (miners.json[i].eth.split(';')[0] / 1000 < c.target_eth * (1 - config.tolerance / 100)) {
miners.json[i].warning = 'Low hashrate';
miners.json[i].last_good = c.last_good ? c.last_good : 'never';
} else {
miners.json[i].warning = null;
c.last_good = moment().format("YYYY-MM-DD HH:mm:ss");
}
}
})
.on('close', function() {
logger.info(m.name + ': connection closed');
setTimeout(poll, m.poll);
})
.on('error', function(e) {
logger.error(m.name + ': socket error: ' + e.message);
miners.json[i] = {
"name" : m.name,
"host" : hostname(),
"uptime" : "",
"eth" : "",
"dcr" : "",
"eth_hr" : "",
"dcr_hr" : "",
"temps" : "",
"pools" : "",
"ver" : "",
"target_eth" : "",
"target_dcr" : "",
"comments" : c.comments,
"offline" : c.offline,
"warning" : null,
"error" : e.name + ': ' + e.message,
"last_seen" : c.last_seen ? c.last_seen : 'never'
};
});
function poll() {
m.socket.connect(m.port, m.host);
};
if ((typeof c.offline === 'undefined') || !c.offline) {
poll();
} else {
miners.json[i] = {
"name" : m.name,
"host" : hostname(),
"uptime" : "",
"eth" : "",
"dcr" : "",
"eth_hr" : "",
"dcr_hr" : "",
"temps" : "",
"pools" : "",
"ver" : "",
"target_eth" : "",
"target_dcr" : "",
"comments" : c.comments,
"offline" : c.offline,
"error" : null
};
}
});
// --------------- /REQUESTER ---------------