Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
0e7d909
chore: basic prometheus end point created
codeperfectplus Sep 19, 2024
aa621a5
optimzed the prometheus endpooint
codeperfectplus Sep 19, 2024
1ca4650
Add Prometheus metrics for CPU temperature, frequency, battery percen…
codeperfectplus Sep 19, 2024
b021d11
Refactor system info logging and metrics collection
codeperfectplus Sep 19, 2024
a0def2a
feat: support for monitor external api/file added
codeperfectplus Sep 20, 2024
5aef69d
web page improved
codeperfectplus Sep 20, 2024
c9fb721
UI improvement
codeperfectplus Sep 20, 2024
acb3843
feat: influx db support added
codeperfectplus Sep 20, 2024
16cd51c
feat: influx support added
codeperfectplus Sep 20, 2024
bec9223
influx installation added
codeperfectplus Sep 21, 2024
1b96a86
Refactor README.md and install_influx.sh scripts
codeperfectplus Sep 21, 2024
1943394
sqlite replaced with prometheus for time series metrics
codeperfectplus Sep 21, 2024
c5b5f02
chore: updating prometheus for central server added
codeperfectplus Sep 21, 2024
10433f8
Refactor log_system_info.py and influxdb_config.py
codeperfectplus Sep 21, 2024
b526879
Refactor setup.sh to fix message box display issue
codeperfectplus Sep 21, 2024
7374dc6
Refactor Docker run command to always restart Prometheus container
codeperfectplus Sep 21, 2024
2d68173
chore: api end point added for multiple target
codeperfectplus Sep 21, 2024
4253df9
Refactor API endpoint URL in graphs.js
codeperfectplus Sep 21, 2024
3567b05
Refactor API endpoint URL in graphs.js to include targets in the filt…
codeperfectplus Sep 21, 2024
6dfee0f
chore: experimental graph created
codeperfectplus Sep 21, 2024
f762469
removed unnessarty import
codeperfectplus Sep 22, 2024
0adbfc6
Refactor API endpoint URL in graphs.js and remove unnecessary import
codeperfectplus Sep 22, 2024
1bf430d
Refactor issue templates and README.md, and update prometheus.sh script
codeperfectplus Sep 22, 2024
85fcac8
chore: :art: graph page improved
codeperfectplus Sep 22, 2024
6cba830
chore: improved the graph
codeperfectplus Sep 22, 2024
0c7fd05
Refactor prometheus.py, README.md, and dashboard_network.html
codeperfectplus Sep 22, 2024
7641ab6
chore: new feature added to add and remove targets
codeperfectplus Sep 22, 2024
c80881d
chore: premetheus container will update after changing yml file
codeperfectplus Sep 22, 2024
7b078bf
chore: bug fixed with updating container
codeperfectplus Sep 22, 2024
568a5b1
Improved the UI
codeperfectplus Sep 22, 2024
f02b6f4
option added to restart prometheus
codeperfectplus Sep 23, 2024
58e0f4c
UI improvements
codeperfectplus Sep 23, 2024
ae2c54f
chore: updated the web page
codeperfectplus Sep 23, 2024
c2e5241
Refactor chart creation and add download and refresh buttons
codeperfectplus Sep 23, 2024
14c3ff1
auth added
codeperfectplus Sep 23, 2024
ad2e53a
Refactor Prometheus config update function and improve authentication…
codeperfectplus Sep 23, 2024
8092574
Refactor Prometheus config update function and improve authentication…
codeperfectplus Sep 23, 2024
9c09a79
chore: about page updated
codeperfectplus Sep 24, 2024
fd7bc88
UI improvement
codeperfectplus Sep 24, 2024
265ef60
more line chart added
codeperfectplus Sep 24, 2024
74216df
systemguard target improve
codeperfectplus Sep 24, 2024
7383c8c
UI changes
codeperfectplus Sep 24, 2024
e74e84e
prometheus config function updated
codeperfectplus Sep 24, 2024
f9c665b
Refactor route for graphs to use experimental template
codeperfectplus Sep 24, 2024
de01e78
fixed bug for change interval
codeperfectplus Sep 24, 2024
e1a8c91
Refactor targets.html template to improve UI and functionality
codeperfectplus Sep 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,31 +8,27 @@ assignees: ''
---

**Describe the bug**

A clear and concise description of what the bug is.

**To Reproduce**

Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**

A clear and concise description of what you expected to happen.

**Screenshots**

If applicable, add screenshots to help explain your problem.

**Desktop (please complete the following information):**
**(please complete the following information):**

- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Version [e.g. 22]

**Smartphone (please complete the following information):**
- Device: [e.g. iPhone6]
- OS: [e.g. iOS8.1]
- Browser [e.g. stock browser, safari]
- Version [e.g. 22]
- Systemguard version [e.g. 22]

**Additional context**

Add any other context about the problem here.
4 changes: 4 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,13 +8,17 @@ assignees: ''
---

**Is your feature request related to a problem? Please describe.**

A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**

A clear and concise description of what you want to happen.

**Describe alternatives you've considered**

A clear and concise description of any alternative solutions or features you've considered.

**Additional context**

Add any other context or screenshots about the feature request here.
5 changes: 5 additions & 0 deletions .gitignore
Original file line numberDiff line numberDiff line change
Expand Up@@ -164,3 +164,8 @@ cython_debug/

predefine_user.json
src/assets/predefine_user.json

influxdb_data
prometheus_config
*.yml
prometheus_config/prometheus.yml
26 changes: 10 additions & 16 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@

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.


## Features 🚀

- Lightweight, open-source, and free to use with a straightforward installation process, out-of-the-box monitoring solution.
Expand All@@ -18,22 +19,15 @@ System Guard is a Flask app designed to monitor server stats such as CPU, Memory
- Role-based dashboards tailored for Developer, Admin, IT Manager, and Manager roles (upcoming feature).
- Update security updates with a single click or automatically update to the latest version to simplify maintenance.

## Old Features 🚀

- Administrators can manage user accounts by creating, updating, or deleting users.
- Admin-level access is required for configuring settings, managing users, and adjusting security and notification - preferences.
- Historical performance data can be viewed as charts, aiding in trend analysis.
- Supports network speed testing directly from the server.
- Provides the ability to terminate resource-heavy processes with a single command.
- Real-time server metric monitoring keeps data consistently updated.
- The interface is responsive and optimized for various devices including mobile, tablets, and desktops.
- The system can automatically update to the latest version to simplify maintenance.
- Installation can be done quickly via a bash script for easy setup.
- Notifications are sent to users and admins when a process is manually terminated.
- Offers website monitoring tasks that trigger email alerts when a website becomes unavailable.
- Configurable email alerts for various actions across the server.
- Option to download historical data in CSV format for detailed analysis (upcoming feature).
- Server status monitoring with alerts for server downtime or recovery (upcoming feature).
## Architecture 🏗️

![SystemGuard-Architecture](/src/docs/images/SystemGuard-Architecture.jpg)

## Tech Stack 🛠️

- **Frontend**: JavaScript, Bootstrap, Chart.js, Grafana
- **Backend**: Python, Flask, SQLAlchemy, SQLite, Prometheus, InfluxDB
- **Monitoring**: psutil, speedtest-cli, nmap, netstat

## Get started 🛠️

Expand Down
11 changes: 4 additions & 7 deletions app.py
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
from src.config import app
from src import routes
from src.background_task import start_website_monitoring, monitor_settings
import os
from src.background_task import start_background_tasks

# Start the background tasks
start_background_tasks()

# background thread to monitor system settings changes
print("FLASK_ENV: ", os.getenv('FLASK_ENV'))
if os.getenv('FLASK_ENV') == 'production':
monitor_settings() # Starts monitoring for system logging changes
start_website_monitoring() # Starts pinging active websites

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
11 changes: 10 additions & 1 deletion requirements.txt
Original file line numberDiff line numberDiff line change
Expand Up@@ -23,4 +23,13 @@ watchdog==5.0.2
requests==2.32.3

# Speedtest CLI for testing internet speed
speedtest-cli==2.1.3
speedtest-cli==2.1.3

# prometheus client for monitoring
prometheus_client==0.20.0

# influxdb-client, optional for writing metrics to InfluxDB
# influxdb-client==1.46.0

# pyyaml for parsing YAML configuration files
pyyaml==6.0.2
11 changes: 9 additions & 2 deletions setup.sh
Original file line numberDiff line numberDiff line change
Expand Up@@ -853,10 +853,12 @@ start_server(){

# Install function
install(){
create_dir "$EXTRACT_DIR"
# PROMETHEUS_INSTALL_SCRIPT
message_box "$APP_NAME Installer $INSATLLER_VERSION" 0
message_box "Welcome on board: $(echo "$USER_NAME" | sed 's/.*/\u&/')" 3
message_box "Welcome on board: $(echo "$USER_NAME" | sed 's/.*/\u&/')" 0
check_dependencies
create_dir "$EXTRACT_DIR"

message_box "Choose the installation method\nNote: Release is recommended for production use." 0
message_box "1. Release (More Stable Version)\n2. Git Repository (Pre-Release Version)\n3. Source Code (Current Directory)" 0

Expand All@@ -878,6 +880,11 @@ install(){
exit 1
;
esac
PROMETHEUS_INSTALL_SCRIPT=$(find "$EXTRACT_DIR" -name prometheus.sh) ||{
log "ERROR" "Prometheus installation script not found."
exit 1
}
sudo -u "$USER_NAME" bash "$PROMETHEUS_INSTALL_SCRIPT"
start_server
message_box "The $APP_NAME server is running at $HOST_URL" 0
# open_browser
Expand Down
17 changes: 16 additions & 1 deletion src/background_task/__init__.py
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
import os
from src.background_task.monitor_website import start_website_monitoring
from src.background_task.log_system_info import monitor_settings
from src.background_task.external_monitoring import fetch_file_metrics_task
from src.logger import logger

__all__ = ["start_website_monitoring", "monitor_settings"]


def start_background_tasks():
"""
Starts the background tasks for the application.
"""
if os.getenv('FLASK_ENV') == 'production':
logger.info("Starting background tasks for production environment.")
start_website_monitoring()
fetch_file_metrics_task()
monitor_settings()
else:
logger.info("Background tasks are not started in development environment.")
52 changes: 52 additions & 0 deletions src/background_task/external_monitoring.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
from flask import Flask, Response
from prometheus_client import Gauge, generate_latest
import os
import time
from src.models import ExternalMonitornig
from src.config import app
import threading
from src.logger import logger

# Define a Gauge metric with a label 'key' to store multiple values from the file
file_metric = Gauge('external_metrics', 'Value read from file for each key', ['key'])

def read_file_and_update_metric(file_path: str) -> None:
"""Reads a file and updates metrics based on its content."""
if os.path.exists(file_path):
with open(file_path, 'r') as file:
for line in file:
try:
key, value = line.strip().split(':')
file_metric.labels(key.strip()).set(float(value.strip()))
except ValueError as ve:
logger.error(f"Value error processing line '{line}':{ve}")
except Exception as e:
logger.error(f"Error processing line '{line}':{e}")
else:
logger.warning(f"File{file_path} does not exist")

def fetch_file_metrics(sleep_duration: int = 5) -> None:
"""Background task to read file paths from the database and update metrics."""
while True:
with app.app_context():
file_paths = ExternalMonitornig.query.all()
current_keys ={sample.labels['key'] for sample in file_metric.collect()[0].samples}
new_keys = set()

for file_path in file_paths:
logger.info(f"Reading file:{file_path.file_path}")
read_file_and_update_metric(file_path.file_path)
new_keys.update({key.strip() for line in open(file_path.file_path) for key in line.split(':')[0].strip()})

# Remove metrics for keys that are no longer in the database
for key in current_keys:
if key not in new_keys:
file_metric.remove(key)

time.sleep(sleep_duration)

def fetch_file_metrics_task() -> None:
"""Starts the background task in a separate thread."""
thread = threading.Thread(target=fetch_file_metrics)
thread.daemon = True
thread.start()
Loading