Monitoring the list of hosts using shell script and write the status over log file and automate the script execution with crontab


Introduction:

In this blog, I am going to demonstrate the functionalities of shell script to monitor multiple hosts and write their status over a log file. The script will write the log for successful pings to one file and will write the log for failed pings to another file.

Requirements:

  • Access to Linux Machine/Linux machine hosted on virtual box/Linux configured on WSL/AWS EC2 Linux instance

Tasks:

  1. Generally, the status of the hosts can be checked with below command.
ping hostname

But the above command will keep on checking the status until it is terminated manually. Hence the below command is fine enough to check the host status.

ping -c 1 hostname #To ping once
ping -c 2 hostname #To ping twice

2. It might be difficult to check there status of the hosts manually and also if there are a huge list of hosts, it could be difficult to check there status and identify which is reachable successfully and which is not.

3. To resolve this issue, I have created a script which will check the get the host names from a file, will check the status of the hosts, if the status is successful, it will print the output to a file, if the status is not successful, it will print the output to another file.

4. I have developed the first step of the script define the file name and its location. If the file is not available, the script will get failed and the error log will be printed to a file.

hostfile="/root/shell_script/projects/hosts"
log_dir="/root/shell_script/projects"
if [[ ! -f $hostfile ]]
then
        echo "[$(date '+%d-%m-%Y %H:%M:%S')] The '$hostfile' doesn't exist!! Please create it along with the host names!!" >> hostfile_$(date +%d%m%Y).err
        exit 1

Note: In above script, “/root/shell_script/projects/hosts” is the file which will contain all hosts details and the logs will be saved to “/root/shell_script/projects” path.

5. My next block of code is defined to read the content of file and execute “ping” command for each line.

else
        for hosts in $(cat $hostfile)
        do
                ping -c 1 $hosts &> /dev/null

6. My next block of code is written to print the status of “ping” command with time stamp and a message “The host $hosts is successfully reachable” to a file host_status_$(date +%d%m%Y).log when ping is successful.

if [[ $? -eq 0 ]]
                then
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "Status of $hosts on $(date '+%d-%m-%Y %H:%M:%S')" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] $(ping -c 1 $hosts)" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] The host '$hosts' is successfully reachable" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).log

Note:

Here, “hosts” is a variable which contains the content of the hosts file

In file name host_status_$(date +%d%m%Y).log, the highlighted part refers to the timestamp for present day.

7. If the ping status is not successful, my next block of code will track the error and a message “The host $hosts is not reachable and needs investigation!!” to a file named host_status_$(date +%d%m%Y).err

else
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "Status of $hosts on $(date '+%d-%m-%Y %H:%M:%S')" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).err
                        ping -c 1 $hosts 2> $log_dir/error_$(date +%d%m%Y).err
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] $(cat error_$(date +%d%m%Y).err)" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] The host '$hosts' is not reachable and needs investigation!!" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).err
                fi

Note:

Here, “hosts” is a variable which contains the content of the hosts file

In file name host_status_$(date +%d%m%Y).err, the highlighted part refers to the timestamp for present day.

8. I have developed all the ascpects my code looks like below.

#!/bin/bash
#
#This script is created to check the status of hosts
#####################################################
hostfile="/root/shell_script/projects/hosts"
log_dir="/root/shell_script/projects"
if [[ ! -f $hostfile ]]
then
        echo "The '$hostfile' doesn't exist!! Please create it along with the host names!!" >> hostfile_$(date +%d%m%Y).err
        exit 1
else
        for hosts in $(cat $hostfile)
        do
                ping -c 1 $hosts &> /dev/null
                if [[ $? -eq 0 ]]
                then
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "Status of $hosts on $(date '+%d-%m-%Y %H:%M:%S')" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] $(ping -c 1 $hosts)" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] The host '$hosts' is successfully reachable" >> $log_dir/host_status_$(date +%d%m%Y).log
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).log
                else
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "Status of $hosts on $(date '+%d-%m-%Y %H:%M:%S')" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "**********************************************************" >> $log_dir/host_status_$(date +%d%m%Y).err
                        ping -c 1 $hosts 2> $log_dir/error_$(date +%d%m%Y).err
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] $(cat error_$(date +%d%m%Y).err)" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo "[$(date '+%d-%m-%Y %H:%M:%S')] The host '$hosts' is not reachable and needs investigation!!" >> $log_dir/host_status_$(date +%d%m%Y).err
                        echo " " >> $log_dir/host_status_$(date +%d%m%Y).err
                fi
        done
fi
rm -f $log_dir/error_$(date +%d%m%Y).err

9. Now it is time to test the code.

Testing the script without the file:

Testing the script with file name containing hosts:

The file which contains the host names exists

Script gets executed without any issue when the file with host names exists and two log files get generated

10. As it is seen, the script is working exactly as desired and printing the log to track the successful status and error in different files for further knowledge and troubleshooting.

11. The above script can be automated with crontab jobs and can be executed automatically in a defined schedule.

I have removed all the log files from the directory.

I have created the below crontab job to execute the script in every 00th minute.

We can see that the script has executed and the log files are generated.

Hence the script is getting executed automatically without any error in a defined time to fulfill the requirement.


Thanks for your time!! If you like my work and want to encourage me to publish more such content, you can buy me a coffee.