-
Notifications
You must be signed in to change notification settings - Fork 739
/
snmp_facts.py
1075 lines (950 loc) · 45.1 KB
/
snmp_facts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# This file is part of Networklore's snmp library for Ansible
#
# The module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
from collections import defaultdict
from ansible.module_utils.basic import AnsibleModule
import six
DOCUMENTATION = '''
---
module: snmp_facts
author: Patrick Ogenstad (@networklore)
notes:
- Version 0.7
short_description: Retrive facts for a device using SNMP.
description:
- Retrieve facts for a device using SNMP, the facts will be
inserted to the ansible_facts key.
requirements:
- pysnmp
options:
host:
description:
- Set to {{ inventory_hostname }}}
required: true
version:
description:
- SNMP Version to use, v2/v2c or v3
choices: [ 'v2', 'v2c', 'v3' ]
required: true
community:
description:
- The SNMP community string, required if version is v2/v2c
required: false
is_dell:
description:
- Whether the nos is dell or not
required: false
is_eos:
description:
- Whether the nos is eos or not
required: false
level:
description:
- Authentication level, required if version is v3
choices: [ 'authPriv', 'authNoPriv' ]
required: false
username:
description:
- Username for SNMPv3, required if version is v3
required: false
integrity:
description:
- Hashing algoritm, required if version is v3
choices: [ 'md5', 'sha' ]
required: false
authkey:
description:
- Authentication key, required if version is v3
required: false
privacy:
description:
- Encryption algoritm, required if level is authPriv
choices: [ 'des', 'aes' ]
required: false
privkey:
description:
- Encryption key, required if version is authPriv
required: false
'''
EXAMPLES = '''
# Gather facts with SNMP version 2
- snmp_facts: host={{ inventory_hostname }} version=2c community=public
# Gather facts using SNMP version 3
- snmp_facts:
host={{ inventory_hostname }}
version=v3
level=authPriv
integrity=sha
privacy=aes
username=snmp-user
authkey=abc12345
privkey=def6789
'''
try:
from pysnmp.proto import rfc1902
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pyasn1.type import univ
has_pysnmp = True
except Exception:
has_pysnmp = False
class DefineOid(object):
def __init__(self, dotprefix=False):
if dotprefix:
dp = "."
else:
dp = ""
# From SNMPv2-MIB
self.sysDescr = dp + "1.3.6.1.2.1.1.1.0"
self.sysObjectId = dp + "1.3.6.1.2.1.1.2.0"
self.sysUpTime = dp + "1.3.6.1.2.1.1.3.0"
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"
self.ifType = dp + "1.3.6.1.2.1.2.2.1.3"
self.ifMtu = dp + "1.3.6.1.2.1.2.2.1.4"
self.ifSpeed = dp + "1.3.6.1.2.1.2.2.1.5"
self.ifPhysAddress = dp + "1.3.6.1.2.1.2.2.1.6"
self.ifAdminStatus = dp + "1.3.6.1.2.1.2.2.1.7"
self.ifOperStatus = dp + "1.3.6.1.2.1.2.2.1.8"
self.ifHighSpeed = dp + "1.3.6.1.2.1.31.1.1.1.15"
self.ifAlias = dp + "1.3.6.1.2.1.31.1.1.1.18"
self.ifInDiscards = dp + "1.3.6.1.2.1.2.2.1.13"
self.ifOutDiscards = dp + "1.3.6.1.2.1.2.2.1.19"
self.ifInErrors = dp + "1.3.6.1.2.1.2.2.1.14"
self.ifOutErrors = dp + "1.3.6.1.2.1.2.2.1.20"
self.ifHCInOctets = dp + "1.3.6.1.2.1.31.1.1.1.6"
self.ifHCOutOctets = dp + "1.3.6.1.2.1.31.1.1.1.10"
self.ifInUcastPkts = dp + "1.3.6.1.2.1.2.2.1.11"
self.ifOutUcastPkts = dp + "1.3.6.1.2.1.2.2.1.17"
# From entity table MIB
self.entPhysDescr = dp + "1.3.6.1.2.1.47.1.1.1.1.2"
self.entPhysContainedIn = dp + "1.3.6.1.2.1.47.1.1.1.1.4"
self.entPhysClass = dp + "1.3.6.1.2.1.47.1.1.1.1.5"
self.entPhyParentRelPos = dp + "1.3.6.1.2.1.47.1.1.1.1.6"
self.entPhysName = dp + "1.3.6.1.2.1.47.1.1.1.1.7"
self.entPhysHwVer = dp + "1.3.6.1.2.1.47.1.1.1.1.8"
self.entPhysFwVer = dp + "1.3.6.1.2.1.47.1.1.1.1.9"
self.entPhysSwVer = dp + "1.3.6.1.2.1.47.1.1.1.1.10"
self.entPhysSerialNum = dp + "1.3.6.1.2.1.47.1.1.1.1.11"
self.entPhysMfgName = dp + "1.3.6.1.2.1.47.1.1.1.1.12"
self.entPhysModelName = dp + "1.3.6.1.2.1.47.1.1.1.1.13"
self.entPhysIsFRU = dp + "1.3.6.1.2.1.47.1.1.1.1.16"
# From entity sensor MIB
self.entPhySensorType = dp + "1.3.6.1.2.1.99.1.1.1.1"
self.entPhySensorScale = dp + "1.3.6.1.2.1.99.1.1.1.2"
self.entPhySensorPrecision = dp + "1.3.6.1.2.1.99.1.1.1.3"
self.entPhySensorValue = dp + "1.3.6.1.2.1.99.1.1.1.4"
self.entPhySensorOperStatus = dp + "1.3.6.1.2.1.99.1.1.1.5"
# From IP-MIB
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 LLDP-MIB: lldpLocalSystemData
self.lldpLocChassisIdSubtype = dp + "1.0.8802.1.1.2.1.3.1"
self.lldpLocChassisId = dp + "1.0.8802.1.1.2.1.3.2"
self.lldpLocSysName = dp + "1.0.8802.1.1.2.1.3.3"
self.lldpLocSysDesc = dp + "1.0.8802.1.1.2.1.3.4"
# From LLDP-MIB: lldpLocPortTable
self.lldpLocPortIdSubtype = dp + "1.0.8802.1.1.2.1.3.7.1.2" # + .ifindex
self.lldpLocPortId = dp + "1.0.8802.1.1.2.1.3.7.1.3" # + .ifindex
self.lldpLocPortDesc = dp + "1.0.8802.1.1.2.1.3.7.1.4" # + .ifindex
# From LLDP-MIB: lldpLocManAddrTables
self.lldpLocManAddrLen = dp + "1.0.8802.1.1.2.1.3.8.1.3" # + .subtype + .man addr
self.lldpLocManAddrIfSubtype = dp + \
"1.0.8802.1.1.2.1.3.8.1.4" # + .subtype + .man addr
self.lldpLocManAddrIfId = dp + "1.0.8802.1.1.2.1.3.8.1.5" # + .subtype + .man addr
self.lldpLocManAddrOID = dp + "1.0.8802.1.1.2.1.3.8.1.6" # + .subtype + .man addr
# From LLDP-MIB: lldpRemTable
# + .time mark + .ifindex + .rem index
self.lldpRemChassisIdSubtype = dp + "1.0.8802.1.1.2.1.4.1.1.4"
# + .time mark + .ifindex + .rem index
self.lldpRemChassisId = dp + "1.0.8802.1.1.2.1.4.1.1.5"
# + .time mark + .ifindex + .rem index
self.lldpRemPortIdSubtype = dp + "1.0.8802.1.1.2.1.4.1.1.6"
# + .time mark + .ifindex + .rem index
self.lldpRemPortId = dp + "1.0.8802.1.1.2.1.4.1.1.7"
# + .time mark + .ifindex + .rem index
self.lldpRemPortDesc = dp + "1.0.8802.1.1.2.1.4.1.1.8"
# + .time mark + .ifindex + .rem index
self.lldpRemSysName = dp + "1.0.8802.1.1.2.1.4.1.1.9"
# + .time mark + .ifindex + .rem index
self.lldpRemSysDesc = dp + "1.0.8802.1.1.2.1.4.1.1.10"
# + .time mark + .ifindex + .rem index
self.lldpRemSysCapSupported = dp + "1.0.8802.1.1.2.1.4.1.1.11"
# + .time mark + .ifindex + .rem index
self.lldpRemSysCapEnabled = dp + "1.0.8802.1.1.2.1.4.1.1.12"
# From LLDP-MIB: lldpRemManAddrTable
# + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
self.lldpRemManAddrIfSubtype = dp + "1.0.8802.1.1.2.1.4.2.1.3"
# + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
self.lldpRemManAddrIfId = dp + "1.0.8802.1.1.2.1.4.2.1.4"
# + .time mark + .ifindex + .rem index + .addr_subtype + .man addr
self.lldpRemManAddrOID = dp + "1.0.8802.1.1.2.1.4.2.1.5"
# From Dell Private MIB
self.ChStackUnitCpuUtil5sec = dp + "1.3.6.1.4.1.6027.3.10.1.2.9.1.2.1"
# Memory Check
self.sysTotalMemory = dp + "1.3.6.1.4.1.2021.4.5.0"
self.sysTotalFreeMemory = dp + "1.3.6.1.4.1.2021.4.6.0"
self.sysTotalSharedMemory = dp + "1.3.6.1.4.1.2021.4.13.0"
self.sysTotalBuffMemory = dp + "1.3.6.1.4.1.2021.4.14.0"
self.sysCachedMemory = dp + "1.3.6.1.4.1.2021.4.15.0"
# Swap Info
self.sysTotalSwap = dp + "1.3.6.1.4.1.2021.4.3.0"
self.sysTotalFreeSwap = dp + "1.3.6.1.4.1.2021.4.4.0"
# 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
# + .ifindex.IfDirection.QueueID
self.csqIfQosGroupStats = dp + "1.3.6.1.4.1.9.9.580.1.5.5.1.4"
# From Cisco private MIB (PSU)
self.cefcFRUPowerOperStatus = dp + "1.3.6.1.4.1.9.9.117.1.1.2.1.2" # + .psuindex
# ipCidrRouteTable MIB
self.ipCidrRouteEntry = dp + \
"1.3.6.1.2.1.4.24.4.1.1.0.0.0.0.0.0.0.0.0" # + .next hop IP
self.ipCidrRouteStatus = dp + \
"1.3.6.1.2.1.4.24.4.1.16.0.0.0.0.0.0.0.0.0" # + .next hop IP
# Dot1q MIB
self.dot1qTpFdbEntry = dp + "1.3.6.1.2.1.17.7.1.2.2.1.2" # + .VLAN.MAC
def decode_hex(hexstring):
if len(hexstring) < 3:
return hexstring
if hexstring[:2] == "0x":
return hexstring[2:].decode("hex")
else:
return hexstring
def decode_mac(hexstring):
if len(hexstring) != 14:
return hexstring
if hexstring[:2] == "0x":
return hexstring[2:]
else:
return hexstring
def lookup_adminstatus(int_adminstatus):
adminstatus_options = {
1: 'up',
2: 'down',
3: 'testing'
}
if int_adminstatus in adminstatus_options.keys():
return adminstatus_options[int_adminstatus]
else:
return ""
def lookup_operstatus(int_operstatus):
operstatus_options = {
1: 'up',
2: 'down',
3: 'testing',
4: 'unknown',
5: 'dormant',
6: 'notPresent',
7: 'lowerLayerDown'
}
if int_operstatus in operstatus_options.keys():
return operstatus_options[int_operstatus]
else:
return ""
def decode_type(module, current_oid, val):
if six.PY3:
tagMap = {
rfc1902.Counter32.tagSet: int,
rfc1902.Gauge32.tagSet: int,
rfc1902.Integer32.tagSet: int,
rfc1902.IpAddress.tagSet: str,
univ.Null.tagSet: str,
univ.ObjectIdentifier.tagSet: str,
rfc1902.OctetString.tagSet: str,
rfc1902.TimeTicks.tagSet: int,
rfc1902.Counter64.tagSet: int
}
else:
tagMap = {
rfc1902.Counter32.tagSet: long, # noqa F821
rfc1902.Gauge32.tagSet: long, # noqa F821
rfc1902.Integer32.tagSet: long, # noqa F821
rfc1902.IpAddress.tagSet: str,
univ.Null.tagSet: str,
univ.ObjectIdentifier.tagSet: str,
rfc1902.OctetString.tagSet: str,
rfc1902.TimeTicks.tagSet: long, # noqa F821
rfc1902.Counter64.tagSet: long # noqa F821
}
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)
try:
pyVal = tagMap[val.tagSet](val)
except KeyError:
module.fail_json(
msg="KeyError: Unable to convert ASN1 type to python type. Value: %s" % val)
return pyVal
def main():
module = AnsibleModule(
argument_spec=dict(
host=dict(required=True),
timeout=dict(reqired=False, type='int', default=5),
version=dict(required=True, choices=['v2', 'v2c', 'v3']),
community=dict(required=False, default=False),
username=dict(required=False),
level=dict(required=False, choices=['authNoPriv', 'authPriv']),
integrity=dict(required=False, choices=['md5', 'sha']),
privacy=dict(required=False, choices=['des', 'aes']),
authkey=dict(required=False),
privkey=dict(required=False),
is_dell=dict(required=False, default=False, type='bool'),
is_eos=dict(required=False, default=False, type='bool'),
include_swap=dict(required=False, default=False, type='bool'),
removeplaceholder=dict(required=False)),
required_together=(['username', 'level', 'integrity', 'authkey'], [
'privacy', 'privkey'],),
supports_check_mode=False)
m_args = module.params
if not has_pysnmp:
module.fail_json(msg='Missing required pysnmp module (check docs)')
cmdGen = cmdgen.CommandGenerator()
# Verify that we receive a community when using snmp v2
if m_args['version'] == "v2" or m_args['version'] == "v2c":
if m_args['community'] is False:
module.fail_json(msg='Community not set when using snmp version 2')
if m_args['version'] == "v3":
if m_args['username'] is None:
module.fail_json(msg='Username not set when using snmp version 3')
if m_args['level'] == "authPriv" and m_args['privacy'] is 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":
integrity_proto = cmdgen.usmHMACMD5AuthProtocol
if m_args['privacy'] == "aes":
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'])
# Use SNMP Version 3 with authNoPriv
elif m_args['level'] == "authNoPriv":
snmp_auth = cmdgen.UsmUserData(
m_args['username'], authKey=m_args['authkey'], authProtocol=integrity_proto)
# Use SNMP Version 3 with authPriv
else:
snmp_auth = cmdgen.UsmUserData(m_args['username'], authKey=m_args['authkey'],
privKey=m_args['privkey'], authProtocol=integrity_proto,
privProtocol=privacy_proto)
# Use p to prefix OIDs with a dot for polling
p = DefineOid(dotprefix=True)
# Use v without a prefix to use with return values
v = DefineOid(dotprefix=False)
def Tree(): return defaultdict(Tree)
results = Tree()
# Getting system description could take more than 1 second on some Dell platform
# (e.g. S6000) when cpu utilization is high, increse timeout to tolerate the delay.
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
snmp_auth,
cmdgen.UdpTransportTarget(
(m_args['host'], 161), timeout=m_args['timeout']),
cmdgen.MibVariable(p.sysDescr,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying system description.')
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if current_oid == v.sysDescr:
results['ansible_sysdescr'] = decode_hex(current_val)
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.sysObjectId,),
cmdgen.MibVariable(p.sysUpTime,),
cmdgen.MibVariable(p.sysContact,),
cmdgen.MibVariable(p.sysName,),
cmdgen.MibVariable(p.sysLocation,),
lookupMib=False, lexicographicMode=False
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying system infomation.')
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if current_oid == v.sysObjectId:
results['ansible_sysobjectid'] = current_val
elif current_oid == v.sysUpTime:
results['ansible_sysuptime'] = current_val
elif current_oid == v.sysContact:
results['ansible_syscontact'] = current_val
elif current_oid == v.sysName:
results['ansible_sysname'] = current_val
elif current_oid == v.sysLocation:
results['ansible_syslocation'] = current_val
# Cisco 8800 has lots of interfacts, add timeout to tolerate the latency
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161), timeout=m_args['timeout']),
cmdgen.MibVariable(p.ifIndex,),
cmdgen.MibVariable(p.ifDescr,),
cmdgen.MibVariable(p.ifType,),
cmdgen.MibVariable(p.ifMtu,),
cmdgen.MibVariable(p.ifSpeed,),
cmdgen.MibVariable(p.ifPhysAddress,),
cmdgen.MibVariable(p.ifAdminStatus,),
cmdgen.MibVariable(p.ifOperStatus,),
cmdgen.MibVariable(p.ifHighSpeed,),
cmdgen.MibVariable(p.ipAdEntAddr,),
cmdgen.MibVariable(p.ipAdEntIfIndex,),
cmdgen.MibVariable(p.ipAdEntNetMask,),
cmdgen.MibVariable(p.ifAlias,),
lookupMib=False, lexicographicMode=False
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying interface details')
interface_indexes = []
all_ipv4_addresses = []
ipv4_networks = Tree()
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if 'No more variables left in this MIB View' in current_val:
continue
if v.ifIndex in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifindex'] = current_val
interface_indexes.append(ifIndex)
if v.ifDescr in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['name'] = current_val
if v.ifType in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['type'] = current_val
if v.ifMtu in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['mtu'] = current_val
if v.ifSpeed in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['speed'] = current_val
if v.ifPhysAddress in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['mac'] = decode_mac(
current_val)
if v.ifAdminStatus in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['adminstatus'] = lookup_adminstatus(
int(current_val))
if v.ifOperStatus in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['operstatus'] = lookup_operstatus(
int(current_val))
if v.ifHighSpeed in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifHighSpeed'] = current_val
if v.ipAdEntAddr in current_oid:
curIPList = current_oid.rsplit('.', 4)[-4:]
curIP = ".".join(curIPList)
ipv4_networks[curIP]['address'] = current_val
all_ipv4_addresses.append(current_val)
if v.ipAdEntIfIndex in current_oid:
curIPList = current_oid.rsplit('.', 4)[-4:]
curIP = ".".join(curIPList)
ipv4_networks[curIP]['interface'] = current_val
if v.ipAdEntNetMask in current_oid:
curIPList = current_oid.rsplit('.', 4)[-4:]
curIP = ".".join(curIPList)
ipv4_networks[curIP]['netmask'] = current_val
if v.ifAlias in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['description'] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.ifInDiscards,),
cmdgen.MibVariable(p.ifOutDiscards,),
cmdgen.MibVariable(p.ifInErrors,),
cmdgen.MibVariable(p.ifOutErrors,),
cmdgen.MibVariable(p.ifHCInOctets,),
cmdgen.MibVariable(p.ifHCOutOctets,),
cmdgen.MibVariable(p.ifInUcastPkts,),
cmdgen.MibVariable(p.ifOutUcastPkts,),
lookupMib=False, lexicographicMode=False
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying interface counters')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.ifInDiscards in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifInDiscards'] = current_val
if v.ifOutDiscards in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifOutDiscards'] = current_val
if v.ifInErrors in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifInErrors'] = current_val
if v.ifOutErrors in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifOutErrors'] = current_val
if v.ifHCInOctets in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifHCInOctets'] = current_val
if v.ifHCOutOctets in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifHCOutOctets'] = current_val
if v.ifInUcastPkts in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifInUcastPkts'] = current_val
if v.ifOutUcastPkts in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['ifOutUcastPkts'] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.entPhysDescr,),
cmdgen.MibVariable(p.entPhysContainedIn, ),
cmdgen.MibVariable(p.entPhysClass,),
cmdgen.MibVariable(p.entPhyParentRelPos, ),
cmdgen.MibVariable(p.entPhysName,),
cmdgen.MibVariable(p.entPhysHwVer,),
cmdgen.MibVariable(p.entPhysFwVer,),
cmdgen.MibVariable(p.entPhysSwVer,),
cmdgen.MibVariable(p.entPhysSerialNum,),
cmdgen.MibVariable(p.entPhysMfgName,),
cmdgen.MibVariable(p.entPhysModelName,),
cmdgen.MibVariable(p.entPhysIsFRU, ),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) + ' querying physical table')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.entPhysDescr in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysDescr'] = current_val
if v.entPhysContainedIn in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysContainedIn'] = int(
current_val)
if v.entPhysClass in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysClass'] = int(
current_val)
if v.entPhyParentRelPos in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhyParentRelPos'] = int(
current_val)
if v.entPhysName in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysName'] = current_val
if v.entPhysHwVer in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysHwVer'] = current_val
if v.entPhysFwVer in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysFwVer'] = current_val
if v.entPhysSwVer in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysSwVer'] = current_val
if v.entPhysSerialNum in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysSerialNum'] = current_val
if v.entPhysMfgName in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysMfgName'] = current_val
if v.entPhysModelName in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysModelName'] = current_val
if v.entPhysIsFRU in current_oid:
entity_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_physical_entities'][entity_oid]['entPhysIsFRU'] = int(
current_val)
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.entPhySensorType,),
cmdgen.MibVariable(p.entPhySensorScale,),
cmdgen.MibVariable(p.entPhySensorPrecision,),
cmdgen.MibVariable(p.entPhySensorValue,),
cmdgen.MibVariable(p.entPhySensorOperStatus,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) + ' querying physical table')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.entPhySensorType in current_oid:
sensor_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_sensors'][sensor_oid]['entPhySensorType'] = current_val
if v.entPhySensorScale in current_oid:
sensor_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_sensors'][sensor_oid]['entPhySensorScale'] = int(
current_val)
if v.entPhySensorPrecision in current_oid:
sensor_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_sensors'][sensor_oid]['entPhySensorPrecision'] = current_val
if v.entPhySensorValue in current_oid:
sensor_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_sensors'][sensor_oid]['entPhySensorValue'] = current_val
if v.entPhySensorOperStatus in current_oid:
sensor_oid = int(current_oid.rsplit('.', 1)[-1])
results['snmp_sensors'][sensor_oid]['entPhySensorOperStatus'] = current_val
interface_to_ipv4 = {}
for ipv4_network in ipv4_networks:
current_interface = ipv4_networks[ipv4_network]['interface']
current_network = {
'address': ipv4_networks[ipv4_network]['address'],
'netmask': ipv4_networks[ipv4_network]['netmask']
}
if current_interface not in interface_to_ipv4:
interface_to_ipv4[current_interface] = []
interface_to_ipv4[current_interface].append(current_network)
else:
interface_to_ipv4[current_interface].append(current_network)
for interface in interface_to_ipv4:
results['snmp_interfaces'][int(
interface)]['ipv4'] = interface_to_ipv4[interface]
results['ansible_all_ipv4_addresses'] = all_ipv4_addresses
if m_args['is_dell']:
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.ChStackUnitCpuUtil5sec,),
lookupMib=False, lexicographicMode=False
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying CPU busy indeces')
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if current_oid == v.ChStackUnitCpuUtil5sec:
results['ansible_ChStackUnitCpuUtil5sec'] = decode_type(
module, current_oid, val)
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.lldpLocChassisIdSubtype,),
cmdgen.MibVariable(p.lldpLocChassisId,),
cmdgen.MibVariable(p.lldpLocSysName,),
cmdgen.MibVariable(p.lldpLocSysDesc,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying lldp local system infomation.')
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if current_oid == v.lldpLocChassisIdSubtype:
results['snmp_lldp']['lldpLocChassisIdSubtype'] = current_val
elif current_oid == v.lldpLocChassisId:
results['snmp_lldp']['lldpLocChassisId'] = current_val
elif current_oid == v.lldpLocSysName:
results['snmp_lldp']['lldpLocSysName'] = current_val
elif current_oid == v.lldpLocSysDesc:
results['snmp_lldp']['lldpLocSysDesc'] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.lldpLocPortIdSubtype,),
cmdgen.MibVariable(p.lldpLocPortId,),
cmdgen.MibVariable(p.lldpLocPortDesc,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying lldpLocPortTable counters')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.lldpLocPortIdSubtype in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['lldpLocPortIdSubtype'] = current_val
if v.lldpLocPortId in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['lldpLocPortId'] = current_val
if v.lldpLocPortDesc in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['lldpLocPortDesc'] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.lldpLocManAddrLen,),
cmdgen.MibVariable(p.lldpLocManAddrIfSubtype,),
cmdgen.MibVariable(p.lldpLocManAddrIfId,),
cmdgen.MibVariable(p.lldpLocManAddrOID,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying lldpLocPortTable counters')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.lldpLocManAddrLen in current_oid:
results['snmp_lldp']['lldpLocManAddrLen'] = current_val
if v.lldpLocManAddrIfSubtype in current_oid:
results['snmp_lldp']['lldpLocManAddrIfSubtype'] = current_val
if v.lldpLocManAddrIfId in current_oid:
results['snmp_lldp']['lldpLocManAddrIfId'] = current_val
if v.lldpLocManAddrOID in current_oid:
results['snmp_lldp']['lldpLocManAddrOID'] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.lldpRemChassisIdSubtype,),
cmdgen.MibVariable(p.lldpRemChassisId,),
cmdgen.MibVariable(p.lldpRemPortIdSubtype,),
cmdgen.MibVariable(p.lldpRemPortId,),
cmdgen.MibVariable(p.lldpRemPortDesc,),
cmdgen.MibVariable(p.lldpRemSysName,),
cmdgen.MibVariable(p.lldpRemSysDesc,),
cmdgen.MibVariable(p.lldpRemSysCapSupported,),
cmdgen.MibVariable(p.lldpRemSysCapEnabled,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying lldpLocPortTable counters')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.lldpRemChassisIdSubtype in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemChassisIdSubtype'] = current_val
if v.lldpRemChassisId in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemChassisId'] = current_val
if v.lldpRemPortIdSubtype in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemPortIdSubtype'] = current_val
if v.lldpRemPortId in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemPortId'] = current_val
if v.lldpRemPortDesc in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemPortDesc'] = current_val
if v.lldpRemSysName in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemSysName'] = current_val
if v.lldpRemSysDesc in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemSysDesc'] = current_val
if v.lldpRemSysCapSupported in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemSysCapSupported'] = current_val
if v.lldpRemSysCapEnabled in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemSysCapEnabled'] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.lldpRemManAddrIfSubtype,),
cmdgen.MibVariable(p.lldpRemManAddrIfId,),
cmdgen.MibVariable(p.lldpRemManAddrOID,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) +
' querying lldpLocPortTable counters')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.lldpRemManAddrIfSubtype in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemManAddrIfSubtype'] = current_val
if v.lldpRemManAddrIfId in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemManAddrIfId'] = current_val
if v.lldpRemManAddrOID in current_oid:
ifIndex = int(current_oid.split('.')[12])
results['snmp_interfaces'][ifIndex]['lldpRemManAddrOID'] = current_val
# Cisco 8800 has lots of interfacts, add timeout to tolerate the latency
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161), timeout=m_args['timeout']),
cmdgen.MibVariable(p.cpfcIfRequests,),
cmdgen.MibVariable(p.cpfcIfIndications,),
cmdgen.MibVariable(p.requestsPerPriority,),
cmdgen.MibVariable(p.indicationsPerPriority,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) + ' querying PFC counters')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.cpfcIfRequests in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['cpfcIfRequests'] = current_val
if v.cpfcIfIndications in current_oid:
ifIndex = int(current_oid.rsplit('.', 1)[-1])
results['snmp_interfaces'][ifIndex]['cpfcIfIndications'] = current_val
if v.requestsPerPriority in current_oid:
ifIndex = int(current_oid.split('.')[-2])
prio = int(current_oid.split('.')[-1])
results['snmp_interfaces'][ifIndex]['requestsPerPriority'][prio] = current_val
if v.indicationsPerPriority in current_oid:
ifIndex = int(current_oid.split('.')[-2])
prio = int(current_oid.split('.')[-1])
results['snmp_interfaces'][ifIndex]['indicationsPerPriority'][prio] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.csqIfQosGroupStats,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) + ' querying QoS stats')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.csqIfQosGroupStats in current_oid:
ifIndex = int(current_oid.split('.')[-4])
ifDirection = int(current_oid.split('.')[-3])
queueId = int(current_oid.split('.')[-2])
counterId = int(current_oid.split('.')[-1])
results['snmp_interfaces'][ifIndex]['queues'][ifDirection][queueId][counterId] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.cefcFRUPowerOperStatus,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) + ' querying FRU')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.cefcFRUPowerOperStatus in current_oid:
psuIndex = int(current_oid.split('.')[-1])
results['snmp_psu'][psuIndex]['operstatus'] = current_val
errorIndication, errorStatus, errorIndex, varTable = cmdGen.nextCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.ipCidrRouteEntry,),
cmdgen.MibVariable(p.ipCidrRouteStatus,),
lookupMib=False,
)
if errorIndication:
module.fail_json(msg=str(errorIndication) + ' querying CidrRouteTable')
for varBinds in varTable:
for oid, val in varBinds:
current_oid = oid.prettyPrint()
current_val = val.prettyPrint()
if v.ipCidrRouteEntry in current_oid:
# extract next hop ip from oid
next_hop = current_oid.split(v.ipCidrRouteEntry + ".")[1]
results['snmp_cidr_route'][next_hop]['route_dest'] = current_val
if v.ipCidrRouteStatus in current_oid:
next_hop = current_oid.split(v.ipCidrRouteStatus + ".")[1]
results['snmp_cidr_route'][next_hop]['status'] = current_val
if not m_args['is_eos']:
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
snmp_auth,
cmdgen.UdpTransportTarget((m_args['host'], 161)),
cmdgen.MibVariable(p.sysTotalMemory,),
cmdgen.MibVariable(p.sysTotalFreeMemory,),
cmdgen.MibVariable(p.sysTotalSharedMemory,),
cmdgen.MibVariable(p.sysTotalBuffMemory,),
cmdgen.MibVariable(p.sysCachedMemory,),
lookupMib=False, lexicographicMode=False
)