Email hipchat with postfix, fluentd and python on Azure

Overview

  1. [Premise] Alert mail is sent from the monitoring system (this time it is troublesome, so use a command from localhost)
  2. Receive mail with postfix
  3. Kick python script 1 with / etc / aliases
  1. Fluentd parses depending on the contents of the email and kicks python script 2 (this time it passes through without parse)

Let's go in order

Build a server on Azure

Register from the following when you can get a free ticket for 20,000 yen for free in the first month. Microsoft Azure Free Trial: Try Azure | Azure Free Trial

The phone number, the first 0 is pulled out ... or something like that, it's usually registered. reference: [[Subscription] Personal authentication for new Windows Azure contracts --Microsoft Azure support team site --Site Home --MSDN Blogs](http://blogs.msdn.com/b/dsazurejp/archive/2013/09/12/ new-subscription-personal-indentification.aspx)

I made centos6.5 (what is OpenLogic?) With 1 core CPU and 1.75GB memory. The management screen is a resource, so it's Shareotsu.

azure.png

I installed fluentd (for fluent-cat) with td-agent, ruby2.0.0 and gem.

reference: Install ruby using rbenv (CentOS edition) --Qiita

Script execution from postfix

I haven't messed with the annoying virtual domain or anything around it. Please feel free to use it.

I messed with postfix in 3 places

/etc/postfix/main.cf


#Add the following
allow_mail_to_commands = alias,forward,include

/etc/aliases


#Add the following
hoge: :include:/home/hoge/.forward

/home/hoge/.forward


"|/usr/bin/python /home/hoge/mail_parser/script1.py"

It's usually sendmail, so you'll need to switch to postfix, but this one was annoying. The reason for including is to change the execution user of script1.py, isn't it? I got angry with a Permission Error, so ...

After changing the settings, gently

$ newaliases
$ /etc/init.d/postfix reload

And reflect the settings.

reference: [K-One Enterprise Engineer Memo (`・ ω ・ ´) Business !!: postfix Setting Part 5 Start the program triggered by receiving an email](http://k-1-ne-jp.blogspot.jp/ 2013/01/postfix_11.html) Skip mail with one-line mail command-Mountain walking programmer

Post fluentd from standard input

Script 1 is like the following, feeding raw mail to the parser from standard input.

/home/hoge/mail_parser/script1.py


#! /usr/local/bin/python
# -*- coding:utf-8 -*-

from fluent import sender
from fluent import event
import sys
import email

### get stdin
input_lines = sys.stdin.read()

### get mail text
mail_text = email.message_from_string(input_lines)

### send message to fluentd
sender.setup('out.test', host='localhost', port=24224)
event.Event('follow', {
    'from': mail_text["from"],
    'to': mail_text["to"],
    'date': mail_text["date"],
    'subject': mail_text["subject"],
    'body': mail_text.get_payload()
    })

fluent (td-agent) settings

Receive with tcp with in_forward and execute script 2 with out_exec.

/etc/td-agent/td-agent.conf


### built-in TCP input
<source>
  type forward
  port 24224
</source>

### file output
<match local.**>
  type file
  path /var/log/td-agent/access
</match>

### debug stdout
<match debug.**>
  type stdout
</match>

### out_exec
<match out.**>
  type exec
  command /usr/bin/python /var/td-agent/bin/hipchat_poster/script2.py
  time_key got_at
  time_format %Y-%m-%d %H:%M:%S
  format json
  flush_interval 5s
  buffer_path /var/td-agent/tmp/buffer
  buffer_chunk_limit 256m
  buffer_queue_limit 10
  retry_limit 3
  retry_wait 1s
</match>

If you change out.test to local.test or debug.test in script1.py above,

echo "postfix test mail" | mail -s "test mail" hoge@localhost

You can send a test email and check that the contents of the email are written to a file.

By the way, when passing data to out_exec, the path of the buffer file containing the data is passed to the argument of the execution command, so be careful there. Thankfully, the people around me told me about it, but I'm afraid of misunderstanding the specifications ... I was wondering if it would come with standard input ...

Also, if you don't control the fluentd buffer properly, it will get stuck, so be careful about that. I still have to understand a lot about it.

reference: exec Output Plugin | Fluentd Multi-stage fluentd + mongodb hamari place --stanaka's blog

Hipchat post from json in the argument file path

And script 2 reads json of the argument file path and posts a message to hipchat as follows.

It is necessary to create a hipchat account and issue tokens in advance, put hypcaht with pip, and open directories.

/var/td-agent/bin/hipchat_poster/script2.py



from hypchat import HypChat
import sys
import json

def main():
        ### api call rundown
        API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        ROOM_NUMBER = 000000
        DEFAULT_COLOR = "purple"

        ### get fluentd log
        f = open(sys.argv[1], 'r')
        data = json.load(f)
        input_lines = "[" + data["date"] + "] " + data["subject"] + ":" + data["body"]

        ### post message to hipchat
        hc = HypChat(API_KEY)
        room = hc.get_room(ROOM_NUMBER)
        message = input_lines
        room.notification(message, color=DEFAULT_COLOR)
        return 0

if __name__ == "__main__":
        main()

reference: Check the operation of Fluentd plugins in_exec and out_exec | OpenGroove

After that

echo "postfix test mail" | mail -s "test mail" hoge@localhost

When I posted it, I was able to post it to hipchat like the following. I'm happy.

HipChat.png

Bonus: Reasons for implementation

By the way, for the time being, I put monit or something in the monitoring and tried to fly the alert mail, but since it is for studying, I will make various explicit alert mails, and if I write a fluentd parser for it, I am very satisfied with this creation. I've done it, so next time.

that's all.

Recommended Posts

Email hipchat with postfix, fluentd and python on Azure
Using Python and MeCab with Azure Databricks
A memo with Python2.7 and Python3 on CentOS
Use Python and MeCab with Azure Functions
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Use Python and word2vec (learned) with Azure Databricks
Validate E-Mail with Python
Send email with Python
Install selenium on Mac and try it with python
Automatic follow on Twitter with python and selenium! (RPA)
Get comments on youtube Live with [python] and [pytchat]!
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Automate Chrome with Python and Selenium on your Chromebook
Programming with Python and Tkinter
Encryption and decryption with Python
Send Japanese email with Python3
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
python with pyenv and venv
Works with Python and R
IP spoof using tor on macOS and check with python
Test Python with Miniconda on OS X and Linux with travis-ci
Notes on deploying pyenv with Homebrew and managing Python versions
Communicate with FX-5204PS with Python and PyUSB
Notes on Python and dictionary types
Robot running with Arduino and python
Python 3.6 on Windows ... and to Xamarin.
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Create AtCoder Contest appointments on Google Calendar with Python and GAS
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Scraping with Python, Selenium and Chromedriver
Build a Python environment on your Mac with Anaconda and PyCharm
[Automation] Send Outlook email with Python
Error and solution when installing python3 with homebrew on mac (catalina 10.15)
Linking Python and Arduino to display IME On / Off with LED
Integrate Modelica and Python on Windows
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Easy email sending with haste python3
Reading and writing NetCDF with Python
I played with PyQt5 and Python3
Notes on using rstrip with python.
Reading and writing CSV with Python
Install lp_solve on Mac OS X and call it with python.
Notify HipChat with AWS Lambda (Python)
Multiple integrals with Python and Sympy
Getting started with Python 3.8 on Windows
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
[Python] Send an email with outlook
Sugoroku game and addition game with python
FM modulation and demodulation with Python
[Memo] Tweet on twitter with python
Send email via gmail with Python 3.4.3.
Don't work Python with OpenCV on AMD Ryzen CPU on WSL2 Ubuntu 18.04 And 20.04