Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions ansible/library/snmp_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(self,dotprefix=False):
self.sysContact = dp + "1.3.6.1.2.1.1.4.0"
self.sysName = dp + "1.3.6.1.2.1.1.5.0"
self.sysLocation = dp + "1.3.6.1.2.1.1.6.0"

# From IF-MIB
self.ifIndex = dp + "1.3.6.1.2.1.2.2.1.1"
self.ifDescr = dp + "1.3.6.1.2.1.2.2.1.2"
Expand All @@ -140,20 +140,22 @@ def __init__(self,dotprefix=False):
self.ipAdEntAddr = dp + "1.3.6.1.2.1.4.20.1.1"
self.ipAdEntIfIndex = dp + "1.3.6.1.2.1.4.20.1.2"
self.ipAdEntNetMask = dp + "1.3.6.1.2.1.4.20.1.3"

# From Dell Private MIB
self.ChStackUnitCpuUtil5sec = dp + "1.3.6.1.4.1.6027.3.10.1.2.9.1.2.1"

# From Cisco private MIB
# From Cisco private MIB (PFC and queue counters)
self.cpfcIfRequests = dp + "1.3.6.1.4.1.9.9.813.1.1.1.1" # + .ifindex
self.cpfcIfIndications = dp + "1.3.6.1.4.1.9.9.813.1.1.1.2" # + .ifindex
self.requestsPerPriority = dp + "1.3.6.1.4.1.9.9.813.1.2.1.2" # + .ifindex.prio
self.indicationsPerPriority = dp + "1.3.6.1.4.1.9.9.813.1.2.1.3" # + .ifindex.prio
self.csqIfQosGroupStats = dp + "1.3.6.1.4.1.9.9.580.1.5.5.1.4" # + .ifindex.IfDirection.QueueID

# From Cisco private MIB (PSU)
self.cefcFRUPowerOperStatus = dp + "1.3.6.1.4.1.9.9.117.1.1.2.1.2" # + .psuindex

def decode_hex(hexstring):

if len(hexstring) < 3:
return hexstring
if hexstring[:2] == "0x":
Expand Down Expand Up @@ -208,7 +210,7 @@ def decode_type(module, current_oid, val):
rfc1902.TimeTicks.tagSet: long,
rfc1902.Counter64.tagSet: long
}

if val is None or not val:
module.fail_json(msg="Unable to convert ASN1 type to python type. No value was returned for OID %s" % current_oid)

Expand Down Expand Up @@ -248,15 +250,15 @@ def main():
if m_args['version'] == "v2" or m_args['version'] == "v2c":
if m_args['community'] == False:
module.fail_json(msg='Community not set when using snmp version 2')

if m_args['version'] == "v3":
if m_args['username'] == None:
module.fail_json(msg='Username not set when using snmp version 3')

if m_args['level'] == "authPriv" and m_args['privacy'] == None:
module.fail_json(msg='Privacy algorithm not set when using authPriv')


if m_args['integrity'] == "sha":
integrity_proto = cmdgen.usmHMACSHAAuthProtocol
elif m_args['integrity'] == "md5":
Expand All @@ -266,7 +268,7 @@ def main():
privacy_proto = cmdgen.usmAesCfb128Protocol
elif m_args['privacy'] == "des":
privacy_proto = cmdgen.usmDESPrivProtocol

# Use SNMP Version 2
if m_args['version'] == "v2" or m_args['version'] == "v2c":
snmp_auth = cmdgen.CommunityData(m_args['community'])
Expand All @@ -285,16 +287,16 @@ def main():
v = DefineOid(dotprefix=False)

Tree = lambda: defaultdict(Tree)

results = Tree()

errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.sysDescr,),
cmdgen.MibVariable(p.sysObjectId,),
cmdgen.MibVariable(p.sysObjectId,),
cmdgen.MibVariable(p.sysUpTime,),
cmdgen.MibVariable(p.sysContact,),
cmdgen.MibVariable(p.sysContact,),
cmdgen.MibVariable(p.sysName,),
cmdgen.MibVariable(p.sysLocation,),
)
Expand Down Expand Up @@ -328,18 +330,18 @@ def main():
cmdgen.MibVariable(p.ifPhysAddress,),
cmdgen.MibVariable(p.ifAdminStatus,),
cmdgen.MibVariable(p.ifOperStatus,),
cmdgen.MibVariable(p.ipAdEntAddr,),
cmdgen.MibVariable(p.ipAdEntIfIndex,),
cmdgen.MibVariable(p.ipAdEntNetMask,),
cmdgen.MibVariable(p.ipAdEntAddr,),
cmdgen.MibVariable(p.ipAdEntIfIndex,),
cmdgen.MibVariable(p.ipAdEntNetMask,),
cmdgen.MibVariable(p.ifAlias,),
)

if errorIndication:
module.fail_json(msg=str(errorIndication))

interface_indexes = []
all_ipv4_addresses = []

all_ipv4_addresses = []
ipv4_networks = Tree()

for varBinds in varTable:
Expand Down Expand Up @@ -515,10 +517,23 @@ def main():
counterId = int(current_oid.split('.')[-1])
results['snmp_interfaces'][ifIndex]['queues'][ifDirection][queueId][counterId] = current_val

module.exit_json(ansible_facts=results)

errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.cefcFRUPowerOperStatus,),
)

main()
if errorIndication:
module.fail_json(msg=str(errorIndication))

for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.csqIfQosGroupStats in current_oid:
psuIndex = int(current_oid.split('.')[-1])
results['snmp_psu'][psuIndex]['operstatus'] = current_val

module.exit_json(ansible_facts=results)

main()
3 changes: 3 additions & 0 deletions ansible/roles/test/tasks/snmp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@

- name: include snmp queues test
include: roles/test/tasks/snmp/queues.yml

- name: include snmp PSU test
include: roles/test/tasks/snmp/psu.yml
when: testcase_name is defined
19 changes: 19 additions & 0 deletions ansible/roles/test/tasks/snmp/psu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Gather facts with SNMP version 2
- name: Gathering basic snmp facts about the device
snmp_facts: host={{ ansible_host }} version=v2c community={{ snmp_rocommunity }}
connection: local

# We assume that all the PSUs tested should be in good status, otherwise, fix the PSU manually
- fail:
msg: "PSU index {{ item.key }} operstatus is not OK"
when: "{{ item.value.operstatus | int }} != 2"
with_dict: "{{ snmp_psu }}"

- name: Get the PSU count from command line
shell: psuutil numpsus
become: yes
register: numpsus_out

- fail:
msg: "PSU counts mismatch"
when: "{{ numpsus_out.stdout | int }} != {{ snmp_psu | length }}"