-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add script to periodically update oper status of management interface #21245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
db57986
Add script to periodicall update mgmt interface status in STATE_DB
SuvarnaMeenakshi ab4ee3e
Update state_db
SuvarnaMeenakshi 895fd3f
Update mgmt_oper_status script and add unit test
SuvarnaMeenakshi 71a40db
Modify script name in unit test
SuvarnaMeenakshi 45cc1f6
Modify as per review comment
SuvarnaMeenakshi d829495
Modify as per review comment
SuvarnaMeenakshi 10f6e69
Fix unit test
SuvarnaMeenakshi 8207fc2
Modify as per review comment
SuvarnaMeenakshi 9c14bf4
Return error if exception
SuvarnaMeenakshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
""" | ||
import sys | ||
import subprocess | ||
import syslog | ||
|
||
from sonic_py_common import multi_asic, device_info | ||
from swsscommon.swsscommon import SonicV2Connector | ||
|
||
|
||
def main(): | ||
db = SonicV2Connector(use_unix_socket_path=True) | ||
db.connect('CONFIG_DB') | ||
db.connect('STATE_DB') | ||
mgmt_ports_keys = db.keys(db.CONFIG_DB, 'MGMT_PORT|*' ) | ||
if not mgmt_ports_keys: | ||
syslog.syslog(syslog.LOG_DEBUG, 'No management interface found') | ||
else: | ||
try: | ||
mgmt_ports = [key.split('MGMT_PORT|')[-1] for key in mgmt_ports_keys] | ||
for port in mgmt_ports: | ||
state_db_mgmt_port = db.keys(db.STATE_DB, 'MGMT_PORT_TABLE|*' ) | ||
state_db_key = "MGMT_PORT_TABLE|{}".format(port) | ||
prev_oper_status = 'unknown' | ||
if state_db_key in state_db_mgmt_port: | ||
prev_oper_status = db.get(db.STATE_DB, state_db_key, 'oper_status') | ||
port_operstate_path = '/sys/class/net/{}/operstate'.format(port) | ||
oper_status = subprocess.run(['cat', port_operstate_path], capture_output=True, text=True) | ||
current_oper_status = oper_status.stdout.strip() | ||
if current_oper_status != prev_oper_status: | ||
db.set(db.STATE_DB, state_db_key, 'oper_status', current_oper_status) | ||
log_level = syslog.LOG_INFO if current_oper_status == 'up' else syslog.LOG_WARNING | ||
syslog.syslog(log_level, "mgmt_oper_status: {}".format(current_oper_status)) | ||
except Exception as e: | ||
syslog.syslog(syslog.LOG_ERR, "mgmt_oper_status exception : {}".format(str(e))) | ||
db.set(db.STATE_DB, state_db_key, 'oper_status', 'unknown') | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
sys.exit(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
import subprocess | ||
import syslog | ||
import sys | ||
import mgmt_oper_status | ||
|
||
class TestMgmtOperStatusCheck(unittest.TestCase): | ||
|
||
@patch('mgmt_oper_status.SonicV2Connector') | ||
@patch('mgmt_oper_status.subprocess.run') | ||
@patch('mgmt_oper_status.syslog.syslog') | ||
def test_main_no_mgmt_ports(self, mock_syslog, mock_subprocess, mock_SonicV2Connector): | ||
mock_db = MagicMock() | ||
mock_SonicV2Connector.return_value = mock_db | ||
mock_db.keys.return_value = [] | ||
|
||
mgmt_oper_status.main() | ||
|
||
mock_syslog.assert_called_with(syslog.LOG_DEBUG, 'No management interface found') | ||
|
||
@patch('mgmt_oper_status.SonicV2Connector') | ||
@patch('mgmt_oper_status.subprocess.run') | ||
@patch('mgmt_oper_status.syslog.syslog') | ||
def test_main_with_mgmt_ports(self, mock_syslog, mock_subprocess, mock_SonicV2Connector): | ||
mock_db = MagicMock() | ||
mock_SonicV2Connector.return_value = mock_db | ||
mgmt_ports_keys = ['MGMT_PORT|eth0', 'MGMT_PORT|eth1'] | ||
mock_db.keys.return_value = mgmt_ports_keys | ||
mock_db.set.return_value = None | ||
|
||
mock_subprocess.return_value = subprocess.CompletedProcess(args=['cat', '/sys/class/net/eth0/operstate'], returncode=0, stdout='up', stderr='') | ||
|
||
mgmt_oper_status.main() | ||
|
||
mock_syslog.assert_any_call(syslog.LOG_INFO, 'mgmt_oper_status: up') | ||
mock_syslog.assert_any_call(syslog.LOG_INFO, 'mgmt_oper_status: up') | ||
|
||
mock_db.set.assert_any_call(mock_db.STATE_DB, 'MGMT_PORT_TABLE|eth0', 'oper_status', 'up') | ||
mock_db.set.assert_any_call(mock_db.STATE_DB, 'MGMT_PORT_TABLE|eth1', 'oper_status', 'up') | ||
|
||
@patch('mgmt_oper_status.SonicV2Connector') | ||
@patch('mgmt_oper_status.subprocess.run') | ||
@patch('mgmt_oper_status.syslog.syslog') | ||
def test_main_with_mgmt_port_down(self, mock_syslog, mock_subprocess, mock_SonicV2Connector): | ||
mock_db = MagicMock() | ||
mock_SonicV2Connector.return_value = mock_db | ||
mgmt_ports_keys = ['MGMT_PORT|eth0'] | ||
mock_db.keys.return_value = mgmt_ports_keys | ||
mock_db.set.return_value = None | ||
|
||
mock_subprocess.return_value = subprocess.CompletedProcess(args=['cat', '/sys/class/net/eth0/operstate'], returncode=0, stdout='down', stderr='') | ||
|
||
mgmt_oper_status.main() | ||
|
||
mock_syslog.assert_any_call(syslog.LOG_WARNING, 'mgmt_oper_status: down') | ||
|
||
mock_db.set.assert_any_call(mock_db.STATE_DB, 'MGMT_PORT_TABLE|eth0', 'oper_status', 'down') | ||
|
||
|
||
@patch('mgmt_oper_status.SonicV2Connector') | ||
@patch('mgmt_oper_status.subprocess.run') | ||
@patch('mgmt_oper_status.syslog.syslog') | ||
def test_main_exception_handling(self, mock_syslog, mock_subprocess, mock_SonicV2Connector): | ||
mock_db = MagicMock() | ||
mock_SonicV2Connector.return_value = mock_db | ||
mgmt_ports_keys = ['MGMT_PORT|eth0'] | ||
mock_db.keys.return_value = mgmt_ports_keys | ||
mock_db.set.return_value = None | ||
|
||
mock_subprocess.side_effect = Exception("File not found") | ||
|
||
mgmt_oper_status.main() | ||
|
||
mock_syslog.assert_called_with(syslog.LOG_ERR, "mgmt_oper_status exception : File not found") | ||
mock_db.set.assert_any_call(mock_db.STATE_DB, 'MGMT_PORT_TABLE|eth0', 'oper_status', 'unknown') | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have we tested out config relolad/minigraph scenario. Are we not getting monit error as that can impact nightly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified reload/reboot, did not see the monit log message, is there any other specific concern on why this error might get logged?