Disable SELinux

To disable SELinux according to the official guidelines, first obtain SELinux status, if enabled, switch to permissive mode.

Get SELinux status
getenforce
# Change to tolerant mode
sudo vi /etc/selinux/config
Change enforing to permissive
Copy the code

Install Nagios

  1. Install dependencies

    sudo yum install gcc glibc glibc-common wget unzip httpd php gd gd-devel perl postfix -y
  2. Download the Nagios Core source file

    CD ~ / && wget HTTP: / / https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.3.tar.gz
  3. Unzip the source file

    CD ~/ && tar -xzvf nagios-4.4.3.tar.gz
  4. Compiling source files
Switch to the source file root directory
cd~ / nagios - 4.4.3# configuration
./configure
# compile file
sudo make all
Copy the code
  1. Create Nagios users and groups, and change the default groups for Apache users

    sudo make install-groups-users && sudo usermod -a -G nagios apache
  2. The installationsudo make install
  3. Install Nagios as a service

    sudo make install-daemoninit && sudo systemctl enable httpd.service
  4. Install command line mode

    sudo make install-commandmode
  5. Installing configuration files

    sudo make install-config
  6. Install the Apache configuration file

    sudo make install-webconf
  7. Configuring the Firewall
Switch to user root
su root
Add port 80 to public. Select a single IP address or IP address range according to the actual situation
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.90" port port="80" protocol= TCP "accept' --permanent
firewall-cmd --reload
Switch to administrator
su kim
Copy the code
  1. Create a Nagios administrator userna_admin

Sudo htpasswd – c/usr/local/nagios/etc/htpasswd. Users na_admin need to enter the administrator password, enter a password again. After creating the system administrator, if you need additional to create other users, use the command sudo htpasswd/usr/local/nagios/etc/htpasswd. Users na_user, don’t use parameters – c or you will replace the original system administrator.

  1. Start the Apache service and Nagios service
# start Apache
sudo systemctl start httpd.service
# start Nagios
sudo systemctl start nagios.service
Copy the code
  1. Test the Nagios

    Open your local browser, type in the Nagios server’s domain name or IP address,http://ip/nagios

Install Nagios plugins

Nagios plug-in installation directory is/usr/local/Nagios/libexec /

  1. Install dependencies

    sudo yum install gcc glibc glibc-common make gettext automake autoconf wget openssl-devel net-snmp net-snmp-utils epel-release -y sudo yum install perl-Net-SNMP -y
  2. Download plugin
cd~ / wget - O nagios plugins - release - 2.2.1. Tar. Gz https://github.com/nagios-plugins/nagios-plugins/archive/release-2.2.1.tar.gz tar XZVF. - Nagios plugins - release - 2.2.1. Tar. GzCopy the code
  1. Compile the installation
cd~ / nagios plugins - release - 2.2.1# configuration
sudo ./tools/setup
./configure
# Use multithreaded compilation
sudo make -j10
# installation
sudo make install
Copy the code

Configure Nagios

The basic information

The Nagios configuration file is located in /usr/local/nagios/etc. The configuration file ends with. CFG. The main file is nagios.cfg. Check_external_comands controls whether to run Nagios directly through the Web interface. If the parameter is 1, Nagios is enabled. Nagios configuration files related to host and service in the/usr/local/Nagios/etc/object directory, about this machine at configuration file associated localhost. CFG. Check for Nagios configuration files can use sudo/usr/local/Nagios/bin/Nagios – v/usr/local/Nagios/etc/Nagios. CFG. The cgi.cfg file also needs to specify which users are allowed to run external commands. Add the newly added Nagios administrator user to the cgi. CFG file, or Nagios Web will prompt you with a user permission error.

Example Add a remote monitoring host

  1. Add a Linux server using the localhost template file:
Copy the localhost template
cd /usr/local/nagios/etc/object
sudo cp localhost.cfg v-centos.cfg
Edit the copied configuration file
sudo vi v-centos.cfg
Modify the file as required
# template, select or add from template.cfg as neededUse linux-server hostname V-centos address 192.168.0.91# HostGroup and service are modified as required
Copy the code
  1. Add the new configuration file tonagios.cfgIn the file

    echo "cfg_file=/usr/local/nagios/etc/objects/v-centos.cfg" > /usr/local/nagios/etc/nagios.cfg
  2. Check the Nagios configuration file

    sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
  3. The configuration file is correct. Restart the Nagios service

    sudo systemctl restart nagios
  4. Example Enable port 5666 on the remote host
su root
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.92" port port="5666" protocol=" TCP "Accept ' --permanent
firewall-cmd --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.0.92" port port="5666" protocol=" UDP "accept' --permanent
firewall-cmd -reload
Copy the code
  1. Open the Nagios Web page to view the new host
  2. Change the default port for checking SSH

    On our new host, the SSH port has been changed to 9022, and the default Nagios command uses the default port 22 for checking.

    The modification method is added after the command! -p 9022Can.
Modify the v-centos. CFG file
Check SSH service
define service {

    use                     local-service host_name v-centos service_description SSH check_command check_ssh! -p 9022 notifications_enabled 0 }# Restart Nagios service
sudo systemctl restart nagios
Copy the code

reference

  1. Install Nagios on CentOS7
  2. Nagiso remote monitor