DEV Community

Uyi Oboite
Uyi Oboite

Posted on

Building Devopsfetch for Server Information Retrieval and Monitoring

Introduction

Devopsfetch is a Bash script that provides detailed information about system components, including active ports, Docker containers, Nginx configurations, user logins, and system activities. The script is used directly in the terminal, offering command-line access to monitor and retrieve system information, making it an essential tool for system administrators and DevOps engineers.

Creating the Script

  1. Navigate to Your Project Directory:
    First, you need to navigate to the directory where you want to create your script. You can do this using the cd command in the terminal.
    Image description

  2. Create the Script File:
    Use the touch command to create an empty file. This command will simply create the file without opening it for editing
    Image description

  3. Edit the File:
    Open the file with a text editor of your choice, such as nano, vim, or any other editor. Using vim editor in this case can be seen below
    Image description

  4. Write the Script:
    Copy and paste the script content into the editor
    Image description

  5. Save and Exit the Editor:
    If you’re using vim

  • Enter Insert Mode:
    Press i to enter insert mode, where you can start typing or paste
    your script content.
    Paste or Write the Script:
    Paste or write your script content while in insert mode.

  • Save and Exit:
    After entering the script content, follow these steps to save and
    exit:
    Press Esc to exit insert mode.
    Type :wq and press Enter.
    The :wq command writes (saves) the file and then quits vim.
    If you only want to save the file and not exit vim, use :w instead.
    To quit without saving, use :q!

Click this link for the content of the devopsfetch script

Sample Information Retrieval:

  1. Display all active ports and services (-p or --port)
    Image description

  2. Provide detailed information about a specific port (-p)
    Image description

  3. List all Docker images and containers (-d or --docker)
    Image description

  4. Provide detailed information about a specific container
    (-d container_name)
    Image description

  5. Display all Nginx domains and their ports (-n or --nginx)
    Image description

  6. Provide detailed configuration information for a specific domain
    (-n domain)
    Image description

  7. List all users and their last login times (-u or --users)
    Image description

  8. Provide detailed information about a specific user (-u username)
    Image description

  9. Display activities within a specified time range (-t or --time)
    Image description

Automating Dependency Installation and System Monitoring

The install_dependencies.sh Script

This script is designed to automate the installation and setup of essential system dependencies for a server. This script ensures that Docker, Nginx, and log-rotate are installed and properly configured to start on system boot

Script Overview

  1. Update and Install Dependencies
  2. Updates the package lists for the Ubuntu system.
  3. Installs Docker, Nginx, and logrotate if they are not already installed

  4. Enable and Start Services

  5. Enables Docker and Nginx services to start automatically on system boot.

  6. Starts Docker and Nginx services immediately after installation.

Script Breakdown

  1. Update and Install Dependencies
  • Updates the package lists for the Ubuntu system. This command updates the local package index with the latest changes made in the repositories. It ensures that you install the latest versions of the software packages

Image description

  1. Installing Docker, Nginx, and Logrotate Image description
  • docker.io: The package for Docker, which is used to manage containers.
  • nginx: The package for Nginx, which is a web server and reverse proxy server.
  • logrotate: The package for managing and rotating log files.
  • The -y flag automatically answers "yes" to any prompts during installation
  1. Enabling and Starting Docker Service Image description
  • systemctl enable docker: Configures Docker to start automatically when the system boots.
  • systemctl start docker: Starts the Docker service immediately.
  1. Enabling and Starting Nginx Service
    Image description

  2. Ensuring Log-rotate is Installed
    Installs log-rotate to manage and rotate log files, ensuring that logs do not consume excessive disk space
    Image description

Usage

  1. Make the Script Executable
    Before running the script, ensure it has executable permissions
    Image description

  2. Run the Script
    Execute the script to perform the installation and setup
    Image description

install_dependencies script

The devopsfetch_monitor.sh Script

A devopsfetch_monitor.sh script is designed to continuously monitor and log various system metrics and configurations. It collects information such as system details, CPU and memory usage, disk usage, active users, recent logins, open ports, and Nginx domain information. The collected data is then logged into a specified log file
Image description

Setup

  1. Location of the Script:
    The script is located at
    /home/praisephs/server_monitoring/devopsfetch_monitor.sh
    Image description

  2. Log File Configuration:
    The log file where the monitoring data is stored is specified in the script
    LOG_FILE="/home/praisephs/server_monitoring/devopsfetch_logs/devopsfetch.log"
    Image description

How It Works

  1. Initialization:
  • The script starts by defining the log file location and the format for timestamps:
    Image description

  • It logs the start of the monitoring process:
    Image description

  1. Continuous Monitoring Loop:
  • The script enters an infinite loop, where it performs the following actions at each iteration:

System Information Collection: It logs a timestamp and then collects various system metrics:

System Information: General system information is collected using the uname -a command

CPU and Memory Usage: Top processes by CPU and memory usage are listed using the top command

Disk Usage: Disk space usage for all mounted filesystems is collected using the df -h command

Memory Status: Memory usage and availability are logged using the free -h command

Active Users: Currently logged-in users are listed using the who command

Recent User Logins: Recent login attempts are logged using the
last -n 5 command

Open Ports: A list of open network ports is generated using the ss -tuln command

Nginx Domain Information: The script extracts domain information from Nginx configuration files located in /etc/nginx/sites-available/

  • Logging the Data: All collected data is appended to the specified log file in a well-structured format

  • Delay Between Iterations: The script waits for an hour before starting the next data collection cycle
    Image description

Link to devopsfetch_monitor,sh

Logging the Data: All collected data is appended to the specified log file in a well-structured format

Log Rotation
To manage the size of the log file and ensure the system does not run out of disk space, a manual log rotation script manual_log_rotate.sh is used.

  1. Location of the Log Rotation Script
    The log rotation script is located at:
    /home/praisephs/server_monitoring/manual_log_rotate.sh
    Image description

  2. How It Works
    The log rotation script checks the size of the log file and rotates it if it exceeds a specified size

  3. Log File and Maximum Size Configuration
    Image description

  • Log Size Check and Rotation The script checks if the log file size exceeds MAX_SIZE. If it does, the log file is renamed with a timestamp, and a new log file is created Image description

The log rotation script should is scheduled to run periodically using a cron job to ensure the log file does not grow too large

In simple terms, a cron job is a scheduled task that runs automatically at specific intervals on a Unix-based system (like Linux). You can think of it as a timer that triggers a particular command or script to execute at regular times, such as daily, weekly, or every hour

Usage
Starting the Monitoring Script
To start the monitoring, run the script using:
sudo /home/praisephs/server_monitoring/devopsfetch_monitor.sh

NB: Ensure that the script has execute permissions and is run with appropriate permissions to access all required system information

  1. Starting the Log Rotation Script
    The log rotation script can be run manually or scheduled to run periodically:
    sudo /home/praisephs/server_monitoring/manual_log_rotate.sh

  2. Log File Access
    The log file containing the monitoring data is located at:
    /home/praisephs/server_monitoring/devopsfetch_logs/devopsfetch.log

Users can access and review this log file to analyze system metrics and activity

To print the status of system monitoring use:
cat /home/praisephs/server_monitoring/devopsfetch_logs/devopsfetch.log
Image description

Confirmation of System Monitoring

Image description

Seeing both devopsfetch.log and devopsfetch.log.1.gz in devopsfetch_logs directory indicates that the system monitoring is working and data archiving is in place

  • devopsfetch.log: This is the current active log file where new log entries are appended

  • devopsfetch.log.1.gz: This is a compressed archive of the previous log file. The .gz extension indicates that it's been compressed using gzip

The presence of the archived log file (devopsfetch.log.1.gz) shows that log rotation is working, and old log data is being archived properly to save space and manage log files effectively.

Top comments (0)