-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_app.py
75 lines (66 loc) · 2.62 KB
/
function_app.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
import logging
import os
import azure.functions as func
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
import make_slack_message
CHANNEL_ID = os.environ.get("SLACK_CHANNEL_ID")
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
@app.function_name(name="AzureMonitorTrigger")
@app.route(route="azure_monitor")
def azure_monitor_http_trigger(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Azure Monitor Alert Triggered')
# 1) 수신된 경고 데이터 읽기
try:
request_body = req.get_json()
logging.info(request_body)
except Exception as e:
logging.error(f'Error at Get Json: {e}')
return func.HttpRecsponse(status_code=500)
# 2) 저장한 데이터 파싱 & 슬랙 전송용도 메시지 만들기
else:
payload = request_body['data']
try:
slack_message = make_slack_message.azure_monitor(payload)
except Exception as e:
logging.error(f'Error at Make Slack Message: {e}')
return func.HttpResponse(status_code=500)
# 3) 슬랙으로 메시지 전송
try:
result = client.chat_postMessage(
channel=CHANNEL_ID,
attachments=slack_message
)
logging.info(result)
return func.HttpResponse(status_code=200)
except SlackApiError as e:
logging.error(f"Error at Sending Slack Message: {e}")
return func.HttpResponse(status_code=500 )
# Processing Grafana Alert
@app.function_name(name="GrafanaTrigger")
@app.route(route="grafana")
def grafana_http_trigger(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Grafana Alert Triggered')
try:
request_body = req.get_json()
logging.info(request_body)
except TypeError as e:
logging.error(f'Get Json error occured: {e}')
return func.HttpResponse(status_code=500)
else:
try:
slack_message = make_slack_message.grafana(request_body)
except Exception as e:
logging.error(f'Error at Make Slack Message: {e}')
return func.HttpResponse(status_code=500)
try:
result = client.chat_postMessage(
channel=CHANNEL_ID,
attachments=slack_message
)
logging.info(result)
return func.HttpResponse(status_code=200)
except SlackApiError as e:
logging.info(e)
return func.HttpResponse(status_code=500)