This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-monitor.py
executable file
·65 lines (56 loc) · 1.9 KB
/
log-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
#!/usr/bin/python3.6
import requests
import json
import jira
# This is a MS Teams webhook which when POSTed to sends a message to the channel where it is activated.
URL = ""
data = {
"@context": "https://schema.org/extensions",
"@type": "MessageCard",
"themeColor": "0072C6",
"title": "Alert Raised",
"text": "",
"potentialAction": []
}
import subprocess
import json
import sqlite3
# Custom tags for which the message's contents are checked. If it contains the tag, it attaches the `potentialAction`
CUSTOM_TAGS = {
"TAG:UPTIME": {
"@type":
"OpenUri",
"name":
"Check Uptime page",
"targets": [{
"os": "default",
"uri": "https://reduce.isis.cclrc.ac.uk/kibana/app/uptime"
}]
}
}
# Set up the SQLite3 db
con = sqlite3.connect("processed.db")
cur = con.cursor()
try:
cur.execute("""CREATE TABLE processed (timestamp text)""")
con.commit()
except sqlite3.OperationalError:
pass
# Get the last message with `tac` (print file backwards) and `grep -m1` (stop on first match)
output = subprocess.check_output(
"""tac /var/log/kibana/kibana.log | grep -m1 '"type":"log"'""", shell=True)
message = json.loads(output)
print("got message", message)
# Find previously cached messages with the same @timestamp value. Kibana may repeat the messages in the logs
# but they all have the same @timestamp
entries = list(
cur.execute(
f"SELECT COUNT(timestamp) FROM processed WHERE timestamp='{message['@timestamp']}'"
))[0][0]
# Kibana prepends "Server log" in alerts specifically raised to the server log connector
# If entires is not 0 then this message has been processed before, so it is skippped
if "Server log" in message["message"] and entries == 0:
# cache the message into the db
cur.execute(f"INSERT INTO processed VALUES ('{message['@timestamp']}')")
con.commit()
jira.send(message["message"])