-
Notifications
You must be signed in to change notification settings - Fork 817
Add watchdogutil to control the hw watchdog #945
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 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5a40f00
Add watchdogutil to control the hw watchdog
sujinmkang 93c4b6c
fix LGTM
sujinmkang a4ad009
Fixed based on review comments
sujinmkang c7316da
replace the is_armed() and get_remaining_time to status() subcommand
sujinmkang 8d49320
syntax error
sujinmkang a75cc3c
Add more info to the output
sujinmkang 0e68361
re-format of output
sujinmkang 6adda87
remove spaces
sujinmkang 1e8a3be
change the version number of watchdogutil
sujinmkang fd970b8
Change the output parsing for the watchdog arm case
sujinmkang ee17e1b
typo
sujinmkang 9623fd8
fix more review comments
sujinmkang 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
Empty file.
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,128 @@ | ||
| #!/usr/bin/env python | ||
| # | ||
| # main.py | ||
| # | ||
| # Command-line utility for interacting with HW Watchdog in SONiC | ||
| # | ||
|
|
||
| try: | ||
| import sys | ||
| import os | ||
| import click | ||
| import syslog | ||
| import sonic_platform | ||
| except ImportError as e: | ||
| raise ImportError("%s - required module not found" % str(e)) | ||
|
|
||
| VERSION = '2.0' | ||
|
|
||
| SYSLOG_IDENTIFIER = "watchdogutil" | ||
|
|
||
| WATCHDOG_LOAD_ERROR = -1 | ||
| CHASSIS_LOAD_ERROR = -2 | ||
|
|
||
| # Global platform-specific watchdog class instance | ||
| platform_watchdog = None | ||
|
|
||
|
|
||
| # ========================== Syslog wrappers ========================== | ||
|
|
||
|
|
||
| def log_info(msg, also_print_to_console=False): | ||
| syslog.openlog(SYSLOG_IDENTIFIER) | ||
| syslog.syslog(syslog.LOG_INFO, msg) | ||
| syslog.closelog() | ||
|
|
||
| if also_print_to_console: | ||
| click.echo(msg) | ||
|
|
||
|
|
||
| def log_warning(msg, also_print_to_console=False): | ||
| syslog.openlog(SYSLOG_IDENTIFIER) | ||
| syslog.syslog(syslog.LOG_WARNING, msg) | ||
| syslog.closelog() | ||
|
|
||
| if also_print_to_console: | ||
| click.echo(msg) | ||
|
|
||
|
|
||
| def log_error(msg, also_print_to_console=False): | ||
| syslog.openlog(SYSLOG_IDENTIFIER) | ||
| syslog.syslog(syslog.LOG_ERR, msg) | ||
| syslog.closelog() | ||
|
|
||
| if also_print_to_console: | ||
| click.echo(msg) | ||
|
|
||
|
|
||
| # ==================== Methods for initialization ==================== | ||
|
|
||
| # Loads platform specific watchdog module from source | ||
| def load_platform_watchdog(): | ||
| global platform_watchdog | ||
|
|
||
| platform = sonic_platform.platform.Platform() | ||
|
|
||
| chassis = platform.get_chassis() | ||
| if not chassis: | ||
| log_error("Failed to get chassis") | ||
| return CHASSIS_LOAD_ERROR | ||
|
|
||
| platform_watchdog = chassis.get_watchdog() | ||
| if not platform_watchdog: | ||
| log_error("Failed to get watchdog module") | ||
| return WATCHDOG_LOAD_ERROR | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| # ==================== CLI commands and groups ==================== | ||
|
|
||
|
|
||
| # This is our main entrypoint - the main 'watchdogutil' command | ||
| @click.group() | ||
| def watchdogutil(): | ||
| """watchdogutil - Command line utility for providing HW watchdog interface""" | ||
|
|
||
| if os.geteuid() != 0: | ||
| click.echo("Root privileges are required for this operation") | ||
| sys.exit(1) | ||
|
|
||
| # Load platform-specific watchdog class | ||
| err = load_platform_watchdog() | ||
| if err != 0: | ||
| sys.exit(2) | ||
|
|
||
| # 'version' subcommand | ||
| @watchdogutil.command() | ||
| def version(): | ||
| """Display version info""" | ||
| click.echo("watchdogutil version {0}".format(VERSION)) | ||
|
|
||
| # 'is_armed' subcommand | ||
| @watchdogutil.command() | ||
sujinmkang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| def is_armed(): | ||
| """Check if watchdog is armed""" | ||
| click.echo(str(platform_watchdog.is_armed())) | ||
|
|
||
| # 'get_remaining_time' subcommand | ||
| @watchdogutil.command() | ||
| def get_remaining_time(): | ||
| """Check the remaining watchdog time""" | ||
| click.echo(str(platform_watchdog.get_remaining_time())) | ||
|
|
||
| # 'disarm' subcommand | ||
| @watchdogutil.command() | ||
| def disarm(): | ||
| """Disarm HW watchdog""" | ||
| click.echo(str(platform_watchdog.disarm())) | ||
jleveque marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # 'arm' subcommand | ||
| @watchdogutil.command() | ||
| @click.option('-s', '--seconds', default=180, type=int, help="the default timeout of HW watchdog") | ||
| def arm(seconds): | ||
| """Arm HW watchdog""" | ||
| click.echo(str(platform_watchdog.arm(seconds))) | ||
jleveque marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if __name__ == '__main__': | ||
| cli() | ||
jleveque marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.