1#!/usr/bin/env python3

2# Copyright 2021 Canonical Ltd.

3# All rights reserved.

4#

5# Written by:

6# Vic Liu <vic.liu@canonical.com>

7#

8# Checkbox is free software: you can redistribute it and/or modify

9# it under the terms of the GNU General Public License version 3,

10# as published by the Free Software Foundation.

11#

12# Checkbox is distributed in the hope that it will be useful,

13# but WITHOUT ANY WARRANTY; without even the implied warranty of

14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

15# GNU General Public License for more details.

16#

17# You should have received a copy of the GNU General Public License

18# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

19

20 import subprocess

21

22 from checkbox_support.snap_utils.system import on_ubuntucore

23 from checkbox_support.snap_utils.system import get_series

24

25

26 def get_systemd_wdt_usec():

27 """

28 Return value of systemd-watchdog RuntimeWatchdogUSec

29 """

30 cmd = ["systemctl", "show", "-p", "RuntimeWatchdogUSec"]

31 try:

32 result = subprocess.check_output(cmd, universal_newlines=True)

33 except Exception as err:

34 raise SystemExit("Error: {}".format(err))

35

36 if result:

37 runtime_watchdog_usec = result.split("=")[1].strip()

38 return runtime_watchdog_usec

39 else:

40 raise SystemExit(

41 "Unexpected failure occurred when executing: {}".format(cmd)

42 )

43

44

45 def watchdog_service_check():

46 """

47 Check if the watchdog service is configured correctly

48 """

49 cmd = ["systemctl", "is-active", "watchdog.service", "--quiet"]

50 try:

51 return not subprocess.run(cmd).returncode

52 except Exception as err:

53 raise SystemExit("Error: {}".format(err))

54

55

56 def main():

57 runtime_watchdog_usec = get_systemd_wdt_usec()

58 systemd_wdt_configured = runtime_watchdog_usec != "0"

59 wdt_service_configured = watchdog_service_check()

60 ubuntu_version = int(get_series().split(".")[0])

61 watchdog_config_ready = True

62

63 if (ubuntu_version >= 20) or (on_ubuntucore()):

64 if not systemd_wdt_configured:

65 print(

66 "systemd watchdog should be enabled but reset timeout: "

67 "{}".format(runtime_watchdog_usec)

68 )

69 watchdog_config_ready = False

70 if wdt_service_configured:

71 print("found unexpected active watchdog.service unit")

72 watchdog_config_ready = False

73 if watchdog_config_ready:

74 print(

75 "systemd watchdog enabled, reset timeout: {}".format(

76 runtime_watchdog_usec

77 )

78 )

79 print("watchdog.service is not active")

80 else:

81 if systemd_wdt_configured:

82 print(

83 "systemd watchdog should not be enabled but reset timeout: "

84 "{}".format(runtime_watchdog_usec)

85 )

86 watchdog_config_ready = False

87 if not wdt_service_configured:

88 print("watchdog.service unit does not report as active")

89 watchdog_config_ready = False

90 if watchdog_config_ready:

91 print("systemd watchdog disabled")

92 print("watchdog.service active")

93

94 raise SystemExit(not watchdog_config_ready)

95

96

97 if __name__ == "__main__":

98 main()