2020-03-20 08:50:48 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-02-02 03:49:11 +00:00
|
|
|
|
2020-03-20 08:50:48 +00:00
|
|
|
Serialize various node information
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
2020-07-29 08:57:06 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2020-03-20 08:50:48 +00:00
|
|
|
from gevent import sleep
|
|
|
|
|
2020-03-24 08:18:05 +00:00
|
|
|
from psutil import Process, WINDOWS
|
2020-04-03 08:51:31 +00:00
|
|
|
import ujson as json
|
2020-03-20 08:50:48 +00:00
|
|
|
|
|
|
|
from coredb import blockmetadb
|
2020-03-24 08:18:05 +00:00
|
|
|
from utils.sizeutils import size, human_size
|
|
|
|
from utils.identifyhome import identify_home
|
2020-03-20 08:50:48 +00:00
|
|
|
import communicator
|
2020-07-29 08:57:06 +00:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from deadsimplekv import DeadSimpleKV
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
2019-02-02 03:49:11 +00:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
|
|
|
|
2019-02-02 03:49:11 +00:00
|
|
|
|
|
|
|
class SerializedData:
|
2019-08-04 04:52:57 +00:00
|
|
|
def __init__(self):
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
2019-02-02 03:49:11 +00:00
|
|
|
Serialized data is in JSON format:
|
|
|
|
{
|
|
|
|
'success': bool,
|
|
|
|
'foo': 'bar',
|
|
|
|
etc
|
|
|
|
}
|
2019-11-20 10:52:50 +00:00
|
|
|
"""
|
2020-03-20 08:50:48 +00:00
|
|
|
|
2019-09-14 06:29:31 +00:00
|
|
|
def get_stats(self):
|
2019-11-20 10:52:50 +00:00
|
|
|
"""Return statistics about our node"""
|
2019-02-02 03:49:11 +00:00
|
|
|
stats = {}
|
2020-03-20 08:50:48 +00:00
|
|
|
proc = Process()
|
2020-03-24 08:18:05 +00:00
|
|
|
|
|
|
|
def get_open_files():
|
2020-07-29 08:57:06 +00:00
|
|
|
if WINDOWS:
|
|
|
|
return proc.num_handles()
|
2020-03-24 08:18:05 +00:00
|
|
|
return proc.num_fds()
|
|
|
|
|
2019-08-05 05:25:27 +00:00
|
|
|
try:
|
|
|
|
self._too_many
|
|
|
|
except AttributeError:
|
2020-03-20 08:50:48 +00:00
|
|
|
sleep(1)
|
2020-07-29 08:57:06 +00:00
|
|
|
comm_inst = self._too_many.get(communicator.OnionrCommunicatorDaemon,
|
|
|
|
args=(self._too_many,))
|
2020-07-24 19:37:01 +00:00
|
|
|
kv: "DeadSimpleKV" = comm_inst.shared_state.get_by_string("DeadSimpleKV")
|
2019-10-11 09:28:43 +00:00
|
|
|
connected = []
|
2020-07-29 08:57:06 +00:00
|
|
|
[connected.append(x)
|
|
|
|
for x in kv.get('onlinePeers') if x not in connected]
|
|
|
|
stats['uptime'] = kv.get('getUptime')
|
2019-10-11 09:28:43 +00:00
|
|
|
stats['connectedNodes'] = '\n'.join(connected)
|
2019-07-17 16:25:29 +00:00
|
|
|
stats['blockCount'] = len(blockmetadb.get_block_list())
|
2020-07-24 19:37:01 +00:00
|
|
|
stats['blockQueueCount'] = len(kv.get('blockQueue'))
|
2020-03-20 08:50:48 +00:00
|
|
|
stats['threads'] = proc.num_threads()
|
2020-03-20 23:39:26 +00:00
|
|
|
stats['ramPercent'] = proc.memory_percent()
|
2020-03-24 08:18:05 +00:00
|
|
|
stats['fd'] = get_open_files()
|
|
|
|
stats['diskUsage'] = human_size(size(identify_home()))
|
2019-02-02 03:49:11 +00:00
|
|
|
return json.dumps(stats)
|