|
| 1 | +""" |
| 2 | +Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | +Licensed under the Universal Permissive License v1.0 as shown at |
| 4 | +https://oss.oracle.com/licenses/upl. |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +from logging import Logger |
| 9 | +from typing import Annotated |
| 10 | + |
| 11 | +import oci |
| 12 | +from fastmcp import FastMCP |
| 13 | +from oracle.oci_monitoring_mcp_server import __project__, __version__ |
| 14 | + |
| 15 | +logger = Logger(__name__, level="INFO") |
| 16 | + |
| 17 | +mcp = FastMCP(name=__project__) |
| 18 | + |
| 19 | + |
| 20 | +class MonitoringAlarmTools: |
| 21 | + def __init__(self): |
| 22 | + logger.info("Loaded metric metadata entries") |
| 23 | + |
| 24 | + def register(self, mcp): |
| 25 | + """Register all Metrics tools with the MCP server.""" |
| 26 | + # Register list_alarms tool |
| 27 | + mcp.tool(name="list_alarms")(self.list_alarms) |
| 28 | + |
| 29 | + def get_monitoring_client(self): |
| 30 | + logger.info("entering get_monitoring_client") |
| 31 | + config = oci.config.from_file( |
| 32 | + profile_name=os.getenv("OCI_CONFIG_PROFILE", oci.config.DEFAULT_PROFILE) |
| 33 | + ) |
| 34 | + user_agent_name = __project__.split("oracle.", 1)[1].split("-server", 1)[0] |
| 35 | + config["additional_user_agent"] = f"{user_agent_name}/{__version__}" |
| 36 | + |
| 37 | + private_key = oci.signer.load_private_key_from_file(config["key_file"]) |
| 38 | + token_file = config["security_token_file"] |
| 39 | + token = None |
| 40 | + with open(token_file, "r") as f: |
| 41 | + token = f.read() |
| 42 | + signer = oci.auth.signers.SecurityTokenSigner(token, private_key) |
| 43 | + return oci.monitoring.MonitoringClient(config, signer=signer) |
| 44 | + |
| 45 | + def list_alarms( |
| 46 | + self, |
| 47 | + compartment_id: Annotated[ |
| 48 | + str, |
| 49 | + "The ID of the compartment containing the resources" |
| 50 | + "monitored by the metric that you are searching for.", |
| 51 | + ], |
| 52 | + ) -> list[dict]: |
| 53 | + monitoring_client = self.get_monitoring_client() |
| 54 | + response = monitoring_client.list_alarms(compartment_id=compartment_id) |
| 55 | + alarms = response.data |
| 56 | + result = [] |
| 57 | + for alarm in alarms: |
| 58 | + result.append( |
| 59 | + { |
| 60 | + "id": alarm.id, |
| 61 | + "display_name": alarm.display_name, |
| 62 | + "severity": alarm.severity, |
| 63 | + "lifecycle_state": alarm.lifecycle_state, |
| 64 | + "namespace": alarm.namespace, |
| 65 | + "query": alarm.query, |
| 66 | + } |
| 67 | + ) |
| 68 | + return result |
0 commit comments