diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 6a8dc0f..eb4d33f 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -26,6 +26,7 @@ #include "json_rpc_request.h" #include "json_parsers.h" #include "block_template.h" +#include "p2pool_api.h" #include #include #include @@ -1032,6 +1033,7 @@ void P2PServer::on_timer() update_peer_connections(); check_zmq(); check_block_template(); + api_update_local_stats(); } void P2PServer::flush_cache() @@ -1220,6 +1222,52 @@ void P2PServer::on_shutdown() uv_close(reinterpret_cast(&m_showPeersAsync), nullptr); } +void P2PServer::api_update_local_stats() +{ + if (!m_pool->api() || !m_pool->params().m_localStats || ((m_timerCounter % 30) != 5)) { + return; + } + + m_pool->api()->set(p2pool_api::Category::LOCAL, "p2p", + [this](log::Stream& s) + { + const uint64_t cur_time = seconds_since_epoch(); + + s << "{\"connections\":" << m_numConnections.load() + << ",\"incoming_connections\":" << m_numIncomingConnections.load() + << ",\"peer_list_size\":" << m_peerList.size() + << ",\"peers\":["; + + bool first = true; + + for (P2PClient* client = static_cast(m_connectedClientsList->m_next); client != m_connectedClientsList; client = static_cast(client->m_next)) { + if (client->m_listenPort >= 0) { + char buf[32] = {}; + log::Stream s1(buf); + if (client->m_SoftwareVersion) { + s1 << client->software_name() << " v" << (client->m_SoftwareVersion >> 16) << '.' << (client->m_SoftwareVersion & 0xFFFF); + } + + if (!first) { + s << ','; + } + + s << '"' + << (client->m_isIncoming ? "I," : "O,") + << (cur_time - client->m_connectedTime) << ',' + << client->m_pingTime << ',' + << static_cast(buf) << ',' + << client->m_broadcastMaxHeight << ',' + << static_cast(client->m_addrString) + << '"'; + first = false; + } + } + + s << "],\"uptime\":" << cur_time - m_pool->start_time() << '}'; + }); +} + P2PServer::P2PClient::~P2PClient() { } diff --git a/src/p2p_server.h b/src/p2p_server.h index 73aaf1c..ac481ee 100644 --- a/src/p2p_server.h +++ b/src/p2p_server.h @@ -259,6 +259,8 @@ private: void show_peers() const; void on_shutdown() override; + + void api_update_local_stats(); }; } // namespace p2pool diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index d4be1a0..293f349 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -1306,10 +1306,10 @@ void StratumServer::api_update_local_stats(uint64_t timestamp) double current_effort = static_cast(hashes_since_last_share) * 100.0 / m_pool->side_chain().difficulty().to_double(); - int connections = m_numConnections; - int incoming_connections = m_numIncomingConnections; + uint32_t connections = m_numConnections; + uint32_t incoming_connections = m_numIncomingConnections; - m_pool->api()->set(p2pool_api::Category::LOCAL, "stats", + m_pool->api()->set(p2pool_api::Category::LOCAL, "stratum", [hashrate_15m, hashrate_1h, hashrate_24h, total_hashes, shares_found, shares_failed, average_effort, current_effort, connections, incoming_connections](log::Stream& s) { s << "{\"hashrate_15m\":" << hashrate_15m