@@ -16,11 +16,13 @@ import sys # pylint: disable=C0413
1616
1717import lib .base # pylint: disable=C0413
1818import lib .shell # pylint: disable=C0413
19+ import lib .txt # pylint: disable=C0413
20+ import lib .version # pylint: disable=C0413
1921from 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
2527DESCRIPTION = """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.\n Stdout: {}\n Stderr: {}' .format (command , stdout , stderr ),
0 commit comments