| 1 |
from jnpr.junos.exception import RpcError |
| 2 |
import re |
| 3 |
|
| 4 |
|
| 5 |
def provides_facts(): |
| 6 |
""" |
| 7 |
Returns a dictionary keyed on the facts provided by this module. The value |
| 8 |
of each key is the doc string describing the fact. |
| 9 |
""" |
| 10 |
return { |
| 11 |
"current_re": "A list of internal routing instance hostnames " |
| 12 |
"for the current RE. These hostnames identify " |
| 13 |
"things like the RE's slot ('re0' or 're1'), the " |
| 14 |
"RE's mastership state ('master' or 'backup'), " |
| 15 |
"and node in a VC ('member0' or 'member1')", |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
def get_facts(device): |
| 20 |
""" |
| 21 |
The RPC-equivalent of show interfaces terse on private routing instance. |
| 22 |
""" |
| 23 |
current_re = [] |
| 24 |
|
| 25 |
try: |
| 26 |
if device.facts["_is_linux"]: |
| 27 |
rsp = device.rpc.get_system_nodes_information(normalize=True) |
| 28 |
|
| 29 |
# Get the RE (Active) list from the response |
| 30 |
res_list = rsp.xpath( |
| 31 |
"system-nodes-info-entry/system-node-info-node-attributes-list/" |
| 32 |
"system-node-info-node-attribute[text()='RE (Active)']/../.." |
| 33 |
) |
| 34 |
for re_ele in res_list: |
| 35 |
# Use the node-name element ot get RE name |
| 36 |
re_name = re_ele.xpath("system-node-info-node-name") |
| 37 |
current_re.append(re_name[0].text) |
| 38 |
else: |
| 39 |
rsp = device.rpc.get_interface_information( |
| 40 |
normalize=True, |
| 41 |
routing_instance="__juniper_private1__", |
| 42 |
terse=True, |
| 43 |
) |
| 44 |
|
| 45 |
# Get the local IPv4 addresses from the response. |
| 46 |
for ifa in rsp.iterfind( |
| 47 |
".//address-family[address-family-name='inet']/" |
| 48 |
"interface-address/ifa-local" |
| 49 |
): |
| 50 |
ifa_text = ifa.text |
| 51 |
if ifa_text is not None: |
| 52 |
# Separate the IP from the mask |
| 53 |
(ip, _, _) = ifa.text.partition("/") |
| 54 |
if ip is not None: |
| 55 |
# Use the _iri_hostname fact to map the IP address to |
| 56 |
# an internal routing instance hostname. |
| 57 |
if ip in device.facts["_iri_hostname"]: |
| 58 |
for host in device.facts["_iri_hostname"][ip]: |
| 59 |
if host not in current_re: |
| 60 |
current_re.append(host) |
| 61 |
# An SRX platform in an HA cluster uses a different |
| 62 |
# algorithm for assigning IRI IP addresses |
| 63 |
elif device.facts["srx_cluster_id"] is not None: |
| 64 |
try: |
| 65 |
# Split the IRI IP into a list of 4 octets |
| 66 |
octets = ip.split(".", 3) |
| 67 |
# The 2nd octet will be cluster-id << 4 |
| 68 |
cluster_id_octet = str( |
| 69 |
(int(device.facts["srx_cluster_id"]) & 0x000F) << 4 |
| 70 |
) |
| 71 |
if cluster_id_octet == "0": |
| 72 |
cluster_id_octet = str( |
| 73 |
device.facts["srx_cluster_id"] |
| 74 |
) |
| 75 |
# node0 will have an IP of |
| 76 |
# 129.<cluster_id_octet>.0.1 |
| 77 |
# node1 will have an IP of |
| 78 |
# 130.<cluster_id_octet>.0.1 |
| 79 |
# primary will have an IP of |
| 80 |
# 143.<cluster_id_octet>.0.1 |
| 81 |
if ( |
| 82 |
octets[1] == cluster_id_octet |
| 83 |
and octets[2] == "0" |
| 84 |
and octets[3] == "1" |
| 85 |
): |
| 86 |
host = None |
| 87 |
if octets[0] == "129": |
| 88 |
host = "node0" |
| 89 |
elif octets[0] == "130": |
| 90 |
host = "node1" |
| 91 |
elif octets[0] == "143": |
| 92 |
host = "primary" |
| 93 |
if host is not None and host not in current_re: |
| 94 |
current_re.append(host) |
| 95 |
# Problem splitting IP into octets and indexing them. |
| 96 |
# Keep looping to check the other IRI IPs. |
| 97 |
except IndexError: |
| 98 |
pass |
| 99 |
except RpcError: |
| 100 |
# Check to see if it's JDM of Junos Node slicing |
| 101 |
try: |
| 102 |
current_re_sw = device.rpc.get_software_information() |
| 103 |
if current_re_sw is not None: |
| 104 |
server_slot = current_re_sw.findtext( |
| 105 |
'./package-information[name="Server ' 'slot"]/comment' |
| 106 |
) |
| 107 |
slot_num = re.findall(r"Server slot : (\d+)", server_slot)[0] |
| 108 |
current_re = ["server" + slot_num] |
| 109 |
except Exception: |
| 110 |
pass |
| 111 |
|
| 112 |
# An empty list indicates a problem finding any current_re info. |
| 113 |
# Return None. |
| 114 |
if len(current_re) == 0: |
| 115 |
current_re = None |
| 116 |
|
| 117 |
return { |
| 118 |
"current_re": current_re, |
| 119 |
} |