template_get
Retrieve Zabbix templates using specific filters like template IDs, host group IDs, or host IDs. Returns a JSON-formatted list for easy integration and analysis.
Instructions
Get templates from Zabbix with optional filtering.
Args:
templateids: List of template IDs to retrieve
groupids: List of host group IDs to filter by
hostids: List of host IDs to filter by
output: Output format
search: Search criteria
filter: Filter criteria
Returns:
str: JSON formatted list of templates
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filter | No | ||
| groupids | No | ||
| hostids | No | ||
| output | No | extend | |
| search | No | ||
| templateids | No |
Implementation Reference
- src/zabbix_mcp_server.py:595-630 (handler)The handler function decorated with @mcp.tool() that implements the template_get MCP tool. It retrieves templates from the Zabbix API using client.template.get with optional filters and returns formatted JSON.def template_get(templateids: Optional[List[str]] = None, groupids: Optional[List[str]] = None, hostids: Optional[List[str]] = None, output: Union[str, List[str]] = "extend", search: Optional[Dict[str, str]] = None, filter: Optional[Dict[str, Any]] = None) -> str: """Get templates from Zabbix with optional filtering. Args: templateids: List of template IDs to retrieve groupids: List of host group IDs to filter by hostids: List of host IDs to filter by output: Output format (extend or list of specific fields) search: Search criteria filter: Filter criteria Returns: str: JSON formatted list of templates """ client = get_zabbix_client() params = {"output": output} if templateids: params["templateids"] = templateids if groupids: params["groupids"] = groupids if hostids: params["hostids"] = hostids if search: params["search"] = search if filter: params["filter"] = filter result = client.template.get(**params) return format_response(result)
- src/zabbix_mcp_server.py:595-595 (registration)The @mcp.tool() decorator registers the template_get function as an MCP tool.def template_get(templateids: Optional[List[str]] = None,
- src/zabbix_mcp_server.py:91-101 (helper)Helper function used by template_get to format the API response as indented JSON.def format_response(data: Any) -> str: """Format response data as JSON string. Args: data: Data to format Returns: str: JSON formatted string """ return json.dumps(data, indent=2, default=str)
- src/zabbix_mcp_server.py:38-80 (helper)Helper function used by template_get to obtain the authenticated Zabbix API client.def get_zabbix_client() -> ZabbixAPI: """Get or create Zabbix API client with proper authentication. Returns: ZabbixAPI: Authenticated Zabbix API client Raises: ValueError: If required environment variables are missing Exception: If authentication fails """ global zabbix_api if zabbix_api is None: url = os.getenv("ZABBIX_URL") if not url: raise ValueError("ZABBIX_URL environment variable is required") logger.info(f"Initializing Zabbix API client for {url}") # Configure SSL verification verify_ssl = os.getenv("VERIFY_SSL", "true").lower() in ("true", "1", "yes") logger.info(f"SSL certificate verification: {'enabled' if verify_ssl else 'disabled'}") # Initialize client zabbix_api = ZabbixAPI(url=url, validate_certs=verify_ssl) # Authenticate using token or username/password token = os.getenv("ZABBIX_TOKEN") if token: logger.info("Authenticating with API token") zabbix_api.login(token=token) else: user = os.getenv("ZABBIX_USER") password = os.getenv("ZABBIX_PASSWORD") if not user or not password: raise ValueError("Either ZABBIX_TOKEN or ZABBIX_USER/ZABBIX_PASSWORD must be set") logger.info(f"Authenticating with username: {user}") zabbix_api.login(user=user, password=password) logger.info("Successfully authenticated with Zabbix API") return zabbix_api