
    Vh=                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZ d d	lmZ  ej$                  d
      5  d dlmZ d dlmZ ddd        G d de      Zd Zd Zd Zd Zd Zedk(  r e        yy# 1 sw Y   1xY w)    )absolute_importdivisionprint_functionaz  
module: snmp_facts
author:
  - Patrick Ogenstad (@ogenstad)
short_description: Retrieve facts for a device using SNMP
description:
  - Retrieve facts for a device using SNMP, the facts will be inserted to the C(ansible_facts) key.
requirements:
  - pysnmp
extends_documentation_fragment:
  - community.general.attributes
  - community.general.attributes.facts
  - community.general.attributes.facts_module
attributes:
  check_mode:
    version_added: 3.3.0
    # This was backported to 2.5.4 and 1.3.11 as well, since this was a bugfix
options:
  host:
    description:
      - Set to target SNMP server (normally C({{ inventory_hostname }})).
    type: str
    required: true
  version:
    description:
      - SNMP Version to use, V(v2), V(v2c) or V(v3).
    type: str
    required: true
    choices: [v2, v2c, v3]
  community:
    description:
      - The SNMP community string, required if O(version) is V(v2) or V(v2c).
    type: str
  level:
    description:
      - Authentication level.
      - Required if O(version=v3).
    type: str
    choices: [authNoPriv, authPriv]
  username:
    description:
      - Username for SNMPv3.
      - Required if O(version=v3).
    type: str
  integrity:
    description:
      - Hashing algorithm.
      - Required if O(version=v3).
    type: str
    choices: [md5, sha]
  authkey:
    description:
      - Authentication key.
      - Required O(version=v3).
    type: str
  privacy:
    description:
      - Encryption algorithm.
      - Required if O(level=authPriv).
    type: str
    choices: [aes, des]
  privkey:
    description:
      - Encryption key.
      - Required if O(level=authPriv).
    type: str
  timeout:
    description:
      - Response timeout in seconds.
    type: int
    version_added: 2.3.0
  retries:
    description:
      - Maximum number of request retries, 0 retries means just a single request.
    type: int
    version_added: 2.3.0
a  
- name: Gather facts with SNMP version 2
  community.general.snmp_facts:
    host: '{{ inventory_hostname }}'
    version: v2c
    community: public
  delegate_to: local

- name: Gather facts using SNMP version 3
  community.general.snmp_facts:
    host: '{{ inventory_hostname }}'
    version: v3
    level: authPriv
    integrity: sha
    privacy: aes
    username: snmp-user
    authkey: abc12345
    privkey: def6789
  delegate_to: localhost
a  
ansible_sysdescr:
  description: A textual description of the entity.
  returned: success
  type: str
  sample: "Linux ubuntu-user 4.4.0-93-generic #116-Ubuntu SMP Fri Aug 11 21:17:51 UTC 2017 x86_64"
ansible_sysobjectid:
  description: The vendor's authoritative identification of the network management subsystem contained in the entity.
  returned: success
  type: str
  sample: 1.3.6.1.4.1.8072.3.2.10
ansible_sysuptime:
  description: The time (in hundredths of a second) since the network management portion of the system was last re-initialized.
  returned: success
  type: int
  sample: 42388
ansible_syscontact:
  description: The textual identification of the contact person for this managed node, together with information on how to
    contact this person.
  returned: success
  type: str
  sample: Me <me@example.org>
ansible_sysname:
  description: An administratively-assigned name for this managed node.
  returned: success
  type: str
  sample: ubuntu-user
ansible_syslocation:
  description: The physical location of this node (for example, V(telephone closet, 3rd floor)).
  returned: success
  type: str
  sample: Sitting on the Dock of the Bay
ansible_all_ipv4_addresses:
  description: List of all IPv4 addresses.
  returned: success
  type: list
  sample: ["127.0.0.1", "172.17.0.1"]
ansible_interfaces:
  description: Dictionary of each network interface and its metadata.
  returned: success
  type: dict
  sample:
    {
      "1": {
        "adminstatus": "up",
        "description": "",
        "ifindex": "1",
        "ipv4": [
          {
            "address": "127.0.0.1",
            "netmask": "255.0.0.0"
          }
        ],
        "mac": "",
        "mtu": "65536",
        "name": "lo",
        "operstatus": "up",
        "speed": "65536"
      },
      "2": {
        "adminstatus": "up",
        "description": "",
        "ifindex": "2",
        "ipv4": [
          {
            "address": "192.168.213.128",
            "netmask": "255.255.255.0"
          }
        ],
        "mac": "000a305a52a1",
        "mtu": "1500",
        "name": "Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)",
        "operstatus": "up",
        "speed": "1500"
      }
    }
Ndefaultdict)deps)AnsibleModule)to_textpysnmp)cmdgen)EndOfMibViewc                       e Zd ZddZy)	DefineOidc                 f   |rd}nd}|dz   | _         |dz   | _        |dz   | _        |dz   | _        |dz   | _        |dz   | _        |d	z   | _        |d
z   | _        |dz   | _        |dz   | _	        |dz   | _
        |dz   | _        |dz   | _        |dz   | _        |dz   | _        |dz   | _        |dz   | _        y )N. z1.3.6.1.2.1.1.1.0z1.3.6.1.2.1.1.2.0z1.3.6.1.2.1.1.3.0z1.3.6.1.2.1.1.4.0z1.3.6.1.2.1.1.5.0z1.3.6.1.2.1.1.6.0z1.3.6.1.2.1.2.2.1.1z1.3.6.1.2.1.2.2.1.2z1.3.6.1.2.1.2.2.1.4z1.3.6.1.2.1.2.2.1.5z1.3.6.1.2.1.2.2.1.6z1.3.6.1.2.1.2.2.1.7z1.3.6.1.2.1.2.2.1.8z1.3.6.1.2.1.31.1.1.1.18z1.3.6.1.2.1.4.20.1.1z1.3.6.1.2.1.4.20.1.2z1.3.6.1.2.1.4.20.1.3)sysDescrsysObjectId	sysUpTime
sysContactsysNamesysLocationifIndexifDescrifMtuifSpeedifPhysAddressifAdminStatusifOperStatusifAliasipAdEntAddripAdEntIfIndexipAdEntNetMask)self	dotprefixdps      p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/snmp_facts.py__init__zDefineOid.__init__   s    BB 00 331122// 33 1111//
11"77"77!6655  66 #99 #99    N)F)__name__
__module____qualname__r(    r)   r'   r   r      s    :r)   r   c                 x    t        |       dk  r| S | d d dk(  r!t        t        j                  | dd              S | S )N      0x)lenr
   binascii	unhexlify	hexstrings    r'   
decode_hexr7      sD    
9~!}x)))AB-899r)   c                 @    t        |       dk7  r| S | d d dk(  r| dd  S | S )N   r0   r1   )r2   r5   s    r'   
decode_macr:      s4    
9~!}}r)   c                 "    dddd}| |v r||    S y)Nupdowntesting)   r0   r/   r   r-   )int_adminstatusadminstatus_optionss     r'   lookup_adminstatusrB      s,    
 --"?33r)   c                 *    dddddddd}| |v r||    S y	)
Nr<   r=   r>   unknowndormant
notPresentlowerLayerDown)r?   r0   r/               r   r-   )int_operstatusoperstatus_optionss     r'   lookup_operstatusrN     s8     ++!.11r)   c                  ^   t        t        t        dd      t        ddg d      t        d      t        d      t        dddg	      t        dd
dg	      t        dddg	      t        dd      t        dd      t        d      t        d            g dddgfd      } | j                  }t        j                  |        t        j                         }dD ci c]  }||   |||    }}|d   dv r|d   | j                  d       d }d }|d   dk(  r|d   | j                  d       |d   dk(  r|d   | j                  d       |d    dk(  rt
        j                  }n|d    d
k(  rt
        j                  }|d   dk(  rt
        j                  }n|d   dk(  rt
        j                  }|d   dv rt        j                  |d         }nJ|d   dk(  rt        j                  |d   |d!   |"      }n#t        j                  |d   |d!   |d   ||#      }t        d$      }t        d%$      }	fd&        }
|j                  |t        j                   |d'   d(ffi |t        j"                  |j$                        t        j"                  |j&                        t        j"                  |j(                        t        j"                  |j*                        t        j"                  |j,                        t        j"                  |j.                        d%)	      \  }}}}|r| j                  t1        |             |D ]  \  }}|j3                         }|j3                         }||	j$                  k(  rt5        |      |
d*<   D||	j&                  k(  r||
d+<   Y||	j(                  k(  r||
d,<   n||	j*                  k(  r||
d-<   ||	j,                  k(  r||
d.<   ||	j.                  k(  s||
d/<    |j7                  |t        j                   |d'   d(ffi |t        j"                  |j8                        t        j"                  |j:                        t        j"                  |j<                        t        j"                  |j>                        t        j"                  |j@                        t        j"                  |jB                        t        j"                  |jD                        t        j"                  |jF                        t        j"                  |jH                        t        j"                  |jJ                        t        j"                  |jL                        d%)      \  }}}}|r| j                  t1        |             g }g }        }|D ]  }|D ]  \  }}tO        |tP              r|j3                         }|j3                         }|	j8                  |v r:tS        |jU                  d0d1      d2         }||
d3   |   d4<   |jW                  |       |	j:                  |v r)tS        |jU                  d0d1      d2         }||
d3   |   d5<   |	j<                  |v r)tS        |jU                  d0d1      d2         }||
d3   |   d6<   |	j>                  |v r)tS        |jU                  d0d1      d2         }||
d3   |   d7<   |	j@                  |v r2tS        |jU                  d0d1      d2         }tY        |      |
d3   |   d8<   |	jB                  |v r;tS        |jU                  d0d1      d2         }t[        tS        |            |
d3   |   d9<   |	jD                  |v r;tS        |jU                  d0d1      d2         }t]        tS        |            |
d3   |   d:<   |	jF                  |v r?|jU                  d0d;      d<d  }d0j_                  |      }|||   d=<   |jW                  |       |	jH                  |v r.|jU                  d0d;      d<d  }d0j_                  |      }|||   d><   |	jJ                  |v r.|jU                  d0d;      d<d  }d0j_                  |      }|||   d?<   |	jL                  |v stS        |jU                  d0d1      d2         }||
d3   |   d@<      i }|D ]M  }||   d>   }||   d=   ||   d?   dA}||vrg ||<   ||   jW                  |       :||   jW                  |       O |D ]  }||   |
d3   tS        |         dB<    ||
dC<   | ja                  |
D       y c c}w )ENstrT)typerequired)v2v2cv3)rQ   rR   choices)rQ   
authNoPrivauthPriv)rQ   rV   md5shaaesdes)rQ   no_logint)hostversion	communityusernamelevel	integrityprivacyauthkeyprivkeytimeoutretries)rb   rc   rd   rf   re   rg   )argument_specrequired_togethersupports_check_mode)rh   ri   r`   )rS   rT   ra   z+Community not set when using snmp version 2)msgrU   rb   z*Username not set when using snmp version 3rc   z-Privacy algorithm not set when using authPrivrd   rf   )authKeyauthProtocol)rn   privKeyro   privProtocol)r%   Fc                      t               S )Nr   )Trees   r'   rs   zmain.<locals>.Tree_  s    4  r)   r_      )	lookupMibansible_sysdescransible_sysobjectidansible_sysuptimeansible_syscontactansible_sysnameansible_syslocationr   r?   ansible_interfacesifindexnamemtuspeedmacadminstatus
operstatusrH   address	interfacenetmaskdescription)r   r   ipv4ansible_all_ipv4_addresses)ansible_facts)1r	   dictparamsr   validater   CommandGenerator	fail_jsonusmHMACSHAAuthProtocolusmHMACMD5AuthProtocolusmAesCfb128ProtocolusmDESPrivProtocolCommunityDataUsmUserDatar   getCmdUdpTransportTargetMibVariabler   r   r   r   r   r   rP   prettyPrintr7   nextCmdr   r   r   r   r   r   r   r!   r"   r#   r    
isinstancer   r^   rsplitappendr:   rB   rN   join	exit_json) modulem_argscmdGenktransport_optsintegrity_protoprivacy_proto	snmp_authpvresultserrorIndicationerrorStatus
errorIndexvarBindsoidvalcurrent_oidcurrent_valvarTableinterface_indexesall_ipv4_addressesipv4_networksr   	curIPListcurIPinterface_to_ipv4ipv4_networkcurrent_interfacecurrent_networkr   rs   s                                   @r'   mainr     sZ	   540ed<OP&u%EL*+EFu~>eeU^<eD1eD1e$e$
 :	"
 !%F* ]]FMM&$$&F (!9  	
6!9N  iM)+&!NOOMiD *%!MN'?j(VI->-F!PQ+%'$;;OK E)$;;O)%"77MI%'"55M iM)(()<=	 
L	(&&vj'96)CTcrs	 &&vj'96)CT^den^o  N4AC	 	D!AE"A! fG9?!!6&>3"7J>J1::'1==*1;;(1<<)199&1==* :G 
:6O[*h S12 9Soo'oo'!**$*4[*AG&'AMM)-8G)*AKK'+6G'(ALL(,7G()AII%)4G%&AMM)-8G)*9  :@!!6&>3"7J>J199&199&177$199&1??,1??,1>>+1==*1++-1++-199& :H :6O[*h$ S12FM ,T  +	THC#|,//+K//+KyyK'k00a8<=DO,-g6yA!((1yyK'k00a8<=AL,-g6v>ww+%k00a8<=@K,-g6u=yyK'k00a8<=BM,-g6w?+-k00a8<=@J;@W,-g6u=+-k00a8<=HZ[^_j[kHl,-g6}E~~,k00a8<=GXY\]hYiGj,-g6|D}}+'..sA6rs;	+2=e$Y/"))+6;.'..sA6rs;	+4?e$[1;.'..sA6rs;	+2=e$Y/yyK'k00a8<=HS,-g6}EW+	T,T\ % 
I),7D$\29=$\29=
 $5535/0/077H/077H
I ' ]	@QR[@\$%c)n5f=] -?G()
7+]s   !d*__main__)
__future__r   r   r   rQ   __metaclass__DOCUMENTATIONEXAMPLESRETURNr3   collectionsr   :ansible_collections.community.general.plugins.module_utilsr   ansible.module_utils.basicr	   +ansible.module_utils.common.text.convertersr
   declarepysnmp.entity.rfc3413.onelinerr   pysnmp.proto.rfc1905r   objectr   r7   r:   rB   rN   r   r*   r-   r)   r'   <module>r      s    A @L\*L
\  # K 4 ?T\\( 2512
: :@I,X zF {2 2s   A>>B