|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright VyOS maintainers and contributors <[email protected]> |
| 4 | +# |
| 5 | +# This program is free software; you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License version 2 or later as |
| 7 | +# published by the Free Software Foundation. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | +from sys import exit |
| 20 | + |
| 21 | +from vyos.config import Config |
| 22 | +from vyos.template import render |
| 23 | +from vyos.utils.file import write_file |
| 24 | +from vyos.utils.process import cmd |
| 25 | +from vyos import ConfigError |
| 26 | +from vyos import airbag |
| 27 | + |
| 28 | +airbag.enable() |
| 29 | + |
| 30 | +watchdog_config_dir = r'/etc/systemd/system.conf.d' |
| 31 | +watchdog_config_file = r'/etc/systemd/system.conf.d/watchdog.conf' |
| 32 | +modules_load_file = r'/etc/modules-load.d/vyos-watchdog.conf' |
| 33 | + |
| 34 | + |
| 35 | +def get_config(config=None): |
| 36 | + if config: |
| 37 | + conf = config |
| 38 | + else: |
| 39 | + conf = Config() |
| 40 | + base = ['system', 'watchdog'] |
| 41 | + |
| 42 | + if not conf.exists(base): |
| 43 | + return None |
| 44 | + |
| 45 | + watchdog = conf.get_config_dict( |
| 46 | + base, key_mangling=('-', '_'), get_first_key=True, with_recursive_defaults=True |
| 47 | + ) |
| 48 | + |
| 49 | + return watchdog |
| 50 | + |
| 51 | + |
| 52 | +def verify(watchdog): |
| 53 | + if not watchdog: |
| 54 | + return None |
| 55 | + |
| 56 | + # Check if watchdog is enabled |
| 57 | + if 'enable' not in watchdog: |
| 58 | + # Allow setting only the module without enabling watchdog |
| 59 | + allowed_keys = {'module'} |
| 60 | + extra_keys = set(watchdog.keys()) - allowed_keys |
| 61 | + if extra_keys: |
| 62 | + raise ConfigError( |
| 63 | + 'Watchdog must be enabled to configure timeout values!\n' |
| 64 | + 'Use "set system watchdog enable" to enable the watchdog.' |
| 65 | + ) |
| 66 | + |
| 67 | + return None |
| 68 | + |
| 69 | + |
| 70 | +def generate(watchdog): |
| 71 | + # If watchdog node removed entirely, clean up everything |
| 72 | + if not watchdog: |
| 73 | + if os.path.exists(watchdog_config_file): |
| 74 | + os.unlink(watchdog_config_file) |
| 75 | + if os.path.exists(modules_load_file): |
| 76 | + os.unlink(modules_load_file) |
| 77 | + return None |
| 78 | + |
| 79 | + # Persist kernel module autoload on boot if specified (even if not enabled) |
| 80 | + module = watchdog.get('module') |
| 81 | + if module: |
| 82 | + try: |
| 83 | + write_file(modules_load_file, f"{module}\n") |
| 84 | + except Exception as e: |
| 85 | + print(f"Warning: Failed writing modules-load configuration: {e}") |
| 86 | + else: |
| 87 | + # If module option removed, drop persisted autoload file |
| 88 | + if os.path.exists(modules_load_file): |
| 89 | + os.unlink(modules_load_file) |
| 90 | + |
| 91 | + # If not enabled, ensure systemd watchdog config is absent and return |
| 92 | + if 'enable' not in watchdog: |
| 93 | + if os.path.exists(watchdog_config_file): |
| 94 | + os.unlink(watchdog_config_file) |
| 95 | + return None |
| 96 | + |
| 97 | + # Try to load kernel module if specified and /dev/watchdog0 is missing |
| 98 | + if not os.path.exists('/dev/watchdog0'): |
| 99 | + if module: |
| 100 | + # Try to load the module using vyos cmd wrapper for logging/airbag integration |
| 101 | + try: |
| 102 | + cmd(f'modprobe {module}') |
| 103 | + except Exception as e: |
| 104 | + print(f"Warning: Could not load watchdog module '{module}': {e}") |
| 105 | + # Re-check for device |
| 106 | + if not os.path.exists('/dev/watchdog0'): |
| 107 | + print( |
| 108 | + "Warning: /dev/watchdog0 not found. Systemd watchdog will not be enabled." |
| 109 | + ) |
| 110 | + if os.path.exists(watchdog_config_file): |
| 111 | + os.unlink(watchdog_config_file) |
| 112 | + return None |
| 113 | + |
| 114 | + # Ensure the directory exists |
| 115 | + os.makedirs(watchdog_config_dir, exist_ok=True) |
| 116 | + |
| 117 | + # Pass through configured time values directly (systemd accepts raw seconds or unit-suffixed values) |
| 118 | + |
| 119 | + render(watchdog_config_file, 'system/watchdog.conf.j2', watchdog) |
| 120 | + |
| 121 | + return None |
| 122 | + |
| 123 | + |
| 124 | +def apply(watchdog): |
| 125 | + # Reload systemd daemon to apply watchdog configuration |
| 126 | + # The watchdog settings take effect after systemd is reloaded |
| 127 | + cmd('systemctl daemon-reload') |
| 128 | + |
| 129 | + return None |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == '__main__': |
| 133 | + try: |
| 134 | + c = get_config() |
| 135 | + verify(c) |
| 136 | + generate(c) |
| 137 | + apply(c) |
| 138 | + except ConfigError as e: |
| 139 | + print(e) |
| 140 | + exit(1) |
0 commit comments