forked from sonic-net/sonic-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
111 lines (85 loc) · 2.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
#
# main.py
#
# Command-line utility for interacting with HW Watchdog in SONiC
#
try:
import os
import sys
import click
import sonic_platform
from sonic_py_common import logger
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
# Global logger instance
log = logger.Logger(SYSLOG_IDENTIFIER)
# ==================== 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.log_error("Failed to get chassis")
return CHASSIS_LOAD_ERROR
platform_watchdog = chassis.get_watchdog()
if not platform_watchdog:
log.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()