-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
97 lines (76 loc) · 2.62 KB
/
main.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
import logging
import os
import socket
import urllib.request
import requests
import uvicorn
from fastapi import FastAPI, Request, Response
logger = logging.getLogger('gunicorn.error')
app = FastAPI()
host = os.environ['host']
username = os.environ['username']
password = os.environ['password']
platformPartnerId = os.environ['platformPartnerId']
def format_message(alert):
status = alert['status'].upper()
labels = alert['labels']
name = labels['alertname']
cluster_id = labels['tenant_cluster_id']
annotations = alert['annotations']
description = ''
if 'description' in annotations:
description = annotations['description']
elif 'summery' in annotations:
description = annotations['summery']
elif 'consequence' in annotations:
description = annotations['consequence']
elif 'action' in annotations:
description = annotations['action']
else:
description = 'No description provided'
return '[{}] {} in {}\n{}'.format(
status,
name,
cluster_id,
description)
def send_sms(message, recipients):
recipients = recipients.split(',')
status_code = 200
for recipient in recipients:
if not recipient.startswith('+'):
recipient = '+47' + recipient
url = 'https://{}/sms/send'.format(host)
resp = requests.post(url, data={}, auth=(username, password), json={
'source': 'NAIS',
'platformId': 'SMS',
'platformPartnerId': platformPartnerId,
'destination': recipient,
'userData': message,
})
if resp.status_code != 200:
logger.error('Failed sending message: {}'.format(resp.text))
status_code = resp.status_code
return status_code
@app.get("/")
async def index():
return {"message": "Hello from {}".format(socket.gethostname())}
@app.post("/sms")
async def sms(recipients, request: Request, response: Response):
json = await request.json()
if len(json['alerts']) > 1:
logger.info(json['alerts'])
response.status_code = 200
for alert in json['alerts']:
try:
message = format_message(alert)
status_code = send_sms(message, recipients)
if status_code > response.status_code:
response.status_code = status_code
except TypeError as e:
logger.error(e)
logger.error(alert)
if response.status_code != 200:
return {"message": "Failed to send SMS"}
return {"message": "SMS sent"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=int(os.getenv('APP_PORT', 8080)))