Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions ansible/library/lag_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
from ansible.module_utils.basic import *

from sonic_py_common import multi_asic
DOCUMENTATION = '''
---
module: lag_facts
Expand Down Expand Up @@ -32,7 +32,7 @@ def __init__(self):
),
supports_check_mode=False,
)
self.lag_names = []
self.lag_names = {}
self.lags = {}
return

Expand All @@ -41,53 +41,60 @@ def run(self):
Main method of the class
'''
self.get_po_names()
for po in self.lag_names:
for po,ns in self.lag_names.items():
self.lags[po] = {}
self.lags[po]['po_stats'] = self.get_po_status(po)
self.lags[po]['po_config'] = self.get_po_config(po)
self.lags[po]['po_intf_stat'] = self.get_po_intf_stat(po)
ns_id = multi_asic.get_asic_id_from_name(ns) if ns else ''
self.lags[po]['po_stats'] = self.get_po_status(po, ns_id)
self.lags[po]['po_config'] = self.get_po_config(po, ns_id)
self.lags[po]['po_intf_stat'] = self.get_po_intf_stat(po, ns)
self.lags[po]['po_namespace_id'] = ns_id
self.module.exit_json(ansible_facts={'lag_facts': {'names': self.lag_names, 'lags': self.lags}})
return

def get_po_names(self):
'''
Collect configured lag interface names
'''
rt, out, err = self.module.run_command("sonic-cfggen -m /etc/sonic/minigraph.xml -v \"PORTCHANNEL.keys() | join(' ')\"")
if rt != 0:
fail_msg="Command to retrieve portchannel names failed return=%d, out=%s, err=%s" %(rt, out, err)
self.module.fail_json(msg=fail_msg)
else:
self.lag_names = out.split()
namespace_list = multi_asic.get_namespace_list()
for ns in namespace_list:
rt, out, err = self.module.run_command("sonic-cfggen -m /etc/sonic/minigraph.xml {} -v \"PORTCHANNEL.keys() | join(' ')\"".format('-n ' + ns if ns else ''))
if rt != 0:
fail_msg="Command to retrieve portchannel names failed return=%d, out=%s, err=%s" %(rt, out, err)
self.module.fail_json(msg=fail_msg)
else:
for po in out.split():
if multi_asic.is_port_channel_internal(po):
continue
self.lag_names[po] = ns
return

def get_po_status(self, po_name):
def get_po_status(self, po_name, ns):
'''
Collect lag information by command docker teamdctl
'''
rt, out, err = self.module.run_command("docker exec -i teamd teamdctl "+po_name+" state dump")
rt, out, err = self.module.run_command("docker exec -i teamd{} teamdctl ".format(ns) +po_name+" state dump")
if rt != 0:
fail_msg="failed dump port channel %s status return=%d, out=%s, err=%s" %(po_name, rt, out, err)
self.module.fail_json(msg=fail_msg)
json_info = json.loads(out)
return json_info

def get_po_config(self, po_name):
def get_po_config(self, po_name, ns):
'''
Collect lag information by command docker teamdctl
'''
rt, out, err = self.module.run_command("docker exec -i teamd teamdctl "+po_name+" config dump")
rt, out, err = self.module.run_command("docker exec -i teamd{} teamdctl ".format(ns) +po_name+" config dump")
if rt != 0:
fail_msg="failed dump port channel %s config return=%d, out=%s, err=%s" %(po_name, rt, out, err)
self.module.fail_json(msg=fail_msg)
json_info = json.loads(out)
return json_info

def get_po_intf_stat(self, po_name):
def get_po_intf_stat(self, po_name, ns):
'''
Collect lag information by command docker teamdctl
'''
rt, out, err = self.module.run_command("ip link show " + po_name)
rt, out, err = self.module.run_command("sudo ip {} link show ".format('-n ' + ns if ns else '') + po_name)
if rt != 0:
fail_msg="failed show interface status of %s return=%d, out=%s, err=%s" %(po_name, rt, out, err)
self.module.fail_json(msg=fail_msg)
Expand Down