Send email alerts from NXLog using Python, Perl, or Ruby

NXLog is a versatile log collector that easily integrates with other software, platforms, and programming languages. Out-of-the-box it supports integration with many third-party solutions through its input, output, and extension modules. Moreover, extending NXLog with custom functionality is as easy as writing an application or script in your favorite programming language and loading it from the configuration.

Email notifications of events indicating potential security breaches or severe application errors are a standard procedure for IT admins and DevOps engineers. Such emails ensure that the relevant personnel are immediately alerted, allowing them to handle critical situations efficiently. Let’s see how easy it is to extend NXLog to send email alerts with Python, Perl, or Ruby.

Sending emails with Python

You can send emails with Python using the smtplib module, or a third-party library such as Twilio’s SendGrid. The following example provides a sample script and a corresponding NXLog configuration for sending email alerts.

This script uses the Python smtplib module to send a simple email with TLS encryption. It reads configuration settings, such as the SMTP server details, sender email and password, and recipient email addresses, from a file in JSON format. The main() function contains the code to send the email and is the function that the NXLog configuration needs to execute. This script works only with Python 3, not Python 2.

send_email.py
import smtplib
import ssl
import json
import nxlog

def main(event):
        with open("config.json") as config_file:
                config = json.load(config_file)

        try:
                module = event.module
                host = event.get_field('Hostname')
                message = event.get_field('Message')
                server = smtplib.SMTP(config["smtp_server"],config["port"])
                server.starttls(context=ssl.create_default_context())
                server.login(config["sender_email"], config["password"])
                content = "Subject: NXLog Email Alert\nTo: %s\nFrom: %s\n\nHost: %s\nMessage: %s" % (config["receiver_email"], config["sender_email"], host, message)
                server.sendmail(config["sender_email"], config["receiver_email"], content)
        except Exception as e:
                print(e)

This script uses the configuration file below to load the email settings. Update the values according to your SMTP server settings and place the file in the same folder as the script.

config.json
{
  "smtp_server": "<your_smtp_server>",
  "port": "<port>",
  "sender_email": "<your_email>",
  "password": "<your_password>",
  "receiver_email": ["first_email","second_email","third_email"]
}

NXLog provides the xm_python module to execute Python code. The module requires the PythonCode directive to specify the location of your Python script. In turn, the python_call() procedure executes a function defined in the script. The specified function must accept an nxlog.LogData object as a parameter.

The example below demonstrates how to configure NXLog with the xm_python module to trigger email alerts when the event message contains specific keywords. The input module instance reads syslog messages from a file and processes each record as follows:

  1. First, it parses log lines with the parse_syslog() procedure of the xm_syslog module, which creates the $Message field.

  2. Then it uses a regular expression to search the $Message field for the text failed or fail.

  3. If it finds the text, the python_call() procedure executes the main() function of the Python script above.

Note
The NXLog Python modules are not installed by default on Linux-based systems and must be installed separately. Refer to the deployment instructions of your platform for more information. The Python modules are not available for macOS installations.
nxlog.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Extension syslog>
    Module        xm_syslog
</Extension>

<Extension python>
    Module        xm_python
    PythonCode    /path/to/send_email.py
</Extension>

<Input auth_logs>
    Module        im_file
    File          '/var/log/auth.log'
    <Exec>
        parse_syslog();
        if $Message =~ /failed|fail/ {
            python_call("main");
        }
    </Exec>
</Input>

Sending emails with Perl

The Perl Net::SMTP module provides an interface for sending emails to an SMTP server. The following example includes a sample script and a corresponding NXLog configuration for sending email alerts.

This Perl script uses the Net::SMTP module to send a simple email with TLS encryption. The process() subroutine contains the code to send the email and is the subroutine that the NXLog configuration needs to call. It uses local variables to define configuration settings such as the SMTP server details, sender email and password, and recipient email address. You need to replace the values within the angled brackets < > according to your SMTP server settings.

send_email.pl
#!/usr/bin/perl

use strict;
use warnings;
use Net::SMTP;
use Log::Nxlog;

sub process
{
    my $smtp_server = "<smtp_server>";
    my $hello_msg = "<hello_message>";
    my $sender_email = "<sender_email_address>";
    my $sender_password = "<password>";
    my $recipient_email = "<recipient_email_address>";

    my ( $event ) = @_;
    my $host = Log::Nxlog::get_field($event, 'Hostname');
    my $message = Log::Nxlog::get_field($event, 'Message');

    my $smtp = Net::SMTP->new(Host => $smtp_server,
                        Hello => $hello_msg,
                        SSL => 1,
                        Port => <smtp_port>,
                        Debug => 1) or die "Failed the SMTP Connection: $!";

    $smtp->auth($sender_email, $sender_password);
    $smtp->mail($sender_email);
    $smtp->to($recipient_email);
    $smtp->data();
    $smtp->datasend("To:\n");
    $smtp->datasend($recipient_email);
    $smtp->datasend("\nSubject: NXLog Email Alert");
    $smtp->datasend("\nHostname:\n");
    $smtp->datasend($host);
    $smtp->datasend("\nMessage:\n");
    $smtp->datasend($message);
    $smtp->dataend();
    $smtp->quit;
}

The NXLog xm_perl module provides the functionality to execute Perl code. The module requires the PerlCode directive to specify the location of your Perl script. In turn, the perl_call() procedure executes a subroutine defined in the script.

The example below demonstrates how to configure NXLog with the xm_perl module to trigger email alerts when the event message contains specific keywords. The input module instance reads syslog messages from a file and processes each record as follows:

  1. First, it parses log lines with the parse_syslog() procedure of the xm_syslog module, which creates the $Message field.

  2. Then it uses a regular expression to search the $Message field for the text failed or fail.

  3. If it finds the text, the perl_call() procedure executes the process() subroutine of the Perl script above.

Note
The NXLog Perl modules are not installed by default on Linux-based systems and must be installed separately. Refer to the deployment instructions of your platform for more information. In addition, executing Perl scripts from NXLog on Microsoft Windows requires Strawberry Perl version 5.28.2.1. Newer versions of Perl are currently not supported. The Perl modules are not available for macOS installations.
nxlog.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Extension syslog>
    Module      xm_syslog
</Extension>

<Extension perl>
    Module      xm_perl
    PerlCode    /path/to/send_email.pl
</Extension>

<Input auth_logs>
    Module      im_file
    File        '/var/log/auth.log'
    <Exec>
        parse_syslog();
        if $Message =~ /failed|fail/ {
            perl_call("process");
        }
    </Exec>
</Input>

Sending emails with Ruby

The Ruby mail library provides functionality to generate and send emails over SMTP. The following example includes a sample script and a corresponding NXLog configuration for sending email alerts.

This Ruby script uses the mail module to send a simple email with TLS encryption. The send_email() method contains the code to send the email and is the method that the NXLog configuration needs to call. You need to replace the values within the angled brackets < > according to your environment.

send_email.rb
require 'mail'

def send_email(event)
   $content = event.get_field('Message')

   options = { :address               => '<smtp_server>',
               :port                  => 587,
               :user_name             => '<sender_email_address>',
               :password              => '<password>',
               :authentication        => 'plain',
               :enable_startttls_auto => true }

   Mail.defaults do
      delivery_method :smtp, options
   end

   Mail.deliver do
        to '<recipient_email_address>'
          from '<sender_email_address>'
       subject 'NXLog Email Alert'
          body 'Alert with the following message: '+$content
   end
end

The NXLog xm_ruby module provides the functionality to execute Ruby code. The module requires the RubyCode directive to specify the location of your Ruby script. In turn, the call() procedure executes a method defined in the script.

The example below demonstrates how to configure NXLog with the xm_ruby module to trigger email alerts when the event message contains specific keywords. The input module instance reads syslog messages from file and processes each record as follows:

  1. First, it parses log lines with the parse_syslog() procedure of the xm_syslog module, which creates the $Message field.

  2. Then it uses a regular expression to search the $Message field for the text failed or fail.

  3. If it finds the text, the call() procedure executes the send_email() method of the Ruby script above.

Note
The NXLog Ruby modules are not installed by default on Linux-based systems and must be installed separately. Refer to the deployment instructions of your platform for more information. The Ruby modules are not available for Microsoft Windows and macOS installations.
nxlog.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Extension syslog>
    Module      xm_syslog
</Extension>

<Extension ruby>
    Module      xm_ruby
    RubyCode    /path/to/send_email.rb
</Extension>

<Input auth_logs>
    Module      im_file
    File        '/var/log/auth.log'
    <Exec>
        parse_syslog();
        if $Message =~ /failed|fail/ {
            ruby->call("send_email");
        }
    </Exec>
</Input>

Summary

You can easily extend NXLog’s functionality using custom applications or scripts. In addition to the Python, Perl, and Ruby modules mentioned here, NXLog provides more programming language modules for Go and Java, as well as a further module for executing external programs. Here we described how to send email alerts when NXLog encounters high-severity events to help your team stay on top of critical situations. However, this is only the tip of the iceberg of what you can accomplish with NXLog. We hope that the samples we provided serve as a starting point to help you attain your goal, whether it’s processing complex logs or executing custom actions with NXLog. Have an interesting use case where you enhanced NXLog’s functionality or need support with the implementation? Get in touch; we’d love to hear from you!

GET STARTED TODAY:
Disclaimer

While we endeavor to keep the information in this topic up to date and correct, NXLog makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability, or availability of the content represented here.

The accurateness of the content was tested and proved to be working in our lab environment at the time of the last revision with the following software versions:

NXLog EE 5.5.7535
Ubuntu 20.04.4 LTS, RHEL 7
Python 3.8.10
Ruby 1.2.7
Perl 5.30.0

Last revision: 03 August 2022

NXLog Ltd. develops multi-platform log collection tools that support many different log sources, formats, transports, and integrations. The tools help administrators collect, parse, and forward logs so they can more easily respond to security issues, investigate operational problems, and analyze event data. NXLog distributes the free and open source NXLog Community Edition and offers additional features and support with the NXLog Enterprise Edition.

This document is provided for informational purposes only and is subject to change without notice. Trademarks are the properties of their respective owners.

Download a fully functional trial of the Enterprise Edition for free