Bor Status
This script is a Node.js application that monitors the synchronization status of a local Bor node.
var http = require('http');
var url = require('url');
const fs = require('fs');
let jsonToReturn = {};
let lastBlock = 0;
let syncedBlocks = 0;
let blocksToLive = 0;
let status = { "logs": [] };
function getLocalData() {
var post_data = '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params": ["latest",false],"id":1}';
var post_options = {
host: 'localhost',
port: '8545',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
var post_req = http.request(post_options, function (res) {
var body = '';
res.setEncoding('utf8');
res.on('readable', function () {
var chunk = this.read() || '';
body += chunk;
});
res.on('end', function () {
let latestBlock = JSON.parse(body);
let latestBlockNumber = parseInt(latestBlock.result.number);
let latestBlockTimestamp = parseInt(latestBlock.result.timestamp);
let currentTimestamp = Date.now() / 1000;
let difference = currentTimestamp - latestBlockTimestamp;
let humanCurrentTimestamp = new Date(currentTimestamp * 1000).toLocaleString();
let humanLatestBlockTimestamp = new Date(latestBlockTimestamp * 1000).toLocaleString();
let synced = false;
let newLog = {};
if (difference > 120) {
synced = false;
newLog = {
"synced": false,
"timestamp": currentTimestamp,
"dateTime": humanCurrentTimestamp,
"message": "Block #" + latestBlockNumber + " is " + (difference / 60) + " minutes late (" + humanCurrentTimestamp + ") compared to its original timestamp (" + humanLatestBlockTimestamp + ")"
};
} else {
synced = true;
newLog = {
"synced": true,
"timestamp": currentTimestamp,
"dateTime": humanCurrentTimestamp,
"message": "Block #" + latestBlockNumber + " is on time. Block original timestamp is " + humanLatestBlockTimestamp + " (" + (difference / 60) + " minutes from now)"
};
}
status.logs.push(newLog);
status = {
"synced": synced,
"timestamp": currentTimestamp,
"humanCurrentTimstamp": humanCurrentTimestamp,
"lastBlock": latestBlockNumber,
"syncedBlocks": latestBlockNumber - lastBlock,
"logs": status.logs
};
lastBlock = latestBlockNumber;
if (status.logs.length > 10) {
status.logs.shift();
}
console.log("Latest block number : " + latestBlockNumber);
console.log("Latest block timestamp : " + latestBlockTimestamp);
console.log("Current timestamp : " + currentTimestamp);
console.log("Current difference : " + difference);
console.log(humanCurrentTimestamp);
console.log(humanLatestBlockTimestamp);
console.log(status);
fs.appendFile('message.txt', JSON.stringify(status.logs) + "\r\n", function (err) {
if (err) throw err;
});
});
post_req.on('error', function (e) {
console.log("error" + e.message);
});
});
post_req.write(post_data);
post_req.end();
setTimeout(function () {
getLocalData();
}, 60000)
}
getLocalData();
var port = 9615;
http.createServer(function (request, response) {
try {
let logsLine = []
let fileContentsTable = ""
const allFileContents = fs.readFileSync('message.txt', 'utf-8');
allFileContents.split(/\r?\n/).forEach(line => {
try {
let liness = JSON.parse(line);
fileContentsTable += '<tr style="background-color: ' + (liness.synced ? '#93f5b0' : '#f59393') + ';"><td style="background-color: ' + (liness.synced ? '#93f5b0' : '#f59393') + ';">Synced : ' + liness.synced + '</td><td style="background-color: ' + (liness.synced ? '#93f5b0' : '#f59393') + ';">' + liness.dateTime + " - " + liness.message + "</td></tr>"
} catch (e) {
console.log("Error");
}
});
var requestUrl = url.parse(request.url);
response.writeHead(200);
let html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Status Page</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 20px;
}
/* Restante do estilo CSS... */
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Status Dashboard</h1>
</div>
<div class="card">
<h2>Status</h2>
<p class="synced-${status.synced ? 'true' : 'false'}">${status.synced ? 'Synced' : 'Not Synced'}</p>
</div>
<div class="card">
<h2>Block Information</h2>
<p>Last Block: ${status.lastBlock}</p>
<p>Timestamp: ${status.humanCurrentTimstamp}</p>
<p>Synced Blocks: ${status.syncedBlocks}</p>
</div>
<div class="card">
<h2>Logs</h2>
${status.logs.map(log => `
<div class="synced-${log.synced ? 'true' : 'false'}">
<p>${log.synced ? 'Synced' : 'Not Synced'} - ${log.dateTime}</p>
<p>${log.message}</p>
</div>
`).join('')}
</div>
</div>
</body>
</html>`;
jsonToReturn.syncedBlocks = status.syncedBlocks;
jsonToReturn.humanCurrentTimstamp = status.humanCurrentTimstamp;
jsonToReturn.fileContents = logsLine;
jsonToReturn.timestamp = status.timestamp;
response.end(html);
} catch (e) {
response.writeHead(500);
response.end();
console.log(e.stack);
}
}).listen(port);
console.log("listening on port " + port);
Make sure you have Node.js installed on your system. Open the script in a text editor and adjust the initial settings if necessary, such as the server port, polling interval, and the location of the Bor node.
Execute the following command to install the necessary dependencies:
npm install
In the same terminal, execute the following command to start the script:
node <name-script>.js
Last updated