DEV Community

akintola abdulazeez oladele
akintola abdulazeez oladele

Posted on

DevOpsFetch: A system monitoring and management tool.

Introduction

Welcome to DevOpsFetch, your new best friend in system monitoring and management. Picture this: You’re a DevOps engineer managing lot of servers, multitasking between Docker containers, Nginx configurations, and user activities. It’s a typical day, and you’re bombarded with requests for information—active ports, container statuses, and more.

Instead of desperately typing away commands to gather this information, you can rely on DevOpsFetch to streamline the process. This script is designed to simplify your life by retrieving and logging various system information including active ports, Docker images and containers, Nginx domains, user logins, and system activities within a specified time range.

What the Script Does

DevOpsFetch is a comprehensive script that automates the retrieval and logging of critical system information. Here’s a detailed breakdown of its functionality:

Log File Creation and Permissions:

  • The script first checks if the log file (/var/log/devopsfetch.log) exists.

  • If it doesn’t exist, it creates the log file and sets the necessary permissions to allow writing.

LOG_FILE="/var/log/devopsfetch.log"

#Create the log file if it doesn't exist and set permissions

if [ ! -f "$LOG_FILE" ]; then
    sudo touch "$LOG_FILE"
    sudo chmod 666 "$LOG_FILE"
fi
Enter fullscreen mode Exit fullscreen mode

Help Display:

  • Provides a help message (--help or -h) that shows usage instructions and available options.
show_help() {
    echo "Usage: $0 [OPTIONS]"
    echo "Options:"
    echo "  -p, --port                List all active ports and services"
    echo "  -p <port_number>          Detailed information about a specific port"
    echo "  -d, --docker              List all Docker images and containers"
    echo "  -d <container_name>       Detailed information about a specific container"
    echo "  -n, --nginx               Display all Nginx domains and their ports"
    echo "  -n <domain>               Detailed configuration information for a specific domain"
    echo "  -u, --users               List all users and their last login times"
    echo "  -u <username>             Detailed information about a specific user"
    echo "  -t, --time <start> <end>  Display activities within a specified time range"
    echo "  --install                 Install dependencies and set up the service"
    echo "  -h, --help                Show this help message"
}
Enter fullscreen mode Exit fullscreen mode

Logging Mechanism:

  • Defines a function (log_message) to log messages to the log file, ensuring all output is consistently recorded.
log_message() {
    echo "$1" | sudo tee -a "$LOG_FILE"
}
Enter fullscreen mode Exit fullscreen mode

Port Information:

  • Lists all active ports and services (--port or -p).

  • Provides detailed information about a specific port (-p <port_number>).

list_ports() {
    log_message "Listing all active ports and services..."
    netstat -tuln | sudo tee -a "$LOG_FILE"
}

port_details() {
    local port=$1
    log_message "Detailed information about port $port"
    netstat -tuln | grep ":$port" | sudo tee -a "$LOG_FILE"
}
Enter fullscreen mode Exit fullscreen mode

Docker Information:

  • Lists all Docker images and containers (--docker or -d).

  • Provides detailed information about a specific container (-d <container_name>).

list_docker() {
    log_message "Listing all Docker images..."
    docker images | sudo tee -a "$LOG_FILE"
    log_message "Listing all Docker containers..."
    docker ps -a | sudo tee -a "$LOG_FILE"
}

container_details() {
    local container_name=$1
    log_message "Detailed information about container $container_name..."
    docker inspect "$container_name" | sudo tee -a "$LOG_FILE"
}
Enter fullscreen mode Exit fullscreen mode

Nginx Information:

  • Displays all Nginx domains and their ports (--nginx or -n).

  • Provides detailed configuration information for a specific domain (-n <domain>).

domain_details() {
    local domain=$1
    log_message "Detailed configuration for domain $domain..."
    grep -r "server_name $domain" /etc/nginx/sites-enabled/ | sudo tee -a "$LOG_FILE"
}
Enter fullscreen mode Exit fullscreen mode

User Information:

  • Lists all users and their last login times (--users or -u).

  • Provides detailed information about a specific user (-u <username>).

list_users() {
    log_message "Listing all users and their last login times..."
    lastlog | sudo tee -a "$LOG_FILE"
}

user_details() {
    local username=$1
    log_message "Detailed information about user $username..."
    lastlog | grep "$username" | sudo tee -a "$LOG_FILE"
}
Enter fullscreen mode Exit fullscreen mode

Time-Range Activities:

  • Displays system activities within a specified time range (--time <start> <end>).
filter_by_time_range() {
    local start_time=$1
    local end_time=$2
    log_message "Activities from $start_time to $end_time..."
    journalctl --since="$start_time" --until="$end_time" | sudo tee -a "$LOG_FILE"
}
Enter fullscreen mode Exit fullscreen mode

Installation:

  • Installs necessary dependencies and sets up the systemd service for DevOpsFetch (--install).
if [ "$1" == "--install" ]; then
    log_message "Installing dependencies and setting up the service..."
    sudo apt-get update
    sudo apt-get install -y net-tools nginx docker.io
    sudo systemctl enable docker
    sudo systemctl start docker

    sudo tee /etc/systemd/system/devopsfetch.service > /dev/null <<EOL
[Unit]
Description=devopsfetch - DevOps System Information Retrieval Tool
After=network.target

[Service]
ExecStart=/home/hayzedak/HNG5/devopsfetch.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOL

    sudo systemctl daemon-reload
    sudo systemctl enable devopsfetch
    sudo systemctl start devopsfetch
Enter fullscreen mode Exit fullscreen mode

Main Script Logic:

  • Handles the different options provided by the user and calls the appropriate functions.
elif [ "$1" == "-p" ] || [ "$1" == "--port" ]; then
    if [ -n "$2" ]; then
        port_details "$2"
    else
        list_ports
    fi
elif [ "$1" == "-d" ] || [ "$1" == "--docker" ]; then
    if [ -n "$2" ]; then
        container_details "$2"
    else
        list_docker
    fi
elif [ "$1" == "-n" ] || [ "$1" == "--nginx" ]; then
    if [ -n "$2" ]; then
        domain_details "$2"
    else
        grep -r "server_name" /etc/nginx/sites-enabled/ | sudo tee -a "$LOG_FILE"
    fi
elif [ "$1" == "-u" ] || [ "$1" == "--users" ]; then
    if [ -n "$2" ]; then
        user_details "$2"
    else
        list_users
    fi
elif [ "$1" == "-t" ] || [ "$1" == "--time" ]; then
    if [ -n "$2" ] && [ -n "$3" ]; then
        filter_by_time_range "$2" "$3"
    else
        echo "Please provide both start and end times in the format 'YYYY-MM-DD HH:MM:SS'"
    fi
elif [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    show_help
else
    echo "Unknown option: $1"
    show_help
fi

Enter fullscreen mode Exit fullscreen mode

Installation and Configuration

Fork or Clone This Repo

Fork or clone the DevOpsFetch repository to your local machine here.

You can choose to place the script in the root directory /usr/local/bin/devopsfetch.sh and set the ExecStart path to /etc/systemd/system/devopsfetch.service or save the script to your desired location, such as /home/hayzedak/HNG5/devopsfetch.sh, thereby making similar changes to the ExecStart path.

Make the Script Executable

To ensure the script can be executed, change its permissions:

sudo chmod +x /path/to/devopsfetch.sh
Enter fullscreen mode Exit fullscreen mode

Install Dependencies and Set Up the Service

Run the script with the --install option to install necessary dependencies and set up the systemd service:

./devopsfetch.sh --install
Enter fullscreen mode Exit fullscreen mode

This command will:

  • Update the package list.
  • Install net-tools, nginx, and docker.io.
  • Enable and start the Docker service.
  • Create a systemd service file for DevOpsFetch.
  • Enable and start the devopsfetch service.

Enable and Start the Service

If you need to enable and start the service manually:

sudo systemctl daemon-reload
sudo systemctl enable devopsfetch
sudo systemctl start devopsfetch
Enter fullscreen mode Exit fullscreen mode

Usage

Run the script with different options to retrieve specific system information.

./devopsfetch.sh [OPTIONS]
Enter fullscreen mode Exit fullscreen mode

Options

  • -p, --port: List all active ports and services.
  • -p <port_number>: Detailed information about a specific port.
  • -d, --docker: List all Docker images and containers.
  • -d <container_name>: Detailed information about a specific container.
  • -n, --nginx: Display all Nginx domains and their ports.
  • -n <domain>: Detailed configuration information for a specific domain.
  • -u, --users: List all users and their last login times.
  • -u <username>: Detailed information about a specific user.
  • -t, --time <start> <end>: Display activities within a specified time range.
  • --install: Install dependencies and set up the service.
  • -h, --help: Show the help message.

Examples

  • List all active ports and services:
./devopsfetch.sh --port
Enter fullscreen mode Exit fullscreen mode

-Get detailed information about a specific port:

./devopsfetch.sh --port 80
Enter fullscreen mode Exit fullscreen mode
  • List all Docker images and containers:
./devopsfetch.sh --docker

Enter fullscreen mode Exit fullscreen mode
  • Get detailed information about a specific container:
./devopsfetch.sh --docker my_container
Enter fullscreen mode Exit fullscreen mode
  • Display all Nginx domains and their ports:
./devopsfetch.sh --nginx
Enter fullscreen mode Exit fullscreen mode
  • Get detailed configuration information for a specific domain:
./devopsfetch.sh --nginx mydomain.com
Enter fullscreen mode Exit fullscreen mode
  • List all users and their last login times:
./devopsfetch.sh --users
Enter fullscreen mode Exit fullscreen mode
  • Get detailed information about a specific user:
./devopsfetch.sh -u username
Enter fullscreen mode Exit fullscreen mode
  • Display activities within a specified time range:
./devopsfetch.sh --time "2024-08-01 00:00:00" "2024-08-01 23:59:59"
Enter fullscreen mode Exit fullscreen mode
  • Show the help message:
./devopsfetch.sh --help
Enter fullscreen mode Exit fullscreen mode

Logging Mechanism

Log File

All output from the script is logged to /var/log/devopsfetch.log. This log file is created and its permissions are set to ensure it can be written to by the script.

Viewing Logs

To view the log file, use:

sudo cat /var/log/devopsfetch.log
Enter fullscreen mode Exit fullscreen mode

To continuously monitor the log file, use:

sudo tail -f /var/log/devopsfetch.log
Enter fullscreen mode Exit fullscreen mode

Conclusion

This is a useful tool designed to make the life of a DevOps engineer easier by automating the retrieval and logging of important system information. Whether you're monitoring ports, managing Docker containers, configuring Nginx, or tracking user activities, it has you covered. Its simple yet effective logging mechanism ensures that all actions are recorded, providing a comprehensive audit trail for your system's activities.

By automating these tasks, you can save valuable time and reduce the risk of human error, allowing you to focus on more critical aspects of your work. It is easily configurable and extensible, integrating seamlessly with your existing workflows.

Feel free to reach out if you have any questions or need further assistance. Happy monitoring!

Top comments (0)