This guide will walk you through setting up a Nagios monitoring system on CentOS 7 using Vagrant and VirtualBox. We’ll configure a master server to monitor remote servers in a private network environment.
Vagrant Configuration
First, let’s define our virtual machines (VMs) using a Vagrantfile. This file contains the configuration for creating the VMs using Vagrant and VirtualBox.
Vagrant.configure("2") do |config|  
  config.vm.define "master" do |master|
    master.vm.hostname = "master"
    master.vm.box = "centos/7"
    master.vm.network "private_network", ip: "192.100.10.10"
    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 = "centos/7"
    remote.vm.network "private_network", ip: "192.100.10.20"
    remote.vm.provider "virtualbox" do |vb|
      vb.memory = "2048"
      vb.cpus = 2
    end
  end
endExplanation:
- Vagrant.configure("2") do |config|: Initializes the configuration block with version 2 of Vagrant’s configuration syntax.
- config.vm.define "master": Defines a VM named “master”. The hostname is set to “master” and it’s using the- centos/7box.
- master.vm.network "private_network", ip: "192.100.10.10": Assigns a private IP to the “master” VM.
- master.vm.provider "virtualbox" do |vb|: Specifies that VirtualBox is the provider and allocates 2048 MB of memory and 2 CPUs.
- The same steps are repeated for the “remote” VM, with an IP of 192.100.10.20.
Example:
After writing the above configuration, run vagrant up in your terminal to create and start the VMs. This command brings up both VMs based on the configuration defined in the Vagrantfile.
Configuring the Master Server
Once your VMs are up, SSH into the master server and begin setting it up.
vagrant ssh masterUpdate and Upgrade the System
Start by updating the system to ensure you have the latest packages:
sudo yum update
sudo yum upgradeExplanation:
- sudo yum update: Fetches the latest versions of the package metadata and updates the packages that have newer versions available.
- sudo yum upgrade: Upgrades the packages to their latest versions.
Example:
Always perform a system update before installing new software to ensure compatibility and security.
Install Required Packages
Next, install the necessary software packages for Nagios:
sudo yum install httpd php php-cli gcc unzip wget glibc glibc-common gd gd-devel net-snmp -y
sudo yum install glibc-common make openssl-devel xinetd vim -yExplanation:
- This command installs various packages required by Nagios, such as httpd(Apache web server),php(scripting language), andgcc(GNU Compiler Collection).
- The -yoption automatically answers “yes” to prompts during installation.
Example:
If you encounter dependency issues, use sudo yum clean all to clear the cache and try reinstalling.
Start the Apache Server
sudo service httpd startExplanation:
- This starts the Apache web server, which is needed to serve the Nagios web interface.
Create Nagios User and Group
sudo useradd nagios
sudo passwd nagios
password
sudo groupadd nagcmd
sudo usermod -a -G nagcmd nagios
sudo usermod -a -G nagcmd apacheExplanation:
- sudo useradd nagios: Adds a new user named “nagios”.
- sudo passwd nagios: Sets the password for the Nagios user.
- sudo groupadd nagcmd: Creates a new group named “nagcmd”.
- sudo usermod -a -G nagcmd nagios: Adds the Nagios user to the- nagcmdgroup.
- sudo usermod -a -G nagcmd apache: Adds the Apache user to the- nagcmdgroup.
Example:
Use the same commands to create additional users if needed, substituting “nagios” with the desired username.
Download and Install Nagios
cd /opt/
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 ./configure --with-command-group=nagcmd
sudo make all
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-webconfExplanation:
- The wgetcommand downloads the Nagios Core tarball.
- tar xzf: Extracts the tarball.
- ./configure --with-command-group=nagcmd: Configures the installation to use the- nagcmdgroup.
- makeand- make install: Compiles and installs Nagios.
Example:
After installation, you can verify it by running sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg.
Setting Up Nagios Web Interface
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
password
sudo service httpd restartExplanation:
- htpasswd -c: Creates a new Nagios web interface user.
- service httpd restart: Restarts Apache to apply the configuration changes.
Example:
Visit http://192.100.10.10/nagios/ in your browser to access the Nagios web interface.
Additional Configuration (Optional)
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --reload
sudo systemctl restart httpd
sudo systemctl restart nagios
sudo systemctl enable httpd
sudo systemctl enable nagiosExplanation:
- These commands configure the firewall to allow HTTP traffic, restart the services, and enable them to start on boot.
Example:
Use sudo systemctl status nagios to check if Nagios is running correctly.
Installing Nagios Plugins
cd /opt
sudo wget http://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
sudo make
sudo make installExplanation:
- Installs Nagios plugins, essential for monitoring different services.
Example:
To verify, run sudo /usr/local/nagios/libexec/check_nrpe -H localhost.
Final Steps
Ensure everything is working by starting the Nagios service:
sudo service nagios startAccess the Nagios web interface using your browser and log in with the credentials you created.





