Skip to content

Commit

Permalink
chore: Update SystemGuard app setup script and dependencies in crontab
Browse files Browse the repository at this point in the history
  • Loading branch information
codeperfectplus committed Aug 31, 2024
1 parent 7ec81f8 commit daadb3f
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions load_testing.sh
Original file line number Diff line number Diff line change
@@ -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
68 changes: 63 additions & 5 deletions installer.sh → setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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..."
Expand Down Expand Up @@ -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!"
}

Expand Down Expand Up @@ -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"
Expand All @@ -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"
}

Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/cronjob.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/dashboard.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion locustfile.py → src/scripts/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
File renamed without changes.

0 comments on commit daadb3f

Please sign in to comment.