반응형
1. pinpoint에서 slack webhook api등을 제공하지 않으며 기본적인 Email 을 이용하여 작업
2. 구조 : Pinpoint Email → Smtp Server:1025 → Slack Push
3. <pinpoint> Email 설정
## Email 설정 1025포트는 smtp 서버용 python 데몬 사용 예정
[root@PINPOINT sites]# cd /usr/local/pinpoint/tomcat9/webapps/ROOT/WEB-INF/classes
[root@PINPOINT classes]# vim batch.properties
#batch enable config
batch.enable=true
#batch server ip to execute batch
batch.server.ip={pinpoint ip}
batch.server.env=release
#smtp config
pinpoint.url=http://{pinpoint domain}
alarm.mail.server.url={smtp server}
alarm.mail.server.port=1025
alarm.mail.server.username=
alarm.mail.server.password=
alarm.mail.sender.address={sender email}
alarm.mail.transport.protocol=smtp
alarm.mail.smtp.port=1025
alarm.mail.smtp.auth=false
alarm.mail.smtp.starttls.enable=false
alarm.mail.smtp.starttls.required=false
alarm.mail.debug=false
#flink server list
batch.flink.server=
## 톰캣 재기동
4. <SMTP> smtp-to-slack.py
## email 메세지를 가공함에 있어 오류 가능성이 있음.
[root@smtp pinpoint-webhook]# pwd
/home/pinpoint-webhook
[root@smtp pinpoint-webhook]# cat smtp-to-slack.py
#!/usr/bin/python3
import smtpd
import asyncore
import threading
import os
import subprocess
import json
class CustomSMTPServer(smtpd.SMTPServer):
def process_message(self, peer, mailfrom, rcpttos, data):
data_list=data.split('\n')
for var in data_list:
var_list=var.split(':')
var2_sub=0
for var2 in var_list:
if var2_sub == 1:
subject=var2
if var2 == 'Subject':
var2_sub=1
body=data_list[-1]
body=body.replace("<strong>","")
body=body.replace("</strong>","")
body=body.replace("<br>","\n")
body=body.replace("</br>","\n")
body=body.replace("</br>","\n")
body=body.replace('<a href="http://{pinpoint IP}" >',"")
body=body.replace('<a href="http://{pinpoint domain}" >',"")
body=body.replace('</a>',"")
webhook="https://hooks.slack.com/services/-----------------------------------"
username="PINPOINT"
channel="#alert"
icon_emoji=":round_pushpin:"
color='warning'
payload={}
payload['channel']=channel
payload['username']=username
payload['icon_emoji']=icon_emoji
payload['attachments']=[{}]
payload['attachments'][0]['fallback']=subject
payload['attachments'][0]['pretext']=subject
payload['attachments'][0]['color']=color
payload['attachments'][0]['fields']=[{}]
payload['attachments'][0]['fields'][0]['title']='Message'
payload['attachments'][0]['fields'][0]['value']=body
payload['attachments'][0]['fields'][0]['short']=False
payload_json=json.dumps(payload)
slack_send = "curl -X POST --data-urlencode 'payload={a}' {b}".format(a=payload_json,b=webhook)
subprocess.check_output(slack_send, shell=True, universal_newlines=True)
#print(payload_json)
return
class SMTPServer():
def __init__(self):
self.port = 1025
def start(self):
'''Start listening on self.port'''
# create an instance of the SMTP server, derived from asyncore.dispatcher
self.smtp = CustomSMTPServer(('0.0.0.0', self.port), None)
# start the asyncore loop, listening for SMTP connection, within a thread
# timeout parameter is important, otherwise code will block 30 seconds
# after the smtp channel has been closed
kwargs = {'timeout':1, 'use_poll': True}
self.thread = threading.Thread(target=asyncore.loop, kwargs=kwargs)
self.thread.start()
def stop(self):
'''Stop listening to self.port'''
# close the SMTPserver to ensure no channels connect to asyncore
self.smtp.close()
# now it is safe to wait for asyncore.loop() to exit
self.thread.join()
# check for emails in a non-blocking way
def get(self):
'''Return all emails received so far'''
return self.smtp.emails
if __name__ == '__main__':
server = CustomSMTPServer(('0.0.0.0', 1025), None)
asyncore.loop()
반응형
'Monitoring Tools > Pinpoint' 카테고리의 다른 글
17. pinpoint | Nginx Http Header 추가 (0) | 2021.03.17 |
---|---|
16. pinpoint | [오류수정] API-METADATA-NOT-FOUND (0) | 2021.03.17 |
pinpoint | 14. 로그인 페이지 생성 (0) | 2021.03.17 |
13.[기타] WEB - API ERROR Check (0) | 2021.03.17 |
pinpoint | 12. WEB - Alarm 설정 (0) | 2021.03.17 |