eris

- Elasticsearch Recon Ingestion Scripts (ERIS) 🔎
git clone git://git.acid.vegas/eris.git
Log | Files | Refs | Archive | README | LICENSE

elastictop.py (4243B)

      1 #!/usr/bin/env python
      2 # estop - developed by acidvegas (https://git.acid.vegas/eris)
      3 
      4 '''
      5 Little script to show some basic information about an Elasticsearch cluster.
      6 '''
      7 
      8 import argparse
      9 import os
     10 
     11 try:
     12     from elasticsearch import Elasticsearch
     13 except ImportError:
     14     raise ImportError('Missing required \'elasticsearch\' library. (pip install elasticsearch)')
     15 
     16 
     17 def bytes_to_human_readable(num_bytes):
     18     '''Convert bytes to a human-readable format.'''
     19     for unit in ['bytes', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb']:
     20         if abs(num_bytes) < 1024.0:
     21             return f"{num_bytes:3.1f}{unit}"
     22         num_bytes /= 1024.0
     23     return f"{num_bytes:.1f}YB"
     24 
     25 def main():
     26     '''Main function when running this script directly.'''
     27 
     28     parser = argparse.ArgumentParser(description='Index data into Elasticsearch.')
     29     parser.add_argument('--host', default='localhost', help='Elasticsearch host')
     30     parser.add_argument('--port', type=int, default=9200, help='Elasticsearch port')
     31     parser.add_argument('--user', default='elastic', help='Elasticsearch username')
     32     parser.add_argument('--password', default=os.getenv('ES_PASSWORD'), help='Elasticsearch password (if not provided, check environment variable ES_PASSWORD)')
     33     parser.add_argument('--api-key', help='Elasticsearch API Key for authentication')
     34     parser.add_argument('--self-signed', action='store_false', help='Elasticsearch is using self-signed certificates')
     35     args = parser.parse_args()
     36 
     37     es_config = {
     38         'hosts': [f'{args.host}:{args.port}'],
     39         'verify_certs': args.self_signed,
     40         'ssl_show_warn': args.self_signed,
     41         'basic_auth': (args.user, args.password)
     42     }
     43     es = Elasticsearch(**es_config)
     44 
     45     while True:
     46         os.system('clear')
     47 
     48         stats = es.cluster.stats()
     49 
     50         name = stats['cluster_name']
     51         status = stats['status']
     52         indices = {
     53             'total': stats['indices']['count'],
     54             'shards': stats['indices']['shards']['total'],
     55             'docs': stats['indices']['docs']['count'],
     56             'size': bytes_to_human_readable(stats['indices']['store']['size_in_bytes'])
     57         }
     58         nodes = {
     59             'total': stats['_nodes']['total'],
     60             'successful': stats['_nodes']['successful'],
     61             'failed': stats['_nodes']['failed']
     62         }
     63 
     64         if status == 'green':
     65             print(f'Cluster   {name} (\033[92m{status}\033[0m)')
     66         elif status == 'yellow':
     67             print(f'Cluster   {name} (\033[93m{status}\033[0m)')
     68         elif status == 'red':
     69             print(f'Cluster   {name} (\033[91m{status}\033[0m)')
     70         
     71         print(f'\nNodes     {nodes["total"]} Total, {nodes["successful"]} Successful, {nodes["failed"]} Failed')
     72 
     73         nodes_info = es.nodes.info()
     74 
     75         for node_id, node_info in nodes_info['nodes'].items():
     76             node_name = node_info['name']
     77             transport_address = node_info['transport_address']
     78             #node_stats = es.nodes.stats(node_id=node_id)
     79             version = node_info['version']
     80             memory = bytes_to_human_readable(int(node_info['settings']['node']['attr']['ml']['machine_memory']))
     81             print(f"          {node_name.ljust(7)} | Host: {transport_address.rjust(21)} | Version: {version.ljust(7)} | Processors: {node_info['os']['available_processors']} | Memory: {memory}")
     82 
     83         indices_stats = es.cat.indices(format="json")
     84         
     85         print(f'\nIndices   {indices["total"]:,} Total {indices["shards"]:,}, Shards')
     86         for index in indices_stats:
     87             index_name = index['index']
     88             document_count = f'{int(index['docs.count']):,}'
     89             store_size = index['store.size']
     90             number_of_shards = int(index['pri'])  # primary shards
     91             number_of_replicas = int(index['rep'])  # replicas
     92 
     93             if index_name.startswith('.') or document_count == '0':
     94                 continue
     95 
     96             print(f"          {index_name.ljust(15)} | Documents: {document_count.rjust(15)} | {store_size.rjust(7)} [Shards: {number_of_shards:,}, Replicas: {number_of_replicas:,}]")
     97 
     98         dox = f'{indices["docs"]:,}'
     99 
    100         print(f'\nTotal {dox.rjust(48)} {indices["size"].rjust(9)}')
    101 
    102 if __name__ == '__main__':
    103     main()