Skip to main content
Glama

get_jstat_output

Execute jstat commands to monitor JVM performance metrics like garbage collection, class loading, and compiler statistics for Java processes.

Instructions

执行 jstat 监控命令

        Args:
            pid (str): 进程ID,使用字符串形式(如:"12345")
            option (Optional[str]): jstat选项,如gc、class、compiler等
            interval (str): 采样间隔(毫秒),使用字符串形式(如:"1000"表示1秒)
            count (str): 采样次数,使用字符串形式(如:"10")

        Returns:
            Dict: 包含jstat执行结果的字典,包含以下字段:
                - raw_output (str): 原始输出
                - timestamp (float): 时间戳
                - success (bool): 是否成功
                - error (Optional[str]): 错误信息
        

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pidNo
optionNo
intervalNo
countNo

Implementation Reference

  • Main handler function for the 'get_jstat_output' MCP tool. Validates inputs (PID, interval, count), executes JstatCommand via the executor, and formats the response dictionary with raw output, success status, and error handling.
    @self.mcp.tool()
    def get_jstat_output(pid: str = "", 
                        option: Optional[str] = None, 
                        interval: str = "", 
                        count: str = "") -> Dict:
        """执行 jstat 监控命令
    
        Args:
            pid (str): 进程ID,使用字符串形式(如:"12345")
            option (Optional[str]): jstat选项,如gc、class、compiler等
            interval (str): 采样间隔(毫秒),使用字符串形式(如:"1000"表示1秒)
            count (str): 采样次数,使用字符串形式(如:"10")
    
        Returns:
            Dict: 包含jstat执行结果的字典,包含以下字段:
                - raw_output (str): 原始输出
                - timestamp (float): 时间戳
                - success (bool): 是否成功
                - error (Optional[str]): 错误信息
        """
        try:
            validated_pid = self._validate_and_convert_id(pid if pid else None, "process ID")
            if validated_pid is None:
                return {
                    "raw_output": "",
                    "timestamp": time.time(),
                    "success": False,
                    "error": "Invalid process ID"
                }
            
            validated_interval = self._validate_and_convert_id(interval if interval else None, "interval")
            validated_count = self._validate_and_convert_id(count if count else None, "count")
            
        except ValueError as e:
            return {
                "raw_output": "",
                "timestamp": time.time(),
                "success": False,
                "error": str(e)
            }
        
        cmd = JstatCommand(self.executor, JstatFormatter())
        result = cmd.execute(str(validated_pid), option=option, interval=validated_interval, count=validated_count)
        return {
            "raw_output": result.get('output', ''),
            "timestamp": time.time(),
            "success": result.get('success', False),
            "error": result.get('error')
        }
  • Helper classes JstatCommand and JstatFormatter. JstatCommand constructs the jstat command line (jstat -option pid [interval [count]]). JstatFormatter formats the command result into a simple dictionary with success, output/error, and metadata.
    class JstatCommand(BaseCommand):
        """Jstat命令实现"""
    
        def __init__(self, executor, formatter):
            super().__init__(executor, formatter)
            self.timeout = 30
    
        def get_command(
                self,
                pid: str,
                option: Optional[str] = None,
                interval: Optional[int] = None,
                count: Optional[int] = None,
                *args,
                **kwargs) -> str:
            # option: gc, gccapacity, class, compiler, util, ...
            cmd = f'jstat'
            if option:
                cmd += f' -{option}'
            cmd += f' {pid}'
            if interval is not None:
                cmd += f' {interval}'
                if count is not None:
                    cmd += f' {count}'
            return cmd
    
    class JstatFormatter(OutputFormatter):
        """Jstat输出格式化器(仅文本输出)"""
    
        def format(self, result: CommandResult) -> Dict[str, Any]:
            if not result.success:
                return {
                    "success": False,
                    "error": result.error,
                    "timestamp": result.timestamp.isoformat()
                    }
            return {
                "success": True,
                "output": result.output,
                "execution_time": result.execution_time,
                "timestamp": result.timestamp.isoformat()
                }

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/xzq-xu/jvm-mcp-server'

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