-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkuberstack-monitoring-events.py
61 lines (45 loc) · 2.03 KB
/
kuberstack-monitoring-events.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
import os
import time
import datetime
from kubernetes import client, config
from alerts.slack import send_alert as slack_send_alert
from alerts.telegram import send_alert as telegram_send_alert
from settings import UPDATE_INTERVAL, ALERTS_LEVELS_EXCLUDE, NOTIFICATOR
# Kubernetes API init
if os.environ.get('DEVELOPMENT_MODE'):
config.load_kube_config()
else:
config.load_incluster_config()
kubeapi = client.CoreV1Api()
def get_sorted_events():
events = kubeapi.list_event_for_all_namespaces().items
return sorted(events, key=lambda k: k.metadata.creation_timestamp)
def get_timestamp(text):
return datetime.datetime.strptime(text, '%Y-%m-%dT%H:%M:%SZ')
def main():
last_event = get_sorted_events()[-1]
events_last_name = last_event.metadata.name
events_last_timestamp = get_timestamp(last_event.metadata.creation_timestamp)
try:
while not time.sleep(UPDATE_INTERVAL):
for event in get_sorted_events():
event_name = event.metadata.name
event_time = get_timestamp(event.metadata.creation_timestamp)
if event_time <= events_last_timestamp or event_name == events_last_name:
continue
if event.type in ALERTS_LEVELS_EXCLUDE:
continue
if NOTIFICATOR == "telegram":
telegram_send_alert(event.source.host, event.type, event.message,
event_time, event.metadata.self_link, event.involved_object)
elif NOTIFICATOR == "slack":
slack_send_alert(event.source.host, event.type, event.message,
event_time, event.metadata.self_link, event.involved_object)
else:
raise Exception('Unsupported notificator "{notificator}"'.format(notificator=NOTIFICATOR))
events_last_name = event_name
events_last_timestamp = event_time
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()