티스토리 뷰

자빅스 Mattermost 연동

 

zabbix_server.conf 설정 확인

 : zabbix_server.conf 설정에 AlertScriptsPath 설정되어 있는지 확인합니다.

$ vim /etc/zabbix/zabbix_server.conf

ListenPort=10051
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_server.pid
SocketDir=/var/run/zabbix
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=password
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
StartSNMPTrapper=1
Timeout=4
AlertScriptsPath=/usr/lib/zabbix/alertscripts
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1

 

WebHooks URL 확인

Incoming WebHooks

 

Mattermost 스크립트 작성

- URL, ICON 변경

$ vim /usr/lib/zabbix/alertscripts/Mattermost.py

#!/bin/python3
#coding: utf8

import json
import os
import sys
import requests

# Mattermost incoming web-hook URL and user name
URL = "https://matt.sangchul.com/hooks/x8rzdcx56f8ztpiu5i5qq38cwh"
USERNAME = "Zabbix"
ICON = "http://zbx.sangchul.com/zabbix/zabbix-monitoring.png"
LEVELS = ["Warning", "High", "Disaster"]

def send_to_mattermost(webhook, channel, message, username="zabbix", color="#FF2A00", icon="", highlight=False):
        # Build our JSON payload and send it as a POST request to the Mattermost incoming web-hook URL
        payload = {"icon_url": icon, "attachments": [{"color": color, "text": message}], "channel": channel, "username": username, "icon_emoji": ""}
        if highlight:
                payload["message"] = ":warning: Cc <!here>"
        requests.post(webhook, data={"payload": json.dumps(payload)})

if __name__ == '__main__':
        if len(sys.argv) < 4:
                print("Usage: python "+sys.argv[0]+" <CHANNEL> <SUBJECT> <MESSAGE>")
                sys.exit(1)

        ## Values received by this script:
        # To = sys.argv[1] (Mattermost channel or user to send the message to, specified in the Zabbix web interface; "@username" or "#channel")
        # Subject = sys.argv[2] (usually containing either "Problem" or "Resolved")
        # Message = sys.argv[3] (whatever message the Zabbix action sends)

        # Get the Mattermost channel or user (sys.argv[1]) and Zabbix subject (sys.argv[2])
        channel = sys.argv[1]
        subject = sys.argv[2]
        if "{{OK}}" in subject:
                subject=subject.replace("{{OK}}",":white_check_mark:")
        elif "{{WARNING}}" in subject:
                subject=subject.replace("{{WARNING}}",":warning:")
        color = "#00FF13" if "Resolved" in subject else "#FF2A00"

        # The message that we want to send to Mattermost  is the "subject" value (sys.argv[2] / $subject - that we got earlier)
        #  followed by the message that Zabbix actually sent us (sys.argv[3])
        message = "### "+subject + "\n\n"+sys.argv[3]

        # Let's highlight every connected people if the level is serious
        highlight = False
        for level in LEVELS:
                if level in message:
                        highlight = True
                        break

        # Send notification
        send_to_mattermost(URL, channel, message, username=USERNAME, color=color, icon=ICON, highlight=highlight)

 

github - jirouette/zabbix-mattermost-alertscript : mattermost.py

 

자빅스 웹 페이지에서 미디어 타입 설정

Administration > Media types > Create media type

스크립트 파라미터

{ALERT.SENDTO}

{ALERT.SUBJECT}

{ALERT.MESSAGE}

 

자빅스 웹 페이지에서 사용자에 미디어 추가

Administration > Users > guest(user) > Media

 

자빅스 웹 페이지에서 액션 생성/설정

Configuration > Actions > Create action

 

자빅스 웹 페이지에서 Message template 수정

Administration > Media tapes > Message template

 

Mattermost 메세지

댓글
댓글쓰기 폼