Skip to content

Commit

Permalink
Merge pull request #32 from codeperfectplus/features/prometheus_support
Browse files Browse the repository at this point in the history
Features/prometheus support
  • Loading branch information
codeperfectplus authored Sep 24, 2024
2 parents cf0d7a0 + e1a8c91 commit 9a47e88
Show file tree
Hide file tree
Showing 69 changed files with 2,675 additions and 862 deletions.
20 changes: 8 additions & 12 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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 number Diff line number Diff 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

0 comments on commit 9a47e88

Please sign in to comment.