Skip to content

Commit

Permalink
Update postgres and mongo checks
Browse files Browse the repository at this point in the history
  • Loading branch information
shloka-bhalgat-unskript committed Apr 25, 2024
1 parent 24fd7a1 commit 17432cd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
)
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 17432cd

Please sign in to comment.