In this project, we will create a network monitoring tool using Bash. This tool will help you monitor network activity, check connectivity, and log network statistics. By the end of this project, you will have a functional script that can be used to monitor network performance and troubleshoot network issues.

Objectives

  • Understand the basics of network monitoring.
  • Learn how to use common network monitoring commands in Bash.
  • Create a script to monitor network activity and log the results.
  • Implement error handling and logging.

Key Concepts

  • Network Monitoring: The process of observing network traffic and performance.
  • Ping: A command to check the connectivity between two nodes.
  • Traceroute: A command to trace the path packets take to reach a destination.
  • Netstat: A command to display network connections, routing tables, and interface statistics.
  • Logging: Recording the output of commands to a file for later analysis.

Step-by-Step Guide

Step 1: Setting Up the Script

Create a new Bash script file named network_monitor.sh:

#!/bin/bash
# network_monitor.sh - A simple network monitoring tool

# Ensure the script is executed with root privileges
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

# Log file location
LOG_FILE="/var/log/network_monitor.log"

# Function to log messages
log_message() {
    local MESSAGE=$1
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $MESSAGE" | tee -a $LOG_FILE
}

log_message "Network monitoring script started."

Step 2: Checking Connectivity with Ping

Add a function to check connectivity to a list of hosts using the ping command:

# List of hosts to monitor
HOSTS=("google.com" "github.com" "stackoverflow.com")

# Function to check connectivity
check_connectivity() {
    for HOST in "${HOSTS[@]}"; do
        if ping -c 1 $HOST &> /dev/null; then
            log_message "Ping to $HOST successful."
        else
            log_message "Ping to $HOST failed."
        fi
    done
}

check_connectivity

Step 3: Tracing the Route with Traceroute

Add a function to trace the route to a host using the traceroute command:

# Function to trace route
trace_route() {
    local HOST=$1
    log_message "Tracing route to $HOST"
    traceroute $HOST | tee -a $LOG_FILE
}

# Trace route to the first host in the list
trace_route ${HOSTS[0]}

Step 4: Displaying Network Statistics with Netstat

Add a function to display network statistics using the netstat command:

# Function to display network statistics
display_netstat() {
    log_message "Displaying network statistics"
    netstat -tuln | tee -a $LOG_FILE
}

display_netstat

Step 5: Scheduling the Script

To run the script at regular intervals, you can use cron. Edit the crontab file:

sudo crontab -e

Add the following line to run the script every 5 minutes:

*/5 * * * * /path/to/network_monitor.sh

Step 6: Error Handling and Logging

Ensure that all commands are logged and errors are handled gracefully:

# Function to check connectivity with error handling
check_connectivity() {
    for HOST in "${HOSTS[@]}"; do
        if ping -c 1 $HOST &> /dev/null; then
            log_message "Ping to $HOST successful."
        else
            log_message "Ping to $HOST failed."
        fi
    done
}

# Function to trace route with error handling
trace_route() {
    local HOST=$1
    log_message "Tracing route to $HOST"
    if traceroute $HOST &> /dev/null; then
        traceroute $HOST | tee -a $LOG_FILE
    else
        log_message "Traceroute to $HOST failed."
    fi
}

# Function to display network statistics with error handling
display_netstat() {
    log_message "Displaying network statistics"
    if netstat -tuln &> /dev/null; then
        netstat -tuln | tee -a $LOG_FILE
    else
        log_message "Failed to display network statistics."
    fi
}

Complete Script

Here is the complete network_monitor.sh script:

#!/bin/bash
# network_monitor.sh - A simple network monitoring tool

# Ensure the script is executed with root privileges
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

# Log file location
LOG_FILE="/var/log/network_monitor.log"

# Function to log messages
log_message() {
    local MESSAGE=$1
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $MESSAGE" | tee -a $LOG_FILE
}

log_message "Network monitoring script started."

# List of hosts to monitor
HOSTS=("google.com" "github.com" "stackoverflow.com")

# Function to check connectivity
check_connectivity() {
    for HOST in "${HOSTS[@]}"; do
        if ping -c 1 $HOST &> /dev/null; then
            log_message "Ping to $HOST successful."
        else
            log_message "Ping to $HOST failed."
        fi
    done
}

# Function to trace route
trace_route() {
    local HOST=$1
    log_message "Tracing route to $HOST"
    if traceroute $HOST &> /dev/null; then
        traceroute $HOST | tee -a $LOG_FILE
    else
        log_message "Traceroute to $HOST failed."
    fi
}

# Function to display network statistics
display_netstat() {
    log_message "Displaying network statistics"
    if netstat -tuln &> /dev/null; then
        netstat -tuln | tee -a $LOG_FILE
    else
        log_message "Failed to display network statistics."
    fi
}

# Run the functions
check_connectivity
trace_route ${HOSTS[0]}
display_netstat

log_message "Network monitoring script completed."

Summary

In this project, you learned how to create a network monitoring tool using Bash. You covered:

  • Checking connectivity with ping.
  • Tracing routes with traceroute.
  • Displaying network statistics with netstat.
  • Logging the output to a file.
  • Scheduling the script to run at regular intervals using cron.

This tool can be expanded with additional features such as monitoring specific ports, alerting on failures, and more. Use this as a foundation to build more complex network monitoring solutions.

© Copyright 2024. All rights reserved