S2模拟器MCP服务器
概述
该项目实现了一个MCP(多云平台)服务器,旨在与S2可观测性模拟器进行交互和控制。它允许用户在稳态(基线)模式或异常注入模式下生成网络可观测性度量,模拟各种网络问题。在运行此程序之前,请确保您拥有所有必要的文件。
这是一个学习MCP的实验,因此它不够干净,无法直接调用API,而是依赖于在后台调用另一个工具。但它做它的工作:) 因此,需要事先准备好文件
特性
- 稳态仿真:生成基线网络可观测性度量。
- 异常注射:模拟各种网络问题,如CPU峰值、内存峰值、链路故障、BGP切换等。
- 灵活输入:通过结构化JSON有效载荷或自由格式的命令字符串接受意图。
- 后台处理:将模拟作为后台进程运行,允许非阻塞操作。
- 会话管理:停止并监视正在运行的模拟会话。
安装
- 克隆存储库 (如果你还没有)。和https://github.com/ARD92/s2-observability-simulator.git这是模拟器工具。
- 安装依赖项:服务器依赖于
mcp和python-dotenv.
pip install -r requirements.txt- 确保
generate_all_stats.py可用的:此服务器需要generate_all_stats.py脚本位于由指定的目录中SCRIPT_DIR(默认值:/root/s2-observability-simulator/).您可能需要调整SCRIPT_DIR和PYTHON_BIN如果您的设置不同,则使用环境变量。克隆https://github.com/ARD92/s2-observability-simulator.git获取该工具
用法
MCP服务器配置
{
"mcpServers": {
"simulator": {
"command": "npx",
"args": [
"mcp-remote",
"http://192.168.1.10:8050/mcp/",
"--allow-http"
]
}
}
}MCP服务器公开了以下工具:
simulator_intent:开始新的模拟。simulator_stop_run:停止正在运行的后台模拟。simulator_status:列出所有跟踪的模拟过程及其状态。
启动MCP服务器
要启动MCP服务器,请运行 main.py:
python main.py运行模拟
模拟可以使用以下方式启动 simulator_intent 工具。您可以提供结构化的JSON有效负载或自由形式的文本,并允许llm进行解释。
稳态模式
要使用拓扑文件和可选的保证/BPG文件生成稳态指标:
{
"mode": "steady",
"topology": "topology.csv",
"assurance": "assurance.csv",
"bgp": "bgp.csv",
"tunnel": "tunnel.csv"
}异常模式
要注入异常,请设置 mode “异常”并提供 anomaly 有效载荷:
{
"mode": "anomaly",
"topology": "topology.csv",
"assurance": "assurance.csv",
"bgp": "bgp.csv",
"anomaly": {"cpuspike": ["nylf01"], "memspike": ["nyag01"], "linkdown": ["nylf01_et-0/0/0"]}
}use simulator tool with topology=mcp-topology.csv, bgp = mcp-bgp_config.csv, mode=anomaly, anamoly cpuspike for CASPJNP01, memspike for device CASPJNP02, linkdown on CALFJNP02 interface et-0/0/0
If you want to modify existing , use the following prompt
> kill the above session and reinitiate the same with interface et-0/0/4 instead of et-0/0/0
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ simulator_stop_run (simulator MCP Server) {"payload":{"proc_id":"5db1c996-3d88-48b8-a047-85b2e01c39cb"},"ctx":""} │
│ │
│ { │
│ "status": "stopped", │
│ "proc_id": "5db1c996-3d88-48b8-a047-85b2e01c39cb", │
│ "stopped": true, │
│ "signal": "SIGTERM" │
│ } │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ simulator_intent (simulator MCP Server) {"ctx":"","payload":{"topology":"mcp-topology.csv","mode":"anomaly","bgp":"mcp-bgp_config.csv","anoma… │
│ │
│ { │
│ "status": "spawned", │
│ "proc_id": "df5ee7b1-cf7a-4e2b-929d-09a3e66b84ed", │
│ "pid": 1389897 │
│ } │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The previous simulator session has been stopped. I have re-initiated the simulator with the same anomalies, but with the linkdown on CALFJNP02
interface et-0/0/4.异常类型
支持以下异常类型 anomaly 有效载荷:
| 关键字 | 描述/示例用法 |
|---|---|
trafficprofile | 修改给定接口上的流量。 |
例子: \'trafficprofile\': [\'nylf01_et-0/0/0_50-2-2\'] | |
linkdown | 标记一个或多个链接。 |
例子: \'linkdown\': [\'nylf01_et-0/0/0\'] | |
nodedown | 标记一个或多个节点。 |
例子: \'nodedown\': [\'nylf01\', \'nyag01\'] | |
intflap | 启用界面摆动。 |
例子: \'intflap\': [\'nylf01_et-0/0/0\'] | |
intindisc | 引入界面输入丢弃或错误。 |
例子: \'intindisc\': [\'nylf01_et-0/0/0\'] | |
cpuspike | 模拟给定节点上的CPU利用率峰值。 |
例子: \'cpuspike\': [\'nylf01\'] | |
memspike | 模拟给定节点上的内存尖峰。 |
例子: \'memspike\': [\'nylf01\'] | |
assurance | 将异常注入保证指标。 |
例子: \'assurance\': [\'nylf01\'] | |
bgpflap | 模拟BGP会话翻转事件。 |
例子: \'bgpflap\': [\'nylf01\'] | |
tundown | 将一条隧道标记为向下。 |
例子: \'tundown\': [\'tun01\'] | |
tunflap | 模拟隧道拍打。 |
例子: \'tunflap\': [\'tun01\'] | |
intoutdisc | 引入接口输出丢弃。 |
例子: \'intoutdisc\': [\'nylf01_et-0/0/0\'] | |
intinerr | 引入界面输入错误。 |
例子: \'intinerr\': [\'nylf01_et-0/0/0\'] | |
intouterr | 引入接口输出错误。 |
例子: \'intouterr\': [\'nylf01_et-0/0/0\'] | |
ospfflap | 引入接口输出错误。 |
例子: \'intouterr\': [\'nylf01_et-0/0/0\'] | |
ospfdown | 引入接口输出错误。 |
例子: \'intouterr\': [\'nylf01_et-0/0/0\'] |
管理会话
停止会话
要停止正在运行的会话,请使用 simulator_stop_run 随着 proc_id 生成命令时获得:
{
"proc_id": "YOUR_PROCESS_ID"
}或
>> > kill all sessions
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ simulator_stop_run (simulator MCP Server) {"payload":{"proc_id":"38018225-c96b-495a-8f90-cc1bc524c5b6"},"ctx":""} │
│ │
│ { │
│ "status": "stopped", │
│ "proc_id": "38018225-c96b-495a-8f90-cc1bc524c5b6", │
│ "stopped": true, │
│ "signal": "SIGTERM" │
│ } │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ All active simulator sessions have been stopped.
查看会话状态
要查看所有活动和已完成的模拟器会话:
{}(空有效载荷 simulator_status)或使用
>> show all sessions
Here are all the simulator sessions:
* Session ID: 5db1c996-3d88-48b8-a047-85b2e01c39cb (Finished)
* Command: generate_all_stats.py -t mcp-topology.csv -a anomaly -b mcp-bgp_config.csv -av {"memspike": ["CASPJNP02"], "cpuspike": ["CASPJNP01"],
"linkdown": ["CALFJNP02_et-0/0/0"]}
* Session ID: df5ee7b1-cf7a-4e2b-929d-09a3e66b84ed (Finished)
* Command: generate_all_stats.py -t mcp-topology.csv -a anomaly -b mcp-bgp_config.csv -av {"cpuspike": ["CASPJNP01"], "linkdown":
["CALFJNP02_et-0/0/4"], "memspike": ["CASPJNP02"]}
* Session ID: 38018225-c96b-495a-8f90-cc1bc524c5b6 (Running)
* Command: generate_all_stats.py -t mcp-topology.csv -a anomaly -b mcp-bgp_config.csv -av {"cpuspike": ["CASPJNP01"], "linkdown":
["CALFJNP02_et-0/0/1"], "memspike": ["CASPJNP02"]}回放日志
> use simulator with mode replay , file logs.csv and topology backbone_topology.csv
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ x simulator_intent (simulator MCP Server) {"ctx":"simulator","payload":{"logfile":"logs.csv","topology":"backbone_topology.csv","mode":"replay"}} │
│ │
│ MCP tool 'simulator_intent' reported tool error for function call: │
│ {"name":"simulator_intent","args":{"ctx":"simulator","payload":{"logfile":"logs.csv","topology":"backbone_topology.csv","mode":"replay"}}} with response: │
│ [{"functionResponse":{"name":"simulator_intent","response":{"error":{"content":[{"type":"text","text":"Error executing tool simulator_intent: replay_file is required for replay │
│ mode"}],"isError":true}}}}] │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I apologize for the error. It seems the correct parameter for the log file in replay mode is replay_file. I will restart the simulator with the correct parameter.
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ ✓ simulator_intent (simulator MCP Server) {"ctx":"simulator","payload":{"topology":"backbone_topology.csv","replay_file":"logs.csv","mode":"replay"}} │
│ │
│ { │
│ "status": "done", │
│ "cmd": [ │
│ "/root/mcp-sim/.venv/bin/python3", │
│ "generate_all_stats.py", │
│ "-t", │
│ "backbone_topology.csv", │
│ "-a", │
│ "replay", │
│ "-f", │
│ "logs.csv" │
│ ], │
│ "returncode": 0, │
│ "stdout": "file is logs. sending to logminer\n", │
│ "stderr": "" │
│ } │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The simulator has successfully completed the replay mode operation using logs.csv and backbone_topology.csv.
