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
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.
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
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
Write the Script:
Copy and paste the script content into the editor
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:
Provide detailed information about a specific container
(-d container_name)
Provide detailed configuration information for a specific domain
(-n domain)
Provide detailed information about a specific user (-u username)
Display activities within a specified time range (-t or --time)
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
- Update and Install Dependencies
- Updates the package lists for the Ubuntu system.
Installs Docker, Nginx, and logrotate if they are not already installed
Enable and Start Services
Enables Docker and Nginx services to start automatically on system boot.
Starts Docker and Nginx services immediately after installation.
Script Breakdown
- 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
- Installing Docker, Nginx, and Logrotate
- 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
- Enabling and Starting Docker Service
- systemctl enable docker: Configures Docker to start automatically when the system boots.
- systemctl start docker: Starts the Docker service immediately.
Ensuring Log-rotate is Installed
Installs log-rotate to manage and rotate log files, ensuring that logs do not consume excessive disk space
Usage
Make the Script Executable
Before running the script, ensure it has executable permissions
Run the Script
Execute the script to perform the installation and setup
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
Setup
Location of the Script:
The script is located at
/home/praisephs/server_monitoring/devopsfetch_monitor.sh
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"
How It Works
- Initialization:
- 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
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.
Location of the Log Rotation Script
The log rotation script is located at:
/home/praisephs/server_monitoring/manual_log_rotate.sh
How It Works
The log rotation script checks the size of the log file and rotates it if it exceeds a specified size
- 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
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
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.shLog 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
Confirmation of System Monitoring
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)