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
    Solutions by industry
    Financial Services
    Government & Education
    Entertainment & Gambling
    Telecommunications
    Medical & Healthcare
    Military & Defense
    Law Firms & Legal Counsel
    Industrial & Manufacturing
  • Plans
  • Partners
    Find a Reseller
    Partner Program
  • 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
Solutions by industry
Financial Services
Government & Education
Entertainment & Gambling
Telecommunications
Medical & Healthcare
Military & Defense
Law Firms & Legal Counsel
Industrial & Manufacturing


Find a Reseller
Partner Program

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

Company
Careers

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

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

By Arielle Bonnici

Share
ALL SIEM STRATEGY SECURITY ANNOUNCEMENT DEPLOYMENT COMPLIANCE COMPARISON 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

Announcing NXLog Platform 1.6
April 22, 2025
Announcing NXLog Platform 1.5
February 27, 2025
Announcing NXLog Platform 1.4
December 20, 2024
NXLog redefines log management for the digital age
December 19, 2024
2024 and NXLog - a review
December 19, 2024
Announcing NXLog Platform 1.3
October 25, 2024
NXLog redefines the market with the launch of NXLog Platform: a new centralized log management solution
September 24, 2024
Welcome to the future of log management with NXLog Platform
August 28, 2024
Announcing NXLog Enterprise Edition 5.11
June 20, 2024
Raijin announces release of version 2.1
May 31, 2024
Ingesting log data from Debian UFW to Loki and Grafana
May 21, 2024
Announcing NXLog Enterprise Edition 6.3
May 13, 2024
Raijin announces release of version 2.0
March 14, 2024
NXLog Enterprise Edition on Submarines
March 11, 2024
The evolution of event logging: from clay tablets to Taylor Swift
February 6, 2024
Migrate to NXLog Enterprise Edition 6 for our best ever log collection experience
February 2, 2024
Raijin announces release of version 1.5
January 26, 2024
2023 and NXLog - a review
December 22, 2023
Announcing NXLog Enterprise Edition 5.10
December 21, 2023
Raijin announces release of version 1.4
December 12, 2023
Announcing NXLog Enterprise Edition 6.2
December 4, 2023
Announcing NXLog Manager 5.7
November 3, 2023
Announcing NXLog Enterprise Edition 6.1
October 20, 2023
Raijin announces release of version 1.3
October 6, 2023
Upgrading from NXLog Enterprise Edition 5 to NXLog Enterprise Edition 6
September 11, 2023
Announcing NXLog Enterprise Edition 6.0
September 11, 2023
The cybersecurity challenges of modern aviation systems
September 8, 2023
Raijin announces release of version 1.2
August 11, 2023
The Sarbanes-Oxley (SOX) Act and security observability
August 9, 2023
Log Management and PCI DSS 4.0 compliance
August 2, 2023
Detect threats using NXLog and Sigma
July 27, 2023
HIPAA compliance logging requirements
July 19, 2023
Announcing NXLog Enterprise Edition 5.9
June 20, 2023
Industrial cybersecurity - The facts
June 8, 2023
Raijin announces release of version 1.1
May 30, 2023
CISO starter pack - Security Policy
May 2, 2023
Announcing NXLog Enterprise Edition 5.8
April 24, 2023
CISO starter pack - Log collection fundamentals
April 3, 2023
Raijin announces release of version 1.0
March 9, 2023
Avoid vendor lock-in and declare SIEM independence
February 13, 2023
Announcing NXLog Enterprise Edition 5.7
January 20, 2023
NXLog - 2022 in review
December 22, 2022
Need to replace syslog-ng? Changing to NXLog is easier than you think
November 23, 2022
The EU's response to cyberwarfare
November 22, 2022
Looking beyond Cybersecurity Awareness Month
November 8, 2022
GDPR compliance and log data
September 23, 2022
NXLog in an industrial control security context
August 10, 2022
Raijin vs Elasticsearch
August 9, 2022
NXLog provides native support for Google Chronicle
May 11, 2022
Aggregating macOS logs for SIEM systems
February 17, 2022
How a centralized log collection tool can help your SIEM solutions
April 1, 2020

Categories

  • SIEM
  • STRATEGY
  • SECURITY
  • ANNOUNCEMENT
  • DEPLOYMENT
  • COMPLIANCE
  • COMPARISON
logo

Subscribe to our newsletter to get the latest updates, news, and products releases. 

© Copyright 2024 NXLog FZE.

Privacy Policy. General Terms of Use

Follow us

  • Product
  • NXLog Platform 
  • Log collection
  • Log management and analysis
  • Log storage
  • Integration
  • Professional Services
  • Plans
  • Resources
  • Documentation
  • Blog
  • White papers
  • Videos
  • Webinars
  • Case studies
  • Community Program
  • Community forum
  • Support
  • Getting started guide
  • Support portals
  • About NXLog
  • About us
  • Careers
  • Find a reseller
  • Partner program
  • Contact us