[![logo](https://www.hongsnet.net/images/logo.gif)](https://www.hongsnet.net)

# www.hongsnet.net monitoring

![source_overview](./images/zabbix-monitoring.png)


Dedicated 및 VM 시스템의 경우 Zabbix를 이용하여 모니터링 한다. 구성요소는 다음과 같다.

# 구성 요소

- Dockerized Envionment
- Mattermost Push Alert


## Dockerfile

```bash
# cat Dockerfile
FROM registry.hongsnet.net/joohan.hong/docker/centos:7.6.1810
MAINTAINER Hongs <master@hongsnet.net>

ENV TZ=Asia/Seoul

COPY config/yum.conf /etc/yum.conf
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone &&\
    yum -y install epel-release

ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

RUN yum -y install epel-release

RUN yum -y install glibc glibc-common openssl-devel net-tools vi vim iproute wget postfix cronie crontabs supervisor python-pip
RUN rpm -ivh https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm

COPY config/zabbix_server.conf /etc/zabbix/zabbix_server.conf
COPY config/supervisord.conf /etc/supervisor/supervisord.conf
COPY config/zabbix.conf /etc/supervisor/conf.d/zabbix.conf
COPY config/php.ini /etc/php.ini

RUN pip install requests
RUN chown -R apache.apache /etc/zabbix/web
RUN rm -rf /etc/alternatives/zabbix-web-font
RUN mv /usr/share/fonts/dejavu/DejaVuSans.ttf /usr/share/fonts/dejavu/DejaVuSans.ttf_bak

COPY config/fonts/NanumFont/NanumGothic.ttf /usr/share/fonts/dejavu/DejaVuSans.ttf
COPY config/bashrc /root/.bashrc

RUN localedef -f UTF-8 -i ko_KR ko_KR.utf8

EXPOSE 10051 443

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]

```

> `!중요` : Mattermost의 Push Alert을 위해서는 python pip 패키지설치 및 request 모듈의 설치가 필요하다.


## Mattermost Push Alert Script


> 출처 : https://github.com/jirouette/zabbix-mattermost-alertscript/blob/master/mattermost.py


```python
#!/usr/bin/python
#coding: utf8

import json
import os
import sys
import requests

# Mattermost incoming web-hook URL and user name
URL = "<incoming webbook uri>" # example: httpsL//mattermost.example.com/hooks/ere5h9gfbbbk8gdxsei1tt8ewewechjsd
USERNAME = "zabbix"
ICON = "<icon_url>"
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)
```

- Alert Example

![zabbix_mattermost](./images/zabbix_mattermost.png)

