From 8feb089ebf5fd84b0f0ca8f24466c523dd3a3262 Mon Sep 17 00:00:00 2001 From: shloka-bhalgat-unskript Date: Wed, 29 May 2024 22:37:04 +0530 Subject: [PATCH] PR comments --- .../elasticsearch_get_index_health.py | 36 ++++++++----------- .../mongodb_check_large_index_size.py | 5 +-- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/ElasticSearch/legos/elasticsearch_get_index_health/elasticsearch_get_index_health.py b/ElasticSearch/legos/elasticsearch_get_index_health/elasticsearch_get_index_health.py index fad7b3d9f..44396686d 100644 --- a/ElasticSearch/legos/elasticsearch_get_index_health/elasticsearch_get_index_health.py +++ b/ElasticSearch/legos/elasticsearch_get_index_health/elasticsearch_get_index_health.py @@ -52,30 +52,24 @@ def elasticsearch_get_index_health(handle, index_name="") -> Tuple: """ try: if index_name: - index_names = [index_name] + # Fetch specific index health + health_url = f"/_cat/indices/{index_name}?v&h=index,status&format=json" + health_response = handle.web_request(health_url, "GET", None) + index_stats = [health_response[0]] if health_response and 'error' not in health_response else [] else: - # Fetches all indices; ensure only non-empty lines and non-system indices are considered - response = handle.web_request("/_cat/indices?h=index", "GET", None) - index_names = [line.strip() for line in response.splitlines() if line.strip() and not line.startswith('.')] + # Fetches all indices health; skips empty lines and system indices + health_url = "/_cat/indices?v&h=index,status&format=json" + health_response = handle.web_request(health_url, "GET", None) + index_stats = [idx for idx in health_response if not idx['index'].startswith('.')] if health_response and 'error' not in health_response else [] - all_indices_stats = [] + if not index_stats: + print(f"No indices found or error retrieving indices: {health_response.get('error', 'No response') if health_response else 'No data'}") + return (True, None) - for current_index in index_names: - health_url = f"/_cat/indices/{current_index}?format=json" - health_response = handle.web_request(health_url, "GET", None) - if not health_response or "error" in health_response: - print(f"Error retrieving health for index {current_index}: {health_response.get('error', 'Unknown error')}") - continue - - # Parsing the health data correctly assuming the correct format and keys are present - health_data = health_response[0] - if health_data.get('health') in ['yellow', 'red']: - index_stats = { - "index": current_index, - "health": health_data.get('health'), - "status": health_data.get('status'), - } - all_indices_stats.append(index_stats) + all_indices_stats = [ + {"index": idx['index'], "status": idx['status']} + for idx in index_stats + ] except Exception as e: print(f"Error processing index health: {str(e)}") diff --git a/Mongo/legos/mongodb_check_large_index_size/mongodb_check_large_index_size.py b/Mongo/legos/mongodb_check_large_index_size/mongodb_check_large_index_size.py index 6f48a298b..2d1581697 100644 --- a/Mongo/legos/mongodb_check_large_index_size/mongodb_check_large_index_size.py +++ b/Mongo/legos/mongodb_check_large_index_size/mongodb_check_large_index_size.py @@ -4,11 +4,12 @@ ## from typing import Tuple, Optional from pydantic import BaseModel, Field +DEFAULT_SIZE= 2048000 # 2GB in KB class InputSchema(BaseModel): index_threshold: Optional[float] = Field( - 2048000, # 2GB in KB + DEFAULT_SIZE, description='The threshold for total index size. Default is 512000KB.', title='Index threshold(in KB)', ) @@ -26,7 +27,7 @@ def mongodb_check_large_index_size_printer(output): print(f"Alert! Index size of {alert['indexSizeKB']} KB for database '{alert['db']}' in collection '{alert['collection']}' exceeds threshold !") -def mongodb_check_large_index_size(handle, threshold: float = 2048000) -> Tuple: +def mongodb_check_large_index_size(handle, threshold: float = DEFAULT_SIZE) -> Tuple: """ mongodb_check_large_index_size checks the index sizes for all databases and collections. It compares the size of each index with a given threshold and returns any indexes that exceed the threshold.