Skip to main content
Glama
Heht571
by Heht571

analyze_logs

Analyzes server log files to identify errors, warnings, and critical issues using customizable search patterns for troubleshooting.

Instructions

分析服务器日志文件中的错误和警告

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostnameYes
usernameYes
passwordNo
portNo
log_fileNo/var/log/syslog
patternNoerror|fail|critical
linesNo
timeoutNo

Implementation Reference

  • Primary handler function for the 'analyze_logs' tool. Connects to remote server via SSH, retrieves recent log lines, filters for error patterns, parses and categorizes log entries by level (critical, error, warning, fail, other), and returns a structured summary with counts and entries.
    @handle_exceptions
    def analyze_logs(
        hostname: str,
        username: str,
        password: str = "",
        port: int = 22,
        log_file: str = "/var/log/syslog",
        pattern: str = "error|fail|critical",
        lines: int = 100,
        timeout: int = 30
    ) -> dict:
        """分析服务器日志文件中的错误和警告"""
        result = {"status": "unknown", "entries": [], "summary": {}, "error": ""}
    
        try:
            with SSHManager(hostname, username, password, port, timeout) as ssh:
                # 获取日志的最后几行
                tail_command = f"tail -n {lines} {log_file}"
                stdin, stdout, stderr = ssh.exec_command(tail_command, timeout=timeout)
                log_output = stdout.read().decode().strip()
    
                if not log_output:
                    result["error"] = f"无法读取日志文件 {log_file}"
                    result["status"] = "error"
                    return result
    
                # 使用grep过滤包含指定模式的行
                grep_command = f"echo '{log_output}' | grep -E '{pattern}'"
                stdin, stdout, stderr = ssh.exec_command(grep_command, timeout=timeout)
                matched_output = stdout.read().decode().strip()
    
                # 初始化计数器
                pattern_counts = {
                    "critical": 0,
                    "error": 0,
                    "warning": 0,
                    "fail": 0,
                    "other": 0
                }
    
                entries = []
    
                for line in matched_output.split('\n'):
                    if not line:
                        continue
    
                    # 尝试提取时间戳
                    timestamp = ""
                    try:
                        # 假设日志的前部分是时间戳
                        timestamp_part = ' '.join(line.split()[:3])
                        timestamp = timestamp_part
                    except:
                        pass
    
                    # 确定日志级别
                    level = "other"
                    line_lower = line.lower()
                    if "critical" in line_lower:
                        level = "critical"
                        pattern_counts["critical"] += 1
                    elif "error" in line_lower:
                        level = "error"
                        pattern_counts["error"] += 1
                    elif "warning" in line_lower or "warn" in line_lower:
                        level = "warning"
                        pattern_counts["warning"] += 1
                    elif "fail" in line_lower:
                        level = "fail"
                        pattern_counts["fail"] += 1
                    else:
                        pattern_counts["other"] += 1
    
                    entries.append({
                        "timestamp": timestamp,
                        "level": level,
                        "message": line
                    })
    
                result["entries"] = entries
                result["summary"] = {
                    "total_entries": len(entries),
                    "counts_by_level": pattern_counts
                }
    
                result["status"] = "success"
    
        except Exception as e:
            result["status"] = "error"
            result["error"] = str(e)
    
        return result
  • Secondary handler function for the 'analyze_logs' tool in the non-SSE codebase. Nearly identical logic to the SSE version.
    @handle_exceptions
    def analyze_logs(
        hostname: str,
        username: str,
        password: str = "",
        port: int = 22,
        log_file: str = "/var/log/syslog",
        pattern: str = "error|fail|critical",
        lines: int = 100,
        timeout: int = 30
    ) -> dict:
        """分析服务器日志文件中的错误和警告"""
        result = {"status": "unknown", "entries": [], "summary": {}, "error": ""}
    
        try:
            with SSHManager(hostname, username, password, port, timeout) as ssh:
                # 获取日志的最后几行
                tail_command = f"tail -n {lines} {log_file}"
                stdin, stdout, stderr = ssh.exec_command(tail_command, timeout=timeout)
                log_output = stdout.read().decode().strip()
    
                if not log_output:
                    result["error"] = f"无法读取日志文件 {log_file}"
                    result["status"] = "error"
                    return result
    
                # 搜索匹配的日志条目
                grep_command = f"grep -E '{pattern}' <<< '{log_output}'"
                stdin, stdout, stderr = ssh.exec_command(grep_command, timeout=timeout)
                matched_output = stdout.read().decode().strip()
    
                # 解析匹配的日志条目
                entries = []
                pattern_counts = {"error": 0, "warning": 0, "critical": 0, "fail": 0, "other": 0}
    
                for line in matched_output.split('\n'):
                    if not line:
                        continue
    
                    # 尝试提取时间戳
                    timestamp = ""
                    try:
                        # 假设日志的前部分是时间戳
                        timestamp_part = ' '.join(line.split()[:3])
                        timestamp = timestamp_part
                    except:
                        pass
    
                    # 确定日志级别
                    level = "other"
                    line_lower = line.lower()
                    if "critical" in line_lower:
                        level = "critical"
                        pattern_counts["critical"] += 1
                    elif "error" in line_lower:
                        level = "error"
                        pattern_counts["error"] += 1
                    elif "warning" in line_lower or "warn" in line_lower:
                        level = "warning"
                        pattern_counts["warning"] += 1
                    elif "fail" in line_lower:
                        level = "fail"
                        pattern_counts["fail"] += 1
                    else:
                        pattern_counts["other"] += 1
    
                    entries.append({
                        "timestamp": timestamp,
                        "level": level,
                        "message": line
                    })
    
                result["entries"] = entries
                result["summary"] = {
                    "total_entries": len(entries),
                    "counts_by_level": pattern_counts
                }
    
                result["status"] = "success"
    
        except Exception as e:
            result["status"] = "error"
            result["error"] = str(e)
    
        return result
  • Tool dispatch/registration in the SSE server handler. Maps tool name 'analyze_logs' to the analyze_logs function call with argument validation.
    elif name == "analyze_logs":
        required_args = ["hostname", "username"]
        for arg in required_args:
            if arg not in arguments:
                raise ValueError(f"Missing required argument '{arg}'")
    
        result = analyze_logs(
            hostname=arguments["hostname"],
            username=arguments["username"],
            password=arguments.get("password", ""),
            port=arguments.get("port", 22),
            log_file=arguments.get("log_file", "/var/log/syslog"),
            pattern=arguments.get("pattern", "error|fail|critical"),
            lines=arguments.get("lines", 100),
            timeout=arguments.get("timeout", 30)
        )
  • Tool schema definition for 'analyze_logs', including name, description, and parameter types/defaults used in list_available_tools.
    {"name": "analyze_logs", "description": "分析服务器日志文件中的错误和警告", "parameters": [
        {"name": "hostname", "type": "str", "default": None},
        {"name": "username", "type": "str", "default": None},
        {"name": "password", "type": "str", "default": ""},
        {"name": "port", "type": "int", "default": 22},
        {"name": "log_file", "type": "str", "default": "/var/log/syslog"},
        {"name": "pattern", "type": "str", "default": "error|fail|critical"},
        {"name": "lines", "type": "int", "default": 100},
        {"name": "timeout", "type": "int", "default": 30}
    ]},
  • Tool registration dictionary mapping 'analyze_logs' to the handler function, used to dynamically register tools in FastMCP.
    tools_dict = {
        'get_memory_info': get_memory_info,
        'remote_server_inspection': remote_server_inspection,
        'get_system_load': get_system_load,
        'monitor_processes': monitor_processes,
        'check_service_status': check_service_status,
        'get_os_details': get_os_details,
        'check_ssh_risk_logins': check_ssh_risk_logins,
        'check_firewall_config': check_firewall_config,
        'security_vulnerability_scan': security_vulnerability_scan,
        'backup_critical_files': backup_critical_files,
        'inspect_network': inspect_network,
        'analyze_logs': analyze_logs,
        'list_docker_containers': list_docker_containers,
        'list_docker_images': list_docker_images,
        'list_docker_volumes': list_docker_volumes,
        'get_container_logs': get_container_logs,
        'monitor_container_stats': monitor_container_stats,
        'check_docker_health': check_docker_health
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. While 'analyze' implies a read-only operation, the description doesn't specify whether this requires authentication, what permissions are needed, how results are returned, or any rate limits. It mentions analyzing 'errors and warnings' but doesn't explain the analysis methodology or output format.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that gets straight to the point. There's no wasted language or unnecessary elaboration. It's appropriately sized for a tool with this level of complexity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 8 parameters, no annotations, no output schema, and 0% schema description coverage, the description is insufficient. It doesn't explain what the analysis produces, how results are structured, what authentication is required, or provide any parameter context. The agent would struggle to use this tool effectively without additional information.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description provides no information about any of the 8 parameters, and schema description coverage is 0%. The agent must rely entirely on parameter titles and types in the schema, with no contextual explanation of what 'hostname', 'username', 'pattern', or other parameters mean in this specific analysis context.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '分析服务器日志文件中的错误和警告' (analyze errors and warnings in server log files). It specifies both the verb ('analyze') and resource ('server log files'), making it easy to understand what the tool does. However, it doesn't differentiate from sibling tools like 'get_container_logs' or 'check_ssh_risk_logins', which prevents a perfect score.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. There's no mention of prerequisites, when this tool is appropriate versus other logging tools like 'get_container_logs', or any exclusions. The agent must infer usage from the tool name and parameters alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Heht571/ops-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server