rsyslog_check
2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/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()