first commit
This commit is contained in:
22
watcher_visio/manage.py
Normal file
22
watcher_visio/manage.py
Normal 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()
|
||||
52
watcher_visio/metrics/templates/dashboard.html
Normal file
52
watcher_visio/metrics/templates/dashboard.html
Normal 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>
|
||||
8
watcher_visio/metrics/urls.py
Normal file
8
watcher_visio/metrics/urls.py
Normal 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"),
|
||||
]
|
||||
90
watcher_visio/metrics/views.py
Normal file
90
watcher_visio/metrics/views.py
Normal 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")
|
||||
41
watcher_visio/requirements.txt
Normal file
41
watcher_visio/requirements.txt
Normal 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
|
||||
0
watcher_visio/watcher_visio/__init__.py
Normal file
0
watcher_visio/watcher_visio/__init__.py
Normal file
16
watcher_visio/watcher_visio/asgi.py
Normal file
16
watcher_visio/watcher_visio/asgi.py
Normal 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()
|
||||
127
watcher_visio/watcher_visio/settings.py
Normal file
127
watcher_visio/watcher_visio/settings.py
Normal 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'
|
||||
23
watcher_visio/watcher_visio/urls.py
Normal file
23
watcher_visio/watcher_visio/urls.py
Normal 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")),
|
||||
]
|
||||
16
watcher_visio/watcher_visio/wsgi.py
Normal file
16
watcher_visio/watcher_visio/wsgi.py
Normal 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()
|
||||
Reference in New Issue
Block a user