paulbsd-salt/states/zabbix/scripts/systemd_discovery.py
2022-05-14 11:24:24 +02:00

27 lines
752 B
Python

#!/usr/bin/python3
# vim: ft=python
import subprocess
import json
import re
def main():
ret = {"data": []}
output_list = subprocess.run("""
systemctl list-unit-files | grep -E '\.service\s+(generated|enabled)' | awk -F'.service ' '{print $1}'
""",shell=True,capture_output=True).stdout
for line in output_list.splitlines():
output_is_enabled = subprocess.run(f"""
systemctl is-enabled {line.decode("utf-8")}
""",shell=True,capture_output=True).stdout.decode('utf-8')
if re.match("^enabled", output_is_enabled) is not None:
ret["data"].append({"{#SERVICE}": line.decode("utf-8")})
return json.dumps(ret)
if __name__ == "__main__":
res = main()
print(res)