You need to enable JavaScript to run this app. 如何优化生成Cisco接口Nagios监控对象的Ansible脚本
优惠活动
大模型
产品
解决方案
定价
更多

如何优化生成Cisco接口Nagios监控对象的Ansible脚本

Ansible Cisco Trunk接口监控角色优化方案

核心优化思路

  • 第一步先通过单次SNMP查询直接构建索引- trunk状态映射表,过滤出所有trunk状态为1的接口,后续所有操作仅针对这部分接口,完全排除非trunk接口的无效处理
  • 用批量SNMP walk替代单接口循环SNMP get,原本需要为每个接口单独发送的状态、名称、描述查询,改为直接拉取整个OID分支的所有返回结果,再和接口索引做关联,SNMP请求次数从和接口数量成正比降低为固定5次
  • 统一以iface_index作为唯一键存储所有接口属性,避免依赖循环下标对齐属性导致的关联错误,完美解决索引关联问题

优化后代码

---
- name: 获取所有接口的Trunk状态OID及返回值
  delegate_to: 127.0.0.1
  command: "snmpwalk -Oqn -v 3 -l authPriv -u {{ snmpv3user }} -a SHA -A {{ snmpv3_auth_key }} -x AES -X {{ snmpv3_priv_key }} {{ ansible_host }} .1.3.6.1.4.1.9.9.46.1.6.1.1.14"
  register: trunk_oid_raw

- name: 构建Trunk接口索引列表,仅保留trunk状态为1的接口
  set_fact:
    trunk_ifaces: >-
      {{
        trunk_ifaces | default({}) |
        combine({
          item.split(' ')[0].split('.')[-1]: item.split(' ')[1]
        })
      }}
  loop: "{{ trunk_oid_raw.stdout_lines }}"

- name: 筛选出trunk状态为1的接口索引列表
  set_fact:
    valid_trunk_indexes: "{{ trunk_ifaces | dict2items | selectattr('value', 'eq', '1') | map(attribute='key') | list }}"

- name: 批量获取所有接口运行状态
  delegate_to: 127.0.0.1
  command: "snmpwalk -Oqn -v 3 -l authPriv -u {{ snmpv3user }} -a SHA -A {{ snmpv3_auth_key }} -x AES -X {{ snmpv3_priv_key }} {{ ansible_host }} .1.3.6.1.2.1.2.2.1.8"
  register: oper_state_raw

- name: 构建索引-运行状态映射表
  set_fact:
    oper_state_map: >-
      {{
        oper_state_map | default({}) |
        combine({
          item.split(' ')[0].split('.')[-1]: item.split(' ')[1]
        })
      }}
  loop: "{{ oper_state_raw.stdout_lines }}"

- name: 批量获取所有接口名称
  delegate_to: 127.0.0.1
  command: "snmpwalk -Oqn -v 3 -l authPriv -u {{ snmpv3user }} -a SHA -A {{ snmpv3_auth_key }} -x AES -X {{ snmpv3_priv_key }} {{ ansible_host }} .1.3.6.1.2.1.31.1.1.1.1"
  register: iface_name_raw

- name: 构建索引-接口名称映射表
  set_fact:
    iface_name_map: >-
      {{
        iface_name_map | default({}) |
        combine({
          item.split(' ')[0].split('.')[-1]: item.split(' ')[1][1:-1]
        })
      }}
  loop: "{{ iface_name_raw.stdout_lines }}"

- name: 批量获取所有接口描述
  delegate_to: 127.0.0.1
  command: "snmpwalk -Oqn -v 3 -l authPriv -u {{ snmpv3user }} -a SHA -A {{ snmpv3_auth_key }} -x AES -X {{ snmpv3_priv_key }} {{ ansible_host }} .1.3.6.1.2.1.31.1.1.1.18"
  register: iface_desc_raw

- name: 构建索引-接口描述映射表
  set_fact:
    iface_desc_map: >-
      {{
        iface_desc_map | default({}) |
        combine({
          item.split(' ')[0].split('.')[-1]: item.split(' ')[1][1:-1]
        })
      }}
  loop: "{{ iface_desc_raw.stdout_lines }}"

- name: 生成Nagios监控配置
  set_fact:
    nagios_trunks: |
      #
      # Monitor trunking interfaces
      #
      {% for iface_idx in valid_trunk_indexes %}
      {% if oper_state_map[iface_idx] == "1" %}
      define service {
          use                     generic-service
          host_name               {{ inventory_hostname }} 
          service_description     {{ inventory_hostname }}:{{ iface_name_map[iface_idx] }} - {{ iface_desc_map[iface_idx] }}
          check_command           check-snmp-v3!.1.3.6.1.2.1.2.2.1.8.{{ iface_idx }} -r 1
          notifications_enabled   0
      }
      #
      define service {
          use                     generic-service
          host_name               {{ inventory_hostname }}
          service_description     Link Utilisation INPUT - {{ iface_desc_map[iface_idx] }}
          check_command           check-snmp-v3!.1.3.6.1.2.1.31.1.1.1.6.{{ iface_idx }} --rate
          check_interval          5
          notifications_enabled   0
      }
      #
      define service {
          use                     generic-service
          host_name               {{ inventory_hostname }}
          service_description     Link Utilisation OUTPUT - {{ iface_desc_map[iface_idx] }}
          check_command           check-snmp-v3!.1.3.6.1.2.1.31.1.1.1.10.{{ iface_idx }} --rate
          check_interval          5
          notifications_enabled   0
      }
      {% endif %}
      {% endfor %}
      #

关键改动说明

  • 移除了所有针对单接口的循环SNMP请求,所有属性查询均为批量拉取,接口数量越多性能提升越明显
  • 所有接口属性都以索引为键存储在独立映射表中,无需依赖循环下标对齐,彻底避免属性和索引匹配错误的问题
  • 提前过滤非trunk接口,后续所有处理逻辑仅针对有效trunk接口,无任何无效计算
  • 简化了Nagios配置生成逻辑,移除了不必要的raw标签,代码可读性更高

内容的提问来源于stack exchange,提问作者Julian Parkin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 04:42:03