Skip to content

Commit

Permalink
Add watchdogutil to control the hw watchdog (sonic-net#945)
Browse files Browse the repository at this point in the history
* Add watchdogutil to control the hw watchdog

* fix LGTM

* Fixed based on review comments

* replace the is_armed() and get_remaining_time to status() subcommand

* syntax error

* Add more info to the output

* re-format of output

* remove spaces

* change the version number of watchdogutil

* Change the output parsing for the watchdog arm case

* typo

* fix more review comments
  • Loading branch information
sujinmkang authored Jul 8, 2020
1 parent 60e5410 commit 7c512f3
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
'sonic-utilities-tests',
'undebug',
'utilities_common',
'watchdogutil',
],
package_data={
'show': ['aliases.ini'],
Expand Down Expand Up @@ -136,6 +137,7 @@
'sonic-clear = clear.main:cli',
'sonic_installer = sonic_installer.main:cli',
'undebug = undebug.main:cli',
'watchdogutil = watchdogutil.main:watchdogutil',
]
},
# NOTE: sonic-utilities also depends on other packages that are either only
Expand Down
Empty file added watchdogutil/__init__.py
Empty file.
137 changes: 137 additions & 0 deletions watchdogutil/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/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 = "1.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))

# 'status' subcommand
@watchdogutil.command()
def status():
"""Check the watchdog status with remaining_time if it's armed"""
status = platform_watchdog.is_armed()
remaining_time = platform_watchdog.get_remaining_time()
if status is True:
click.echo("Status: Armed")
click.echo("Time remaining: {} seconds".format(remaining_time))
else:
click.echo("Status: Unarmed")


# 'disarm' subcommand
@watchdogutil.command()
def disarm():
"""Disarm HW watchdog"""
result = platform_watchdog.disarm()
if result is True:
click.echo("Watchdog disarmed successfully")
else:
click.echo("Failed to disarm Watchdog")

# '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"""
result = int(platform_watchdog.arm(seconds))
if result < 0:
click.echo("Failed to arm Watchdog for {} seconds".format(seconds))
else:
click.echo("Watchdog armed for {} seconds".format(result))

if __name__ == '__main__':
watchdogutil()

0 comments on commit 7c512f3

Please sign in to comment.