| 1 |
from jnpr.junos.exception import RpcError |
| 2 |
|
| 3 |
|
| 4 |
def provides_facts(): |
| 5 |
""" |
| 6 |
Returns a dictionary keyed on the facts provided by this module. The value |
| 7 |
of each key is the doc string describing the fact. |
| 8 |
""" |
| 9 |
return { |
| 10 |
"srx_cluster": "A boolean indicating if the device is part of an " |
| 11 |
"SRX cluster.", |
| 12 |
"srx_cluster_id": "A string containing the configured cluster id", |
| 13 |
"srx_cluster_redundancy_group": "A multi-level dictionary of " |
| 14 |
"information about the SRX " |
| 15 |
"cluster redundancy groups " |
| 16 |
"on the device. The first-level " |
| 17 |
"key is the redundancy group id. " |
| 18 |
"The second-level keys are: " |
| 19 |
"cluster_id, failover_count, " |
| 20 |
"node0, and node1. The node0 and " |
| 21 |
"node1 keys have third-level keys " |
| 22 |
"of priority, preempt, status, " |
| 23 |
"and failover_mode. The values " |
| 24 |
"for this fact correspond to the " |
| 25 |
"values of the 'show chassis " |
| 26 |
"cluster status' CLI command.", |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
def get_facts(device): |
| 31 |
""" |
| 32 |
Gathers facts from the <get-chassis-cluster-status/> RPC. |
| 33 |
""" |
| 34 |
srx_cluster = None |
| 35 |
srx_cluster_id = None |
| 36 |
redundancy_group = None |
| 37 |
|
| 38 |
try: |
| 39 |
rsp = device.rpc.get_chassis_cluster_status(normalize=True) |
| 40 |
if rsp is not None: |
| 41 |
if rsp.tag == "error": |
| 42 |
srx_cluster = False |
| 43 |
else: |
| 44 |
srx_cluster = True |
| 45 |
srx_cluster_id = rsp.findtext("cluster-id") |
| 46 |
groups = rsp.findall("redundancy-group") |
| 47 |
if groups is not None: |
| 48 |
redundancy_group = {} |
| 49 |
for group in groups: |
| 50 |
group_id = group.findtext("redundancy-group-id") |
| 51 |
redundancy_group[group_id] = { |
| 52 |
"cluster_id": group.findtext("cluster-id"), |
| 53 |
"failover_count": group.findtext( |
| 54 |
"redundancy-group-failover-count" |
| 55 |
), |
| 56 |
} |
| 57 |
# Iterate over the broken XML in <device-stats> |
| 58 |
for stats in group.findall("./device-stats"): |
| 59 |
for node in zip( |
| 60 |
stats.findall("device-name"), |
| 61 |
stats.findall("device-priority"), |
| 62 |
stats.findall("redundancy-group-status"), |
| 63 |
stats.findall("preempt"), |
| 64 |
stats.findall("failover-mode"), |
| 65 |
stats.findall("monitor-failures"), |
| 66 |
): |
| 67 |
redundancy_group[group_id][node[0].text] = { |
| 68 |
"priority": node[1].text, |
| 69 |
"status": node[2].text, |
| 70 |
"preempt": node[3].text, |
| 71 |
"failover_mode": node[4].text, |
| 72 |
"monitor-failures": node[5].text, |
| 73 |
} |
| 74 |
except RpcError: |
| 75 |
# Likely a device that doesn't implement the |
| 76 |
# <get-chassis-cluster-status/> RPC. |
| 77 |
# That's OK. Just ignore it and leave srx_cluster = None. |
| 78 |
pass |
| 79 |
return { |
| 80 |
"srx_cluster": srx_cluster, |
| 81 |
"srx_cluster_id": srx_cluster_id, |
| 82 |
"srx_cluster_redundancy_group": redundancy_group, |
| 83 |
} |