Skip to content

Commit 7c512f3

Browse files
authored
Add watchdogutil to control the hw watchdog (sonic-net#945)
* 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
1 parent 60e5410 commit 7c512f3

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

setup.py

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
'sonic-utilities-tests',
5454
'undebug',
5555
'utilities_common',
56+
'watchdogutil',
5657
],
5758
package_data={
5859
'show': ['aliases.ini'],
@@ -136,6 +137,7 @@
136137
'sonic-clear = clear.main:cli',
137138
'sonic_installer = sonic_installer.main:cli',
138139
'undebug = undebug.main:cli',
140+
'watchdogutil = watchdogutil.main:watchdogutil',
139141
]
140142
},
141143
# NOTE: sonic-utilities also depends on other packages that are either only

watchdogutil/__init__.py

Whitespace-only changes.

watchdogutil/main.py

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#!/usr/bin/env python
2+
#
3+
# main.py
4+
#
5+
# Command-line utility for interacting with HW Watchdog in SONiC
6+
#
7+
8+
try:
9+
import sys
10+
import os
11+
import click
12+
import syslog
13+
import sonic_platform
14+
except ImportError as e:
15+
raise ImportError("%s - required module not found" % str(e))
16+
17+
VERSION = "1.0"
18+
19+
SYSLOG_IDENTIFIER = "watchdogutil"
20+
21+
WATCHDOG_LOAD_ERROR = -1
22+
CHASSIS_LOAD_ERROR = -2
23+
24+
# Global platform-specific watchdog class instance
25+
platform_watchdog = None
26+
27+
28+
# ========================== Syslog wrappers ==========================
29+
30+
31+
def log_info(msg, also_print_to_console=False):
32+
syslog.openlog(SYSLOG_IDENTIFIER)
33+
syslog.syslog(syslog.LOG_INFO, msg)
34+
syslog.closelog()
35+
36+
if also_print_to_console:
37+
click.echo(msg)
38+
39+
40+
def log_warning(msg, also_print_to_console=False):
41+
syslog.openlog(SYSLOG_IDENTIFIER)
42+
syslog.syslog(syslog.LOG_WARNING, msg)
43+
syslog.closelog()
44+
45+
if also_print_to_console:
46+
click.echo(msg)
47+
48+
49+
def log_error(msg, also_print_to_console=False):
50+
syslog.openlog(SYSLOG_IDENTIFIER)
51+
syslog.syslog(syslog.LOG_ERR, msg)
52+
syslog.closelog()
53+
54+
if also_print_to_console:
55+
click.echo(msg)
56+
57+
58+
# ==================== Methods for initialization ====================
59+
60+
# Loads platform specific watchdog module from source
61+
def load_platform_watchdog():
62+
global platform_watchdog
63+
64+
platform = sonic_platform.platform.Platform()
65+
66+
chassis = platform.get_chassis()
67+
if not chassis:
68+
log_error("Failed to get chassis")
69+
return CHASSIS_LOAD_ERROR
70+
71+
platform_watchdog = chassis.get_watchdog()
72+
if not platform_watchdog:
73+
log_error("Failed to get watchdog module")
74+
return WATCHDOG_LOAD_ERROR
75+
76+
return 0
77+
78+
79+
# ==================== CLI commands and groups ====================
80+
81+
82+
# This is our main entrypoint - the main 'watchdogutil' command
83+
@click.group()
84+
def watchdogutil():
85+
"""watchdogutil - Command line utility for providing HW watchdog interface"""
86+
87+
if os.geteuid() != 0:
88+
click.echo("Root privileges are required for this operation")
89+
sys.exit(1)
90+
91+
# Load platform-specific watchdog class
92+
err = load_platform_watchdog()
93+
if err != 0:
94+
sys.exit(2)
95+
96+
# 'version' subcommand
97+
@watchdogutil.command()
98+
def version():
99+
"""Display version info"""
100+
click.echo("watchdogutil version {0}".format(VERSION))
101+
102+
# 'status' subcommand
103+
@watchdogutil.command()
104+
def status():
105+
"""Check the watchdog status with remaining_time if it's armed"""
106+
status = platform_watchdog.is_armed()
107+
remaining_time = platform_watchdog.get_remaining_time()
108+
if status is True:
109+
click.echo("Status: Armed")
110+
click.echo("Time remaining: {} seconds".format(remaining_time))
111+
else:
112+
click.echo("Status: Unarmed")
113+
114+
115+
# 'disarm' subcommand
116+
@watchdogutil.command()
117+
def disarm():
118+
"""Disarm HW watchdog"""
119+
result = platform_watchdog.disarm()
120+
if result is True:
121+
click.echo("Watchdog disarmed successfully")
122+
else:
123+
click.echo("Failed to disarm Watchdog")
124+
125+
# 'arm' subcommand
126+
@watchdogutil.command()
127+
@click.option('-s', '--seconds', default=180, type=int, help="the default timeout of HW watchdog")
128+
def arm(seconds):
129+
"""Arm HW watchdog"""
130+
result = int(platform_watchdog.arm(seconds))
131+
if result < 0:
132+
click.echo("Failed to arm Watchdog for {} seconds".format(seconds))
133+
else:
134+
click.echo("Watchdog armed for {} seconds".format(result))
135+
136+
if __name__ == '__main__':
137+
watchdogutil()

0 commit comments

Comments
 (0)