From 17432cd0f41b050038f8345be2cc2afbddc7b89d Mon Sep 17 00:00:00 2001 From: shloka-bhalgat-unskript Date: Thu, 25 Apr 2024 16:52:22 +0530 Subject: [PATCH] Update postgres and mongo checks --- .../mongodb_compare_disk_size_to_threshold.py | 4 +- .../postgresql_get_cache_hit_ratio.json | 2 +- .../postgresql_get_server_status.py | 90 ++++--------------- 3 files changed, 20 insertions(+), 76 deletions(-) diff --git a/Mongo/legos/mongodb_compare_disk_size_to_threshold/mongodb_compare_disk_size_to_threshold.py b/Mongo/legos/mongodb_compare_disk_size_to_threshold/mongodb_compare_disk_size_to_threshold.py index 88dad2f55..3a8c49ac0 100644 --- a/Mongo/legos/mongodb_compare_disk_size_to_threshold/mongodb_compare_disk_size_to_threshold.py +++ b/Mongo/legos/mongodb_compare_disk_size_to_threshold/mongodb_compare_disk_size_to_threshold.py @@ -8,7 +8,7 @@ class InputSchema(BaseModel): threshold: Optional[float] = Field( - 52428800, # 50GB in KB + 83886080 , # 80GB in KB description='Threshold for disk size in KB.', title='Threshold (in KB)' ) @@ -24,7 +24,7 @@ def mongodb_compare_disk_size_to_threshold_printer(output): print(f"Alert! Disk size of {alert['totalDiskSize']} KB for database {alert['db']} exceeds threshold of {alert['threshold']} KB.") -def mongodb_compare_disk_size_to_threshold(handle, threshold: float=52428800) -> Tuple: +def mongodb_compare_disk_size_to_threshold(handle, threshold: float=83886080) -> Tuple: """ mongodb_compare_disk_size_to_threshold compares the total disk size used by MongoDB to a given threshold. diff --git a/Postgresql/legos/postgresql_get_cache_hit_ratio/postgresql_get_cache_hit_ratio.json b/Postgresql/legos/postgresql_get_cache_hit_ratio/postgresql_get_cache_hit_ratio.json index bb0defb1e..5defb77ff 100644 --- a/Postgresql/legos/postgresql_get_cache_hit_ratio/postgresql_get_cache_hit_ratio.json +++ b/Postgresql/legos/postgresql_get_cache_hit_ratio/postgresql_get_cache_hit_ratio.json @@ -4,7 +4,7 @@ "action_type": "LEGO_TYPE_POSTGRESQL", "action_entry_function": "postgresql_get_cache_hit_ratio", "action_needs_credential": true, - "action_is_check": true, + "action_is_check": false, "action_supports_poll": true, "action_output_type": "ACTION_OUTPUT_TYPE_LIST", "action_supports_iteration": true, diff --git a/Postgresql/legos/postgresql_get_server_status/postgresql_get_server_status.py b/Postgresql/legos/postgresql_get_server_status/postgresql_get_server_status.py index eb43439eb..2be62afa4 100644 --- a/Postgresql/legos/postgresql_get_server_status/postgresql_get_server_status.py +++ b/Postgresql/legos/postgresql_get_server_status/postgresql_get_server_status.py @@ -2,94 +2,38 @@ # Copyright (c) 2023 unSkript, Inc # All rights reserved. ## -from typing import Tuple, Optional -from pydantic import BaseModel, Field +from typing import Tuple +from pydantic import BaseModel class InputSchema(BaseModel): - connection_threshold: Optional[int] = Field( - 10000, - title='Connection threshold', - description='Threshold for the number of connections considered abnormal. Default- 10000 clients') - cache_hit_ratio_threshold: Optional[int] = Field( - 90, - title='Cache hit ratio threshold (in %)', - description='Threshold for the cache hit ratio considered abnormal. Default- 90%') - blocked_query_threshold: Optional[int] = Field( - 10000, - title='Blocked query threshold', - description='TThreshold for the number of blocked queries considered abnormal. Default- 10000') + pass def postgresql_get_server_status_printer(output): - is_healthy, server_status_info = output - - print("PostgreSQL Server Status:") - print(f" Overall Health: {'Healthy' if is_healthy else 'Unhealthy'}") - print(f" Total Connections: {server_status_info['total_connections']}") - print(f" Cache Hit Ratio: {server_status_info['cache_hit_ratio']}%") - print(f" Blocked Queries: {server_status_info['blocked_queries']}") - - abnormal_metrics = server_status_info.get('abnormal_metrics') - if abnormal_metrics: - print("\nAbnormal Metrics:") - for metric in abnormal_metrics: - print(f" - {metric}") - -def postgresql_get_server_status(handle, connection_threshold: int = 10000, cache_hit_ratio_threshold: int = 90, blocked_query_threshold: int = 10000) -> Tuple: + if output[0]: + print("PostgreSQL Server Status: Reachable") + else: + error_message = output[1]['message'] if output[1] else "Unknown error" + print("PostgreSQL Server Status: Unreachable") + print(f"Error: {error_message}") + +def postgresql_get_server_status(handle) -> Tuple: """ - Returns the status of the PostgreSQL instance. + Returns a simple status indicating the reachability of the PostgreSQL server. :type handle: object :param handle: PostgreSQL connection object - :type handle: object - :param connection_threshold: Threshold for the number of connections considered abnormal - - :type handle: object - :param cache_hit_ratio_threshold: Threshold for the cache hit ratio considered abnormal - - :type handle: object - :param blocked_query_threshold: Threshold for the number of blocked queries considered abnormal - - :return: Tuple containing a status and a dictionary with detailed information + :return: Tuple containing a boolean indicating success and optional error message """ - server_status_info = {} - abnormal_metrics = [] - try: cur = handle.cursor() - - # Check total number of connections - cur.execute("SELECT COUNT(*) FROM pg_stat_activity;") - total_connections = cur.fetchone()[0] - server_status_info['total_connections'] = total_connections - if total_connections > connection_threshold: - abnormal_metrics.append(f"High number of connections: {total_connections}") - - # Check cache hit ratio - cur.execute("SELECT sum(heap_blks_hit) / sum(heap_blks_hit + heap_blks_read) * 100 AS ratio FROM pg_statio_user_tables;") - cache_hit_ratio = cur.fetchone()[0] - server_status_info['cache_hit_ratio'] = cache_hit_ratio - if cache_hit_ratio < cache_hit_ratio_threshold: - abnormal_metrics.append(f"Cache hit ratio below {cache_hit_ratio_threshold}%: {cache_hit_ratio}") - - # Check blocked queries - cur.execute("SELECT COUNT(*) FROM pg_locks WHERE granted = false;") - blocked_queries = cur.fetchone()[0] - server_status_info['blocked_queries'] = blocked_queries - if blocked_queries > blocked_query_threshold: - abnormal_metrics.append(f"Blocked queries above threshold: {blocked_queries}") - - # Append abnormal metrics if any are found - if abnormal_metrics: - server_status_info['abnormal_metrics'] = abnormal_metrics - return (False, server_status_info) - - return (True, server_status_info) - + cur.execute("SELECT 1;") + cur.fetchone() + return (True, None) except Exception as e: - raise e + return (False, {"message": str(e)}) finally: handle.close()