|
4 | 4 | This module contains tools for getting the server status, testing the connection, and getting the buckets in the cluster, the scopes and collections in the bucket. |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import json |
7 | 8 | import logging |
8 | 9 | from typing import Any |
9 | 10 |
|
@@ -137,3 +138,43 @@ def get_collections_in_scope( |
137 | 138 | ctx, query, bucket_name=bucket_name, scope_name=scope_name |
138 | 139 | ) |
139 | 140 | return [result["collection_name"] for result in results] |
| 141 | + |
| 142 | + |
| 143 | +def get_cluster_health_and_services( |
| 144 | + ctx: Context, bucket_name: str | None = None |
| 145 | +) -> dict[str, Any]: |
| 146 | + """Get cluster health status and list of all running services. |
| 147 | +
|
| 148 | + This tool provides health monitoring by: |
| 149 | + - Getting health status of all running services with latency information (via ping) |
| 150 | + - Listing all services running on the cluster with their endpoints |
| 151 | + - Showing connection status and node information for each service |
| 152 | +
|
| 153 | + If bucket_name is provided, it actively pings services from the perspective of the bucket. |
| 154 | + Otherwise, it uses cluster-level ping to get the health status of the cluster. |
| 155 | +
|
| 156 | + Returns: |
| 157 | + - Cluster health status with service-level connection details and latency measurements |
| 158 | + """ |
| 159 | + try: |
| 160 | + cluster = get_cluster_connection(ctx) |
| 161 | + |
| 162 | + if bucket_name: |
| 163 | + # Ping services from the perspective of the bucket |
| 164 | + bucket = connect_to_bucket(cluster, bucket_name) |
| 165 | + result = bucket.ping().as_json() |
| 166 | + else: |
| 167 | + # Ping services from the perspective of the cluster |
| 168 | + result = cluster.ping().as_json() |
| 169 | + |
| 170 | + return { |
| 171 | + "status": "success", |
| 172 | + "data": json.loads(result), |
| 173 | + } |
| 174 | + except Exception as e: |
| 175 | + logger.error(f"Error getting cluster health: {e}") |
| 176 | + return { |
| 177 | + "status": "error", |
| 178 | + "error": str(e), |
| 179 | + "message": "Failed to get cluster health and services information", |
| 180 | + } |
0 commit comments