#현재 위치
~/idea/python
pip3 install --upgrade pip
pip3 install virtualenv
virtualenv -p python3 virtualenv
source virtualenv/bin/activate
pip3 install boto3
# 메인 파이선 소스를 코딩한다. 파일명은 반드시 __main__.py 로 해야 하며, def main(args): 가 정의되어야 함
vi __main__.py
import boto3
import json
import logging
import os
from base64 import b64decode
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
def main(args):
# The base-64 encoded, encrypted key (CiphertextBlob) stored in the kmsEncryptedHookUrl environment variable
HOOK_URL = "https://hooks.slack.com/services/TF...M3/BF...5/DB....P1"
# The Slack channel to send a message to stored in the slackChannel environment variable
SLACK_CHANNEL = "ignorable_notification"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
alarm_name = args.get("ruleId", "Infra Notice")
level = args.get("level", "Info")
instanceName = args.get("instanceName", "instanceName")
condition = args.get("condition", "condition")
value = args.get("value", "value")
slack_message = {
'channel': SLACK_CHANNEL,
'text': "alarm_name : %s, level : %s InstanceName : %s, condition : %s, value : %s " % (alarm_name, level, instanceName, condition, value)
}
req = Request(HOOK_URL, json.dumps(slack_message).encode('utf-8'))
try:
response = urlopen(req)
response.read()
logger.info("Message posted to %s", slack_message['channel'])
except HTTPError as e:
logger.error("Request failed: %d %s", e.code, e.reason)
except URLError as e:
logger.error("Server connection failed: %s", e.reason)
return {"payload": "success"}
#virtualenv와 _main_.py를 함께 압축한다.
zip -r slack_py.zip virtualenv __main__.py
Cloud Function에 액션 등록
#디폴트 파라미터 :
{"ruleName":"123","alarmStartTime":1599186752071,"condition":"cpu_usrt_rto > 90","value":"99","ruleId":1234,"level":"INFO","instanceName":"Justin-test"}
언어 : Python:3
타입 : 파일
업로드 : slack_py.zip
Cloud Insight의 파라미터 참고
docs.ncloud.com/ko/compute/compute-15-3-3.html
'AWS_NCP > NCP' 카테고리의 다른 글
Ncloud NAS (0) | 2020.04.24 |
---|