paulbsd-salt/states/zabbix/scripts/systemd_discovery.py

27 lines
752 B
Python
Raw Normal View History

2020-11-04 23:05:21 +01:00
#!/usr/bin/python3
2021-01-24 19:01:26 +01:00
# vim: ft=python
2020-11-04 23:05:21 +01:00
import subprocess
import json
2021-01-24 19:01:26 +01:00
import re
2020-11-04 23:05:21 +01:00
2020-12-16 19:44:10 +01:00
def main():
ret = {"data": []}
2021-01-24 19:01:26 +01:00
output_list = subprocess.run("""
2020-12-16 19:44:10 +01:00
systemctl list-unit-files | grep -E '\.service\s+(generated|enabled)' | awk -F'.service ' '{print $1}'
2021-01-24 19:01:26 +01:00
""",shell=True,capture_output=True).stdout
2020-11-04 23:05:21 +01:00
2021-01-24 19:01:26 +01:00
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")})
2020-11-04 23:05:21 +01:00
2020-12-16 19:44:10 +01:00
return json.dumps(ret)
if __name__ == "__main__":
res = main()
print(res)