From de0ba6d093cca677952d60b21f50c451c30e453d Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Wed, 1 Jan 2025 19:41:08 +0100 Subject: [PATCH] fix: units not displayed correctly on systemd >= 252 --- pynagsystemd.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pynagsystemd.py b/pynagsystemd.py index b0cd651..81572ee 100755 --- a/pynagsystemd.py +++ b/pynagsystemd.py @@ -22,18 +22,21 @@ class SystemdStatus(nagiosplugin.Resource): p = subprocess.Popen(['systemctl', '--failed', '--no-legend'], stderr=subprocess.PIPE, stdin=subprocess.PIPE, - stdout=subprocess.PIPE) - pres, err = p.communicate() + stdout=subprocess.PIPE, + universal_newlines=True) + + stdout, err = p.communicate() except OSError as e: raise nagiosplugin.CheckError(e) if err: raise nagiosplugin.CheckError(err) - if pres: + if stdout: + lines = list(filter(lambda x: "service" in x, stdout.split("\n"))) result = "" - for line in io.StringIO(pres.decode('utf-8')): - result = "%s %s" % (result, line.split(' ')[0]) + for line in lines: + result += "{}".format(line.split(' ')[1]) return [nagiosplugin.Metric('systemd', (False, result), context='systemd')]