diff --git a/ansible/library/snmp_facts.py b/ansible/library/snmp_facts.py index c2f3053803..2447fefd40 100644 --- a/ansible/library/snmp_facts.py +++ b/ansible/library/snmp_facts.py @@ -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" @@ -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": @@ -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) @@ -248,7 +250,7 @@ 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') @@ -256,7 +258,7 @@ def main(): 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": @@ -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']) @@ -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,), ) @@ -328,9 +330,9 @@ 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,), ) @@ -338,8 +340,8 @@ def main(): module.fail_json(msg=str(errorIndication)) interface_indexes = [] - - all_ipv4_addresses = [] + + all_ipv4_addresses = [] ipv4_networks = Tree() for varBinds in varTable: @@ -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() diff --git a/ansible/roles/test/tasks/snmp.yml b/ansible/roles/test/tasks/snmp.yml index b8049f7066..deebf82b4e 100644 --- a/ansible/roles/test/tasks/snmp.yml +++ b/ansible/roles/test/tasks/snmp.yml @@ -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 diff --git a/ansible/roles/test/tasks/snmp/psu.yml b/ansible/roles/test/tasks/snmp/psu.yml new file mode 100644 index 0000000000..13cf10db96 --- /dev/null +++ b/ansible/roles/test/tasks/snmp/psu.yml @@ -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 }}"