def get_stats(self): """Return statistics about our node""" stats = {} proc = Process() def get_open_files(): if WINDOWS: return proc.num_handles() return proc.num_fds() try: self._too_many except AttributeError: sleep(1) comm_inst = self._too_many.get(communicator.OnionrCommunicatorDaemon, args=(self._too_many, )) connected = [] [ connected.append(x) for x in comm_inst.onlinePeers if x not in connected ] stats['uptime'] = comm_inst.getUptime() stats['connectedNodes'] = '\n'.join(connected) stats['blockCount'] = len(blockmetadb.get_block_list()) stats['blockQueueCount'] = len(comm_inst.blockQueue) stats['threads'] = proc.num_threads() stats['ramPercent'] = proc.memory_percent() stats['fd'] = get_open_files() stats['diskUsage'] = human_size(size(identify_home())) return json.dumps(stats)
def get_stats(self): """Return statistics about our node""" stats = {} proc = Process() def get_open_files(): if WINDOWS: return proc.num_handles() return proc.num_fds() try: self._too_many except AttributeError: sleep(1) kv: "DeadSimpleKV" = self._too_many.get_by_string("DeadSimpleKV") connected = [] [ connected.append(x) for x in kv.get('onlinePeers') if x not in connected ] stats['uptime'] = get_epoch() - kv.get('startTime') stats['connectedNodes'] = '\n'.join(connected) stats['blockCount'] = len(blockmetadb.get_block_list()) stats['blockQueueCount'] = len(kv.get('blockQueue')) stats['threads'] = proc.num_threads() stats['ramPercent'] = proc.memory_percent() stats['fd'] = get_open_files() stats['diskUsage'] = human_size(size(identify_home())) return json.dumps(stats)
def show_stats(): """Print/log statistic info about our Onionr install.""" try: # define stats messages here totalBlocks = len(blockmetadb.get_block_list()) home = identifyhome.identify_home() totalBanned = len(onionrblacklist.OnionrBlackList().getList()) messages = { # info about local client 'Onionr Daemon Status': ((logger.colors.fg.green + 'Online') if check_communicator( timeout=9) else logger.colors.fg.red + 'Offline'), # file and folder size stats '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')), # count stats '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), 'Deleted Blocks': str(totalBanned) } # color configuration colors = { 'title': logger.colors.bold, 'key': logger.colors.fg.lightgreen, 'val': logger.colors.fg.green, 'border': logger.colors.fg.lightblue, 'reset': logger.colors.reset } # pre-processing maxlength = 0 width = getconsolewidth.get_console_width() 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 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) for key, val in messages.items(): if not (type(val) is bool and val is True): val = [ str(val)[i:i + groupsize] for i in range(0, len(str(val)), groupsize) ] logger.info(colors['key'] + str(key).rjust(maxlength) + colors['reset'] + colors['border'] + ' | ' + colors['reset'] + colors['val'] + str(val.pop(0)) + colors['reset'], terminal=True) for value in val: logger.info(' ' * maxlength + colors['border'] + ' | ' + colors['reset'] + colors['val'] + str(value) + colors['reset'], terminal=True) else: 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)