Skip to content

Commit daadb3f

Browse files
chore: Update SystemGuard app setup script and dependencies in crontab
1 parent 7ec81f8 commit daadb3f

File tree

8 files changed

+92
-11
lines changed

8 files changed

+92
-11
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ System Guard is a Flask app designed to monitor server stats such as CPU, Memory
55
## Installation
66

77
```bash
8-
wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/installer.sh
9-
chmod +x installer.sh && sudo mv installer.sh /usr/local/bin/systemguard-installer
8+
wget https://raw.githubusercontent.com/codeperfectplus/SystemGuard/main/setup.sh
9+
chmod +x setup.sh && sudo mv setup.sh /usr/local/bin/systemguard-installer
1010
```
1111

1212
### To install the SystemGuard app, run the following command:

load_testing.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
3+
# Script to start Locust server
4+
5+
# Define the path to your Locust file
6+
LOCUST_FILE="src/scripts/locustfile.py"
7+
# Define the host URL for Locust
8+
HOST_URL="http://localhost:5050"
9+
10+
# Check if Locust is installed
11+
if ! command -v locust &> /dev/null
12+
then
13+
echo "Locust is not installed. Please install it first."
14+
exit 1
15+
fi
16+
17+
# Start Locust server
18+
echo "Starting Locust server..."
19+
locust -f "$LOCUST_FILE" --host="$HOST_URL"
20+
21+
# Optionally, you can pass additional Locust flags here if needed
22+
# For example, to run Locust in headless mode:
23+
# locust -f "$LOCUST_FILE" --host="$HOST_URL" --headless -u 10 -r 1 --run-time 1m

installer.sh renamed to setup.sh

+63-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@
22

33
# SystemGuard Installer Script
44
# ----------------------------
5-
# This script installs, uninstalls, backs up, and restores SystemGuard by managing its installation, cleanup, and configuration.
5+
# This script installs, uninstalls, backs up, restores SystemGuard, and includes load testing using Locust.
66

7-
# Variables
7+
# Determine the correct user's home directory
8+
get_user_home() {
9+
if [ -n "$SUDO_USER" ]; then
10+
# When using sudo, SUDO_USER gives the original user who invoked sudo
11+
TARGET_USER="$SUDO_USER"
12+
else
13+
# If not using sudo, use LOGNAME to find the current user
14+
TARGET_USER="$LOGNAME"
15+
fi
16+
17+
# Get the home directory of the target user
18+
USER_HOME=$(eval echo ~$TARGET_USER)
19+
echo "$USER_HOME"
20+
}
21+
22+
# Set paths relative to the correct user's home directory
23+
USER_HOME=$(get_user_home)
824
DOWNLOAD_DIR="/tmp"
9-
EXTRACT_DIR="/home/$USER/.systemguard"
25+
EXTRACT_DIR="$USER_HOME/.systemguard"
1026
LOG_DIR="$HOME/logs"
1127
LOG_FILE="$LOG_DIR/systemguard-installer.log"
12-
BACKUP_DIR="/home/$USER/.systemguard_backup"
28+
BACKUP_DIR="$USER_HOME/.systemguard_backup"
1329
EXECUTABLE="/usr/local/bin/systemguard-installer"
30+
LOCUST_FILE="$EXTRACT_DIR/SystemGuard-*/src/scripts/locustfile.py"
31+
HOST_URL="http://localhost:5050"
32+
INSTALLER_SCRIPT='setup.sh'
1433

34+
echo "User: $(whoami)"
1535
# Create necessary directories
1636
mkdir -p "$LOG_DIR"
1737
mkdir -p "$BACKUP_DIR"
@@ -73,6 +93,23 @@ restore() {
7393
fi
7494
}
7595

96+
# Function to install the script as an executable
97+
install_executable() {
98+
# Use $0 to get the full path of the currently running script
99+
# CURRENT_SCRIPT=$(realpath "$0")
100+
cd $EXTRACT_DIR/SystemGuard-*/
101+
CURRENT_SCRIPT=$(pwd)/$INSTALLER_SCRIPT
102+
echo "Current script: $CURRENT_SCRIPT"
103+
# Verify that the script exists before attempting to copy
104+
if [ -f "$CURRENT_SCRIPT" ]; then
105+
log "Installing executable to /usr/local/bin/systemguard-installer..."
106+
cp "$CURRENT_SCRIPT" "$EXECUTABLE"
107+
log "Executable installed successfully."
108+
else
109+
log "Error: Script file not found. Cannot copy to /usr/local/bin."
110+
fi
111+
}
112+
76113
# Install function
77114
install() {
78115
log "Starting installation of SystemGuard..."
@@ -155,7 +192,7 @@ install() {
155192

156193
# Install the executable
157194
log "Installing executable to /usr/local/bin/systemguard-installer..."
158-
# cp "$(basename "$0")" "$EXECUTABLE"
195+
install_executable
159196
log "SystemGuard version $VERSION installed successfully!"
160197
}
161198

@@ -190,6 +227,24 @@ uninstall() {
190227
fi
191228
}
192229

230+
# Load test function to start Locust server
231+
load_test() {
232+
log "Starting Locust server for load testing..."
233+
234+
# Check if Locust is installed
235+
if ! command -v locust &> /dev/null
236+
then
237+
log "Locust is not installed. Please install it first."
238+
exit 1
239+
fi
240+
241+
# Start Locust server
242+
log "Starting Locust server..."
243+
locust -f "$LOCUST_FILE" --host="$HOST_URL"
244+
# Optionally, you can pass additional Locust flags here if needed
245+
# locust -f "$LOCUST_FILE" --host="$HOST_URL" --headless -u 10 -r 1 --run-time 1m
246+
}
247+
193248
# Display help
194249
show_help() {
195250
echo "SystemGuard Installer"
@@ -198,6 +253,7 @@ show_help() {
198253
echo " --install Install SystemGuard"
199254
echo " --uninstall Uninstall SystemGuard"
200255
echo " --restore Restore SystemGuard from a backup"
256+
echo " --load-test Start Locust load testing"
201257
echo " --help Display this help message"
202258
}
203259

@@ -207,6 +263,7 @@ for arg in "$@"; do
207263
--install) ACTION="install" ;;
208264
--uninstall) ACTION="uninstall" ;;
209265
--restore) ACTION="restore" ;;
266+
--load-test) ACTION="load_test" ;;
210267
--help) show_help; exit 0 ;;
211268
*) echo "Unknown option: $arg"; show_help; exit 1 ;;
212269
esac
@@ -217,5 +274,6 @@ case $ACTION in
217274
install) install ;;
218275
uninstall) uninstall ;;
219276
restore) restore ;;
277+
load_test) load_test ;;
220278
*) echo "No action specified. Use --help for usage information." ;;
221279
esac

src/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
app = Flask(__name__)
55

66
# Configure the SQLite database
7-
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dashboard.db'
7+
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///systemguard.db'
88
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
99
app.config['SECRET_KEY'] = 'secret'
1010

src/scripts/cronjob.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
username=$(whoami)
55
log_dir="/home/$username/logs"
66
mkdir -p "$log_dir"
7-
CRON_JOB="* * * * * /bin/bash $(pwd)/dashboard.sh >> $log_dir/systemdashboard_cron.log 2>&1"
7+
CRON_JOB="* * * * * /bin/bash $(pwd)/dashboard.sh >> $log_dir/systemguard_cron.log 2>&1"
88

99
echo "Total cron jobs before: $(crontab -l | grep -v '^#' | wc -l)"
1010

src/scripts/dashboard.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ PROJECT_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")"
1111
FLASK_APP_PATH="${FLASK_APP_PATH:-$PROJECT_DIR/app.py}"
1212
REQUIREMENTS_FILE="${REQUIREMENTS_FILE:-$PROJECT_DIR/requirements.txt}"
1313
FLASK_PORT="${FLASK_PORT:-5050}"
14-
LOG_FILE="/home/$(whoami)/logs/systemdashboard_flask.log"
14+
LOG_FILE="/home/$(whoami)/logs/systemguard_flask.log"
1515
USERNAME="$(whoami)"
1616

1717
# Ensure log directory exists

locustfile.py renamed to src/scripts/locustfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ class WebsiteUser(HttpUser):
4545

4646
if __name__ == "__main__":
4747
import os
48-
os.system("locust -f locustfile.py --host=http://localhost:5000")
48+
# os.system("locust -f locustfile.py --host=http://localhost:5000")
File renamed without changes.

0 commit comments

Comments
 (0)