2019-12-19 10:34:19 +00:00
|
|
|
"""Onionr - Private P2P Communication.
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
This module defines commands to show stats/details about the local node
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import logger
|
|
|
|
from onionrblocks import onionrblacklist
|
2020-09-10 21:44:30 +00:00
|
|
|
from onionrutils import mnemonickeys
|
2019-12-19 10:34:19 +00:00
|
|
|
from utils import sizeutils, gethostname, getconsolewidth, identifyhome
|
|
|
|
from coredb import blockmetadb, keydb
|
|
|
|
import onionrcrypto
|
|
|
|
import config
|
|
|
|
from etc import onionrvalues
|
2020-09-10 21:44:30 +00:00
|
|
|
from filepaths import lock_file
|
2019-12-19 10:34:19 +00:00
|
|
|
|
2020-09-10 21:44:30 +00:00
|
|
|
import psutil
|
2019-12-19 10:34:19 +00:00
|
|
|
"""
|
2019-03-08 01:08:06 +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-12-19 10:34:19 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-09-10 21:44:30 +00:00
|
|
|
def _is_running():
|
|
|
|
script = onionrvalues.SCRIPT_NAME
|
|
|
|
if os.path.isfile(lock_file):
|
|
|
|
for process in psutil.process_iter():
|
|
|
|
if process.name() == script:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-08-05 23:09:04 +00:00
|
|
|
def show_stats():
|
2019-12-19 10:34:19 +00:00
|
|
|
"""Print/log statistic info about our Onionr install."""
|
2019-03-08 01:08:06 +00:00
|
|
|
try:
|
|
|
|
# define stats messages here
|
2019-07-17 16:25:29 +00:00
|
|
|
totalBlocks = len(blockmetadb.get_block_list())
|
2019-08-05 23:09:04 +00:00
|
|
|
home = identifyhome.identify_home()
|
2019-11-10 02:30:07 +00:00
|
|
|
totalBanned = len(onionrblacklist.OnionrBlackList().getList())
|
|
|
|
|
2019-03-08 01:08:06 +00:00
|
|
|
messages = {
|
|
|
|
# info about local client
|
2020-09-10 21:44:30 +00:00
|
|
|
|
|
|
|
# This line is inaccurate if dev mode is enabled
|
2019-12-19 10:34:19 +00:00
|
|
|
'Onionr Daemon Status':
|
2020-09-10 21:44:30 +00:00
|
|
|
((logger.colors.fg.green + 'Online') \
|
|
|
|
if _is_running() \
|
|
|
|
else logger.colors.fg.red + 'Offline'),
|
2019-03-08 01:08:06 +00:00
|
|
|
|
|
|
|
# file and folder size stats
|
2019-12-19 10:34:19 +00:00
|
|
|
'div1': True, # this creates a solid line across the screen, a div
|
|
|
|
'Total Block Size':
|
|
|
|
sizeutils.human_size(sizeutils.size(home + 'blocks/')),
|
|
|
|
'Total Plugin Size':
|
|
|
|
sizeutils.human_size(sizeutils.size(home + 'plugins/')),
|
|
|
|
'Log File Size':
|
|
|
|
sizeutils.human_size(sizeutils.size(home + 'output.log')),
|
2019-03-08 01:08:06 +00:00
|
|
|
|
|
|
|
# count stats
|
2019-12-19 10:34:19 +00:00
|
|
|
'div2': True,
|
|
|
|
'Known Peers (nodes)':
|
|
|
|
str(max(len(keydb.listkeys.list_adders()) - 1, 0)),
|
|
|
|
'Enabled Plugins':
|
|
|
|
str(len(config.get('plugins.enabled', list()))) + ' / ' +
|
|
|
|
str(len(os.listdir(home + 'plugins/'))),
|
|
|
|
'Stored Blocks': str(totalBlocks),
|
2020-01-30 02:20:02 +00:00
|
|
|
'Deleted Blocks': str(totalBanned)
|
2019-03-08 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# color configuration
|
|
|
|
colors = {
|
2019-12-19 10:34:19 +00:00
|
|
|
'title': logger.colors.bold,
|
|
|
|
'key': logger.colors.fg.lightgreen,
|
|
|
|
'val': logger.colors.fg.green,
|
|
|
|
'border': logger.colors.fg.lightblue,
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
'reset': logger.colors.reset
|
2019-03-08 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# pre-processing
|
|
|
|
maxlength = 0
|
2019-07-30 05:19:22 +00:00
|
|
|
width = getconsolewidth.get_console_width()
|
2019-03-08 01:08:06 +00:00
|
|
|
for key, val in messages.items():
|
|
|
|
if not (type(val) is bool and val is True):
|
|
|
|
maxlength = max(len(key), maxlength)
|
|
|
|
prewidth = maxlength + len(' | ')
|
|
|
|
groupsize = width - prewidth - len('[+] ')
|
|
|
|
|
|
|
|
# generate stats table
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info(colors['title'] + 'Onionr v%s Statistics' %
|
|
|
|
onionrvalues.ONIONR_VERSION + colors['reset'],
|
|
|
|
terminal=True)
|
|
|
|
logger.info(colors['border'] + '-' * (maxlength + 1) +
|
|
|
|
'+' + colors['reset'], terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
for key, val in messages.items():
|
|
|
|
if not (type(val) is bool and val is True):
|
2019-12-19 10:34:19 +00:00
|
|
|
val = [str(val)[i:i + groupsize]
|
|
|
|
for i in range(0, len(str(val)), groupsize)]
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info(colors['key'] + str(key).rjust(maxlength) +
|
|
|
|
colors['reset'] + colors['border'] +
|
|
|
|
' | ' + colors['reset'] + colors['val'] +
|
|
|
|
str(val.pop(0)) + colors['reset'], terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
|
|
|
|
for value in val:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info(' ' * maxlength + colors['border'] + ' | ' +
|
|
|
|
colors['reset'] + colors['val'] + str(
|
|
|
|
value) + colors['reset'], terminal=True)
|
2019-03-08 01:08:06 +00:00
|
|
|
else:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info(colors['border'] + '-' * (maxlength +
|
|
|
|
1) + '+' +
|
|
|
|
colors['reset'], terminal=True)
|
|
|
|
logger.info(colors['border'] + '-' * (maxlength + 1) +
|
|
|
|
'+' + colors['reset'], terminal=True)
|
|
|
|
except Exception as e: # pylint: disable=W0703
|
|
|
|
logger.error('Failed to generate statistics table. ' +
|
|
|
|
str(e), error=e, timestamp=False, terminal=True)
|
|
|
|
|
2019-03-08 01:08:06 +00:00
|
|
|
|
2019-07-31 05:10:28 +00:00
|
|
|
def show_details():
|
2019-12-19 10:34:19 +00:00
|
|
|
"""Print out details.
|
|
|
|
|
|
|
|
node transport address(es)
|
|
|
|
active user ID
|
|
|
|
active user ID in mnemonic form
|
|
|
|
"""
|
2019-03-08 01:08:06 +00:00
|
|
|
details = {
|
2020-03-08 00:51:39 +00:00
|
|
|
'Data directory': identifyhome.identify_home(),
|
2019-12-19 10:34:19 +00:00
|
|
|
'Node Address': gethostname.get_hostname(),
|
|
|
|
'Public Key': onionrcrypto.pub_key.replace('=', ''),
|
|
|
|
'Human-readable Public Key': mnemonickeys.get_human_readable_ID()
|
2019-03-08 01:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for detail in details:
|
2019-12-19 10:34:19 +00:00
|
|
|
logger.info('%s%s: \n%s%s\n' % (logger.colors.fg.lightgreen,
|
|
|
|
detail, logger.colors.fg.green,
|
|
|
|
details[detail]), terminal=True)
|
|
|
|
|
|
|
|
|
|
|
|
show_details.onionr_help = "Shows relevant information " # type: ignore
|
|
|
|
show_details.onionr_help += "for your Onionr install: node " # type: ignore
|
|
|
|
show_details.onionr_help += "address, and active public key." # type: ignore
|
2019-09-06 20:09:39 +00:00
|
|
|
|
2019-12-19 10:34:19 +00:00
|
|
|
show_stats.onionr_help = "Shows statistics for your Onionr " # type: ignore
|
|
|
|
show_stats.onionr_help += "node. Slow if Onionr is not running" # type: ignore
|