使编写Nagios对象以监控Cisco接口的Ansible更加高效



我编写了一个工作角色,负责检查Cisco网络设备的非虚拟接口,查找和选择也在运行的中继线,并编写一些Nagios对象定义来监控状态和利用率。

我一直在努力提高它的效率,以便在每个阶段都只在有趣的接口上工作:

  1. 查找所有中继线
  2. 仅检查中继线的操作状态,而不是所有接口的状态
  3. 只为中继及以上的接口编写Nagios对象定义

下面的代码继续缓慢地检查每个物理接口,即使我们本可以用已经获得的信息消除非中继线。

我一直在努力缩短接口列表,同时在模板中保留正确的iface_index以供以后参考。我曾尝试将iface_index与操作状态和中继状态其他键/值对一起作为字典键,但无法将其组合在一起。

任何灵感都将不胜感激,我觉得应该有更好的方法。也为我的业余代码提前道歉。

---
- name: Get physical interface indexes
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: iface_oids
- name: Extract only the index
set_fact:
iface_index: "{{ iface_oids.stdout | regex_findall('\.([0-9]+) ','last') }}"
- name: Get interface trunk state
delegate_to: 127.0.0.1
command: "snmpwalk -Oqv -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: iface_trunk_state

- name: Get interface oper state
delegate_to: 127.0.0.1
command: "snmpwalk -Oqv -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.{{ item }}"
loop: "{{ iface_index }}"
register: iface_oper_state
- name: Get interface descriptions
delegate_to: 127.0.0.1
command: "snmpget -Oqv -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.{{ item }}"
loop: "{{ iface_index }}"
register: iface_description
- name: Get interface names
delegate_to: 127.0.0.1
loop: "{{ iface_index }}"
command: "snmpget -Oqv -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.{{ item }}"
register: iface_name
- set_fact:
nagios_trunks: |
#
# Monitor trunking interfaces
#
{% for iface in iface_index %}
{% if (iface_trunk_state.stdout_lines[loop.index0] == "1") and (iface_oper_state.results[loop.index0].stdout[0] == "1") %}
{% raw %}define service {
{% endraw %}
use                     generic-service
host_name               {{ inventory_hostname }} 
service_description     {{ inventory_hostname }}:{{ iface_name.results[loop.index0].stdout[1:-1] }} - {{ iface_description.results[loop.index0].stdout[1:-1] }}
check_command           check-snmp-v3!.1.3.6.1.2.1.2.2.1.8.{{ iface }} -r 1
notifications_enabled   0
{% raw %}}
{% endraw %}
#
{% raw %}define service {
{% endraw %}
use                     generic-service
host_name               {{ inventory_hostname }}
service_description     Link Utilisation INPUT - {{ iface_description.results[loop.index0].stdout[1:-1] }}
check_command           check-snmp-v3!.1.3.6.1.2.1.31.1.1.1.6.{{ iface }} --rate
check_interval          5
notifications_enabled   0
{% raw %}}
{% endraw %}
#
{% raw %}define service {
{% endraw %}
use                     generic-service
host_name               {{ inventory_hostname }}
service_description     Link Utilisation OUTPUT - {{ iface_description.results[loop.index0].stdout[1:-1] }}
check_command           check-snmp-v3!.1.3.6.1.2.1.31.1.1.1.10.{{ iface }} --rate
check_interval          5
notifications_enabled   0
{% raw %}}
{% endraw %}
{% endif %}
{% endfor %}
#

你正在做的事情被称为"抨击主义",这不是一种好的风格。如果你正在做一些复杂的事情,command之间有很多互连,那么最好使用Python并编写一个好的Ansible模块。这会让游戏变得更简单,而且你手头有合适的工具。

请参阅https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html

最新更新