commit a8473eec11dc589ecc16d3220a879824e8ec0cf5 Author: Nikolay Tatarinov Date: Fri Nov 28 00:20:23 2025 +0300 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64d49ae --- /dev/null +++ b/.gitignore @@ -0,0 +1,216 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[codz] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py.cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +# Pipfile.lock + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# uv.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +# poetry.lock +# poetry.toml + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python. +# https://pdm-project.org/en/latest/usage/project/#working-with-version-control +# pdm.lock +# pdm.toml +.pdm-python +.pdm-build/ + +# pixi +# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control. +# pixi.lock +# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one +# in the .venv directory. It is recommended not to include this directory in version control. +.pixi + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# Redis +*.rdb +*.aof +*.pid + +# RabbitMQ +mnesia/ +rabbitmq/ +rabbitmq-data/ + +# ActiveMQ +activemq-data/ + +# SageMath parsed files +*.sage.py + +# Environments +.env +.envrc +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +# .idea/ + +# Abstra +# Abstra is an AI-powered process automation framework. +# Ignore directories containing user credentials, local state, and settings. +# Learn more at https://abstra.io/docs +.abstra/ + +# Visual Studio Code +# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore +# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore +# and can be added to the global gitignore or merged into this file. However, if you prefer, +# you could uncomment the following to ignore the entire vscode folder +# .vscode/ + +# Ruff stuff: +.ruff_cache/ + +# PyPI configuration file +.pypirc + +# Marimo +marimo/_static/ +marimo/_lsp/ +__marimo__/ + +# Streamlit +.streamlit/secrets.toml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1f17f74 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +FROM alpine:3 AS build + +RUN apk update && \ + apk add --no-cache --virtual .build-deps \ + ca-certificates gcc postgresql-dev linux-headers musl-dev \ + libffi-dev jpeg-dev zlib-dev \ + git bash build-base python3-dev + +RUN python3 -m venv /venv +ENV PATH "/venv/bin:$PATH" +COPY ./watcher_visio/requirements.txt / +RUN pip install -r /requirements.txt + + +FROM alpine:3 + +ENV LANG C.UTF-8 +ENV LC_ALL C.UTF-8 + +ENV PYTHONUNBUFFERED 1 +ENV PATH "/venv/bin:$PATH" + +RUN apk add --no-cache --update python3 + +COPY --from=build /venv /venv + +RUN mkdir /app +COPY ./watcher_visio/ /app +WORKDIR /app + +CMD [ "python", "manage.py", "runserver", "0.0.0.0:5000" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7670ec8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +services: + watcher-visio: + build: . + ports: + - "5000:5000" + volumes: + - ./watcher_visio:/app \ No newline at end of file diff --git a/watcher_visio/manage.py b/watcher_visio/manage.py new file mode 100644 index 0000000..a89d676 --- /dev/null +++ b/watcher_visio/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'watcher_visio.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/watcher_visio/metrics/templates/dashboard.html b/watcher_visio/metrics/templates/dashboard.html new file mode 100644 index 0000000..3d223e0 --- /dev/null +++ b/watcher_visio/metrics/templates/dashboard.html @@ -0,0 +1,52 @@ + + + + + Prometheus dashboard + + + +

Prometheus dashboard

+
+ + + Download PDF +
+ + + + + + diff --git a/watcher_visio/metrics/urls.py b/watcher_visio/metrics/urls.py new file mode 100644 index 0000000..94829c9 --- /dev/null +++ b/watcher_visio/metrics/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path("", views.dashboard, name="dashboard"), + path("api/metrics/", views.metrics_api, name="metrics_api"), + path("report/pdf/", views.report_pdf, name="report_pdf"), +] diff --git a/watcher_visio/metrics/views.py b/watcher_visio/metrics/views.py new file mode 100644 index 0000000..f47c796 --- /dev/null +++ b/watcher_visio/metrics/views.py @@ -0,0 +1,90 @@ +import os +import json +import time +import requests +from django.conf import settings +from django.shortcuts import render +from django.http import JsonResponse, HttpResponse +from django.template.loader import render_to_string + +# Helper: query Prometheus HTTP API (query_range) +def query_prometheus_range(query, start, end, step="60s"): + url = settings.PROMETHEUS_URL.rstrip("/") + "/api/v1/query_range" + params = {"query": query, "start": start, "end": end, "step": step} + r = requests.get(url, params=params, timeout=10) + r.raise_for_status() + return r.json() + +# API endpoint used by Chart.js frontend +def metrics_api(request): + # get parameters or default (last 1 hour) + metric = request.GET.get("metric", settings.PROMETHEUS_DEFAULT_METRIC) + now = int(time.time()) + start = request.GET.get("start", str(now - 3600)) # unix epoch seconds + end = request.GET.get("end", str(now)) + step = request.GET.get("step", "60s") + + # Example: if the metric is a gauge giving bytes, we may want to convert ... keep raw for now + q = metric + data = query_prometheus_range(q, start, end, step) + # Prometheus returns JSON; keep it minimal for Chart.js: {labels: [...], datasets: [{label,...,data:[...]}]} + series = [] + labels = [] + datasets = [] + + if data.get("status") != "success": + return JsonResponse({"error": "prometheus error", "detail": data}) + + result = data["data"]["result"] # list of time series + # if no series, return empty + if not result: + return JsonResponse({"labels": [], "datasets": []}) + + # Build labels from first series timestamps + # Prometheus returns values as [[ts, value], ...] + first_values = result[0]["values"] + labels = [int(float(t[0])) * 1000 for t in first_values] # JS prefers ms + for s in result: + # create dataset for each timeseries (label from metric labels) + metric_labels = s.get("metric", {}) + label = metric_labels.get("instance") or metric_labels.get("domain") or json.dumps(metric_labels) + values = [float(v[1]) if v[1] != "NaN" else None for v in s["values"]] + datasets.append({ + "label": label, + "data": values, + }) + + return JsonResponse({"labels": labels, "datasets": datasets}) + +# Dashboard page (Jinja template) +def dashboard(request): + # let template ask API for data with JS. + return render(request, "dashboard.html", { + "default_metric": settings.PROMETHEUS_DEFAULT_METRIC, + }) + +# Render page to PDF using WeasyPrint +def report_pdf(request): + # optionally accept ?metric=...&start=...&end=... + metric = request.GET.get("metric", settings.PROMETHEUS_DEFAULT_METRIC) + now = int(time.time()) + start = int(request.GET.get("start", now - 3600)) + end = int(request.GET.get("end", now)) + + # fetch data server-side to include in report + try: + resp = query_prometheus_range(metric, start, end, step="60s") + except Exception as e: + return HttpResponse(f"Error fetching metrics: {e}", status=500) + + context = { + "metric": metric, + "prom_data": resp.get("data", {}), + "generated_at": time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()), + } + html = render_to_string("report.html", context) + + # Generate PDF via WeasyPrint + from weasyprint import HTML + pdf = HTML(string=html, base_url=request.build_absolute_uri("/")).write_pdf() + return HttpResponse(pdf, content_type="application/pdf") diff --git a/watcher_visio/requirements.txt b/watcher_visio/requirements.txt new file mode 100644 index 0000000..232680c --- /dev/null +++ b/watcher_visio/requirements.txt @@ -0,0 +1,41 @@ +asgiref==3.11.0 +brotli==1.2.0 +certifi==2025.11.12 +cffi==2.0.0 +charset-normalizer==3.4.4 +cryptography==46.0.3 +cssselect2==0.8.0 +decorator==5.2.1 +Django==5.2.8 +dogpile.cache==1.5.0 +fonttools==4.60.1 +idna==3.11 +iso8601==2.1.0 +Jinja2==3.1.6 +jmespath==1.0.1 +jsonpatch==1.33 +jsonpointer==3.0.0 +keystoneauth1==5.12.0 +MarkupSafe==3.0.3 +openstacksdk==4.8.0 +os-service-types==1.8.2 +pbr==7.0.3 +pillow==12.0.0 +platformdirs==4.5.0 +psutil==7.1.3 +pycparser==2.23 +pydyf==0.11.0 +pyphen==0.17.2 +PyYAML==6.0.3 +requests==2.32.5 +requestsexceptions==1.4.0 +sqlparse==0.5.3 +stevedore==5.6.0 +tinycss2==1.5.1 +tinyhtml5==2.0.0 +typing_extensions==4.15.0 +tzdata==2025.2 +urllib3==2.5.0 +weasyprint==66.0 +webencodings==0.5.1 +zopfli==0.4.0 diff --git a/watcher_visio/watcher_visio/__init__.py b/watcher_visio/watcher_visio/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/watcher_visio/watcher_visio/asgi.py b/watcher_visio/watcher_visio/asgi.py new file mode 100644 index 0000000..94488f4 --- /dev/null +++ b/watcher_visio/watcher_visio/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for watcher_visio project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'watcher_visio.settings') + +application = get_asgi_application() diff --git a/watcher_visio/watcher_visio/settings.py b/watcher_visio/watcher_visio/settings.py new file mode 100644 index 0000000..4897b68 --- /dev/null +++ b/watcher_visio/watcher_visio/settings.py @@ -0,0 +1,127 @@ +""" +Django settings for watcher_visio project. + +Generated by 'django-admin startproject' using Django 5.2.8. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.2/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-747*14ir*49hoo6c2225)kxr%4^am0ub_s-m^_7i4cctu)v$g8' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'metrics', +] + +# Prometheus settings (environment override recommended) +PROMETHEUS_URL = "http://localhost:9090" # replace with your Prometheus HTTP endpoint +PROMETHEUS_DEFAULT_METRIC = "libvirt_domain_info_memory_usage_bytes" + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'watcher_visio.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'watcher_visio.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/5.2/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/5.2/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.2/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/watcher_visio/watcher_visio/urls.py b/watcher_visio/watcher_visio/urls.py new file mode 100644 index 0000000..fc9c26f --- /dev/null +++ b/watcher_visio/watcher_visio/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for watcher_visio project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path("", include("metrics.urls")), +] diff --git a/watcher_visio/watcher_visio/wsgi.py b/watcher_visio/watcher_visio/wsgi.py new file mode 100644 index 0000000..1cb4612 --- /dev/null +++ b/watcher_visio/watcher_visio/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for watcher_visio project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'watcher_visio.settings') + +application = get_wsgi_application()