Skip to content

Instantly share code, notes, and snippets.

@andris9
Last active March 11, 2019 00:53
Show Gist options
  • Save andris9/f96298d181ffd524a7d52de375c7c660 to your computer and use it in GitHub Desktop.
Save andris9/f96298d181ffd524a7d52de375c7c660 to your computer and use it in GitHub Desktop.
Retrieve UDP logs from ZoneMTA

Step 1. Install MessagePack module

ZoneMTA emits logs over UDP in MessagePack format, so a MessagePack parser is needed to read log messages

npm install msgpack-js

Step 2. Update MTA config

Edit your ZoneMTA application config to activate remote UDP logging. Add remote to the log section. Port and host should point to the server where logger.js app is running.

{
  "log": {
    "syslog": ...,
    "level": ...,
    "remote": {
      "protocol": "udp4",
      "host": "127.0.0.1",
      "port": 31239
    }
  }
}

Step 3. Create logging app

Logging app should listen on the UDP port specified in ZoneMTA log.remote.port config

See the below for logger.js

Step 4. Create logging app

Run the logging app

node logger.js

You should see the following output if everything worked:

Log server listening on port 31239

Once ZoneMTA does something with messages it should send the transaction info to this logger. The payload should be loged to console.

'use strict';
// This is the logger app demo. It receives all log messages, unpacks these and logs to console.
const dgram = require('dgram');
const msgpack = require('msgpack-js');
const server = dgram.createSocket('udp4');
server.bind(31239, () => {
console.log('Log server listening on port %s', 31239);
server.on('message', (payload, rinfo) => {
let message = msgpack.decode(payload);
console.log(message.id, JSON.stringify(message));
});
});
$ node logger.js
Log server listening on port 31239
15b193a9c9f0002c25 {"id":"15b193a9c9f0002c25","action":"QUEUED","message-id":"<ffef7bdd-693d-8151-c525-06d6b8fd1d4d@localtest.me>","from":"andris@localtest.me","to":"rcpt-2@sub-12.localtest.me","source":"127.0.0.1","subject":"\"[2] Nodemailer is unicode friendly ✔ (1490776922483) ascii\"","body":16246,"md5":"a95deceb08b9","interface":"feeder","originhost":"[127.0.0.1]","transhost":"localhost","transtype":"ESMTP"}
15b193a9c9f0002c25 {"id":"15b193a9c9f0002c25","seq":"001","action":"REJECTED","category":"recipient","zone":"default","from":"andris@localtest.me","returnPath":"andris@localtest.me","to":"rcpt-2@sub-12.localtest.me","mx":"sub-12.localtest.me","response":"550 Network error when connecting to MX server sub-12.localtest.me[127.0.0.1] for sub-12.localtest.me: Connection refused"}
@rquast
Copy link

rquast commented Jun 21, 2017

This is great. I can adapt this plugin to send GrayLog messages in GELF format. The plugin system you wrote is magic.

@elmehdiabdi
Copy link

Thanks you are my problems solver .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment