Skip to content

Commit 51a22a0

Browse files
authored
feat(systemd-unit): implement support for systemctl --machine and --user (#871)
1 parent b25da7d commit 51a22a0

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ and this project does NOT adhere to [Semantic Versioning](https://semver.org/spe
1111

1212
## [Unreleased]
1313

14+
### Added ("feat")
15+
16+
Monitoring Plugins:
17+
18+
* systemd-unit: implement support for systemctl --machine and --user
19+
20+
1421
### Fixed ("fix")
1522

1623
Monitoring Plugins:

check-plugins/systemd-unit/systemd-unit

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ import sys # pylint: disable=C0413
1616

1717
import lib.base # pylint: disable=C0413
1818
import lib.shell # pylint: disable=C0413
19+
import lib.txt # pylint: disable=C0413
20+
import lib.version # pylint: disable=C0413
1921
from lib.globals import (STATE_CRIT, STATE_OK, # pylint: disable=C0413
2022
STATE_UNKNOWN, STATE_WARN)
2123

2224
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
23-
__version__ = '2024120601'
25+
__version__ = '2025041501'
2426

2527
DESCRIPTION = """Checks the state of a service, socket, device, mount, automount, swap, target,
2628
path, timer, slice or scope - using systemd/systemctl. For example, to check if the
@@ -84,6 +86,16 @@ def parse_args():
8486
],
8587
)
8688

89+
parser.add_argument(
90+
'--machine',
91+
help='Connect to a different system scope bus via machine transport. May be '
92+
'optionally prefixed by "username@" to specify a user to connect as. '
93+
'Use the special string ".host" to connect to the local system. (This '
94+
'allows to connect to the user\'s service manager using "--machine user@.host") '
95+
'Default: %(default)s',
96+
dest='MACHINE',
97+
)
98+
8799
parser.add_argument(
88100
'--severity',
89101
help='If something was found, the check returns WARN unless set here. '
@@ -182,6 +194,15 @@ def parse_args():
182194
]
183195
)
184196

197+
parser.add_argument(
198+
'--user',
199+
help='Use the calling user\'s service manager, instead of the system\'s. '
200+
'Default: %(default)s',
201+
dest='USER',
202+
action='store_true',
203+
default=False,
204+
)
205+
185206
return parser.parse_args()
186207

187208

@@ -195,8 +216,27 @@ def main():
195216
except SystemExit:
196217
sys.exit(STATE_UNKNOWN)
197218

198-
command = 'systemctl show -p LoadState,ActiveState,SubState,UnitFileState '
199-
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(command + lib.txt.to_text(args.UNIT)))
219+
options = ''
220+
221+
if args.MACHINE:
222+
stdout, _, _ = lib.base.coe(lib.shell.shell_exec('systemctl --version'))
223+
224+
systemd_version = (stdout.split()[1:2] or ['unknown'])[0] # Systemd reports the version number in the second word/field.
225+
226+
if lib.version.version(systemd_version) < lib.version.version('209'):
227+
lib.base.oao(
228+
f'Currently installed systemctl version "{systemd_version}" does not support the `--machine` argument',
229+
STATE_UNKNOWN,
230+
)
231+
232+
options += f'--machine {lib.txt.to_text(args.MACHINE)} '
233+
234+
if args.USER:
235+
options += '--user '
236+
237+
command = f'systemctl {options}show -p LoadState,ActiveState,SubState,UnitFileState {lib.txt.to_text(args.UNIT)}'
238+
239+
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(command))
200240
if (stderr or retc != 0):
201241
lib.base.oao(
202242
'Bash command `{}` failed.\nStdout: {}\nStderr: {}'.format(command, stdout, stderr),

0 commit comments

Comments
 (0)