first commit

This commit is contained in:
2025-11-28 00:20:23 +03:00
commit a8473eec11
13 changed files with 649 additions and 0 deletions

216
.gitignore vendored Normal file
View File

@@ -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

31
Dockerfile Normal file
View File

@@ -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" ]

7
docker-compose.yml Normal file
View File

@@ -0,0 +1,7 @@
services:
watcher-visio:
build: .
ports:
- "5000:5000"
volumes:
- ./watcher_visio:/app

22
watcher_visio/manage.py Normal file
View File

@@ -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()

View File

@@ -0,0 +1,52 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Prometheus dashboard</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<h1>Prometheus dashboard</h1>
<div>
<label>Metric: <input id="metric" value="{{ default_metric }}"/></label>
<button id="reload">Reload</button>
<a id="pdfLink" href="/report/pdf/?metric={{ default_metric }}" target="_blank">Download PDF</a>
</div>
<canvas id="chart" width="900" height="400"></canvas>
<script>
const ctx = document.getElementById('chart').getContext('2d');
let chart = null;
async function loadData() {
const metric = document.getElementById('metric').value;
const res = await fetch(`/api/metrics/?metric=${encodeURIComponent(metric)}`);
const payload = await res.json();
const labels = (payload.labels || []).map(ts => new Date(ts));
const datasets = (payload.datasets || []).map((d, i) => ({
label: d.label,
data: d.data.map((v, idx) => ({x: labels[idx], y: v})),
fill: false,
// Chart.js will auto pick colors
}));
if (chart) chart.destroy();
chart = new Chart(ctx, {
type: 'line',
data: { datasets },
options: {
parsing: false,
scales: {
x: { type: 'time', time: { unit: 'minute' } },
y: { beginAtZero: true }
}
}
});
// update PDF link
document.getElementById('pdfLink').href = `/report/pdf/?metric=${encodeURIComponent(metric)}`;
}
document.getElementById('reload').addEventListener('click', loadData);
loadData();
</script>
</body>
</html>

View File

@@ -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"),
]

View File

@@ -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")

View File

@@ -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

View File

View File

@@ -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()

View File

@@ -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'

View File

@@ -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")),
]

View File

@@ -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()