Skip to content
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

Add show reboot-history #1154

Closed
wants to merge 82 commits into from
Closed
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
e5e19ab
initial commit
sujinmkang Oct 9, 2020
60085a7
group the reboot cause show command
sujinmkang Oct 9, 2020
e1244a5
[setup.py] Pin external runtime dependencies at known working version…
jleveque Oct 9, 2020
c4b2b6c
group the reboot-cause history with reboot-cause
sujinmkang Oct 10, 2020
561d133
[README.md] Add instructions for setting up a build/test environment …
jleveque Oct 11, 2020
a71c72b
[show] Add 'show' CLI for system-health feature (#971)
shlomibitton Oct 12, 2020
b6af9f4
Fix expected neighbor when multiple ports connect to same neighbor (#…
lguohan Oct 13, 2020
3a7457c
[celestica] consutil to support customize tty device name (#1155)
sandycelestica Oct 13, 2020
d8977fe
read last reboot-cause from symlink file
sujinmkang Oct 13, 2020
9531e41
rename
sujinmkang Oct 13, 2020
fcd8647
lgtm
sujinmkang Oct 13, 2020
e66777b
fix test
sujinmkang Oct 13, 2020
29a7e78
fix ut
sujinmkang Oct 13, 2020
89b189c
ut
sujinmkang Oct 14, 2020
4659ee2
ut2
sujinmkang Oct 14, 2020
059cdca
Fix exception for ipaddress in python2 (#1164)
bingwang-ms Oct 14, 2020
a733df5
[watermarkstat] Add unit tests for watermarkstat show commands (#1157)
neethajohn Oct 14, 2020
e51d44f
[consutil] Fix issue where the show line command crash if no ttyUSB e…
Blueve Oct 15, 2020
c382d89
[config/console] Support update console configuration related command…
Blueve Oct 15, 2020
574726f
rename last reboot cause file and fix ut
sujinmkang Oct 15, 2020
28c23f0
review comments & fix ut
sujinmkang Oct 15, 2020
984ef40
ut fix
sujinmkang Oct 15, 2020
4220356
review comments
sujinmkang Oct 15, 2020
3b62af0
show/main.py
sujinmkang Oct 15, 2020
b96fb6b
remove redundant
sujinmkang Oct 16, 2020
c395e14
Modify fast-reboot script to use BGP service script to stop bgp servi…
vaibhavhd Oct 19, 2020
510d0ad
[consutil] Add brief option to show line command (#1176)
Blueve Oct 20, 2020
59a511d
[config/show] Add CLI support for proxy arp (#1168)
theasianpianist Oct 20, 2020
13bd06b
Fixes the issue with show interface counters and for pfc and queue co…
abdosi Oct 20, 2020
5c12ffd
review comments
sujinmkang Oct 21, 2020
ca92e36
[counterpoll] Disable Counter Poll When Entering Fast Reboot (#1174)
tahmed-dev Oct 21, 2020
f08d628
type
sujinmkang Oct 22, 2020
0c3a97f
Remove stdeb.cfg; no longer used now that we build as wheel (#1182)
jleveque Oct 23, 2020
5651b17
fix unit test
sujinmkang Oct 23, 2020
a060cf9
review comments
sujinmkang Oct 23, 2020
00a2570
fdbshow and nbrshow use SonicV2Connector with decode_responses=True, …
qiluo-msft Oct 24, 2020
f14bbe5
[CLI][show][platform] Added ASIC count in the output. (#1185)
smaheshm Oct 26, 2020
dfc73ac
review comments
sujinmkang Oct 26, 2020
e889212
[fwutil]: Set min log priority to INFO. (#1191)
nazariig Oct 26, 2020
987c433
^^
sujinmkang Oct 26, 2020
63c6733
Add license file, remove third-party licenses file (#1192)
jleveque Oct 27, 2020
1753f22
[show] remove 'device2interface_dict' in show interface neighbor expe…
lolyu Oct 27, 2020
f645a38
unit test coverage
sujinmkang Oct 27, 2020
d7e372b
fix expected_output
sujinmkang Oct 27, 2020
615c3b4
unit test
sujinmkang Oct 27, 2020
baeeed9
mock read file
sujinmkang Oct 28, 2020
2f33931
ut
sujinmkang Oct 28, 2020
2decd0c
ut
sujinmkang Oct 28, 2020
8af9aee
Show FG_NHG CLI Commands Added (#1056)
kktheballer Oct 29, 2020
ef5f212
[sfputil] Display 'N/A' for non-SFP ports (#1078)
PJHsieh Nov 2, 2020
b4a0ebe
fix unittest
sujinmkang Nov 2, 2020
9011750
Merge branch 'reboot-history' of https://github.com/sujinmkang/sonic-…
sujinmkang Nov 2, 2020
6d0452b
initial commit
sujinmkang Oct 9, 2020
b4075f6
group the reboot cause show command
sujinmkang Oct 9, 2020
8359c7b
group the reboot-cause history with reboot-cause
sujinmkang Oct 10, 2020
6f838c1
read last reboot-cause from symlink file
sujinmkang Oct 13, 2020
ac52429
rename
sujinmkang Oct 13, 2020
cc4f4e9
lgtm
sujinmkang Oct 13, 2020
b645c24
fix test
sujinmkang Oct 13, 2020
e9fb1fc
fix ut
sujinmkang Oct 13, 2020
c61191d
ut
sujinmkang Oct 14, 2020
b7a634c
ut2
sujinmkang Oct 14, 2020
6dce6ed
rename last reboot cause file and fix ut
sujinmkang Oct 15, 2020
c6f02ec
review comments & fix ut
sujinmkang Oct 15, 2020
17516c5
ut fix
sujinmkang Oct 15, 2020
81d0736
review comments
sujinmkang Oct 15, 2020
df9cfcf
show/main.py
sujinmkang Oct 15, 2020
cd282e9
remove redundant
sujinmkang Oct 16, 2020
e6b55ae
review comments
sujinmkang Oct 21, 2020
ac2eba5
type
sujinmkang Oct 22, 2020
b927ab7
fix unit test
sujinmkang Oct 23, 2020
212274d
review comments
sujinmkang Oct 23, 2020
f452276
review comments
sujinmkang Oct 26, 2020
2e8fad6
^^
sujinmkang Oct 26, 2020
fd2629f
unit test coverage
sujinmkang Oct 27, 2020
b59df08
fix expected_output
sujinmkang Oct 27, 2020
7d20f97
unit test
sujinmkang Oct 27, 2020
08817ad
mock read file
sujinmkang Oct 28, 2020
b18f0c4
ut
sujinmkang Oct 28, 2020
9489bb8
ut
sujinmkang Oct 28, 2020
517c2c5
fix unittest
sujinmkang Nov 2, 2020
02eb1c7
Merge branch 'reboot-history' of https://github.com/sujinmkang/sonic-…
sujinmkang Nov 3, 2020
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
20 changes: 20 additions & 0 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,26 @@ This command displays the cause of the previous reboot
User issued reboot command [User: admin, Time: Mon Mar 25 01:02:03 UTC 2019]
```

**show reboot-cause history**

This command displays the history of the previous reboots up to 10 entry

- Usage:
```
show reboot-cause history
```

- Example:
```
admin@sonic:~$ show reboot-cause history
Name Cause Time User Comment
------------------- ----------- ---------------------------- ------ ---------
2020_10_09_02_33_06 reboot Fri Oct 9 02:29:44 UTC 2020 admin
2020_10_09_01_56_59 reboot Fri Oct 9 01:53:49 UTC 2020 admin
2020_10_09_02_00_53 fast-reboot Fri Oct 9 01:58:04 UTC 2020 admin
2020_10_09_04_53_58 warm-reboot Fri Oct 9 04:51:47 UTC 2020 admin
```

**show uptime**

This command displays the current system uptime
Expand Down
67 changes: 50 additions & 17 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1800,23 +1800,56 @@ def mmu():


#
# 'reboot-cause' command ("show reboot-cause")
#
@cli.command('reboot-cause')
def reboot_cause():
"""Show cause of most recent reboot"""
PREVIOUS_REBOOT_CAUSE_FILE = "/host/reboot-cause/previous-reboot-cause.txt"

# At boot time, PREVIOUS_REBOOT_CAUSE_FILE is generated based on
# the contents of the 'reboot cause' file as it was left when the device
# went down for reboot. This file should always be created at boot,
# but check first just in case it's not present.
if not os.path.isfile(PREVIOUS_REBOOT_CAUSE_FILE):
click.echo("Unable to determine cause of previous reboot\n")
else:
cmd = "cat {}".format(PREVIOUS_REBOOT_CAUSE_FILE)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
click.echo(proc.stdout.read())
# 'reboot-cause' group ("show reboot-cause")
#
@cli.group('reboot-cause', invoke_without_command=True)
@click.pass_context
def reboot_cause(ctx):
if ctx.invoked_subcommand is None:
"""Show cause of reboot"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does moving this docstring inside the if statement prevent Click from using it as the help message?

I think we should move away from this and explicitly declare the help message in the decorator going forward. In this instance, the decorator should read,

@cli.group('reboot-cause', short_help='Show cause of reboot', invoke_without_command=True)

Also, why change the message from "Show cause of most recent reboot"?

REBOOT_CAUSE_DIR = "/host/reboot-cause/"
PREVIOUS_REBOOT_CAUSE_FILE = os.path.join(REBOOT_CAUSE_DIR, "previous-reboot-cause.json")
USER_ISSUED_REBOOT_CAUSE_REGEX ="User issued \'{}\' command [User: {}, Time: {}]"
REBOOT_CAUSE_UNKNOWN = "Unknown"

last_reboot_cause = REBOOT_CAUSE_UNKNOWN
# Read the last previous reboot cause
if os.path.exists(PREVIOUS_REBOOT_CAUSE_FILE):
with open(PREVIOUS_REBOOT_CAUSE_FILE, "r") as last_cause_file:
data = json.load(last_cause_file)
if data['user']:
last_reboot_cause = USER_ISSUED_REBOOT_CAUSE_REGEX.format(data['cause'], data['user'], data['time'])
else:
last_reboot_cause = "{}".format(data['cause'])

click.echo(last_reboot_cause)

# 'history' subcommand ("show reboot-cause history")
@reboot_cause.command()
def history():
"""Show history of reboot-cause"""
REBOOT_CAUSE_TABLE_NAME = "REBOOT_CAUSE"
TABLE_NAME_SEPARATOR = '|'
db = SonicV2Connector(host='127.0.0.1')
db.connect(db.STATE_DB, False) # Make one attempt only
prefix = REBOOT_CAUSE_TABLE_NAME + TABLE_NAME_SEPARATOR
_hash = '{}{}'.format(prefix, '*')
table_keys = db.keys(db.STATE_DB, _hash)
table_keys.sort(reverse=True)

table = []
for tk in table_keys:
entry = db.get_all(db.STATE_DB, tk)
r = []
r.append(tk.replace(prefix,""))
r.append(entry['cause'] if 'cause' in entry else "")
r.append(entry['time'] if 'time' in entry else "")
r.append(entry['user'] if 'user' in entry else "")
r.append(entry['comment'] if 'comment' in entry else "")
table.append(r)

header = ['Name', 'Cause', 'Time', 'User', 'Comment']
click.echo(tabulate(table, header))


#
Expand Down
12 changes: 12 additions & 0 deletions tests/mock_tables/state_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,17 @@
"speed_target": "50",
"led_status": "green",
"timestamp": "20200813 01:32:30"
},
"REBOOT_CAUSE|2020_10_09_04_53_58": {
"cause": "warm-reboot",
"time": "Fri Oct 9 04:51:47 UTC 2020",
"user": "admin",
"comment": ""
},
"REBOOT_CAUSE|2020_10_09_02_33_06": {
"cause": "reboot",
"time": "Fri Oct 9 02:29:44 UTC 2020",
"user": "admin",
"comment": ""
}
}
87 changes: 87 additions & 0 deletions tests/show_reboot_cause_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
import sys
import textwrap

import mock

from click.testing import CliRunner

test_path = os.path.dirname(os.path.abspath(__file__))
modules_path = os.path.dirname(test_path)
sys.path.insert(0, modules_path)

import show.main as show

module_ = "builtins"
module_ = module_ if module_ in sys.modules else '__builtin__'

reboot_cause_json_file="/host/reboot-cause/previous-reboot-cause.json"
"""
Note: The following 'show reboot-cause' commands simply call other SONiC
CLI utilities, so the unit tests for the other utilities are expected
to cover testing their functionality:

show reboot-cause
show reboot-cause history
"""

class TestShowRebootCause(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ["UTILITIES_UNIT_TESTING"] = "1"

# Test 'show reboot-cause' without previous-reboot-cause.json
def test_reboot_cause_no_history_file(self):
expected_output = "Unknown\n"
runner = CliRunner()
result = runner.invoke(show.cli.commands["reboot-cause"], [])
assert result.output == expected_output

# Test 'show reboot-cause' with user issued reboot
def test_reboot_cause_user(self):
expected_output = "User issued reboot command [User: admin, Time: Thu Oct 22 03:11:08 UTC 2020]\n"
reboot_cause_user_json = """\
{"comment": "", "gen_time": "2020_10_22_03_14_07", "cause": "reboot", "user": "admin", "time": "Thu Oct 22 03:11:08 UTC 2020"}
"""
runner = CliRunner()
with mock.patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
open_mocked = mock.mock_open(read_data=textwrap.dedent(reboot_cause_user_json))
with mock.patch("{}.open".format(module_), open_mocked):
result = runner.invoke(show.cli.commands["reboot-cause"], [])
assert result == expected_output
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work without dedenting? Because reboot_cause_user_json is indented above.

open_mocked.assert_called_once_with(reboot_cause_json_file)

# Test 'show reboot-cause' with non-user issue reboot (hardware reboot-cause or unknown reboot-cause)
def test_reboot_cause_non_user(self):
expected_output = "Watchdog\n"
reboot_cause_watchdog_json = """\
{"comment": "", "gen_time": "2020_10_22_03_15_08", "cause": "Watchdog", "user": "", "time": ""}
"""
runner = CliRunner()
with mock.patch("os.path.isfile") as mock_isfile:
mock_isfile.return_value = True
open_mocked = mock.mock_open(read_data=reboot_cause_watchdog_json)
with mock.patch("{}.open".format(module_), open_mocked):
result = runner.invoke(show.cli.commands["reboot-cause"], [])
assert result == expected_output
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work without dedenting? Because reboot_cause_user_json is indented above.

open_mocked.assert_called_once_with(reboot_cause_json_file)

# Test 'show reboot-cause history'
def test_reboot_cause_history(self):
expected_output = """\
Name Cause Time User Comment
------------------- ----------- ---------------------------- ------ ---------
2020_10_09_04_53_58 warm-reboot Fri Oct 9 04:51:47 UTC 2020 admin
2020_10_09_02_33_06 reboot Fri Oct 9 02:29:44 UTC 2020 admin
"""
runner = CliRunner()
result = runner.invoke(show.cli.commands["reboot-cause"].commands["history"], [])
assert result.output == expected_output

@classmethod
def teardown_class(cls):
print("TEARDOWN")
os.environ["PATH"] = os.pathsep.join(os.environ["PATH"].split(os.pathsep)[:-1])
os.environ["UTILITIES_UNIT_TESTING"] = "0"