DevOps

Setting Up Vagrant Machines and Installing Nagios on Ubuntu

This guide will walk you through setting up two Vagrant machines and installing Nagios, a powerful monitoring system, on an Ubuntu environment. We will cover the Vagrant configuration, software installation, and Nagios setup with detailed explanations and examples of command prompt instructions.

Vagrant Configuration

First, create a Vagrantfile to configure your virtual machines. The Vagrantfile defines two machines: master and remote, both running Ubuntu 18.04.

Vagrantfile Example

Vagrant.configure("2") do |config|

  config.vm.define "master" do |master|
    master.vm.hostname = "master"
    master.vm.box = "bento/ubuntu-18.04"
    master.vm.network "private_network", ip: "192.168.33.100"

    master.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
      vb.cpus = 2
    end
  end

  config.vm.define "remote" do |remote|
    remote.vm.hostname = "remote"
    remote.vm.box = "bento/ubuntu-18.04"
    remote.vm.network "private_network", ip: "192.168.33.200"

    remote.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
      vb.cpus = 2
    end
  end

end

This configuration sets up two virtual machines with specified hostnames, IP addresses, and resources (memory and CPUs).

Updating and Upgrading the System

Before installing any software, update and upgrade the system to ensure all packages are up-to-date.

sudo apt-get update
sudo apt-get upgrade
  • sudo apt-get update: Updates the package list.
  • sudo apt-get upgrade: Upgrades all installed packages to the latest version.

Installing Required Packages

Install the necessary packages for building Nagios and its plugins.

sudo apt-get install make wget build-essential unzip openssl libssl-dev -y
sudo apt-get install apache2 php libapache2-mod-php php-gd libgd-dev -y
sudo apt-get install autoconf gcc libc6 -y

These commands install essential development tools, Apache, PHP, and other dependencies required for Nagios.

Adding Nagios User and Group

Create a user and group for Nagios.

sudo adduser nagios
# Enter and confirm the password
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd www-data
  • sudo adduser nagios: Creates a new user named nagios.
  • sudo groupadd nagcmd: Creates a new group named nagcmd.
  • sudo usermod -a -G nagcmd nagios: Adds nagios user to the nagcmd group.
  • sudo usermod -a -G nagcmd www-data: Adds www-data (Apache user) to the nagcmd group.

Preparing for Nagios Installation

Create a directory for Nagios installation files and verify GCC installation.

cd /vagrant
sudo mkdir ng-install
cd ng-install
gcc -v
  • cd /vagrant: Change to the shared Vagrant directory.
  • sudo mkdir ng-install: Create a directory for installation files.
  • cd ng-install: Change to the new directory.
  • gcc -v: Verify GCC installation.

Downloading and Extracting Nagios

Download and extract the Nagios source files.

sudo wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.5.tar.gz
sudo tar xzf nagios-4.4.5.tar.gz
cd nagios-4.4.5
  • sudo wget ...: Downloads the Nagios tarball.
  • sudo tar xzf ...: Extracts the tarball.
  • cd nagios-4.4.5: Change to the Nagios source directory.

Configuring and Installing Nagios

Configure and install Nagios.

sudo ./configure --with-command-group=nagcmd \
--with-httpd-conf=/etc/apache2/sites-available \
--prefix=/usr/local/nagios --sysconfdir=/etc/nagios \
--host=arm

sudo make all
sudo make install-groups-users
sudo make install
sudo make install-init
sudo make install-daemoninit
sudo make install-config
sudo make install-commandmode
sudo make install-exfoliation
sudo make install-webconf
  • sudo ./configure ...: Configures Nagios with specified options.
  • sudo make all: Compiles the Nagios source code.
  • sudo make install-groups-users: Creates required users and groups.
  • sudo make install: Installs Nagios binaries.
  • sudo make install-init: Installs the init script.
  • sudo make install-daemoninit: Installs the daemon initialization script.
  • sudo make install-config: Installs sample configuration files.
  • sudo make install-commandmode: Configures the permissions on the external command directory.
  • sudo make install-exfoliation: Installs the Exfoliation theme for the Nagios web interface.
  • sudo make install-webconf: Installs the Apache configuration files for Nagios.

Setting Up Event Handlers and Apache Modules

Copy event handlers and enable necessary Apache modules.

sudo cp -R contrib/eventhandlers/ /usr/local/nagios/libexec
sudo chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers

sudo a2enmod cgi
sudo a2enmod rewrite
  • sudo cp -R ...: Copies event handlers to the appropriate directory.
  • sudo chown -R ...: Changes ownership of the event handlers.
  • sudo a2enmod cgi: Enables the CGI module in Apache.
  • sudo a2enmod rewrite: Enables the rewrite module in Apache.

Starting Services

Reload the systemd manager configuration, start, and enable Apache and Nagios services.

sudo systemctl daemon-reload
sudo systemctl start apache2
sudo systemctl start nagios
sudo systemctl enable apache2
sudo systemctl enable nagios
  • sudo systemctl daemon-reload: Reloads the systemd manager configuration.
  • sudo systemctl start ...: Starts the specified services.
  • sudo systemctl enable ...: Enables the specified services to start on boot.

Setting Up Nagios Web Interface

Create an admin user for the Nagios web interface and restart services.

sudo htpasswd -c /etc/nagios/htpasswd.users nagiosadmin
# Enter and confirm the password

sudo systemctl restart apache2
sudo systemctl restart nagios
  • sudo htpasswd -c ...: Creates a new HTTP basic authentication user for Nagios.
  • sudo systemctl restart ...: Restarts Apache and Nagios services.

Installing Nagios Plugins

Download, extract, configure, and install Nagios plugins.

cd /vagrant/ng-install
sudo wget http://www.nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
sudo tar xzf nagios-plugins-2.2.1.tar.gz
cd nagios-plugins-2.2.1

sudo ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
sudo make
sudo make install
  • sudo wget ...: Downloads the Nagios plugins tarball.
  • sudo tar xzf ...: Extracts the tarball.
  • cd nagios-plugins-2.2.1: Change to the Nagios plugins source directory.
  • sudo ./configure ...: Configures the plugins with specified options.
  • sudo make: Compiles the plugins.
  • sudo make install: Installs the plugins.

Configuring Firewall

Allow Apache through the firewall and reload the firewall rules.

sudo ufw allow Apache
sudo ufw reload
  • sudo ufw allow Apache: Allows Apache through the firewall.
  • sudo ufw reload: Reloads the firewall rules.

Accessing Nagios Web Interface

Create an admin user for the Nagios web interface.

sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
# Enter and confirm the password

Access the Nagios web interface by navigating to http://192.168.33.100/nagios/ and log in using the username nagiosadmin and the password you set earlier.

Conclusion

By following this guide, you will have a fully functional Nagios setup on your Vagrant environment. This setup will help you monitor your systems effectively.

Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply