1
0
Fork 0

Compare commits

..

No commits in common. "main" and "main" have entirely different histories.
main ... main

3 changed files with 97 additions and 125 deletions

View File

@ -1,36 +1,44 @@
# Log Retrieval Script
# Swap File Creation Script
This script allows you to retrieve logs from a server based on a specified date and time range. It uses `journalctl` to extract logs within the provided time window, saving them to a file for easy access.
This script automatically creates a swap file on your system, allowing you to increase the available virtual memory. It calculates the optimal swap file size based on the available disk space, ensuring that the swap file does not exceed 30GB or 5% of the free disk space, whichever is smaller.
## Features
- Retrieves logs using `journalctl` from the specified start and end date/time.
- Automatically saves the logs to a file for future reference.
- Ensures logs are only retrieved within the given time frame.
- Checks if `journalctl` is available on the system before execution.
- Detects the main disk partition used by the system.
- Calculates the optimal swap file size (up to 30GB or 5% of the free space).
- Creates and activates a swap file at `/swapfile`.
- Optionally persists the swap file across reboots using the `--persist` flag.
- Displays the status of the created swap file.
### Usage
## Usage
To use this script, you can execute it directly from the URL without downloading it.
### Execute Directly from URL
Run the script directly from the URL using `curl` and piping to `bash`:
```bash
curl -sSL https://git.nixc.us/colin/pull-logs/raw/branch/main/pull-logs.sh | bash -s -- 2024-09-28 11:35 5
curl -sSL https://git.nixc.us/colin/swap-increase/raw/branch/main/swap.sh | bash
```
### Output
### Persist the Swap File Across Reboots
This script will:
1. Collect logs from `journalctl` within the 5-minute range before and after the specified target time (Saturday, September 28, 2024, at 11:35 am).
2. Search through relevant files in `/var/log/` (such as `syslog`, `auth.log`, and `kern.log`).
3. Check for logs from specific services (e.g., `nginx`, `apache2`, `mysql`).
4. Print all logs to `stdout` for easy piping into a file.
You can pipe the output to a file:
To ensure the swap file is persisted across reboots, run the script with the `--persist` flag:
```bash
curl -sSL https://git.nixc.us/colin/pull-logs/raw/branch/main/pull-logs.sh | bash -s -- 2024-09-28 11:35 5 > logs_output.txt
curl -sSL https://git.nixc.us/colin/swap-increase/raw/branch/main/swap.sh | bash -s -- --persist
```
### Notes
## Output
- The script assumes all logs follow a standard timestamp pattern and will adjust based on the service if necessary.
- If additional service logs are required, you can extend the `service_logs` array with the path to those log files.
The script will provide the following output:
- The calculated size of the swap file.
- Confirmation of swap file creation and activation.
- Whether the swap file will persist after reboot (based on the `--persist` flag).
- Information on the current swap status using `swapon --show`.
## Note
This script requires `sudo` privileges to create and activate the swap file, as well as to modify the `/etc/fstab` file for persistence across reboots when using the `--persist` flag.

View File

@ -1,104 +0,0 @@
#!/bin/bash
# Check if correct number of arguments are passed
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <start-time> <end-time>"
echo "Example: $0 '2024-09-28 11:30:00' '2024-09-28 11:40:00'"
exit 1
fi
START=$1
END=$2
# Debugging output to ensure correct start and end times
echo "START: $START"
echo "END: $END"
# Ensure that START is before END
if [[ $(date -d "$START" +%s) -ge $(date -d "$END" +%s) ]]; then
echo "Error: START time is greater than or equal to END time."
exit 1
fi
echo "Collecting logs from $START to $END"
# Temporary file to store logs
TEMP_LOG_FILE=$(mktemp)
# Function to check journalctl logs
get_journalctl_logs() {
if command -v journalctl &> /dev/null; then
echo "----- Journalctl Logs -----" >> "$TEMP_LOG_FILE"
journalctl --since="$START" --until="$END" >> "$TEMP_LOG_FILE"
else
echo "journalctl not found, skipping..." >> "$TEMP_LOG_FILE"
fi
}
# Function to check logs in /var/log
get_var_log_logs() {
echo "----- /var/log Logs -----" >> "$TEMP_LOG_FILE"
log_files=(/var/log/syslog /var/log/auth.log /var/log/kern.log /var/log/dmesg)
for log in "${log_files[@]}"; do
if [[ -f $log ]]; then
echo "Logs from $log" >> "$TEMP_LOG_FILE"
awk -v start="$START" -v end="$END" '{
logtime = $1 " " $2 " " $3
logtime_epoch = mktime(gensub(/-|:/, " ", "g", logtime))
start_epoch = mktime(gensub(/-|:/, " ", "g", start))
end_epoch = mktime(gensub(/-|:/, " ", "g", end))
if (logtime_epoch >= start_epoch && logtime_epoch <= end_epoch) {
print $0
}
}' $log >> "$TEMP_LOG_FILE"
else
echo "$log not found, skipping..." >> "$TEMP_LOG_FILE"
fi
done
}
# Function to check logs of specific services like nginx or apache2
get_service_logs() {
echo "----- Service Logs (nginx, apache, mysql, etc.) -----" >> "$TEMP_LOG_FILE"
service_logs=(/var/log/nginx/access.log /var/log/nginx/error.log /var/log/apache2/access.log /var/log/mysql/error.log)
for log in "${service_logs[@]}"; do
if [[ -f $log ]]; then
echo "Logs from $log" >> "$TEMP_LOG_FILE"
awk -v start="$START" -v end="$END" '{
logtime = $1 " " $2
logtime_epoch = mktime(gensub(/-|:/, " ", "g", logtime))
start_epoch = mktime(gensub(/-|:/, " ", "g", start))
end_epoch = mktime(gensub(/-|:/, " ", "g", end))
if (logtime_epoch >= start_epoch && logtime_epoch <= end_epoch) {
print $0
}
}' $log >> "$TEMP_LOG_FILE"
else
echo "$log not found, skipping..." >> "$TEMP_LOG_FILE"
fi
done
}
# Fetch logs
get_journalctl_logs
get_var_log_logs
get_service_logs
# Upload logs to hastebin server
if command -v curl &> /dev/null; then
RESPONSE=$(curl -s -X POST -H "Content-Type: text/plain" --data-binary "@${TEMP_LOG_FILE}" https://haste.nixc.us/documents)
if [[ $RESPONSE == *"key"* ]]; then
KEY=$(echo "$RESPONSE" | jq -r .key)
echo "Logs uploaded: https://haste.nixc.us/$KEY"
else
echo "Failed to upload logs to hastebin server."
fi
else
echo "curl not found, unable to upload logs."
fi
# Clean up temporary file
rm "$TEMP_LOG_FILE"

68
swap.sh Normal file
View File

@ -0,0 +1,68 @@
#!/bin/bash
# Function to create swap file
create_swapfile() {
# Detect the partition mounted on root ('/')
mount_point=$(df / | tail -1 | awk '{print $6}')
# Get available space on the root partition in MB
available_space=$(df -BM $mount_point | tail -1 | awk '{print $4}' | sed 's/M//')
# Calculate 5% of the available space in MB
swap_size=$((available_space * 5 / 100)) # 5% of available space in MB
# Convert swap size from MB to GB
swap_size=$((swap_size / 1024)) # Convert MB to GB
# Set upper limit of 30GB for the swap file
if [ $swap_size -gt 30 ]; then
swap_size=30
fi
# Abort if the calculated swap size is less than 1GB
if [ $swap_size -lt 1 ]; then
echo "Not enough disk space to create a swap file."
exit 1
fi
echo "Creating a ${swap_size}GB swap file at /swapfile..."
# Remove existing swapfile if it exists
if [ -f /swapfile ]; then
sudo swapoff /swapfile
sudo rm /swapfile
echo "Existing swapfile removed."
fi
# Create the swap file
sudo fallocate -l ${swap_size}G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "Swap file of ${swap_size}GB created and activated."
}
# Function to persist the swap file across reboots
persist_swapfile() {
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
echo "Swap file added to /etc/fstab."
}
# Check if --persist flag is passed
persist=false
if [ "$1" == "--persist" ]; then
persist=true
fi
# Main script execution
create_swapfile
if [ "$persist" = true ]; then
persist_swapfile
else
echo "Swap file will not persist after reboot."
fi
# Show current swap status
sudo swapon --show