-
Notifications
You must be signed in to change notification settings - Fork 0
/
alerting_functions.py
138 lines (125 loc) · 4.37 KB
/
alerting_functions.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
import requests
import sys
import logging
import os
import json
from requests_aws4auth import AWS4Auth
import urllib3
from aws_auth import get_aws_auth
urllib3.disable_warnings()
# SETUP LOGGING OPTIONS
logging.basicConfig(stream=sys.stdout,
format="%(asctime)s;%(levelname)s;%(message)s")
log = logging.getLogger("opensearch-alerting")
log.setLevel(logging.DEBUG)
# Authentication for AWS OpenSearch
awsauth = get_aws_auth()
def create(event, _data):
try:
# get kibana objects from API
kibana_url = event.get('kibana_url')
# Kibana API save objects info
url = "%s/_opendistro/_alerting/monitors" % (
kibana_url.rstrip("/"),)
log.info("connected to: " + kibana_url)
# log.info("creating rule: " + alert['name'])
r = requests.post(
url,
auth=(awsauth),
stream=True,
headers={'kbn-xsrf': 'kibana', 'securitytenant': 'global',
'Content-Type': 'application/json'},
data=json.dumps(_data[0], indent=4),
)
log.info("done")
print(r)
except Exception as e:
log.warning(str("something goes wrong creating the rule: " + _data[0]['name']))
log.error(str(e))
def get_monitors(event):
try:
# get kibana objects from API
kibana_url = event.get('kibana_url')
# Kibana API save objects info
url = "%s/_opendistro/_alerting/monitors/_search" % (
kibana_url.rstrip("/"),)
log.info("getting monitors and destinations from "+kibana_url)
query_send = json.dumps({"size": 500, "query": {"match_all": {}}})
r = requests.get(
url,
auth=(awsauth),
headers={'Content-Type': 'application/json'},
data=query_send,
)
alerts = r.json()
alert_json = []
for alert in alerts['hits']['hits']:
name = alert['_source'].get('name')
id = alert['_id']
_type = alert['_source'].get('type')
obj = {
"name": name,
"id": id,
"type": _type
}
alert_json.append(obj)
log.info("done.. returning alert_json list")
return(alert_json)
except Exception as e:
log.warning(str("something goes wrong getting the monitors"))
log.error(str(e))
def get_destinations(event):
try:
# get kibana objects from API
kibana_url = event.get('kibana_url')
# Kibana API save objects info
url = "%s/_opendistro/_alerting/destinations" % (
kibana_url.rstrip("/"),)
log.info("downloading saved objects from "+kibana_url)
query_send = json.dumps({"size": 1000, "query": {"match_all": {}}})
r = requests.get(
url,
auth=(awsauth),
headers={'Content-Type': 'application/json',
'Content-Type': 'application/json'},
data=query_send,
)
destinations = r.json()
destination_json = []
for destination in destinations['destinations']:
name = destination.get('name')
id = destination.get('id')
_type = destination.get('type')
obj = {
"name": name,
"id": id,
"type": _type
}
destination_json.append(obj)
log.info("done.. returning alert_json list")
return(destination_json)
except Exception as e:
log.warning(str("something goes wrong getting the destination"))
log.error(str(e))
def update(event, _data, _id):
try:
# get kibana objects from API
kibana_url = event.get('kibana_url')
# Kibana API save objects info
url = "%s/_opendistro/_alerting/monitors/" % (
kibana_url.rstrip("/"),)
log.info("connected to: " + kibana_url)
log.info("updating rule: " + _id)
r = requests.put(
url+_id,
auth=(awsauth),
headers={'kbn-xsrf': 'kibana', 'securitytenant': 'global',
'Content-Type': 'application/json'},
data=json.dumps(_data[0], indent=4),
)
log.info("done")
except Exception as e:
log.warning(
str("something goes wrong updating the rule: " + data.get('name')))
log.error(str(e))