-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloki.py
67 lines (61 loc) · 1.92 KB
/
loki.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
#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
import requests
import datetime
import sys
headers = {
'Content-type': 'application/json;charset=utf-8'
}
def post_tg(messige):
token = ""
chat_id = ""
url = 'https://api.telegram.org/bot{}/sendMessage'.format(token)
data = {
"chat_id": chat_id,
"parse_mode": "Markdownv2",
"text": messige
}
req = requests.post(url=url, json=data, headers=headers).text
return req
def query_loki():
data="{compose_project=\"opt\"}|= \"ERROR\""
ago2m = (datetime.datetime.now()-datetime.timedelta(minutes=30)).timestamp()
req = requests.get("http://127.0.0.1:3100/loki/api/v1/query_range?query={}&start={}".format(data,ago2m), headers=headers).json()
for i in req["data"]["result"]:
host = i["stream"]["host"]
container_name = i["stream"]["container_name"]
for x in (i["values"]):
values = x[1]
msg = """
Host: ```{}```
App:```{}```
message: ```{}```
"""
if values.find("INFO") != -1:
continue
else:
print(post_tg(msg.format(host,container_name,values)))
query_loki()
def nginx_loki():
date_time = datetime.datetime.now()
data="topk(10, sum by (xff) (count_over_time({app=\"nginx\"} | json | __error__=\"\" [2m])))"
req = requests.get("http://127.0.0.1:3100/loki/api/v1/query?query={}".format(data), headers=headers).json()
for i in req["data"]["result"]:
ip=i["metric"]["xff"]
sum_mumbers=i["value"][-1]
msg = """
报警名称: ip 2分钟访问超过500次
告警ip: {}
访问次数: {}
告警时间: {}
"""
if int(sum_mumbers) >= 500:
m = post_tg(msg.format(ip,sum_mumbers,date_time))
print(m)
if __name__ == "__main__":
if sys.argv[1] == "java":
query_loki()
elif sys.argv[1] == "nginx":
nginx_loki()
else:
sys.exit(1)