github.com/annchain/OG@v0.0.9/scripts/monitor/monitor.py (about)

     1  import datetime
     2  import json
     3  import multiprocessing
     4  import time
     5  import traceback
     6  
     7  import pandas as pd
     8  import requests
     9  
    10  # pd.set_option('display.height', 1000)
    11  pd.set_option('display.max_rows', 500)
    12  pd.set_option('display.max_columns', 500)
    13  pd.set_option('display.width', 1000)
    14  
    15  total = 20
    16  
    17  s = requests.Session()
    18  s.trust_env = False
    19  
    20  id_host_map = {}
    21  host_id_map = {}
    22  
    23  tps = {}  # host ->{}
    24  
    25  
    26  def host_to_show(ip):
    27      # return ip.split(':')[0].split('.')[3]
    28      return ip[-9:]
    29  
    30  
    31  def id_to_show(id):
    32      if id not in id_host_map:
    33          return id + ':N/A'
    34      return host_to_show(id_host_map[id])
    35  
    36  
    37  def myid(host):
    38      try:
    39          resp = s.get('http://%s/net_info' % host, timeout=10)
    40          j = json.loads(resp.text)
    41          return j['data']['short_id']
    42      except Exception as e:
    43          return None
    44  
    45  
    46  def doone(host):
    47      try:
    48          d = {}
    49          try:
    50              resp = s.get('http://%s/sequencer' % host, timeout=5)
    51              j = json.loads(resp.text)
    52              d['seq'] = j['data']['Hash'][-8:]
    53          except Exception as e:
    54              print(e)
    55              return None
    56              # d['seq'] = -1
    57  
    58          try:
    59              peers = []
    60              resp = s.get('http://%s/peers_info' % host, timeout=5)
    61              j = json.loads(resp.text)
    62              for peer in j['data']:
    63                  peers.append(peer['short_id'])
    64                  d['peers'] = peers
    65          except Exception as e:
    66              print(e)
    67              return None
    68  
    69          try:
    70              resp = s.get('http://%s/sync_status' % host, timeout=5)
    71              j = json.loads(resp.text)
    72              d.update(j)
    73              d['error'] = d['error']
    74              d['syncMode'] = d['syncMode'].strip()[10:][0:4]
    75              d['catchupSyncerStatus'] = d['catchupSyncerStatus'].strip()[3:]
    76          except Exception as e:
    77              print(e)
    78              return None
    79  
    80          try:
    81              resp = s.get('http://%s/performance' % host, timeout=5)
    82              j = json.loads(resp.text)
    83              d.update(j['TxCounter'])
    84          except Exception as e:
    85              traceback.print_exc()
    86              return None
    87  
    88          return host, d
    89      except KeyboardInterrupt as e:
    90          return
    91      except Exception as e:
    92          return None
    93  
    94  
    95  def doround(hosts, pool):
    96      r = {}
    97      ever = False
    98      for host in hosts:
    99          if host not in host_id_map:
   100              print('Resolving', host)
   101              id = myid(host)
   102              if id is not None:
   103                  host_id_map[host] = id
   104                  id_host_map[id] = host
   105      # print('Collecting')
   106  
   107      for c in pool.imap(doone, hosts):
   108          if c is None:
   109              continue
   110          host, d = c
   111          if d is not None:
   112              d['bestPeer'] = id_to_show(d['bestPeer'])
   113              ippeers = []
   114              if 'peers' in d:
   115                  for peer in d['peers']:
   116                      ippeers.append(id_to_show(peer))
   117              d['peers'] = ippeers
   118              d['peers'] = len(ippeers)
   119              d['id'] = d['id'][0:4]
   120  
   121              if host in tps:
   122                  # there is results
   123                  v = tps[host]
   124  
   125                  d['tpsReceived'] = (d['txReceived'] + d['sequencerReceived'] - v['v_rcv']) / (time.time() - v['t'])
   126                  d['tpsGenerated'] = (d['txGenerated'] + d['sequencerGenerated'] - v['v_gen']) / (time.time() - v['t'])
   127                  d['tpsConfirmed'] = (d['txConfirmed'] + d['sequencerConfirmed'] - v['v_cfm']) / (time.time() - v['t'])
   128  
   129                  d['tpsReceivedFB'] = (d['txReceived'] + d['sequencerReceived']) / (time.time() - d['startupTime'])
   130                  d['tpsGeneratedFB'] = (d['txGenerated'] + d['sequencerGenerated']) / (time.time() - d['startupTime'])
   131                  d['tpsConfirmedFB'] = (d['txConfirmed'] + d['sequencerConfirmed']) / (time.time() - d['startupTime'])
   132  
   133              tps[host] = {'t': time.time(),
   134                           'v_rcv': d['txReceived'] + d['sequencerReceived'],
   135                           'v_gen': d['txGenerated'] + d['sequencerGenerated'],
   136                           'v_cfm': d['txConfirmed'] + d['sequencerConfirmed']}
   137  
   138              r[host_to_show(host)] = d
   139              ever = True
   140      if not ever:
   141          return None
   142  
   143      # return pd.DataFrame.from_dict(d, orient='index')
   144      return pd.DataFrame.from_dict(r)
   145  
   146  
   147  def hosts(fname):
   148      with open(fname) as f:
   149          return [line.strip() for line in f]
   150  
   151  
   152  if __name__ == '__main__':
   153      # hosts = ['127.0.0.1:%d' % (8000 + i*100) for i in range(total)]
   154      host_ipports = hosts('../data/hosts')
   155      pool = multiprocessing.Pool(processes=7)
   156  
   157      try:
   158          while True:
   159              try:
   160                  df = doround(host_ipports, pool)
   161                  if df is not None:
   162                      print("=" * 20)
   163                      print(datetime.datetime.now())
   164                      print(df)
   165                  time.sleep(1)
   166              except KeyboardInterrupt as e:
   167                  raise e
   168              except Exception as e:
   169                  print(e)
   170                  pass
   171              finally:
   172                  time.sleep(1)
   173      except KeyboardInterrupt as e:
   174          print('Ending')
   175          pool.terminate()
   176      finally:
   177          pool.join()