• Products
    LOG COLLECTOR
    NXLog Enterprise Edition
    Full feature multi-platform log collection
    NXLog Community Edition
    Open-source free log collector
    ADD-ONS FOR NXLOG ENTERPRISE EDITION
    NXLog Add-Ons
    Integration with various software
    AGENT MANAGER FOR NXLOG ENTERPRISE EDITION
    NXLog Manager
    Manage and monitor NXLog instances
    NXLog Minder
    Hyper-scalable, API-first agent management
    DATABASE FOR NXLOG ENTERPRISE EDITION
    Raijin Database Engine
    The schemaless SQL database for storing events
    more from nxlog
    Professional Services
    Compare NXLog EE and CE
    NXLog Solution Packs
  • Downloads
    NXLog Enterprise Edition
    Full feature multi-platform log collection
    NXLog Manager
    Manage and monitor NXLog instances
    NXLog Community Edition
    Open-source free log collector
  • Solutions
    Integrations
    With SIEM, Devices, SaaS...
    Specfic OS support
    AIX, Linux, FreeBSD
    SCADA/ICS
    Energy, Oil & Gas, Transport...
    Windows Event log
    Collect locally or remotely, ..
    DNS Logging
    Enterprise-grade DNS log...
    Log Collection Modes
    Agent-based, Agentless or Cloud
    Agent Management
    Agents management and monitoring
    FIM
    File Integrity Monitoring
    macOS Logging
    ULS events, Apple System Logs ...
    By Industry
    Financial Services
    Government & Education
    Entertainment & Gambling
    Telecommunications
    Medical & Healthcare
    Military & Defense
    Law Firms & Legal Counsel
    Industrial & Manufacturing
  • Partners
    Find a Reseller
    Look for our resellers worldwide
    Technology Ecosystem
    See all our partners and integrations
    Partner Program
    Join our community of partners
  • Resources
    Documentation
    Products guides and integrations
    Blog
    Tutorials, updates and releases
    White papers
    Datasheets, infographics and more
    Videos
    Trainings and tutorial on specific topics
    Webinars
    Community events and webinars
    Case Studies
    Customer success stories
    Community Forum →
  • Support
  • Why Nxlog
    About Us
    Our journey, team and mission
    Customers
    Testimonials and case studies
    Careers
    We are hiring!
    Contact Us →
LOG COLLECTOR
NXLog Enterprise Edition
Full feature multi-platform log collection
NXLog Community Edition
Open-source free log collector
ADD-ONS FOR NXLOG ENTERPRISE EDITION
NXLog Add-Ons
Integration with various software
AGENT MANAGER FOR NXLOG ENTERPRISE EDITION
NXLog Manager
Manage and monitor NXLog instances
NXLog Minder
Hyper-scalable, API-first agent management
DATABASE FOR NXLOG ENTERPRISE EDITION
Raijin Database Engine
The schemaless SQL database for storing events
more from nxlog
Professional Services
Compare NXLog EE and CE
NXLog Solution Packs
NXLog Enterprise Edition
Full feature multi-platform log collection
NXLog Manager
Manage and monitor NXLog instances
NXLog Community Edition
Open-source free log collector
Integrations
With SIEM, Devices, SaaS...
Specfic OS support
AIX, Linux, FreeBSD
SCADA/ICS
Energy, Oil & Gas, Transport...
Windows Event log
Collect locally or remotely, ..
DNS Logging
Enterprise-grade DNS log...
Log Collection Modes
Agent-based, Agentless or Cloud
Agent Management
Agents management and monitoring
FIM
File Integrity Monitoring
macOS Logging
ULS events, Apple System Logs ...
By Industry
Financial Services
Government & Education
Entertainment & Gambling
Telecommunications
Medical & Healthcare
Military & Defense
Law Firms & Legal Counsel
Industrial & Manufacturing
Find a Reseller
Look for our resellers worldwide
Technology Ecosystem
See all our partners and integrations
Partner Program
Join our community of partners
Documentation
Products guides and integrations
Blog
Tutorials, updates and releases
White papers
Datasheets, infographics and more
Videos
Trainings and tutorial on specific topics
Webinars
Community events and webinars
Case Studies
Customer success stories
Community Forum →
About Us
Our journey, team and mission
Customers
Testimonials and case studies
Careers
We are hiring!
Contact Us →
Request trial
  • Loading...
Request Trial
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!

GET STARTED TODAY:
CONTACT US Our experts are happy to help REQUEST A FREE TRIAL Give NXLog Enterprise Edition a try GET PRICING Request a quote
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

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.

  • python
  • perl
  • ruby
  • alerts
Share

Facebook Twitter LinkedIn Reddit Mail
Related Posts

DNS Log Collection on Windows
9 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

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 2023 NXLog Ltd.

PRIVACY POLICY TERMS OF USE

  • PRODUCTS
  • NXLOG ENTERPRISE EDITION
  • NXLOG COMMUNITY EDITION
  • NXLOG ADD-ONS
  • NXLOG MANAGER
  • NXLOG MINDER
  • RAIJIN DATABASE
  • MORE NXLOG
  • COMPARE SOLUTIONS
  • INDUSTRIES
  • INTERGRATIONS
  • FIND A RESELLER
  • PARTNER PROGRAM
  • RESOURCES
  • DOCUMENTATION
  • WHITE PAPERS
  • WEBINARS
  • CASE STUDIES
  • TUTORIALS
  • BLOG
  • COMMUNITY FORUM
  • ABOUT US
  • WHY NXLOG
  • CUSTOMERS
  • CAREERS
  • CONTACT US
  • DOWNLOADS
  • NXLOG ENTERPRISE EDITION
  • NXLOG COMMUNITY EDITION
  • NXLOG MINDER
  • NXLOG MANAGER
  • NXLOG ADD-ONS
  • RAIJIN DATABASE