-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path60_lvs_monitor.py
executable file
·105 lines (95 loc) · 3.75 KB
/
60_lvs_monitor.py
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
103
104
105
#!/bin/env python
# -*- coding:utf-8 -*-
import json
import time
import os
import re
import commands
import urllib2
##start time
Tstart = time.time() - 60
TIME_START_DAY = time.localtime(Tstart).tm_mday
TIME_START_HOUR = "%s:%s" % (str(time.localtime(Tstart).tm_hour).zfill(2), str(time.localtime(Tstart).tm_min).zfill(2))
TIME_START_MON = time.strftime("%b", time.localtime(time.time()))
TIME_START = "%s %s %s" % (TIME_START_MON, TIME_START_DAY, TIME_START_HOUR)
##end time
Tend = time.time()
TIME_END_DAY = time.localtime(Tend).tm_mday
TIME_END_HOUR = "%s:%s:%s" % (str(time.localtime(Tend).tm_hour).zfill(2), str(time.localtime(Tend).tm_min).zfill(2),
str(time.localtime(Tend).tm_sec).zfill(2))
TIME_END_MON = time.strftime("%b", time.localtime(time.time()))
TIME_END = "%s %s %s" % (TIME_END_MON, TIME_END_DAY, TIME_END_HOUR)
# 接受日志监控URL 可以不传
content_post_url = "http://127.0.0.1:8080/api/v1/context/"
LOG_DIR = '/var/log/messages'
step = 60
IP = None
ENDPOINT = None
RESULT_PATTERN_REALSERVER = re.compile(
r'.*\W(\d+:\d+:\d+).*healthcheckers:\W+(.*ling) service (\[\d+.*\]:\d+)\s.* VS\s+\[(.*)\]:\d+')
RESULT_PATTERN_VIP = re.compile(r'.*\W(\d+:\d+:\d+).*zl.py (.*) (\d\d.*);] for VS \[(.*)\]')
def main():
if not os.path.exists("/proc/net/ip_vs_stats"):
exit()
with open('/usr/local/open-falcon/agent/cfg.json') as cfgfile:
config = json.load(cfgfile)
ENDPOINT = config['hostname']
IP = config['ip']
realserver_value = 0
realserver_msg = []
vip_value = 0
vip_msg = []
cmd = 'awk \'/%s/,/%s/\' %s | grep Keepalived_healthcheckers' % (TIME_START, TIME_END, LOG_DIR)
(status, output) = commands.getstatusoutput(cmd)
for line in output.splitlines():
if "ling service" in line:
match = RESULT_PATTERN_REALSERVER.search(line)
dt = match and match.group(1)
status = match and match.group(2).replace("Disabling", "failed").replace("Enabling", "succes")
realserver = (match and match.group(3)).replace("[", "").replace("]", "")
url = match and match.group(4)
realserver_msg.append("%s %s-%s connection-----%s" % (dt, url, realserver, status))
if status == "failed":
realserver_value += 1
elif "Executing" in line:
match = RESULT_PATTERN_VIP.search(line)
url = match and match.group(4)
vip = match and match.group(3)
dt = match and match.group(1)
status = match and match.group(2).replace("-d", "failed").replace("-c", "succes")
vip_msg.append("%s %s-%s-----%s" % (dt, url, vip, status))
if status == "failed":
vip_value += 1
push_data = [
{
'metric': 'lvs.log.monitor',
'endpoint': ENDPOINT,
'timestamp': int(time.time()),
'step': step,
'value': realserver_value,
'counterType': 'GAUGE',
'tags': "type=realserver"
},
{
'metric': 'lvs.log.monitor',
'endpoint': ENDPOINT,
'timestamp': int(time.time()),
'step': step,
'value': vip_value,
'counterType': 'GAUGE',
'tags': "type=vip"
},
]
print json.dumps(push_data)
# 以下为推送日志信息
# realserver_msg_content = "\n".join(realserver_msg) or "all ok"
# vip_msg_content = "\n".join(vip_msg) or "all ok"
# push_data[0]["context"] = realserver_msg_content
# push_data[1]["context"] = vip_msg_content
# try:
# req = urllib2.Request(content_post_url)
# resp = urllib2.urlopen(req, json.dumps(push_data))
# except Exception, e:
# pass
if __name__ == '__main__':
main()