How Do You Install Nagios on RedHat Linux Step-By-Step?

Problem scenario
You have a small AWS server (e.g., t2.micro with 1 GB of RAM) running RedHat Enterprise Linux.  How do you install Nagios on it?  You want thorough directions to install and configure the Nagios monitoring server on RHEL.

Solution
As root or a sudoer user, run a script with the following content:

# Written by continualintegration.com
​# This script installs Nagios.  There is some interactive portion at the beginning.  
# But it should be easy to install Nagios using the following.
# read in password for nagiosadmin
echo "Ensure you have a security group rule to allow inbound connections over port 80 from the IP address of the desktop that will access Nagios."
echo "********************************"
echo "Next..."
echo "Please enter the email address that will be sent a message when Nagios sends an alert: "
read youremail
echo "********************************"
echo "Next..."
echo "Please enter the password for nagiosadmin.  Remember that this password will sent be over the internet.  It will pass over plaintext if you follow this script.  This script is for educational and/or testing purposes.  It is advisable to use SSL for your Nagios instance (and its authentication).  The nagiosadmin user is an interactive user that logs in via a web browser.
***  Enter any password you want and press enter:"
read -s npass

yum -y update
yum -y install wget build-essential httpd php gcc glibc-common gd make net-snmp openssl-devel xinetd unzip sendmail php-gd 

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagios,nagcmd apache
cd /opt
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
tar -xzf nagios*.tar.gz
cd nagios-4.3.1

./configure --enable-command-args --with-nagios-user=nagios --with-nagios-group=nagios --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/x86_64-linux-gnu

make all
make install
make install-commandmode
make install-init
make install-config
make install-webconf
cd /opt
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
tar -xzf nagios-plugins*.tar.gz
cd nagios-plugins-2.2.1
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install
service xinetd restart
cd /usr/local/nagios/etc/
cp nagios.cfg nagios.cfg.bak
perl -i -pe 's/.*/cfg_dir=\/usr\/local\/nagios\/etc\/servers/ if $.==51' nagios.cfg
mkdir -p /usr/local/nagios/etc/servers

sed -i -e 's/nagios@localhost/$youremail/g' /usr/local/nagios/etc/objects/contacts.cfg
htpasswd -b -c /usr/local/nagios/etc/htpasswd.users nagiosadmin $npass

systemctl start nagios.service
#systemctl restart httpd.service # we leave this commented out purely as a reference
chkconfig nagios on
systemctl start nagios.service
echo "The error 'Failed to start nagios.service: Unit not found.' may be ignorable."
echo "You should be ready to open a web browser.  Use the external AWS IP address of the server in a URL like this: "
echo "http://x.x.x.x/nagios (where x.x.x.x is the external IP address of the AWS server)"

How Do You Install and Configure the NRPE Agent on Ubuntu?

Problem scenario
You want the Nagios Core server to monitor another AWS server.  How do you install and configure the NRPE agent on Ubuntu?

Solution
Follow these step-by-step directions if you are using AWS.  If the servers are not in AWS, the directions below may still work. These directions assume that the Nagios core server is running on Ubuntu in AWS. (If you want to know how to set up Nagios core on Ubuntu, see this posting.)  These directions also assume you have root access to the Ubuntu servers in AWS.

#1 (Done from Nagios core server) Obtain the internal IP address of the Nagios core server.  You do this by going to the back end of the Nagios server and issuing ifconfig.

#2 (Done from Nagios client; the server destined to have the NRPE agent.) Run this command on the Ubuntu server you want to be monitored by the Nagios core server:
sudo su -

#3 (Done from Nagios client; the server destined to have the NRPE agent.) Launch a script with this as the content where x.x.x.x is the IP address obtained in step #1.

apt-get -y update
apt-get -y install nagios-nrpe-server nagios-plugins
cd /etc/nagios/
cp nrpe.cfg nrpe.cfg.bak
cat nrpe.cfg.bak | sed -e 's/allowed_hosts=127.0.0.1/allowed_hosts=x.x.x.x/g' > nrpe.cfg
# The line above used to require perl as shown below:
#perl -i -pe 's/.*/server_address=x.x.x.x/ if $.==44' nrpe.cfg
​# /etc/nagios/nrpe.cfg has an allowed_hosts stanza
# This setting can support multiple Nagios core servers.  The IP addresses should be separated by a comma if you have more than one.

service nagios-nrpe-server restart
i=$(ifconfig | grep Bcast | awk '{FS=":"; print $2}' | grep -o '[.,0-9]*')
echo "Go back to the Nagios core server to monitor $i."
h=$(hostname)
echo "Also know that the hostname of this server to get the NRPE agent is $h"

#4 (Done from Nagios core server)  vi /usr/local/nagios/etc/servers/contint.cfg
The content below is what you'll need for this contint.cfg file.  You just have to change three parts:

i) Replace y.y.y.y with the IP address printed at the end of the script of #3 (the internal IP address of the Ubuntu server that will be monitored).

ii) Replace the "contint" in every spot below with the hostname found in step #3.

# Ubuntu Host configuration file.  (Largely taken from https://www.howtoforge.com/tutorial/ubuntu-nagios/)

define host {
        use                          linux-server
        host_name                    contint
        alias                                contint
        address                      y.y.y.y
        register                     1
}

define service {
      host_name                       contint
      service_description             PING
      check_command                   check_ping!100.0,20%!500.0,60%
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       contint
      service_description             Check Users
      check_command           check_local_users!20!50
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       contint
      service_description             Local Disk
      check_command                   check_local_disk!20%!10%!/
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       contint
      service_description             Check SSH
      check_command                   check_ssh
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

define service {
      host_name                       contint
      service_description             Total Process
      check_command                   check_local_procs!250!400!RSZDT
      max_check_attempts              2
      check_interval                  2
      retry_interval                  2
      check_period                    24x7
      check_freshness                 1
      contact_groups                  admins
      notification_interval           2
      notification_period             24x7
      notifications_enabled           1
      register                        1
}

#5 (Done from Nagios core server) 
Run this command:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

If an error is found in the output, correct the problem.  If zero errors are found, proceed to step #6.

#6  (Done from Nagios client; the server destined to have the NRPE agent.) 
Run this command:
sudo service nagios-nrpe-server restart

#7  (Done from Nagios core server) 
Run these commands:
sudo service apache2 restart
sudo service nagios restart

#8 Open a web browser, go to this address (where x.x.x.x is either the external IP address or the FQDN of the Nagios server):

http://x.x.x.x/nagios

You will be challenged for credentials via a pop-up. For the username, use "nagiosadmin" with no quotes. For the password, enter the password you entered when it was installing.

How Do You Install Nagios on Ubuntu Step-By-Step?

Problem scenario
You have a small AWS server (e.g., t2.nano with 0.5 GB of RAM) running Ubuntu.  How do you install Nagios on it?

Solution
1. As root or a sudoer user, run a script with the following content (sudo bash /tmp/install.sh):

# Written by continualintegration.com
​#This script installs Nagios.  There is some interactive portion at the beginning.  
# But it should be easy to install Nagios using the following.
# read in password for nagiosadmin
echo "Ensure you have a security group rule to allow inbound connections over port 80 from the IP address of the desktop that will access Nagios.  You can do this when the script is running."
echo "********************************"
echo "Next..."
echo "Please enter the email address that will be sent a message when Nagios sends an alert and press Enter: "
read youremail
echo "********************************"
echo "Next..."
echo "Please enter the password for nagiosadmin.  Remember that this password will sent be over the internet.  It will pass over plaintext if you follow this script.  This script is for educational and/or testing purposes.  It is advisable to use SSL for your Nagios instance (and its authentication).  The nagiosadmin user is an interactive user that logs in via a web browser.
***  Enter any password you want and press enter:"
read -s npass

apt-get update
apt-get -y install wget build-essential apache2 php apache2-mod-php7.0 php-gd libgd-dev sendmail unzip

useradd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagios,nagcmd www-data
cd /opt
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
tar -xzf nagios*.tar.gz
cd nagios-4.3.1
./configure --with-nagios-group=nagios --with-command-group=nagcmd
make all
make install
make install-commandmode
make install-init
make install-config
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/
chown -R nagios:nagios /usr/local/nagios/libexec/eventhandlers
cd /opt
wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
tar -xzf nagios-plugins*.tar.gz
cd nagios-plugins-2.2.1
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl
make
make install
cd /usr/local/nagios/etc/
cp nagios.cfg nagios.cfg.bak
perl -i -pe 's/.*/cfg_dir=\/usr\/local\/nagios\/etc\/servers/ if $.==51' nagios.cfg
mkdir -p /usr/local/nagios/etc/servers

sed -i -e 's/nagios@localhost/$youremail/g' /usr/local/nagios/etc/objects/contacts.cfg
a2enmod rewrite
a2enmod cgi
htpasswd -b -c /usr/local/nagios/etc/htpasswd.users nagiosadmin $npass
ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/

#service apache2 restart # we leave this commented out purely as a reference
telinit u # this is plan b
systemctl enable /run/systemd/generator.late/nagios.service
service nagios start
echo 'run sudo service apache2 restart'

2. Verify the installation with this command: sudo /usr/local/nagios/bin/nagios --version

3. Open a web browser, go to this address (where x.x.x.x is either the external IP address or the FQDN of the Nagios server):

http://x.x.x.x/nagios

You will be challenged for credentials via a pop-up. For the username, use "nagiosadmin" with no quotes. For the password, enter the password you entered when it was installing.

How Do You Troubleshoot This Error “javac: command not found”?

Problem scenario
You have Java installed. The "java -version" command returns data showing that Java was been installed correctly.  You try to use the javac command to compile a .java file.  You get this error:

-bash: javac: command not found

How do you compile your Java code when javac is not recognized?

Solution
As root, run this command:

apt-get install -y openjdk-7-jdk

You may or may not encounter errors.  These errors may be ignorable.

How Do You Change the MAC Address of Your Wireless Network Adapter on a Windows 7 Machine?

Problem scenario
You want to change the MAC address of your wireless network adapter (e.g., you want a WiFi network to see your laptop as a different or new machine).  How do you do this?

Solution
1.  Go to Control Panel -> Network and Internet

2.  Go to "Network and Sharing Center"

3.  Click on "Change adapter settings"

4.  Right-click on "Wireless Network Connection" and go to "Properties"

5.  Below and to the right of the "Networking" tab click the "Configure" button

6.   Click the "Advanced" tab

7.  Under "Property" click on "Locally Administered MAC Address"

8.  Check the radio button under "Value" if it is not checked. 

9.  Enter a new hexadecimal value (i.e., using numbers and letters from A through F).  

10.  Click "Ok."

How Do You Troubleshoot This Error with a PostgreSQL Front End Application “Connection refused … Is the server running on host “x.x.x.x” and accepting TCP/IP connections on port 5432″?

Problem scenario
With a SQL front end (e.g., pgAdmin or HeidiSQL) you get this error "could not connect to server: Connection refused 0x0000274D/10061) Is the server running on host "x.x.x.x" and accepting TCP/IP connections on port 5432?

You double checked you are trying to connect to the correct IP address (x.x.x.x).  You know port 5432 is not blocked.  You know the postgres service is listening on port 5432 on the destination server. How do you fix this problem?

Solution
With Postgres version 9 on Ubuntu 16.x, the "postmaster" service does not need to be running.  One of these below may solve the problem by itself.

Possible solution #1  Go to the server with Postgres.  Find the postgresql.conf file (e.g., with this find / -name postgresql.conf).  Find the listen_addresses stanza.  Make sure the server's own IP address is listed next to the "localhost," text separated by a space.  A common mistake is to have the IP address of a Postgres client.  The Postgres server's IP address is the one that is needed.  Here is how it should look where x.x.x.x is the IP address of the PostgreSQL server:

listen_addresses = 'localhost, x.x.x.x'      # Replace x.x.x.x with the IP address of the Postgres server.

Possible solution #2  Go to the /etc/hosts file.  Make sure that one IP address to DNS name maps "localhost" to the server's IP address.

Possible solution #3  Make sure that postgresql.conf file does not have this stanza:

tcpip_socket = true

It used to be necessary.  This stanza will now cause this "connection refused" error.  Remove this stanza if it is there.

Comprehending Being Sued for Online Postings (e.g., Frivolous Libel Lawsuits)

Worried about being sued for something you posted online?  You may need to educate yourself about the risks.

American citizens may want to understand the context of online defamation by buying Be Careful Who You SLAPP by Delfino and Day.  To learn about strategic lawsuits designed to silence people in the USA, there is no better book than SLAPPs: Being Sued for Speaking Out by Rock Pring.  For British citizens or those interested with English law, you may want to buy a copy of The Law of Defamation and the Internet.  It is best to be prepared!

To learn more about the law in general, we recommend a Nolo book.


Where Do You Find the IP Address from Most Recent Login on a Linux Server?

Problem scenario
You want to know where the Linux users are logging on from (e.g., for security and configuration reasons).  You want to know the IP addresses of their desktops.  Where do you find the IP address from most recent login on a Linux server?

Solution
To see the most recent login session details (including IP address), run this command:

last

After you press enter you'll see the most recent logins.  To find the source IP address of the users that are currently logged into a Linux server, run this command:

last | grep "still logged in" | awk '{print $3}'

How Do You Troubleshoot a False “State” Value in nmap Results?

Background
For troubleshooting networking issues with a Linux/Unix server, the nmap utility is extremely useful.  For Windows with PowerShell version 3, there is a script can help you almost as much as nmap; see this posting if you are interested.  For newer versions of PowerShell, use the Test-NetConnection command.  

Problem scenario
You use nmap to test a port on an IP address.  The results show the "State" as closed, but you know there are packets flowing through the IP address on this port.  How do you troubleshoot nmap with its "State" result is discrepant with other diagnostics?

Solution
If the IP address belongs to a server that has a VPN connection, there could be an apparent false result from nmap.  Some VPN tunnels are known to make the nmap results show the state of the IP address for a given port as closed.  This is tantamount to nmap reporting that there is no intermediate blockage to the remote server over the given port and that no packets are flowing through this socket.

Other VPNs on remote servers make the nmap show the state as "filtered" as the port you are testing is blocked by the VPN tunnel.  If you know there are TCP/IP packets from the IP address on the given port and you want nmap to accurately report this, use a VPN tunnel configured in such a way that the nmap command will report the "state" correctly.  Alternatively see if the VPN tunnel can be turned off for your nmap testing needs.

How Do You Connect to a Specific Postgres Database without a SQL Front End?

Problem scenario
From a Linux command prompt (e.g., Linux terminal) you want to connect to a specific Postgres database.  How do you create a table or issue SQL commands to a specific Postgres database from the back-end (the Postgres server itself)?

Solution
1.  First enter the Postgres command prompt by running these two commands:

sudo -i -u postgres
psql

2.  Assuming the database's name is cooldb, run this command:

\connect cooldb;

3.  Run your SQL commands. For example:

create table veryneat(id int, name varchar(40));
insert into veryneat values(1, 'words');