Skip to content

Repository files navigation

MONA - Monitor & Analytics Tool

About

MONA is a K8s-based monitoring and analytics tool managed with Terraform and Helm. It collects and analyzes system metrics, built on Python with Celery and Redis as a task broker for metrics collection and ML workloads. FastAPI serves as the REST API/backend, React + Tailwind v4 + TypeScript handles web UI frontend, and PostgreSQL stores all metrics data.

Quick Start:

  1. Ensure Docker Desktop, kind, helm and Terraform are installed.
  2. Set up node-exporter on the device you want to monitor with port 9100.
  3. Clone repo git clone https://github.com/gwill1337/MONA.git
  4. Configure the necessary values in "mona-chart/values"ConfigurationGuide. such as PC's IP & name or just add them via Admin panel
  5. Configure config.py for ML and FastAPI limiter settings.
  6. Create terraform.tfvars in terraform folder from terraform.tfvars.example.
  7. Deploy: Run the automated script:
.\deploy.ps1 -all # or .\deploy.ps1 -deploy

Or via makefile:

make all

For manual deployment, use terraform init && terraform apply inside the /terraform folder.

Usage

Links below are available after setup.

Main links:

  • Admin panel: localhost:30081/admin Default username: admin, password: admin123
  • Dashboards: localhost:30081/admin/dashboard
  • Grafana: localhost:30300/ Default username: admin, password: admin123
  • Prometheus(Cluster): localhost:30091/
  • Prometheus(Monitoring devices): localhost:30391/
Click to view detailed API endpoints

API:

P.S. All endpoint require authentification except prometheus and probes.

  • Swagger: localhost:30080/docs
  • Main-metrics[Get]: localhost:30080/db-metrics
  • Prometheus targets[Get]: localhost:30080/api/prometheus/targets
  • Anomalies[Get]: localhost:30080/anomalies
  • Model-info[Get]: localhost:30080/model-info
  • Devices[Get]: localhost:30080/devices
  • Devices[Post]: localhost:30080/devices available via curl
  • Devices[Delete]: localhost:30080/devices/{device_id} available via curl
  • Train[Post]: localhost:30080/train available via curl
  • Model[Delete]: localhost:30080/model available via curl
  • Dashboard[Get]: localhost:30080/api/dashboard

Probes:

  • Liveness[Get]: localhost:30080/health/live
  • Readiness[Get]: localhost:30080/health/ready

Authentication:

  • Login[Post]: localhost:30080/api/auth/login
  • Logout[Post]: localhost:30080/api/auth/logout
  • Check auth[Get]: localhost:30080/api/auth/me

admin panel

About cluster & pods

The infrastructure is managed using Terraform and Helm. The Kubernetes cluster consists of a control-plane node running the following core workloads.
Helm templates for flexible and fast setup can be configured in values.

  • API Engine: FastAPI endpoints for handling client requests.
  • Task Queue: Celery workers with a Redis broker for background metrics collection and ML tasks.
  • PostgreSQL: For storing metrics and analytics data.
  • Redis: As broker for celery and as storage for sessions and limiter.
  • Monitoring Stack: Prometheus for data scraping, Grafana for visualization (Recharts is also used in the web UI), Alertmanager for pod alert notifications, and Loki with Promtail for pod logs.

dashboard

ML

  • ML: Mona uses Scikit-learn for anomaly detection.
  • Training: The model uses Isolation Forest and builds features with deltas for more accurate anomaly detection.

Model has 2 modes

  1. Auto: Takes between 50 and 500 of the most recent data points and trains the model on the fly. The model is not stored in the database and retrains before each detection (every 60 seconds by default).
  2. Manual: Uses a time range specified by the user. The trained model is stored in the database.

PostgreSQL

Postgres runs as a StatefulSet, which provides stable pod identifiers, persistent storage linked to specific pods, and ordered deployment for stateful applications.
For detailed information about the tables, see db.py.

Postgres has 5 tables:

  • devices: stores devices.
  • metrics: stores collected metrics from the monitored device.
  • anomalies: stores detected anomalies.
  • trained_models: stores trained models.
  • users: stores users.

and can be opened via psql:

kubectl exec -it statefulset/postgres-statefulset -n mona -- psql -U myuser -d mydb
#              ⬆ pod name                          ⬆ namespace    ⬆ Username ⬆ DB name

Authentication & Authorization

MONA uses a stateful, cookie-based authentication system backed by Redis to secure the admin panel and core API endpoints.

Admin Setup

During deployment, the seed_admin function runs automatically on startup. It reads the ADMIN_USERNAMES,ADMIN_PASSWORDS and USER_USERNAMES,USER_PASSWORDS environment variables (configured via your terraform.tfvars or Helm values) and provisions the initial user and admin accounts in the PostgreSQL database if it does not already exist.

  • Session Management: Upon a successful POST request to /api/auth/login, FastAPI generates a cryptographically secure 32-byte session token.
  • Redis Storage: This token is stored in the Redis broker with a 12-hour expiration time (ex=43200), linking the session to the user's ID.
  • Cookies: The token is returned to the frontend as an HttpOnly, Lax cookie named user_session. This ensures the token is automatically sent with subsequent requests while remaining protected from cross-site scripting (XSS) attacks.
  • Access Control: All protected endpoints — except health checks (/health/live, /health/ready) and Prometheus metrics (/api/prometheus/targets) — are routed through role-aware user_router / admin_router dependencies that validate the session token against Redis. Users are restricted to GET requests only, giving them read-only access to dashboards and device data, while admins have full access to all methods — GET, POST, PUT, DELETE — including device management and ML training endpoints. If the session is missing or expired, the API returns 401 Unauthorized; if the session is valid but the account's role doesn't permit the requested method, it returns 403 Forbidden.
  • Logout: Endpoint /api/auth/logout deletes the session key from Redis and clears the client's cookie.

P.S. Endpoints like /health/live, /health/ready, and Prometheus target metrics (/api/prometheus/targets) are intentionally excluded from authentication to allow seamless cluster monitoring.

CI

Automated checks and docker build & push run on every push and pull request:

  • Gitleaks: — leaks scan
  • Checkov: — IaC scan
  • Terraform — format and validation checks
  • Helm — lint for helm charts
  • YAML — lint for values and chart files
  • Python — ruff (lint and format checks), MyPy (type checks), Pytest (Api tests)
  • Docker - scan images via Trivy

Architecture

Here more about architecture and how mona works.

ML

MONA's ML model has CONTAMINATION = 0.05 which means up to 5% of data points might be classified as anomalies, even if there are none. To reduce false positive detections, the model is equipped with a limiter SCORE_THRESHOLD = -0.05.
In other words, it discards anomalies that the model estimates at less than a 5% confidence threshold. To definitively rule out false positives, the model also ignores "combined anomalies" if CPU and RAM usage are below 80%.

FastAPI

Config vs Admin Panel Device Management:

Managing monitored devices in MONA can be done in two ways to ensure flexibility:

  1. Values.yaml: Ideal for bulk-adding devices during deployment and preventing accidental deletions. FastAPI reads these configurations and commits them to the database.
  2. Admin Panel / API: Allows dynamically adding or removing devices on the fly via HTTP POST/DELETE requests.

Prometheus consistently scrapes a dedicated endpoint to fetch the most up-to-date list of devices from the database, seamlessly merging both approaches.

FastAPI & Celery:

To maintain a responsive REST API, heavy ML model training tasks are offloaded to Celery. Because these tasks are asynchronous, FastAPI implements a dedicated task-tracking endpoint using Redis to reliably monitor task status and return responses.

deploy & liveness:

  • Init Containers: FastAPI and Celery deployments use busybox init scripts to wait for the database to be fully operational before starting the pods.
  • Probes: FastAPI includes two health-check endpoints for Kubernetes: one for Liveness (API health) and one for Readiness (Database connectivity).

About

MONA - Monitor & Analytics Tool

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages