News and blog
NXLog main page
  • Products
    NXLog Platform
    Log collection
    Log management and analytics
    Log storage
    NXLog Community Edition
    Integrations
    Professional Services
  • Solutions
    Use cases
    Specific OS support
    SCADA/ICS
    Windows event log
    DNS logging
    MacOS logging
    Open Telemetry
    Solutions by industry
    Financial Services
    Government & Education
    Entertainment & Gambling
    Telecommunications
    Medical & Healthcare
    Military & Defense
    Law Firms & Legal Counsel
    Industrial & Manufacturing
  • Pricing
    Licensing
    Plans
  • Partners
    Find a Reseller
    Partner Program
    Partner Portal
  • Resources
    Documentation
    Blog
    White papers
    Videos
    Webinars
    Case Studies
    Community Program
    Community Forum
  • About
    Company
    Careers
  • Support
    Support portals
    Contact us

NXLog Platform
Log collection
Log management and analytics
Log storage
NXLog Community Edition
Integrations
Professional Services

Use Cases
Specific OS support
SCADA/ICS
Windows event log
DNS logging
MacOS logging
Open Telemetry
Solutions by industry
Financial Services
Government & Education
Entertainment & Gambling
Telecommunications
Medical & Healthcare
Military & Defense
Law Firms & Legal Counsel
Industrial & Manufacturing

Licensing
Plans

Find a Reseller
Partner Program
Partner Portal

Documentation
Blog
White papers
Videos
Webinars
Case Studies
Community Program
Community Forum

Company
Careers

Support portals
Contact us
Let's Talk
  • Start free
  • Interactive demo
Let's Talk
  • Start free
  • Interactive demo
NXLog search
  • Loading...
Let's Talk
  • Start free
  • Interactive demo
August 3, 2022 security

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

By Arielle Bonnici

Share
ALL ANNOUNCEMENT COMPARISON COMPLIANCE DEPLOYMENT SECURITY SIEM STRATEGY RSS

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
<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
<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
<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!

NXLog Platform is an on-premises solution for centralized log management with
versatile processing forming the backbone of security monitoring.

With our industry-leading expertise in log collection and agent management, we comprehensively
address your security log-related tasks, including collection, parsing, processing, enrichment, storage, management, and analytics.

Start free Contact us
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. We update our screenshots and instructions on a best-effort basis.

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

  • python
  • perl
  • ruby
  • alerts
Share

Facebook Twitter LinkedIn Reddit Mail
Related Posts

DNS Log Collection on Windows
8 minutes | May 28, 2020
Flexible, cloud-backed Modbus/TCP log collection with NXLog and Python
16 minutes | June 5, 2021

Stay connected:

Sign up

Keep up to date with our monthly digest of articles.

By clicking singing up, I agree to the use of my personal data in accordance with NXLog Privacy Policy.

Featured posts

Security dashboards go dark: why visibility isn't optional, even when your defenses keep running
February 26, 2026
Building a practical OpenTelemetry pipeline with NXLog Platform
February 25, 2026
Announcing NXLog Platform 1.11
February 23, 2026
Adopting OpenTelemetry without changing your applications
February 10, 2026
Linux security monitoring with NXLog Platform: Extracting key events for better monitoring
January 9, 2026
2025 and NXLog - a recap
December 18, 2025
Announcing NXLog Platform 1.10
December 11, 2025
Announcing NXLog Platform 1.9
October 22, 2025
Gaining valuable host performance metrics with NXLog Platform
September 30, 2025
Security Event Logs: Importance, best practices, and management
July 22, 2025
Enhancing security with Microsoft's Expanded Cloud Logs
June 10, 2025

Categories

  • ANNOUNCEMENT
  • COMPARISON
  • COMPLIANCE
  • DEPLOYMENT
  • SECURITY
  • SIEM
  • STRATEGY
  • Products
  • NXLog Platform
  • NXLog Community Edition
  • Integration
  • Professional Services
  • Licensing
  • Plans
  • Resources
  • Documentation
  • Blog
  • White Papers
  • Videos
  • Webinars
  • Case Studies
  • Community Program
  • Community Forum
  • Compare NXLog Platform
  • Partners
  • Find a Reseller
  • Partner Program
  • Partner Portal
  • About NXLog
  • Company
  • Careers
  • Support Portals
  • Contact Us

Follow us

LinkedIn Facebook YouTube Reddit
logo

© Copyright NXLog Ltd.

Subscribe to our newsletter

Privacy Policy • General Terms of Business