awx/awx/plugins/library/scan_services.py

170 lines
6.8 KiB
Python

#!/usr/bin/env python
import re
from ansible.module_utils.basic import * # noqa
DOCUMENTATION = '''
---
module: scan_services
short_description: Return service state information as fact data
description:
- Return service state information as fact data for various service management utilities
version_added: "1.9"
options:
requirements: [ ]
author: Matthew Jones
'''
EXAMPLES = '''
- monit: scan_services
# Example fact output:
# host | success >> {
# "ansible_facts": {
# "services": [
# {
# "name": "acpid",
# "source": "sysv",
# "state": "running"
# },
# {
# "name": "apparmor",
# "source": "sysv",
# "state": "stopped"
# },
# {
# "name": "atd",
# "source": "sysv",
# "state": "running"
# },
# {
# "name": "cron",
# "source": "sysv",
# "state": "running"
# }, .... ] } }
'''
class BaseService(object):
def __init__(self, module):
self.module = module
class ServiceScanService(BaseService):
def gather_services(self):
services = []
service_path = self.module.get_bin_path("service")
if service_path is None:
return None
initctl_path = self.module.get_bin_path("initctl")
chkconfig_path = self.module.get_bin_path("chkconfig")
# Upstart and sysvinit
if initctl_path is not None and chkconfig_path is None:
rc, stdout, stderr = self.module.run_command("%s --status-all 2>&1 | grep -E \"\\[ (\\+|\\-) \\]\"" % service_path, use_unsafe_shell=True)
for line in stdout.split("\n"):
line_data = line.split()
if len(line_data) < 4:
continue # Skipping because we expected more data
service_name = " ".join(line_data[3:])
if line_data[1] == "+":
service_state = "running"
else:
service_state = "stopped"
services.append({"name": service_name, "state": service_state, "source": "sysv"})
rc, stdout, stderr = self.module.run_command("%s list" % initctl_path)
real_stdout = stdout.replace("\r","")
for line in real_stdout.split("\n"):
line_data = line.split()
if len(line_data) < 2:
continue
service_name = line_data[0]
if line_data[1].find("/") == -1: # we expect this to look like: start/running
continue
service_goal = line_data[1].split("/")[0]
service_state = line_data[1].split("/")[1].replace(",","")
if len(line_data) > 3: # If there's a pid associated with the service it'll be on the end of this string "process 418"
if line_data[2] == 'process':
pid = line_data[3]
else:
pid = None
else:
pid = None # NOQA
payload = {"name": service_name, "state": service_state, "goal": service_goal, "source": "upstart"}
services.append(payload)
# RH sysvinit
elif chkconfig_path is not None:
#print '%s --status-all | grep -E "is (running|stopped)"' % service_path
p = re.compile('(?P<service>.*?)\s+[0-9]:(?P<rl0>on|off)\s+[0-9]:(?P<rl1>on|off)\s+[0-9]:(?P<rl2>on|off)\s+[0-9]:(?P<rl3>on|off)\s+[0-9]:(?P<rl4>on|off)\s+[0-9]:(?P<rl5>on|off)\s+[0-9]:(?P<rl6>on|off)')
rc, stdout, stderr = self.module.run_command('%s' % chkconfig_path, use_unsafe_shell=True)
# extra flags needed for SLES11
if not any(p.match(line) for line in stdout.split('\n')):
# If p pattern is not found but p_simple is, we have single-column ouptut
p_simple = re.compile('(?P<service>.*?)\s+(?P<rl0>on|off)')
if any(p_simple.match(line) for line in stdout.split('\n')):
# Try extra flags " -l --allservices" to output all columns
rc, stdout, stderr = self.module.run_command('%s -l --allservices' % chkconfig_path, use_unsafe_shell=True)
for line in stdout.split('\n'):
m = p.match(line)
if m:
service_name = m.group('service')
service_state = 'stopped'
if m.group('rl3') == 'on':
rc, stdout, stderr = self.module.run_command('%s %s status' % (service_path, service_name), use_unsafe_shell=True)
service_state = rc
if rc in (0,):
service_state = 'running'
#elif rc in (1,3):
else:
service_state = 'stopped'
service_data = {"name": service_name, "state": service_state, "source": "sysv"}
services.append(service_data)
# rc, stdout, stderr = self.module.run_command("%s --list" % chkconfig_path)
# Do something with chkconfig status
return services
class SystemctlScanService(BaseService):
def systemd_enabled(self):
# Check if init is the systemd command, using comm as cmdline could be symlink
try:
f = open('/proc/1/comm', 'r')
except IOError:
# If comm doesn't exist, old kernel, no systemd
return False
for line in f:
if 'systemd' in line:
return True
return False
def gather_services(self):
services = []
if not self.systemd_enabled():
return None
systemctl_path = self.module.get_bin_path("systemctl", opt_dirs=["/usr/bin", "/usr/local/bin"])
if systemctl_path is None:
return None
rc, stdout, stderr = self.module.run_command("%s list-unit-files --type=service | tail -n +2 | head -n -2" % systemctl_path, use_unsafe_shell=True)
for line in stdout.split("\n"):
line_data = line.split()
if len(line_data) != 2:
continue
services.append({"name": line_data[0],
"state": "running" if line_data[1] == "enabled" else "stopped",
"source": "systemd"})
return services
def main():
module = AnsibleModule(argument_spec = dict())
service_modules = (ServiceScanService, SystemctlScanService)
all_services = []
for svc_module in service_modules:
svcmod = svc_module(module)
svc = svcmod.gather_services()
if svc is not None:
all_services += svc
results = dict(ansible_facts=dict(services=all_services))
module.exit_json(**results)
main()