nagios

How To Create Nagios Plugins With Ruby On Ubuntu

Nagios is an exceptional open-source monitoring system that helps organizations keep a close eye on their networks, servers, and applications. The power of Nagios isn’t just in its core functionality; its extensibility via plugins makes it a powerful tool for bespoke monitoring solutions. In this blog post, we’ll walk through how to create Nagios plugins using Ruby on Ubuntu, enabling you to extend your monitoring capabilities effortlessly.

Why Use Ruby for Nagios Plugins?

Ruby is a versatile, expressive programming language that is easy to read and write, making it an excellent choice for developing Nagios plugins. Its rich libraries and straightforward syntax allow for rapid development and flexibility in creating custom monitoring checks tailored to your specific needs.

Prerequisites

Before we dive into plugin creation, ensure you have the following:

  • A server or local machine running Ubuntu
  • Nagios installed and configured on your system
  • Basic familiarity with Ruby and the command line

Step 1: Install Ruby

If you haven’t previously installed Ruby, you can do so easily through the Ubuntu package manager. Open your terminal and execute the following commands:

sudo apt update
sudo apt install ruby-full

Verify the installation by checking the Ruby version:

ruby -v

Step 2: Set Up the Plugin Directory

Nagios typically expects plugins to reside in a specific directory. By default, this is /usr/lib/nagios/plugins or /usr/local/nagios/libexec. Choose one of these directories, or create your own if preferred.

sudo mkdir -p /usr/lib/nagios/plugins

Step 3: Creating Your First Ruby Plugin

Let’s create a simple Nagios plugin. This example will check if a specified URL is reachable, returning an OK status if it is, and a critical status if it isn’t.

  1. Open your text editor and create a new Ruby file:
sudo nano /usr/lib/nagios/plugins/check_url.rb
  1. Add the following code to check_url.rb:
#!/usr/bin/env ruby
require 'net/http'
require 'uri'

def check_url(url)
  uri = URI.parse(url)
  response = Net::HTTP.get_response(uri)

  case response.code.to_i
  when 200
    puts "OK - #{url} is reachable"
    exit 0
  when 404
    puts "Critical - #{url} is not found"
    exit 2
  else
    puts "Unknown - #{url} returned #{response.code}"
    exit 3
  end
end

if ARGV.length != 1
  puts "Usage: #{$0} <url>"
  exit 3
end

check_url(ARGV[0])
  1. Save and exit (if using nano, press CTRL + X, then Y, followed by Enter).

Step 4: Make Your Plugin Executable

Before you can use your plugin, you need to make it executable:

sudo chmod +x /usr/lib/nagios/plugins/check_url.rb

Step 5: Test the Plugin

Let’s test the newly created plugin from the command line. Simply provide a URL as an argument. For example:

/usr/lib/nagios/plugins/check_url.rb https://www.example.com

You should see output indicating whether the URL is reachable or not.

Step 6: Integrating the Plugin with Nagios

Now that your plugin works as expected, you need to integrate it into your Nagios monitoring configuration.

  1. Edit your Nagios commands configuration file (typically found at /usr/local/nagios/etc/commands.cfg or a similar location):
sudo nano /usr/local/nagios/etc/commands.cfg
  1. Add the following command definition:
define command {
    command_name check_url
    command_line /usr/lib/nagios/plugins/check_url.rb "$ARG1$"
}
  1. Next, define the service in a Nagios configuration file (such as localhost.cfg) to monitor the URL:
define service {
    use                 generic-service
    host_name           localhost
    service_description Check Example URL
    check_command       check_url!https://www.example.com
}
  1. Validate your Nagios configuration and restart the service:
sudo nagios -v /usr/local/nagios/etc/nagios.cfg
sudo systemctl restart nagios

Conclusion

Creating Nagios plugins with Ruby on Ubuntu is a straightforward process that allows you to customize your monitoring offering effectively. With just a few lines of Ruby code, you can harness the power of Nagios to monitor almost anything important to your infrastructure, ensuring you have timely alerts to address potential issues before they escalate.

As you gain experience, you can expand your knowledge and create more complex plugins tailored to your specific requirements. Happy coding!