--- title: 高级配置 description: 调整 SwitchOps 的并发、重试、超时、告警阈值、输出位置和厂商命令模板。 updated: 2026-09-04 navigation: icon: lucide:settings --- ## 全局设置 全局配置文件为: ```text config/settings.yaml ``` 默认内容: ```yaml [config/settings.yaml] concurrency: 5 retry_times: 2 conn_timeout: 20 command_timeout: 60 cpu_warn_threshold: 80 cpu_critical_threshold: 90 memory_warn_threshold: 80 memory_critical_threshold: 90 output_root: "outputs" db_enabled: true ``` ## 设置项说明 | 设置 | 默认值 | 作用 | | --- | --- | --- | | `concurrency` | `5` | 同时巡检的最大设备数。 | | `retry_times` | `2` | 首次失败后的重试次数;默认最多尝试 3 次。 | | `conn_timeout` | `20` | SSH 连接、认证、Banner 和基础会话超时秒数。 | | `command_timeout` | `60` | 单条交换机命令的读取超时秒数。 | | `cpu_warn_threshold` | `80` | CPU 大于该值时标记关注。 | | `cpu_critical_threshold` | `90` | CPU 大于等于该值时标记异常。 | | `memory_warn_threshold` | `80` | 内存大于该值时标记关注。 | | `memory_critical_threshold` | `90` | 内存大于等于该值时标记异常。 | | `output_root` | `outputs` | 报告、日志、查询和历史库的根目录。 | | `db_enabled` | `true` | 是否保存 SQLite 历史记录。 | ## 调整并发和超时 大型网络不要直接把并发数调得很高。需要同时考虑: - 管理电脑 CPU、内存和网络能力; - 核心交换机允许的 SSH 并发会话数; - 防火墙、堡垒机或准入设备的连接限制; - 低性能接入交换机处理 CLI 命令的速度。 建议从 `5` 开始,小规模验证后逐步调整。跨区域链路延迟较大时,可以适当增加 `conn_timeout`;MAC 地址表较大或设备回显较慢时,可以增加 `command_timeout`。 ## 修改输出目录 使用绝对路径: ```yaml output_root: "D:/SwitchOpsData" ``` 使用相对路径时,路径相对于程序根目录: - 源码运行:项目根目录。 - EXE 运行:EXE 所在目录。 输出目录必须允许当前 Windows 用户创建目录和文件。 ## 关闭历史库 只需要当次 Excel,不需要长期趋势时: ```yaml db_enabled: false ``` 已有的 `outputs/history.db` 不会被删除,只是后续巡检不再写入。 ## 厂商命令模板 命令配置位于: ```text config/commands.yaml ``` 每个 profile 包含: ```yaml profiles: huawei: paging_disable: - "screen-length 0 temporary" inspect_commands: - "display version" - "display device" - "display health" - "display interface brief" - "display vlan" - "display mac-address" - "display arp all" query_commands: arp: "display arp all" mac: "display mac-address" interface: "display interface {interface}" ``` | 节点 | 作用 | | --- | --- | | `paging_disable` | 关闭分页,避免长回显等待交互。 | | `inspect_commands` | 每次巡检顺序执行的采集命令。 | | `query_commands.arp` | ARP 查询命令。 | | `query_commands.mac` | MAC 查询命令。 | | `query_commands.interface` | 接口查询模板,必须保留 `{interface}`。 | ## 只读命令安全校验 程序只允许以下前缀: - `display`(后跟空格) - `show`(后跟空格) - `screen-length`(后跟空格) 并拒绝包含以下独立危险词的命令: ```text current-configuration saved-configuration save reset delete reboot shutdown undo system-view configure ``` 命令不符合规则时,巡检会在连接设备前直接拒绝执行。 ::callout{type="warning"} 安全校验是最后一道防线,不是扩大账号权限的理由。修改 `commands.yaml` 前必须确认命令不会进入配置模式、改变运行状态或输出不应落盘的敏感配置。 :: ## 增加或适配命令 接入同平台不同型号时,推荐流程: 1. 在测试设备上人工确认只读命令和回显。 2. 复制现有 profile,并使用清晰的 profile 名称。 3. 在 `devices.yaml` 中让 `vendor` 或 `device_type` 对应该 profile。 4. 只添加安全校验允许的命令。 5. 先巡检单台设备。 6. 对比 `raw/` 原始回显与 Excel 解析结果。 7. 运行项目测试后再推广到全量设备。 项目现有纯函数测试不需要连接真实交换机: ```bash pytest tests/ ``` 需要支持全新厂商或完全不同的回显格式时,仅修改 `commands.yaml` 可能不够,还需要扩展 `switch_ops/parser.py` 的解析规则并补充测试。