rsyslog_check 2.83 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# ansible module for the log server IP consistency check
ANSIBLE_METADATA = {
    'metadata_version': '1.0',
    'status': ['preview'],
    'supported_by': 'yoho'
}

DOCUMENTATION = '''
---
module: rsyslog_check
short_description:  检查日志服务器上的debug日志与所有被日志同步的JAVA服务器debug日志是否一致
'''

EXAMPLES = '''

# activity 
- hosts: java-rsyslog
  remote_user: root
  tasks:
    - name: Check the log server IP consistency for activity
      rsyslog_check:
        module_name: activity
        service_ip: " {{ item }} "
        system_name: yohobuy
      with_items:
        - "{{ groups['java-activity'] }}"

'''

from ansible.module_utils.basic import *
import json
import os


'''
检查日志服务器上的debug日志与所有被日志同步的JAVA服务器debug日志是否一致
'''
def check_rsyslog_status(module_name,system_name,service_ip):
    res = {}
    time_res = time.strftime("%Y%m%d")
    if module_name == "nginx-java":
        file_path1 = "/Data/logs/" + system_name + "/" + module_name + "/" + service_ip.strip() + "/" + "api.yoho.cn_access-" + time_res +".log"
    else:
        file_path1 = "/Data/logs/" + system_name + "/" + module_name + "/" + service_ip.strip() + "/" + "debug-log-" + time_res +".log"
    file_path2 = "/Data/logs/" + system_name + "/" + module_name + "/" + service_ip.strip() + "/" + "debug-" + time_res +".log"
    res1 = os.path.exists(file_path1)
    res2 = os.path.exists(file_path2)
    res_total = res1 or res2
    try:
        if res_total :
            res["state"] = True
        else :
            res["state"] = False
            res["err"] = "ip为%s的服务器%s模块日志未同步" % (service_ip, module_name)
    except Exception as e:
        print e
    return res
def run_module():
    #  定义参数
    module_args = dict(
        module_name=dict(type='str', required=True),
        service_ip=dict(type='str', required=True),
        system_name=dict(type='str', required=True),
    )
    result = dict(
        changed=False,
        original_message='',
        message=''
    )

    module = AnsibleModule(
        argument_spec=module_args,
        supports_check_mode=True
    )

    if module.check_mode:
        return result

    service_ip = module.params['service_ip']
    module_name = module.params['module_name']
    system_name = module.params['system_name']

    result['check_name'] = module.params['module_name']
    result['service_ip'] = module.params['service_ip']
    result['changed'] = False

    # check result
    ret = check_rsyslog_status(module_name,system_name,service_ip)
    if ret['state'] == False:
        module.fail_json(msg='Check Failed: Err: %s' % (ret['err']), **result)
    result['response'] = ret
    module.exit_json(**result)


def main():
    run_module()


if __name__ == '__main__':
    main()