본문 바로가기

퍼블릭 클라우드

[aws] EC2 접속 시 슬랙으로 알림

반응형

EC2 접속 시 슬랙으로 알림

ec2-info-send.sh 스크립트 생성

$ vim /etc/profile.d/ec2-info-send.sh
#!/bin/bash

V_slackUrl="https://hooks.slack.com/services/TCT4/wkQM"
V_channelName="#zabbix_bot"
V_username="ec2"
V_emoji=":white_check_mark:"
V_color='#0C7BDC'

CDATE=`date '+%Y-%m-%d, %H:%M:%S' `
IPUBIPV4=`curl -s http://169.254.169.254/latest/meta-data/public-ipv4`
IPLOCALIPV4=`curl -s http://169.254.169.254/latest/meta-data/local-ipv4`
IID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
IREGION=`curl -s http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region`
ITYPE=`curl -s http://169.254.169.254/latest/meta-data/instance-type`
IHOSTNAME=`aws --region ${IREGION} ec2 describe-instances \
        --instance-ids ${IID} \
        --query "Reservations[].Instances[].Tags[?Key=='Name'].Value[]" \
        --output text`

text=text
subject=$1
message="Instance id: $IID
Hostname: $IHOSTNAME
Public ipv4: $IPUBIPV4
Local ipv4: $IPLOCALIPV4
Instance type: $ITYPE
Date : $CDATE"

###PAYLOAD START###
payload="payload={\"channel\": \"${V_channelName}\",  \
\"username\": \"${V_username}\", \
\"attachments\": [{\"fallback\": \"${subject//\"/\\\"}\", \"title\": \"${subject//\"/\\\"}\", \"text\": \"${message//\"/\\\"}\", \"color\": \"${V_color}\"}], \
\"icon_emoji\": \"${V_emoji}\"}"
###PAYLOAD END###

curl \
    -X POST \
    -H "Accept: application/json" \
    --data-urlencode "${payload}" \
    ${V_slackUrl}
chmod +x ec2-info-send.sh

slack 알람

 

참고URL

- EC2 인스턴스에 접속할 때마다 슬랙으로 알림을 보내는 방법 : https://scbyun.com/333

 

728x90
반응형