[ Zabbix - 카카오톡 얼롯팅 설정 ] part 5-2 최종

안녕하세요 

에러 발생시 카카오톡을 통한 얼롯팅 설정 5-1편에 이어 마지막 설정까지 진행 해보도록 하겠습니다.

 

 

 사전 구성

 

카카오톡 Developer 설정 : https://jhdatabase.tistory.com/30

 

[ Zabbix - 카카오톡 알림 설정 ] part 5-1

안녕하세요 이번 글에선 카카오톡 알림 설정을 진행해보겠습니다. 에러 발생시 카카오에서 제공하는 rest api를 이용하여 zabbix에 등록한 파이썬 스크립트를 돌려 자신의 카카오톡으로 알람이 오

jhdatabase.tistory.com

 

 

 

 Kakao talk 설정

 

■ 토큰 불러오기

토큰 유효기간 확인

※ access token은 6시간에 한번씩 새롭게 토큰을 발급해줘야하니, refresh token으로 진행

 

 

 

Access token 생성

[root@mha-manager ~]# vi create.access.py
import requests
url = 'https://kauth.kakao.com/oauth/token'
rest_api_key = '216bbebb1f7dd0755bfac177d321d8c8'
redirect_uri = 'https://example.com/oauth'
authorize_code = 'LN2bb7p6ET2vUhfspGO2MgBKkO3PiiYKIz9XggQELI20HC9MmgUemp-4gQzmiM2h-02GwAopb7kAAAF-0gKEaw'

data = {
    'grant_type':'authorization_code',
    'client_id':rest_api_key,
    'redirect_uri':redirect_uri,
    'code': authorize_code,
    }


response = requests.post(url, data=data)
tokens = response.json()
print(tokens)


# json 저장
import json

with open("kakao_code.json","w") as fp:
    json.dump(tokens, fp)




[root@mha-manager ~]# python3 create.access.py
{'access_token': '-F8mmzvrS14AKYj50FYR-kztNxlziOzP0Thalgo9dVsAAAF-0gLOkA', 'token_type': 'bearer', 'refresh_token': 'bkbydltjkLmEbPFhu1ZXY-KkT1diFkdBd6qYhwo9dVsAAAF-0gLOjg', 'expires_in': 21599, 'scope': 'profile_image talk_message profile_nickname story_permalink friends', 'refresh_token_expires_in': 5183999}

 


 refresh token 생성

[root@mha-manager ~]# vi create.refresh.py

import requests

url = 'https://kauth.kakao.com/oauth/token'
rest_api_key = '216bbebb1f7dd0755bfac177d321d8c8'
redirect_uri = 'https://example.com/oauth'
authorize_code = 'LN2bb7p6ET2vUhfspGO2MgBKkO3PiiYKIz9XggQELI20HC9MmgUemp-4gQzmiM2h-02GwAopb7kAAAF-0gKEaw'
refresh_code = 'bkbydltjkLmEbPFhu1ZXY-KkT1diFkdBd6qYhwo9dVsAAAF-0gLOjg'


data = {
    'grant_type':'refresh_token',
    'client_id':rest_api_key,
    'refresh_token':refresh_code
    }


response = requests.post(url, data=data)
tokens = response.json()
print(tokens)


# json 저장
import json


with open("kakao_code.json","w") as fp:
    json.dump(tokens, fp)




[root@mha-manager ~]# python3 create.refresh.py
{'access_token': '4wHHp6DHiZGSGGmgz2nLmdoKle8vGfBhcv7ksQopcNEAAAF-0gVplg', 'token_type': 'bearer', 'expires_in': 21599}
 
 
 

 리다이렉션될 link 설정

##https://developers.kakao.com/docs/latest/ko/message/message-template#link

내 애플리케이션 -> 플랫폼 -> Web부분에 link로 redirect될 도메인 입력

 

※ ip가 아닌 도메인 명을 써야하기때문에..제 블로그 주소로 썼습니다. zabbix 대쉬보드 ip주소를 도메인을 구입하면 해당 도메인을 기재하고 카카오톡을 통해 바로 접속 할 수 있습니다. 

 
 

 

 카카오톡 메세지 link

[root@mha-manager ~# vi kakao.py

import requests
import json


with open("kakao_code.json","r") as fp:
    tokens = json.load(fp)


url="https://kapi.kakao.com/v2/api/talk/memo/default/send"
# kapi.kakao.com/v2/api/talk/memo/default/send
headers={
    "Authorization" : "Bearer " + tokens["access_token"]
}

data={
    "template_object": json.dumps({
        "object_type":"text",
        "text":"성공적! GoodGood!!!",
        "link":{
            "web_url":"https://jhdatabase.tistory.com/",
            "mobile_web_url":"https://jhdatabase.tistory.com/"
        },
        "button_title":"Monitoring"
    })
}


response = requests.post(url, headers=headers, data=data)
response.status_code
 
 
 
 

 실행

[root@mha-manager ~]# python3 kakao.py
 
 
 
 

 카카오톡 확인

 

 

 

 해당 버튼 클릭 시 입력해둔 URL로 접속되는것을 확인 할 수 있습니다.

 

 
 

 

Zabbix 설정 

 

 Zabbix 내용 추가 

Administration -> Media types -> create media type

 
 

 

 

  Administration > Users > Admin > Media

 

 

 

Action 설정

Configuration -> Actions -> Report problems to Zabbix administrators -> Operations 의 연락 방법을 all로 지정

 

 

 

스크립트 mv

[root@mha-manager ~]# mv kakao.py /usr/lib/zabbix/alertscripts/
[root@mha-manager ~]# mv kakao_code.json  /usr/lib/zabbix/alertscripts/

[root@mha-manager ~]# chmod 777 /usr/lib/zabbix/alertscripts/kakao.py


[root@mha-manager ~]# vi /usr/lib/zabbix/alertscripts/kakao.py

#!/bin/python3
#coding: utf8

import requests
import json
import os
import sys

with open(r"/usr/lib/zabbix/alertscripts/kakao_code.json","r") as fp:
    tokens = json.load(fp)

url="https://kapi.kakao.com/v2/api/talk/memo/default/send"
# kapi.kakao.com/v2/api/talk/memo/default/send
headers={
    "Authorization" : "Bearer " + tokens["access_token"]
}

###KAKAO DATA
data={
    "template_object": json.dumps({

        "object_type":"text",
        "text":message,
        "link":{
            "web_url":"https://jhdatabase.tistory.com",
            "mobile_web_url":"https://jhdatabase.tistory.com"
        },
        "button_title":"Monitoring"
    })
}


response = requests.post(url, headers=headers, data=data)
response.status_code
 
 
 
 

 DB shutdown

[root@node2 ~]# mysqladmin -uroot -proot shutdown

 

 

 

 자빅스 대시보드 확인

 

 

 

카카오톡 얼롯팅 확인

 

 

 

 

이상으로 카카오톡 얼롯팅 설정 포스팅을 마치겠습니다.

 

 

 

 

참고 

 

 https://scbyun.com/707

 

[Zabbix] 자빅스 Mattermost 연동 (Webhook URL, Mattermost slack alert script)

자빅스 Mattermost 연동 zabbix_server.conf 설정 확인  : zabbix_server.conf 설정에 AlertScriptsPath 설정되어 있는지 확인합니다. $ vim /etc/zabbix/zabbix_server.conf ListenPort=10051 LogFile=/var/log/..

scbyun.com