eris- Elasticsearch Recon Ingestion Scripts (ERIS) 🔎 |
git clone git://git.acid.vegas/-c.git |
Log | Files | Refs | Archive | README | LICENSE |
commit 6c4ae3e988920565b33a2604599e59e22789ff72
parent d34aa105f1b4752dae1976f656b68db744fe0a9d Author: acidvegas <acid.vegas@acid.vegas> Date: Fri, 8 Mar 2024 12:13:57 -0500 Many bugs fixed in sniffer and async model. Diffstat:
|
3 files changed, 52 insertions(+), 49 deletions(-) | ||
diff --git a/eris.py b/eris.py @@ -13,7 +13,7 @@ sys.dont_write_bytecode = True try: # This is commented out because there is a bug with the elasticsearch library that requires a patch (see initialize() method below) - #from elasticsearch import AsyncElasticsearch + #from elasticsearch import AsyncElasticsearch from elasticsearch.exceptions import NotFoundError from elasticsearch.helpers import async_streaming_bulk except ImportError: @@ -43,23 +43,23 @@ class ElasticIndexer: 'request_timeout': args.timeout, 'max_retries': args.retries, 'retry_on_timeout': True, - 'sniff_on_start': True, # Is this problematic? + 'sniff_on_start': False, # Problems when True.... 'sniff_on_node_failure': True, 'min_delay_between_sniffing': 60 # Add config option for this? } - if args.api_key: - self.es_config['api_key'] = (args.api_key, '') # Verify this is correct - else: - self.es_config['basic_auth'] = (args.user, args.password) - - + #if args.api_key: + # self.es_config['api_key'] = (args.api_key, '') # Verify this is correct + #else: + self.es_config['basic_auth'] = (args.user, args.password) + + async def initialize(self): '''Initialize the Elasticsearch client.''' # Patching the Elasticsearch client to fix a bug with sniffing (https://github.com/elastic/elasticsearch-py/issues/2005#issuecomment-1645641960) import sniff_patch - self.es = sniff_patch.init_elasticsearch(**self.es_config) + self.es = await sniff_patch.init_elasticsearch(**self.es_config) # Remove the above and uncomment the below if the bug is fixed in the Elasticsearch client: #self.es = AsyncElasticsearch(**es_config) @@ -68,7 +68,7 @@ class ElasticIndexer: async def create_index(self, map_body: dict, pipeline: str = '', replicas: int = 1, shards: int = 1): ''' Create the Elasticsearch index with the defined mapping. - + :param map_body: Mapping for the index :param pipeline: Name of the ingest pipeline to use for the index :param replicas: Number of replicas for the index @@ -106,7 +106,7 @@ class ElasticIndexer: '''Get the health of the Elasticsearch cluster.''' return await self.es.cluster.health() - + async def get_cluster_size(self) -> int: '''Get the number of nodes in the Elasticsearch cluster.''' @@ -128,7 +128,7 @@ class ElasticIndexer: count = 0 total = 0 - + async for ok, result in async_streaming_bulk(self.es, actions=data_generator(file_path), chunk_size=self.chunk_size, max_chunk_bytes=self.chunk_max): action, result = result.popitem() @@ -151,17 +151,17 @@ async def main(): '''Main function when running this script directly.''' parser = argparse.ArgumentParser(description='Index data into Elasticsearch.') - + # General arguments parser.add_argument('input_path', help='Path to the input file or directory') # Required parser.add_argument('--watch', action='store_true', help='Create or watch a FIFO for real-time indexing') - + # Elasticsearch arguments parser.add_argument('--host', default='http://localhost/', help='Elasticsearch host') parser.add_argument('--port', type=int, default=9200, help='Elasticsearch port') parser.add_argument('--user', default='elastic', help='Elasticsearch username') parser.add_argument('--password', default=os.getenv('ES_PASSWORD'), help='Elasticsearch password (if not provided, check environment variable ES_PASSWORD)') - parser.add_argument('--api-key', default=os.getenv('ES_APIKEY'), help='Elasticsearch API Key for authentication (if not provided, check environment variable ES_APIKEY)') + #parser.add_argument('--api-key', default=os.getenv('ES_APIKEY'), help='Elasticsearch API Key for authentication (if not provided, check environment variable ES_APIKEY)') parser.add_argument('--self-signed', action='store_false', help='Elasticsearch is using self-signed certificates') # Elasticsearch indexing arguments @@ -169,7 +169,7 @@ async def main(): parser.add_argument('--pipeline', help='Use an ingest pipeline for the index') parser.add_argument('--replicas', type=int, default=1, help='Number of replicas for the index') parser.add_argument('--shards', type=int, default=1, help='Number of shards for the index') - + # Performance arguments parser.add_argument('--chunk-size', type=int, default=50000, help='Number of records to index in a chunk') parser.add_argument('--chunk-max', type=int, default=100, help='Maximum size of a chunk in bytes') @@ -206,12 +206,15 @@ async def main(): from ingestors import ingest_massdns as ingestor elif args.zone: from ingestors import ingest_zone as ingestor - + + if not isinstance(ingestor, object): + raise ValueError('No ingestor selected') + health = await edx.get_cluster_health() print(health) await asyncio.sleep(5) # Delay to allow time for sniffing to complete - + nodes = await edx.get_cluster_size() logging.info(f'Connected to {nodes:,} Elasticsearch node(s)') @@ -220,7 +223,7 @@ async def main(): map_body = ingestor.construct_map() await edx.create_index(map_body, args.pipeline, args.replicas, args.shards) - + if os.path.isfile(args.input_path): logging.info(f'Processing file: {args.input_path}') await edx.process_data(args.input_path, ingestor.process_data) @@ -245,4 +248,4 @@ async def main(): if __name__ == '__main__': - asyncio.run(main()) -\ No newline at end of file + asyncio.run(main()) |