Authored by chunhua.zhang

Merge branch 'rsyslog_check' into 'master'

日志同步检查



See merge request !87
# Check and synchronize the rsyslog server log directory permissions
# ansible 192.168.103.64 -m file -a "path=/Data/logs state=directory owner=yoholog group=yoholog mode=0755 recurse=yes"
- 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'] }}"
ignore_errors: yes
# - name: Check the log server IP consistency for yoho-bigdata
# rsyslog_check:
# module_name: yoho-bigdata
# service_ip: " {{ item }} "
# system_name: yohobuy
# with_items:
# - "{{ groups['java-bigdata'] }}"
# ignore_errors: yes
- name: Check the log server IP consistency for brower
rsyslog_check:
module_name: brower
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-brower'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for crm
rsyslog_check:
module_name: crm
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-crm'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for erp-gateway
rsyslog_check:
module_name: erp-gateway
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-erpgateway'] }}"
ignore_errors: yes
# - name: Check the log server IP consistency for extendstore
# rsyslog_check:
# module_name: extendstore
# service_ip: " {{ item }} "
# system_name: yohobuy
# with_items:
# - "{{ groups['java-extendstore'] }}"
# ignore_errors: yes
- name: Check the log server IP consistency for gateway
rsyslog_check:
module_name: gateway
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-gateway'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for message
rsyslog_check:
module_name: message
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-message'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for yoho-message-controller
rsyslog_check:
module_name: message-center/yoho-message-controller
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-messagecontroller'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for nginx
rsyslog_check:
module_name: nginx-java
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-nginx'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for order
rsyslog_check:
module_name: order
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-order'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for platform-cms
rsyslog_check:
module_name: platform-cms
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-platform-cms'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for portal-gateway
rsyslog_check:
module_name: portal-gateway
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-portal-gateway'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for product
rsyslog_check:
module_name: product
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-product'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for promotion
rsyslog_check:
module_name: promotion
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-promotion'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for resources
rsyslog_check:
module_name: resources
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-resources'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for reviewed
rsyslog_check:
module_name: reviewed
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-review'] }}"
ignore_errors: yes
# - name: Check the log server IP consistency for risk
# rsyslog_check:
# module_name: risk
# service_ip: " {{ item }} "
# system_name: yohobuy
# with_items:
# - "{{ groups['java-risk'] }}"
# ignore_errors: yes
- name: Check the log server IP consistency for shops
rsyslog_check:
module_name: shops
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-shops'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for sns
rsyslog_check:
module_name: sns
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-sns'] }}"
ignore_errors: yes
# - name: Check the log server IP consistency for social
# rsyslog_check:
# module_name: social
# service_ip: " {{ item }} "
# system_name: yohobuy
# with_items:
# - "{{ groups['java-social'] }}"
# ignore_errors: yes
- name: Check the log server IP consistency for ufo-gateway
rsyslog_check:
module_name: ufo-gateway
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-ufo-fore'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for ufo-platform
rsyslog_check:
module_name: ufo-platform
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-ufo-platform'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for uic
rsyslog_check:
module_name: uic
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-uic'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for union
rsyslog_check:
module_name: union
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-union'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for users
rsyslog_check:
module_name: users
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-users'] }}"
ignore_errors: yes
- name: Check the log server IP consistency for wechat
rsyslog_check:
module_name: wechat
service_ip: " {{ item }} "
system_name: yohobuy
with_items:
- "{{ groups['java-wechat'] }}"
ignore_errors: yes
... ...
#!/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()
... ...