Authored by chunhua.zhang

Merge branch 'rsyslog_check' into 'master'

日志同步检查



See merge request !87
  1 +# Check and synchronize the rsyslog server log directory permissions
  2 +# ansible 192.168.103.64 -m file -a "path=/Data/logs state=directory owner=yoholog group=yoholog mode=0755 recurse=yes"
  3 +
  4 +- hosts: java-rsyslog
  5 + remote_user: root
  6 + tasks:
  7 +
  8 + - name: Check the log server IP consistency for activity
  9 + rsyslog_check:
  10 + module_name: activity
  11 + service_ip: " {{ item }} "
  12 + system_name: yohobuy
  13 + with_items:
  14 + - "{{ groups['java-activity'] }}"
  15 + ignore_errors: yes
  16 +
  17 +# - name: Check the log server IP consistency for yoho-bigdata
  18 +# rsyslog_check:
  19 +# module_name: yoho-bigdata
  20 +# service_ip: " {{ item }} "
  21 +# system_name: yohobuy
  22 +# with_items:
  23 +# - "{{ groups['java-bigdata'] }}"
  24 +# ignore_errors: yes
  25 +
  26 + - name: Check the log server IP consistency for brower
  27 + rsyslog_check:
  28 + module_name: brower
  29 + service_ip: " {{ item }} "
  30 + system_name: yohobuy
  31 + with_items:
  32 + - "{{ groups['java-brower'] }}"
  33 + ignore_errors: yes
  34 +
  35 + - name: Check the log server IP consistency for crm
  36 + rsyslog_check:
  37 + module_name: crm
  38 + service_ip: " {{ item }} "
  39 + system_name: yohobuy
  40 + with_items:
  41 + - "{{ groups['java-crm'] }}"
  42 + ignore_errors: yes
  43 +
  44 + - name: Check the log server IP consistency for erp-gateway
  45 + rsyslog_check:
  46 + module_name: erp-gateway
  47 + service_ip: " {{ item }} "
  48 + system_name: yohobuy
  49 + with_items:
  50 + - "{{ groups['java-erpgateway'] }}"
  51 + ignore_errors: yes
  52 +
  53 +# - name: Check the log server IP consistency for extendstore
  54 +# rsyslog_check:
  55 +# module_name: extendstore
  56 +# service_ip: " {{ item }} "
  57 +# system_name: yohobuy
  58 +# with_items:
  59 +# - "{{ groups['java-extendstore'] }}"
  60 +# ignore_errors: yes
  61 +
  62 + - name: Check the log server IP consistency for gateway
  63 + rsyslog_check:
  64 + module_name: gateway
  65 + service_ip: " {{ item }} "
  66 + system_name: yohobuy
  67 + with_items:
  68 + - "{{ groups['java-gateway'] }}"
  69 + ignore_errors: yes
  70 +
  71 + - name: Check the log server IP consistency for message
  72 + rsyslog_check:
  73 + module_name: message
  74 + service_ip: " {{ item }} "
  75 + system_name: yohobuy
  76 + with_items:
  77 + - "{{ groups['java-message'] }}"
  78 + ignore_errors: yes
  79 +
  80 + - name: Check the log server IP consistency for yoho-message-controller
  81 + rsyslog_check:
  82 + module_name: message-center/yoho-message-controller
  83 + service_ip: " {{ item }} "
  84 + system_name: yohobuy
  85 + with_items:
  86 + - "{{ groups['java-messagecontroller'] }}"
  87 + ignore_errors: yes
  88 +
  89 + - name: Check the log server IP consistency for nginx
  90 + rsyslog_check:
  91 + module_name: nginx-java
  92 + service_ip: " {{ item }} "
  93 + system_name: yohobuy
  94 + with_items:
  95 + - "{{ groups['java-nginx'] }}"
  96 + ignore_errors: yes
  97 +
  98 + - name: Check the log server IP consistency for order
  99 + rsyslog_check:
  100 + module_name: order
  101 + service_ip: " {{ item }} "
  102 + system_name: yohobuy
  103 + with_items:
  104 + - "{{ groups['java-order'] }}"
  105 + ignore_errors: yes
  106 +
  107 + - name: Check the log server IP consistency for platform-cms
  108 + rsyslog_check:
  109 + module_name: platform-cms
  110 + service_ip: " {{ item }} "
  111 + system_name: yohobuy
  112 + with_items:
  113 + - "{{ groups['java-platform-cms'] }}"
  114 + ignore_errors: yes
  115 +
  116 + - name: Check the log server IP consistency for portal-gateway
  117 + rsyslog_check:
  118 + module_name: portal-gateway
  119 + service_ip: " {{ item }} "
  120 + system_name: yohobuy
  121 + with_items:
  122 + - "{{ groups['java-portal-gateway'] }}"
  123 + ignore_errors: yes
  124 +
  125 + - name: Check the log server IP consistency for product
  126 + rsyslog_check:
  127 + module_name: product
  128 + service_ip: " {{ item }} "
  129 + system_name: yohobuy
  130 + with_items:
  131 + - "{{ groups['java-product'] }}"
  132 + ignore_errors: yes
  133 +
  134 + - name: Check the log server IP consistency for promotion
  135 + rsyslog_check:
  136 + module_name: promotion
  137 + service_ip: " {{ item }} "
  138 + system_name: yohobuy
  139 + with_items:
  140 + - "{{ groups['java-promotion'] }}"
  141 + ignore_errors: yes
  142 +
  143 + - name: Check the log server IP consistency for resources
  144 + rsyslog_check:
  145 + module_name: resources
  146 + service_ip: " {{ item }} "
  147 + system_name: yohobuy
  148 + with_items:
  149 + - "{{ groups['java-resources'] }}"
  150 + ignore_errors: yes
  151 +
  152 + - name: Check the log server IP consistency for reviewed
  153 + rsyslog_check:
  154 + module_name: reviewed
  155 + service_ip: " {{ item }} "
  156 + system_name: yohobuy
  157 + with_items:
  158 + - "{{ groups['java-review'] }}"
  159 + ignore_errors: yes
  160 +
  161 +# - name: Check the log server IP consistency for risk
  162 +# rsyslog_check:
  163 +# module_name: risk
  164 +# service_ip: " {{ item }} "
  165 +# system_name: yohobuy
  166 +# with_items:
  167 +# - "{{ groups['java-risk'] }}"
  168 +# ignore_errors: yes
  169 +
  170 + - name: Check the log server IP consistency for shops
  171 + rsyslog_check:
  172 + module_name: shops
  173 + service_ip: " {{ item }} "
  174 + system_name: yohobuy
  175 + with_items:
  176 + - "{{ groups['java-shops'] }}"
  177 + ignore_errors: yes
  178 +
  179 + - name: Check the log server IP consistency for sns
  180 + rsyslog_check:
  181 + module_name: sns
  182 + service_ip: " {{ item }} "
  183 + system_name: yohobuy
  184 + with_items:
  185 + - "{{ groups['java-sns'] }}"
  186 + ignore_errors: yes
  187 +
  188 +# - name: Check the log server IP consistency for social
  189 +# rsyslog_check:
  190 +# module_name: social
  191 +# service_ip: " {{ item }} "
  192 +# system_name: yohobuy
  193 +# with_items:
  194 +# - "{{ groups['java-social'] }}"
  195 +# ignore_errors: yes
  196 +
  197 + - name: Check the log server IP consistency for ufo-gateway
  198 + rsyslog_check:
  199 + module_name: ufo-gateway
  200 + service_ip: " {{ item }} "
  201 + system_name: yohobuy
  202 + with_items:
  203 + - "{{ groups['java-ufo-fore'] }}"
  204 + ignore_errors: yes
  205 +
  206 + - name: Check the log server IP consistency for ufo-platform
  207 + rsyslog_check:
  208 + module_name: ufo-platform
  209 + service_ip: " {{ item }} "
  210 + system_name: yohobuy
  211 + with_items:
  212 + - "{{ groups['java-ufo-platform'] }}"
  213 + ignore_errors: yes
  214 +
  215 + - name: Check the log server IP consistency for uic
  216 + rsyslog_check:
  217 + module_name: uic
  218 + service_ip: " {{ item }} "
  219 + system_name: yohobuy
  220 + with_items:
  221 + - "{{ groups['java-uic'] }}"
  222 + ignore_errors: yes
  223 +
  224 + - name: Check the log server IP consistency for union
  225 + rsyslog_check:
  226 + module_name: union
  227 + service_ip: " {{ item }} "
  228 + system_name: yohobuy
  229 + with_items:
  230 + - "{{ groups['java-union'] }}"
  231 + ignore_errors: yes
  232 +
  233 + - name: Check the log server IP consistency for users
  234 + rsyslog_check:
  235 + module_name: users
  236 + service_ip: " {{ item }} "
  237 + system_name: yohobuy
  238 + with_items:
  239 + - "{{ groups['java-users'] }}"
  240 + ignore_errors: yes
  241 +
  242 + - name: Check the log server IP consistency for wechat
  243 + rsyslog_check:
  244 + module_name: wechat
  245 + service_ip: " {{ item }} "
  246 + system_name: yohobuy
  247 + with_items:
  248 + - "{{ groups['java-wechat'] }}"
  249 + ignore_errors: yes
  1 +#!/usr/bin/python
  2 +# -*- coding: UTF-8 -*-
  3 +# ansible module for the log server IP consistency check
  4 +ANSIBLE_METADATA = {
  5 + 'metadata_version': '1.0',
  6 + 'status': ['preview'],
  7 + 'supported_by': 'yoho'
  8 +}
  9 +
  10 +DOCUMENTATION = '''
  11 +---
  12 +module: rsyslog_check
  13 +short_description: 检查日志服务器上的debug日志与所有被日志同步的JAVA服务器debug日志是否一致
  14 +'''
  15 +
  16 +EXAMPLES = '''
  17 +
  18 +# activity
  19 +- hosts: java-rsyslog
  20 + remote_user: root
  21 + tasks:
  22 + - name: Check the log server IP consistency for activity
  23 + rsyslog_check:
  24 + module_name: activity
  25 + service_ip: " {{ item }} "
  26 + system_name: yohobuy
  27 + with_items:
  28 + - "{{ groups['java-activity'] }}"
  29 +
  30 +'''
  31 +
  32 +from ansible.module_utils.basic import *
  33 +import json
  34 +import os
  35 +
  36 +
  37 +'''
  38 +检查日志服务器上的debug日志与所有被日志同步的JAVA服务器debug日志是否一致
  39 +'''
  40 +def check_rsyslog_status(module_name,system_name,service_ip):
  41 + res = {}
  42 + time_res = time.strftime("%Y%m%d")
  43 + if module_name == "nginx-java":
  44 + file_path1 = "/Data/logs/" + system_name + "/" + module_name + "/" + service_ip.strip() + "/" + "api.yoho.cn_access-" + time_res +".log"
  45 + else:
  46 + file_path1 = "/Data/logs/" + system_name + "/" + module_name + "/" + service_ip.strip() + "/" + "debug-log-" + time_res +".log"
  47 + file_path2 = "/Data/logs/" + system_name + "/" + module_name + "/" + service_ip.strip() + "/" + "debug-" + time_res +".log"
  48 + res1 = os.path.exists(file_path1)
  49 + res2 = os.path.exists(file_path2)
  50 + res_total = res1 or res2
  51 + try:
  52 + if res_total :
  53 + res["state"] = True
  54 + else :
  55 + res["state"] = False
  56 + res["err"] = "ip为%s的服务器%s模块日志未同步" % (service_ip, module_name)
  57 + except Exception as e:
  58 + print e
  59 + return res
  60 +def run_module():
  61 + # 定义参数
  62 + module_args = dict(
  63 + module_name=dict(type='str', required=True),
  64 + service_ip=dict(type='str', required=True),
  65 + system_name=dict(type='str', required=True),
  66 + )
  67 + result = dict(
  68 + changed=False,
  69 + original_message='',
  70 + message=''
  71 + )
  72 +
  73 + module = AnsibleModule(
  74 + argument_spec=module_args,
  75 + supports_check_mode=True
  76 + )
  77 +
  78 + if module.check_mode:
  79 + return result
  80 +
  81 + service_ip = module.params['service_ip']
  82 + module_name = module.params['module_name']
  83 + system_name = module.params['system_name']
  84 +
  85 + result['check_name'] = module.params['module_name']
  86 + result['service_ip'] = module.params['service_ip']
  87 + result['changed'] = False
  88 +
  89 + # check result
  90 + ret = check_rsyslog_status(module_name,system_name,service_ip)
  91 + if ret['state'] == False:
  92 + module.fail_json(msg='Check Failed: Err: %s' % (ret['err']), **result)
  93 + result['response'] = ret
  94 + module.exit_json(**result)
  95 +
  96 +
  97 +def main():
  98 + run_module()
  99 +
  100 +
  101 +if __name__ == '__main__':
  102 + main()