Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shloka-bhalgat-unskript committed May 29, 2024
1 parent dc5ac3b commit 8feb089
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)',
)
Expand All @@ -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.
Expand Down

0 comments on commit 8feb089

Please sign in to comment.