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 13 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
jleveque marked this conversation as resolved.
Show resolved Hide resolved
------------------- ----------- ---------------------------- ------ ---------
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
1 change: 1 addition & 0 deletions show/aliases.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ running-configuration=runningconfiguration
running-config=runningconfiguration
startup-configuration=startupconfiguration
startup-config=startupconfiguration
reboot-cause=reboot cause
jleveque marked this conversation as resolved.
Show resolved Hide resolved
80 changes: 67 additions & 13 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1803,20 +1803,74 @@ 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")
@click.argument('history', required=False, type=click.Choice(["history"]))
def reboot_cause(history):
sujinmkang marked this conversation as resolved.
Show resolved Hide resolved
"""Show cause of reboot"""
REBOOT_CAUSE_DIR = "/host/reboot-cause/"
PREVIOUS_REBOOT_CAUSE_FILE = REBOOT_CAUSE_DIR + "previous-reboot-cause.json"
sujinmkang marked this conversation as resolved.
Show resolved Hide resolved
USER_ISSUE_REBOOT_CAUSE_REGEX ="User issued \'{}\' command [User: {}, Time: {}]"
jleveque marked this conversation as resolved.
Show resolved Hide resolved
REBOOT_CAUSE_UNKNOWN = "Unknown"

def read_last_reboot_cause():
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_ISSUE_REBOOT_CAUSE_REGEX.format(data['cause'], data['user'], data['time'])
else:
last_reboot_cause = "{}".format(data['cause'])
return last_reboot_cause

def read_reboot_cause_dbs():
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)

def remove_prefix(text, prefix):
if text.startswith(prefix):
return text[len(prefix):]
return text

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

if 'time' not in entry:
r.append("")
else:
r.append(entry['time'])

if 'user' not in entry:
r.append("")
else:
r.append(entry['user'])

if 'comment' not in entry:
r.append("")
else:
r.append(entry['comment'])
table.append(r)
return table

if not history:
click.echo(read_last_reboot_cause())
else:
cmd = "cat {}".format(PREVIOUS_REBOOT_CAUSE_FILE)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
click.echo(proc.stdout.read())
table = read_reboot_cause_dbs()
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": ""
}
}
58 changes: 58 additions & 0 deletions tests/show_reboot_cause_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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


TEST_REBOOT_CAUSE = "warm-reboot"
TEST_USER = "admin"
TEST_REBOOT_TIME = "Fri Oct 9 04:51:47 UTC 2020"
jleveque marked this conversation as resolved.
Show resolved Hide resolved


"""
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'
def test_reboot_cause(self):
expected_output = "Unknown\n"
runner = CliRunner()
result = runner.invoke(show.cli.commands["reboot-cause"], [])
assert result.output == expected_output

# 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"], ["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"