From d5deec2db97bb3690c69c23da327114d47cd0793 Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Fri, 30 Aug 2024 20:20:41 +0530 Subject: [PATCH 1/7] chore: Update SystemGuard app setup script and dependencies in crontab --- README.md | 4 +++- setup.sh | 10 ++++++++-- src/scripts/dashboard.sh | 2 -- 3 files changed, 11 insertions(+), 5 deletions(-) mode change 100644 => 100755 setup.sh diff --git a/README.md b/README.md index 6beaead..533eb76 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,9 @@ System Guard is a Flask app designed to monitor server stats such as CPU, Memory ## Installation ```bash -bash setup.sh +wget https://github.com/codeperfectplus/SystemGuard/blob/main/setup.sh +chmod +x setup.sh +./setup.sh ``` It will install the SystemGuard app and its dependencies in the crontab and it will be started automatically every time the server is restarted. The app will be available at `http://localhost:5050`. diff --git a/setup.sh b/setup.sh old mode 100644 new mode 100755 index db93885..2e32796 --- a/setup.sh +++ b/setup.sh @@ -9,9 +9,15 @@ DOWNLOAD_DIR="/tmp" EXTRACT_DIR="/home/$USER/.systemguard" -Prompt the user to enter the version of SystemGuard to install echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):" read VERSION +echo "Warning: This script will remove any existing installation of SystemGuard. Continue? (y/n)" +read CONFIRM + +if [ "$CONFIRM" != "y" ]; then + echo "Installation aborted." + exit 0 +fi # If the user enters "latest", fetch the latest version number from GitHub if [ "$VERSION" == "latest" ]; then @@ -91,4 +97,4 @@ chmod +x cronjob.sh echo "Executing the cronjob setup..." ./cronjob.sh -echo "SystemGuard version $VERSION installed successfully!" +echo "SystemGuard version $VERSION installed successfully!" \ No newline at end of file diff --git a/src/scripts/dashboard.sh b/src/scripts/dashboard.sh index 997857f..161eba4 100755 --- a/src/scripts/dashboard.sh +++ b/src/scripts/dashboard.sh @@ -7,9 +7,7 @@ log_message() { # Determine the directory where this script is located SCRIPT_DIR="$(dirname "$(realpath "$0" 2>/dev/null || readlink -f "$0")")" -# root directory of the project is two levels up PROJECT_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")" -# Define variables for paths relative to the script's directory FLASK_APP_PATH="${FLASK_APP_PATH:-$PROJECT_DIR/app.py}" REQUIREMENTS_FILE="${REQUIREMENTS_FILE:-$PROJECT_DIR/requirements.txt}" FLASK_PORT="${FLASK_PORT:-5000}" From 5fa2f098c9fe82a8fa1f100eb73a78f9fb97c99f Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Fri, 30 Aug 2024 20:26:19 +0530 Subject: [PATCH 2/7] chore: Update setup.sh URL in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 533eb76..2f36045 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ System Guard is a Flask app designed to monitor server stats such as CPU, Memory ## Installation ```bash -wget https://github.com/codeperfectplus/SystemGuard/blob/main/setup.sh +wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/setup.sh chmod +x setup.sh ./setup.sh ``` From eb7b170ab51843201ae84f1351a7a9dbcd1d214c Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Fri, 30 Aug 2024 22:43:30 +0530 Subject: [PATCH 3/7] chore: Update SystemGuard app setup script and dependencies in crontab --- README.md | 29 ++++++- installer.sh | 221 +++++++++++++++++++++++++++++++++++++++++++++++++++ setup.sh | 100 ----------------------- 3 files changed, 248 insertions(+), 102 deletions(-) create mode 100755 installer.sh delete mode 100755 setup.sh diff --git a/README.md b/README.md index 2f36045..d981515 100644 --- a/README.md +++ b/README.md @@ -6,10 +6,35 @@ System Guard is a Flask app designed to monitor server stats such as CPU, Memory ```bash wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/setup.sh -chmod +x setup.sh -./setup.sh +chmod +x installer.sh +sudo mv installer.sh /usr/local/bin/systemguard-installer ``` +### To install the SystemGuard app, run the following command: + +```bash +systemguard-installer --install +``` + +### To uninstall the SystemGuard app, run the following command: + +```bash +systemguard-installer --uninstall +``` + +### To Restore the SystemGuard app, run the following command: + +```bash +systemguard-installer --restore +``` + +### Help + +```bash +systemguard-installer --help +``` + + It will install the SystemGuard app and its dependencies in the crontab and it will be started automatically every time the server is restarted. The app will be available at `http://localhost:5050`. ## Dependencies(must be installed) diff --git a/installer.sh b/installer.sh new file mode 100755 index 0000000..3a247ea --- /dev/null +++ b/installer.sh @@ -0,0 +1,221 @@ +#!/bin/bash + +# SystemGuard Installer Script +# ---------------------------- +# This script installs, uninstalls, backs up, and restores SystemGuard by managing its installation, cleanup, and configuration. + +# Variables +DOWNLOAD_DIR="/tmp" +EXTRACT_DIR="/home/$USER/.systemguard" +LOG_DIR="$HOME/logs" +LOG_FILE="$LOG_DIR/systemguard-installer.log" +BACKUP_DIR="/home/$USER/.systemguard_backup" +EXECUTABLE="/usr/local/bin/systemguard-installer" + +# Create necessary directories +mkdir -p "$LOG_DIR" +mkdir -p "$BACKUP_DIR" + +# Logging function with timestamp +log() { + local message="$1" + echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE" +} + +# Backup function for existing configurations +backup_configs() { + log "Backing up existing configurations..." + if [ -d "$EXTRACT_DIR" ]; then + mkdir -p "$BACKUP_DIR" + cp -r "$EXTRACT_DIR" "$BACKUP_DIR/$(date '+%Y%m%d_%H%M%S')" + log "Backup completed: $BACKUP_DIR" + else + log "No existing installation found to back up." + fi +} + +# Restore function to restore from a backup +restore() { + log "Starting restore process..." + + # List available backups + if [ -d "$BACKUP_DIR" ]; then + echo "Available backups:" + select BACKUP in "$BACKUP_DIR"/*; do + if [ -n "$BACKUP" ]; then + echo "You selected: $BACKUP" + break + else + echo "Invalid selection. Please try again." + fi + done + + # Confirm restoration + echo "Are you sure you want to restore this backup? This will overwrite the current installation. (y/n)" + read CONFIRM + if [ "$CONFIRM" != "y" ]; then + log "Restore aborted by user." + exit 0 + fi + + # Remove existing installation + if [ -d "$EXTRACT_DIR" ]; then + rm -rf "$EXTRACT_DIR" + log "Old installation removed." + fi + + # Restore selected backup + cp -r "$BACKUP" "$EXTRACT_DIR" + log "Restore completed from backup: $BACKUP" + else + log "No backups found to restore." + echo "No backups found in $BACKUP_DIR." + fi +} + +# Install function +install() { + log "Starting installation of SystemGuard..." + echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):" + read VERSION + echo "Warning: This script will remove any existing installation of SystemGuard. Continue? (y/n)" + read CONFIRM + + if [ "$CONFIRM" != "y" ]; then + log "Installation aborted by user." + exit 0 + fi + + # Fetch latest version if specified + if [ "$VERSION" == "latest" ]; then + log "Fetching the latest version of SystemGuard from GitHub..." + VERSION=$(curl -s https://api.github.com/repos/codeperfectplus/SystemGuard/releases/latest | grep -Po '"tag_name": "\K.*?(?=")') + if [ -z "$VERSION" ]; then + log "Error: Unable to fetch the latest version. Please try again or specify a version manually." + exit 1 + fi + log "Latest version found: $VERSION" + fi + + # Define URL after determining the version + ZIP_URL="https://github.com/codeperfectplus/SystemGuard/archive/refs/tags/$VERSION.zip" + log "Installing SystemGuard version $VERSION..." + + # Download the SystemGuard zip file + wget -q $ZIP_URL -O $DOWNLOAD_DIR/systemguard.zip + if [ $? -ne 0 ]; then + log "Error: Failed to download SystemGuard version $VERSION. Please check the version number and try again." + exit 1 + fi + log "Download completed." + + # Backup existing configurations + backup_configs + + # Remove any existing installation of SystemGuard + log "Removing previous installation of SystemGuard, if any..." + if [ -d "$EXTRACT_DIR" ]; then + rm -rf "$EXTRACT_DIR" + log "Old installation removed." + fi + + # Clean up previous cron jobs related to SystemGuard + log "Cleaning up previous cron jobs related to SystemGuard..." + CRON_PATTERN=".systemguard/SystemGuard-.*/dashboard.sh" + if crontab -l | grep -q "$CRON_PATTERN"; then + crontab -l | grep -v "$CRON_PATTERN" | crontab - + log "Old cron jobs removed." + else + log "No previous cron jobs found." + fi + + # Create the extraction directory + log "Setting up installation directory..." + mkdir -p $EXTRACT_DIR + + # Extract the downloaded zip file + log "Extracting SystemGuard package..." + unzip -q $DOWNLOAD_DIR/systemguard.zip -d $EXTRACT_DIR + rm $DOWNLOAD_DIR/systemguard.zip + log "Extraction completed." + + # Navigate to the setup directory and execute setup script + log "Navigating to the SystemGuard setup directory..." + cd $EXTRACT_DIR/SystemGuard-*/src/scripts + if [ ! -f "cronjob.sh" ]; then + log "Error: cronjob.sh not found in the extracted directory. Please verify the contents." + exit 1 + fi + + # Make cronjob.sh executable and run it + log "Preparing cronjob script..." + chmod +x cronjob.sh + log "Executing the cronjob setup..." + ./cronjob.sh + + # Install the executable + log "Installing executable to /usr/local/bin/systemguard-installer..." + cp "$(basename "$0")" "$EXECUTABLE" + log "SystemGuard version $VERSION installed successfully!" +} + +# Uninstall function +uninstall() { + log "Uninstalling SystemGuard..." + + # Remove cron jobs related to SystemGuard + log "Cleaning up cron jobs related to SystemGuard..." + CRON_PATTERN=".systemguard/SystemGuard-.*/dashboard.sh" + if crontab -l | grep -q "$CRON_PATTERN"; then + crontab -l | grep -v "$CRON_PATTERN" | crontab - + log "Cron jobs removed." + else + log "No cron jobs found." + fi + + # Remove the SystemGuard installation directory + if [ -d "$EXTRACT_DIR" ]; then + rm -rf "$EXTRACT_DIR" + log "SystemGuard has been removed from your system." + else + log "SystemGuard is not installed on this system." + fi + + # Remove the executable + if [ -f "$EXECUTABLE" ]; then + rm "$EXECUTABLE" + log "Executable $EXECUTABLE removed." + else + log "No executable found to remove." + fi +} + +# Display help +show_help() { + echo "SystemGuard Installer" + echo "Usage: ./installer.sh [options]" + echo "Options:" + echo " --install Install SystemGuard" + echo " --uninstall Uninstall SystemGuard" + echo " --restore Restore SystemGuard from a backup" + echo " --help Display this help message" +} + +# Parse command-line options +for arg in "$@"; do + case $arg in + --install) ACTION="install" ;; + --uninstall) ACTION="uninstall" ;; + --restore) ACTION="restore" ;; + --help) show_help; exit 0 ;; + *) echo "Unknown option: $arg"; show_help; exit 1 ;; + esac +done + +# Execute based on the action specified +case $ACTION in + install) install ;; + uninstall) uninstall ;; + restore) restore ;; + *) echo "No action specified. Use --help for usage information." ;; +esac diff --git a/setup.sh b/setup.sh deleted file mode 100755 index 2e32796..0000000 --- a/setup.sh +++ /dev/null @@ -1,100 +0,0 @@ -#!/bin/bash - -# SystemGuard Installer Script -# ---------------------------- -# This script installs SystemGuard by downloading the specified version from GitHub, -# cleaning up any previous installations, and setting up the new version. - -# Variables -DOWNLOAD_DIR="/tmp" -EXTRACT_DIR="/home/$USER/.systemguard" - -echo "Enter the version of SystemGuard to install (e.g., v1.0.0 or 'latest' for the latest version):" -read VERSION -echo "Warning: This script will remove any existing installation of SystemGuard. Continue? (y/n)" -read CONFIRM - -if [ "$CONFIRM" != "y" ]; then - echo "Installation aborted." - exit 0 -fi - -# If the user enters "latest", fetch the latest version number from GitHub -if [ "$VERSION" == "latest" ]; then - echo "Fetching the latest version of SystemGuard from GitHub..." - VERSION=$(curl -s https://api.github.com/repos/codeperfectplus/SystemGuard/releases/latest | grep -Po '"tag_name": "\K.*?(?=")') - - # Check if fetching the latest version was successful - if [ -z "$VERSION" ]; then - echo "Error: Unable to fetch the latest version. Please try again or specify a version manually." - exit 1 - fi - echo "Latest version found: $VERSION" -fi - -# Define URL after determining the version -ZIP_URL="https://github.com/codeperfectplus/SystemGuard/archive/refs/tags/$VERSION.zip" - -# Inform the user about the installation process -echo "Installing SystemGuard version $VERSION..." -echo "Downloading SystemGuard package from GitHub..." - -# Download the SystemGuard zip file to the /tmp directory -wget -q $ZIP_URL -O $DOWNLOAD_DIR/systemguard.zip - -# Check if the download was successful -if [ $? -ne 0 ]; then - echo "Error: Failed to download SystemGuard version $VERSION. Please check the version number and try again." - exit 1 -fi -echo "Download completed." - -# Remove any existing installation of SystemGuard -echo "Removing previous installation of SystemGuard, if any..." -if [ -d "$EXTRACT_DIR" ]; then - rm -rf "$EXTRACT_DIR" - echo "Old installation removed." -fi - -# Remove specific cron jobs related to previous SystemGuard installations -echo "Cleaning up previous cron jobs related to SystemGuard..." -CRON_PATTERN=".systemguard/SystemGuard-.*/dashboard.sh" -if crontab -l | grep -q "$CRON_PATTERN"; then - # Remove only the lines that match the specific pattern - crontab -l | grep -v "$CRON_PATTERN" | crontab - - echo "Old cron jobs removed." -else - echo "No previous cron jobs found." -fi - -# Create the extraction directory -echo "Setting up installation directory..." -mkdir -p $EXTRACT_DIR - -# Extract the downloaded zip file to the target directory -echo "Extracting SystemGuard package..." -unzip -q $DOWNLOAD_DIR/systemguard.zip -d $EXTRACT_DIR - -# Remove the downloaded zip file to clean up -rm $DOWNLOAD_DIR/systemguard.zip -echo "Extraction completed." - -Navigate to the extracted SystemGuard directory -echo "Navigating to the SystemGuard setup directory..." -cd $EXTRACT_DIR/SystemGuard-*/src/scripts - -# Check if the cronjob.sh script exists in the extracted content -if [ ! -f "cronjob.sh" ]; then - echo "Error: cronjob.sh not found in the extracted directory. Please verify the contents." - exit 1 -fi - -# Make the cronjob.sh script executable -echo "Preparing cronjob script..." -chmod +x cronjob.sh - -# Execute the cronjob.sh script to set up cron jobs and complete installation -echo "Executing the cronjob setup..." -./cronjob.sh - -echo "SystemGuard version $VERSION installed successfully!" \ No newline at end of file From f97e1c55aff4ed6e1c8286e92c330ac5aa6d64cd Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Fri, 30 Aug 2024 22:58:48 +0530 Subject: [PATCH 4/7] chore: Update SystemGuard app setup script and dependencies in crontab --- README.md | 11 +++++------ installer.sh | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d981515..0169199 100644 --- a/README.md +++ b/README.md @@ -5,27 +5,26 @@ System Guard is a Flask app designed to monitor server stats such as CPU, Memory ## Installation ```bash -wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/setup.sh -chmod +x installer.sh -sudo mv installer.sh /usr/local/bin/systemguard-installer +wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/installer.sh +chmod +x installer.sh && sudo mv installer.sh /usr/local/bin/systemguard-installer ``` ### To install the SystemGuard app, run the following command: ```bash -systemguard-installer --install +sudo systemguard-installer --install ``` ### To uninstall the SystemGuard app, run the following command: ```bash -systemguard-installer --uninstall +sudo systemguard-installer --uninstall ``` ### To Restore the SystemGuard app, run the following command: ```bash -systemguard-installer --restore +sudo systemguard-installer --restore ``` ### Help diff --git a/installer.sh b/installer.sh index 3a247ea..919594b 100755 --- a/installer.sh +++ b/installer.sh @@ -155,7 +155,7 @@ install() { # Install the executable log "Installing executable to /usr/local/bin/systemguard-installer..." - cp "$(basename "$0")" "$EXECUTABLE" + # cp "$(basename "$0")" "$EXECUTABLE" log "SystemGuard version $VERSION installed successfully!" } From 7ec81f83564e81b85bc932e1d2c8d6847198366a Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Fri, 30 Aug 2024 23:00:09 +0530 Subject: [PATCH 5/7] chore: Update FLASK_PORT default value to 5050 --- src/scripts/dashboard.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/dashboard.sh b/src/scripts/dashboard.sh index 161eba4..6220c00 100755 --- a/src/scripts/dashboard.sh +++ b/src/scripts/dashboard.sh @@ -10,7 +10,7 @@ SCRIPT_DIR="$(dirname "$(realpath "$0" 2>/dev/null || readlink -f "$0")")" PROJECT_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")" FLASK_APP_PATH="${FLASK_APP_PATH:-$PROJECT_DIR/app.py}" REQUIREMENTS_FILE="${REQUIREMENTS_FILE:-$PROJECT_DIR/requirements.txt}" -FLASK_PORT="${FLASK_PORT:-5000}" +FLASK_PORT="${FLASK_PORT:-5050}" LOG_FILE="/home/$(whoami)/logs/systemdashboard_flask.log" USERNAME="$(whoami)" From daadb3f1627c47192b35e02aee55957dbc484a8e Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Sat, 31 Aug 2024 08:18:08 +0530 Subject: [PATCH 6/7] chore: Update SystemGuard app setup script and dependencies in crontab --- README.md | 4 +- load_testing.sh | 23 ++++++++ installer.sh => setup.sh | 68 ++++++++++++++++++++-- src/config.py | 2 +- src/scripts/cronjob.sh | 2 +- src/scripts/dashboard.sh | 2 +- locustfile.py => src/scripts/locustfile.py | 2 +- minify.py => src/scripts/minify.py | 0 8 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 load_testing.sh rename installer.sh => setup.sh (75%) rename locustfile.py => src/scripts/locustfile.py (94%) rename minify.py => src/scripts/minify.py (100%) diff --git a/README.md b/README.md index 0169199..5bfabfe 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,8 @@ System Guard is a Flask app designed to monitor server stats such as CPU, Memory ## Installation ```bash -wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/installer.sh -chmod +x installer.sh && sudo mv installer.sh /usr/local/bin/systemguard-installer +wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/setup.sh +chmod +x setup.sh && sudo mv setup.sh /usr/local/bin/systemguard-installer ``` ### To install the SystemGuard app, run the following command: diff --git a/load_testing.sh b/load_testing.sh new file mode 100644 index 0000000..665188c --- /dev/null +++ b/load_testing.sh @@ -0,0 +1,23 @@ + #!/bin/bash + + # Script to start Locust server + + # Define the path to your Locust file + LOCUST_FILE="src/scripts/locustfile.py" + # Define the host URL for Locust + HOST_URL="http://localhost:5050" + + # Check if Locust is installed + if ! command -v locust &> /dev/null + then + echo "Locust is not installed. Please install it first." + exit 1 + fi + + # Start Locust server + echo "Starting Locust server..." + locust -f "$LOCUST_FILE" --host="$HOST_URL" + + # Optionally, you can pass additional Locust flags here if needed + # For example, to run Locust in headless mode: + # locust -f "$LOCUST_FILE" --host="$HOST_URL" --headless -u 10 -r 1 --run-time 1m diff --git a/installer.sh b/setup.sh similarity index 75% rename from installer.sh rename to setup.sh index 919594b..c019061 100755 --- a/installer.sh +++ b/setup.sh @@ -2,16 +2,36 @@ # SystemGuard Installer Script # ---------------------------- -# This script installs, uninstalls, backs up, and restores SystemGuard by managing its installation, cleanup, and configuration. +# This script installs, uninstalls, backs up, restores SystemGuard, and includes load testing using Locust. -# Variables +# Determine the correct user's home directory +get_user_home() { + if [ -n "$SUDO_USER" ]; then + # When using sudo, SUDO_USER gives the original user who invoked sudo + TARGET_USER="$SUDO_USER" + else + # If not using sudo, use LOGNAME to find the current user + TARGET_USER="$LOGNAME" + fi + + # Get the home directory of the target user + USER_HOME=$(eval echo ~$TARGET_USER) + echo "$USER_HOME" +} + +# Set paths relative to the correct user's home directory +USER_HOME=$(get_user_home) DOWNLOAD_DIR="/tmp" -EXTRACT_DIR="/home/$USER/.systemguard" +EXTRACT_DIR="$USER_HOME/.systemguard" LOG_DIR="$HOME/logs" LOG_FILE="$LOG_DIR/systemguard-installer.log" -BACKUP_DIR="/home/$USER/.systemguard_backup" +BACKUP_DIR="$USER_HOME/.systemguard_backup" EXECUTABLE="/usr/local/bin/systemguard-installer" +LOCUST_FILE="$EXTRACT_DIR/SystemGuard-*/src/scripts/locustfile.py" +HOST_URL="http://localhost:5050" +INSTALLER_SCRIPT='setup.sh' +echo "User: $(whoami)" # Create necessary directories mkdir -p "$LOG_DIR" mkdir -p "$BACKUP_DIR" @@ -73,6 +93,23 @@ restore() { fi } +# Function to install the script as an executable +install_executable() { + # Use $0 to get the full path of the currently running script + # CURRENT_SCRIPT=$(realpath "$0") + cd $EXTRACT_DIR/SystemGuard-*/ + CURRENT_SCRIPT=$(pwd)/$INSTALLER_SCRIPT + echo "Current script: $CURRENT_SCRIPT" + # Verify that the script exists before attempting to copy + if [ -f "$CURRENT_SCRIPT" ]; then + log "Installing executable to /usr/local/bin/systemguard-installer..." + cp "$CURRENT_SCRIPT" "$EXECUTABLE" + log "Executable installed successfully." + else + log "Error: Script file not found. Cannot copy to /usr/local/bin." + fi +} + # Install function install() { log "Starting installation of SystemGuard..." @@ -155,7 +192,7 @@ install() { # Install the executable log "Installing executable to /usr/local/bin/systemguard-installer..." - # cp "$(basename "$0")" "$EXECUTABLE" + install_executable log "SystemGuard version $VERSION installed successfully!" } @@ -190,6 +227,24 @@ uninstall() { fi } +# Load test function to start Locust server +load_test() { + log "Starting Locust server for load testing..." + + # Check if Locust is installed + if ! command -v locust &> /dev/null + then + log "Locust is not installed. Please install it first." + exit 1 + fi + + # Start Locust server + log "Starting Locust server..." + locust -f "$LOCUST_FILE" --host="$HOST_URL" + # Optionally, you can pass additional Locust flags here if needed + # locust -f "$LOCUST_FILE" --host="$HOST_URL" --headless -u 10 -r 1 --run-time 1m +} + # Display help show_help() { echo "SystemGuard Installer" @@ -198,6 +253,7 @@ show_help() { echo " --install Install SystemGuard" echo " --uninstall Uninstall SystemGuard" echo " --restore Restore SystemGuard from a backup" + echo " --load-test Start Locust load testing" echo " --help Display this help message" } @@ -207,6 +263,7 @@ for arg in "$@"; do --install) ACTION="install" ;; --uninstall) ACTION="uninstall" ;; --restore) ACTION="restore" ;; + --load-test) ACTION="load_test" ;; --help) show_help; exit 0 ;; *) echo "Unknown option: $arg"; show_help; exit 1 ;; esac @@ -217,5 +274,6 @@ case $ACTION in install) install ;; uninstall) uninstall ;; restore) restore ;; + load_test) load_test ;; *) echo "No action specified. Use --help for usage information." ;; esac diff --git a/src/config.py b/src/config.py index 40b8c5d..74e8697 100644 --- a/src/config.py +++ b/src/config.py @@ -4,7 +4,7 @@ app = Flask(__name__) # Configure the SQLite database -app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dashboard.db' +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///systemguard.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SECRET_KEY'] = 'secret' diff --git a/src/scripts/cronjob.sh b/src/scripts/cronjob.sh index f204e94..a1c0074 100755 --- a/src/scripts/cronjob.sh +++ b/src/scripts/cronjob.sh @@ -4,7 +4,7 @@ username=$(whoami) log_dir="/home/$username/logs" mkdir -p "$log_dir" -CRON_JOB="* * * * * /bin/bash $(pwd)/dashboard.sh >> $log_dir/systemdashboard_cron.log 2>&1" +CRON_JOB="* * * * * /bin/bash $(pwd)/dashboard.sh >> $log_dir/systemguard_cron.log 2>&1" echo "Total cron jobs before: $(crontab -l | grep -v '^#' | wc -l)" diff --git a/src/scripts/dashboard.sh b/src/scripts/dashboard.sh index 6220c00..6c42ae5 100755 --- a/src/scripts/dashboard.sh +++ b/src/scripts/dashboard.sh @@ -11,7 +11,7 @@ PROJECT_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")" FLASK_APP_PATH="${FLASK_APP_PATH:-$PROJECT_DIR/app.py}" REQUIREMENTS_FILE="${REQUIREMENTS_FILE:-$PROJECT_DIR/requirements.txt}" FLASK_PORT="${FLASK_PORT:-5050}" -LOG_FILE="/home/$(whoami)/logs/systemdashboard_flask.log" +LOG_FILE="/home/$(whoami)/logs/systemguard_flask.log" USERNAME="$(whoami)" # Ensure log directory exists diff --git a/locustfile.py b/src/scripts/locustfile.py similarity index 94% rename from locustfile.py rename to src/scripts/locustfile.py index 2d07d4f..085e8cc 100644 --- a/locustfile.py +++ b/src/scripts/locustfile.py @@ -45,4 +45,4 @@ class WebsiteUser(HttpUser): if __name__ == "__main__": import os - os.system("locust -f locustfile.py --host=http://localhost:5000") + # os.system("locust -f locustfile.py --host=http://localhost:5000") diff --git a/minify.py b/src/scripts/minify.py similarity index 100% rename from minify.py rename to src/scripts/minify.py From d10b29962025c18665685320f5cd05e940c0f4d6 Mon Sep 17 00:00:00 2001 From: deepak-linux Date: Sat, 31 Aug 2024 11:24:55 +0530 Subject: [PATCH 7/7] chore: Update SystemGuard app setup script and dependencies in crontab --- README.md | 8 +++---- setup.sh | 9 ++++---- src/scripts/cronjob.sh | 51 ++++++++++++++++++++++++++---------------- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 5bfabfe..4ee8e62 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# SystemGuard +# SystemGuard 💂 System Guard is a Flask app designed to monitor server stats such as CPU, Memory, Disk, and Network. It also provides real-time monitoring capabilities which can be useful for system administrators, developers, and DevOps engineers to keep track of their server's performance and troubleshoot issues. The app uses the `psutil` library to retrieve system stats and the `speedtest-cli` library to perform a network speed test. -## Installation +## Installation 🛠️ ```bash wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/setup.sh @@ -40,7 +40,7 @@ It will install the SystemGuard app and its dependencies in the crontab and it w - Anaconda3/Miniconda3 -## Features +## Features 🚀 - Monitor server stats like CPU, Memory, Disk, and Network. - Check the network speed of the server using a speed test. @@ -52,7 +52,7 @@ It will install the SystemGuard app and its dependencies in the crontab and it w - Easy download and installation using a bash script. -## Product Screenshots +## Product Screenshots 📸 ### HomePage diff --git a/setup.sh b/setup.sh index c019061..4459e5a 100755 --- a/setup.sh +++ b/setup.sh @@ -22,7 +22,7 @@ get_user_home() { # Set paths relative to the correct user's home directory USER_HOME=$(get_user_home) DOWNLOAD_DIR="/tmp" -EXTRACT_DIR="$USER_HOME/.systemguard" +EXTRACT_DIR="$USER_HOME/.systemguard/" LOG_DIR="$HOME/logs" LOG_FILE="$LOG_DIR/systemguard-installer.log" BACKUP_DIR="$USER_HOME/.systemguard_backup" @@ -99,7 +99,6 @@ install_executable() { # CURRENT_SCRIPT=$(realpath "$0") cd $EXTRACT_DIR/SystemGuard-*/ CURRENT_SCRIPT=$(pwd)/$INSTALLER_SCRIPT - echo "Current script: $CURRENT_SCRIPT" # Verify that the script exists before attempting to copy if [ -f "$CURRENT_SCRIPT" ]; then log "Installing executable to /usr/local/bin/systemguard-installer..." @@ -139,12 +138,12 @@ install() { log "Installing SystemGuard version $VERSION..." # Download the SystemGuard zip file - wget -q $ZIP_URL -O $DOWNLOAD_DIR/systemguard.zip - if [ $? -ne 0 ]; then + log "Downloading SystemGuard version $VERSION from $ZIP_URL..." + if ! wget -q "$ZIP_URL" -O "$DOWNLOAD_DIR/systemguard.zip"; then log "Error: Failed to download SystemGuard version $VERSION. Please check the version number and try again." exit 1 fi - log "Download completed." + log "Download completed successfully." # Backup existing configurations backup_configs diff --git a/src/scripts/cronjob.sh b/src/scripts/cronjob.sh index a1c0074..4c7e7a3 100755 --- a/src/scripts/cronjob.sh +++ b/src/scripts/cronjob.sh @@ -1,21 +1,34 @@ #!/bin/bash -# Define the cron job command -username=$(whoami) -log_dir="/home/$username/logs" -mkdir -p "$log_dir" -CRON_JOB="* * * * * /bin/bash $(pwd)/dashboard.sh >> $log_dir/systemguard_cron.log 2>&1" - -echo "Total cron jobs before: $(crontab -l | grep -v '^#' | wc -l)" - -# Add the cron job to the crontab -(crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab - - -echo "Cron job added: $CRON_JOB" - -# show all cron jobs ignoring comments and empty lines - -echo "Total cron jobs after: $(crontab -l | grep -v '^#' | wc -l)" - - - +# Function to add a cron job with error handling +add_cron_job() { + # Define log directory and cron job command + local username=$(whoami) + local log_dir="/home/$username/logs" + local cron_job="* * * * * /bin/bash $(pwd)/dashboard.sh >> $log_dir/systemguard_cron.log 2>&1" + + # Create log directory with error handling + mkdir -p "$log_dir" + if [ $? -ne 0 ]; then + echo "Error: Failed to create log directory: $log_dir" + exit 1 + fi + + # Verify user retrieval + if [ -z "$username" ]; then + echo "Error: Unable to retrieve current username." + exit 1 + fi + + # Add cron job to crontab with error handling + (crontab -l 2>/dev/null; echo "$cron_job") | crontab - + if [ $? -ne 0 ]; then + echo "Error: Failed to add the cron job to the crontab." + exit 1 + fi + + echo "Cron job added: $cron_job" +} + +# Call the function to add the cron job +add_cron_job