feat: ping/DNS status and latency in hosts list and dashboard summary
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
from quart import Blueprint, render_template
|
||||
from __future__ import annotations
|
||||
from quart import Blueprint, render_template, current_app
|
||||
from sqlalchemy import select, func
|
||||
from fablednetmon.auth.middleware import require_role
|
||||
from fablednetmon.models.users import UserRole
|
||||
from fablednetmon.models.hosts import Host
|
||||
from fablednetmon.models.monitors import PingResult, DnsResult
|
||||
|
||||
dashboard_bp = Blueprint("dashboard", __name__)
|
||||
|
||||
@@ -8,4 +12,51 @@ dashboard_bp = Blueprint("dashboard", __name__)
|
||||
@dashboard_bp.get("/")
|
||||
@require_role(UserRole.viewer)
|
||||
async def index():
|
||||
return await render_template("dashboard/index.html")
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
# Latest ping per host
|
||||
latest_ping_subq = (
|
||||
select(PingResult.host_id, func.max(PingResult.probed_at).label("max_at"))
|
||||
.group_by(PingResult.host_id)
|
||||
.subquery()
|
||||
)
|
||||
pr = await db.execute(
|
||||
select(PingResult).join(
|
||||
latest_ping_subq,
|
||||
(PingResult.host_id == latest_ping_subq.c.host_id)
|
||||
& (PingResult.probed_at == latest_ping_subq.c.max_at),
|
||||
)
|
||||
)
|
||||
latest_pings = list(pr.scalars())
|
||||
ping_up = sum(1 for p in latest_pings if p.status.value == "up")
|
||||
ping_down = sum(1 for p in latest_pings if p.status.value == "down")
|
||||
|
||||
# Latest DNS per host
|
||||
latest_dns_subq = (
|
||||
select(DnsResult.host_id, func.max(DnsResult.resolved_at).label("max_at"))
|
||||
.group_by(DnsResult.host_id)
|
||||
.subquery()
|
||||
)
|
||||
dr = await db.execute(
|
||||
select(DnsResult).join(
|
||||
latest_dns_subq,
|
||||
(DnsResult.host_id == latest_dns_subq.c.host_id)
|
||||
& (DnsResult.resolved_at == latest_dns_subq.c.max_at),
|
||||
)
|
||||
)
|
||||
latest_dns = list(dr.scalars())
|
||||
dns_ok = sum(1 for d in latest_dns if d.status.value == "resolved")
|
||||
dns_fail = sum(1 for d in latest_dns if d.status.value != "resolved")
|
||||
|
||||
total_hosts = (await db.execute(select(func.count()).select_from(Host))).scalar()
|
||||
|
||||
poll_interval = current_app.config.get("MONITORS_POLL_INTERVAL", 60)
|
||||
|
||||
return await render_template(
|
||||
"dashboard/index.html",
|
||||
ping_up=ping_up,
|
||||
ping_down=ping_down,
|
||||
dns_ok=dns_ok,
|
||||
dns_fail=dns_fail,
|
||||
total_hosts=total_hosts,
|
||||
poll_interval=poll_interval,
|
||||
)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from __future__ import annotations
|
||||
from quart import Blueprint, render_template, request, redirect, url_for, current_app
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, func
|
||||
from fablednetmon.auth.middleware import require_role
|
||||
from fablednetmon.models.hosts import Host, ProbeType
|
||||
from fablednetmon.models.monitors import PingResult, DnsResult
|
||||
from fablednetmon.models.users import UserRole
|
||||
|
||||
hosts_bp = Blueprint("hosts", __name__, url_prefix="/hosts")
|
||||
@@ -14,7 +15,43 @@ async def list_hosts():
|
||||
async with current_app.db_sessionmaker() as db:
|
||||
result = await db.execute(select(Host).order_by(Host.name))
|
||||
hosts = result.scalars().all()
|
||||
return await render_template("hosts/list.html", hosts=hosts)
|
||||
|
||||
# Latest ping result per host
|
||||
latest_ping_subq = (
|
||||
select(PingResult.host_id, func.max(PingResult.probed_at).label("max_at"))
|
||||
.group_by(PingResult.host_id)
|
||||
.subquery()
|
||||
)
|
||||
pr = await db.execute(
|
||||
select(PingResult).join(
|
||||
latest_ping_subq,
|
||||
(PingResult.host_id == latest_ping_subq.c.host_id)
|
||||
& (PingResult.probed_at == latest_ping_subq.c.max_at),
|
||||
)
|
||||
)
|
||||
latest_pings = {r.host_id: r for r in pr.scalars()}
|
||||
|
||||
# Latest DNS result per host
|
||||
latest_dns_subq = (
|
||||
select(DnsResult.host_id, func.max(DnsResult.resolved_at).label("max_at"))
|
||||
.group_by(DnsResult.host_id)
|
||||
.subquery()
|
||||
)
|
||||
dr = await db.execute(
|
||||
select(DnsResult).join(
|
||||
latest_dns_subq,
|
||||
(DnsResult.host_id == latest_dns_subq.c.host_id)
|
||||
& (DnsResult.resolved_at == latest_dns_subq.c.max_at),
|
||||
)
|
||||
)
|
||||
latest_dns = {r.host_id: r for r in dr.scalars()}
|
||||
|
||||
return await render_template(
|
||||
"hosts/list.html",
|
||||
hosts=hosts,
|
||||
latest_pings=latest_pings,
|
||||
latest_dns=latest_dns,
|
||||
)
|
||||
|
||||
|
||||
@hosts_bp.get("/new")
|
||||
|
||||
@@ -2,31 +2,60 @@
|
||||
{% block title %}Dashboard — FabledNetMon{% endblock %}
|
||||
{% block content %}
|
||||
<h1 style="margin-bottom:1.5rem;color:#c0c0ff;">Dashboard</h1>
|
||||
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:1rem;">
|
||||
<div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(240px,1fr));gap:1rem;">
|
||||
|
||||
<div class="card">
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;">Ping Monitor</h3>
|
||||
<p style="color:#606080;font-size:0.9rem;">
|
||||
<a href="/hosts/" style="color:#a0a0ff;">Manage hosts</a> to begin monitoring.
|
||||
</p>
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;font-size:0.9rem;text-transform:uppercase;letter-spacing:0.05em;">Ping</h3>
|
||||
{% if ping_up + ping_down == 0 %}
|
||||
<p style="color:#606080;font-size:0.9rem;"><a href="/hosts/" style="color:#a0a0ff;">Add hosts</a> to begin monitoring.</p>
|
||||
{% else %}
|
||||
<div style="display:flex;gap:1.5rem;align-items:baseline;">
|
||||
<div>
|
||||
<span style="font-size:2rem;font-weight:bold;color:#40c040;">{{ ping_up }}</span>
|
||||
<span style="color:#606080;font-size:0.85rem;margin-left:0.25rem;">up</span>
|
||||
</div>
|
||||
{% if ping_down > 0 %}
|
||||
<div>
|
||||
<span style="font-size:2rem;font-weight:bold;color:#c04040;">{{ ping_down }}</span>
|
||||
<span style="color:#606080;font-size:0.85rem;margin-left:0.25rem;">down</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
<p style="color:#404060;font-size:0.8rem;margin-top:0.5rem;">every {{ poll_interval }}s — <a href="/hosts/" style="color:#6060a0;">view all</a></p>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;">DNS Monitor</h3>
|
||||
<p style="color:#606080;font-size:0.9rem;">
|
||||
Enable DNS monitoring on a <a href="/hosts/" style="color:#a0a0ff;">host</a>.
|
||||
</p>
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;font-size:0.9rem;text-transform:uppercase;letter-spacing:0.05em;">DNS</h3>
|
||||
{% if dns_ok + dns_fail == 0 %}
|
||||
<p style="color:#606080;font-size:0.9rem;">Enable DNS monitoring on a <a href="/hosts/" style="color:#a0a0ff;">host</a>.</p>
|
||||
{% else %}
|
||||
<div style="display:flex;gap:1.5rem;align-items:baseline;">
|
||||
<div>
|
||||
<span style="font-size:2rem;font-weight:bold;color:#40c040;">{{ dns_ok }}</span>
|
||||
<span style="color:#606080;font-size:0.85rem;margin-left:0.25rem;">ok</span>
|
||||
</div>
|
||||
{% if dns_fail > 0 %}
|
||||
<div>
|
||||
<span style="font-size:2rem;font-weight:bold;color:#c04040;">{{ dns_fail }}</span>
|
||||
<span style="color:#606080;font-size:0.85rem;margin-left:0.25rem;">failed</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;">Alerts</h3>
|
||||
<p style="color:#606080;font-size:0.9rem;">
|
||||
<a href="/alerts/" style="color:#a0a0ff;">Configure alert rules</a> against any metric.
|
||||
</p>
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;font-size:0.9rem;text-transform:uppercase;letter-spacing:0.05em;">Hosts</h3>
|
||||
<span style="font-size:2rem;font-weight:bold;color:#c0c0e0;">{{ total_hosts }}</span>
|
||||
<span style="color:#606080;font-size:0.85rem;margin-left:0.25rem;">configured</span>
|
||||
<p style="margin-top:0.5rem;"><a href="/hosts/" style="color:#6060a0;font-size:0.85rem;">Manage →</a></p>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;">Ansible</h3>
|
||||
<p style="color:#606080;font-size:0.9rem;">
|
||||
<a href="/ansible/browse" style="color:#a0a0ff;">Browse playbooks</a> or view
|
||||
<a href="/ansible/" style="color:#a0a0ff;">run history</a>.
|
||||
</p>
|
||||
<h3 style="color:#8080ff;margin-bottom:0.75rem;font-size:0.9rem;text-transform:uppercase;letter-spacing:0.05em;">Alerts</h3>
|
||||
<p style="color:#606080;font-size:0.9rem;"><a href="/alerts/" style="color:#a0a0ff;">Configure alert rules</a> against any metric.</p>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@@ -13,19 +13,56 @@
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Name</th>
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Address</th>
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Probe</th>
|
||||
<th style="text-align:left;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Monitors</th>
|
||||
<th style="text-align:center;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Ping</th>
|
||||
<th style="text-align:center;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">Latency</th>
|
||||
<th style="text-align:center;padding:0.75rem 1rem;color:#8080a0;font-weight:normal;">DNS</th>
|
||||
<th style="padding:0.75rem 1rem;"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for host in hosts %}
|
||||
{% set ping = latest_pings.get(host.id) %}
|
||||
{% set dns = latest_dns.get(host.id) %}
|
||||
<tr style="border-bottom:1px solid #1a1a3a;">
|
||||
<td style="padding:0.75rem 1rem;">{{ host.name }}</td>
|
||||
<td style="padding:0.75rem 1rem;color:#a0a0c0;">{{ host.address }}</td>
|
||||
<td style="padding:0.75rem 1rem;color:#a0a0c0;">{{ host.probe_type.value | upper }}:{{ host.probe_port }}</td>
|
||||
<td style="padding:0.75rem 1rem;color:#a0a0c0;">
|
||||
{% if host.ping_enabled %}<span style="color:#60c060;">ping</span>{% endif %}
|
||||
{% if host.dns_enabled %}<span style="color:#6090c0;margin-left:0.5rem;">dns</span>{% endif %}
|
||||
<td style="padding:0.75rem 1rem;color:#a0a0c0;font-size:0.85rem;">
|
||||
{{ host.probe_type.value | upper }}{% if host.probe_type.value == 'tcp' %}:{{ host.probe_port }}{% endif %}
|
||||
</td>
|
||||
<td style="padding:0.75rem 1rem;text-align:center;">
|
||||
{% if not host.ping_enabled %}
|
||||
<span style="color:#404060;font-size:0.8rem;">off</span>
|
||||
{% elif ping is none %}
|
||||
<span style="color:#606080;font-size:0.8rem;">—</span>
|
||||
{% elif ping.status.value == 'up' %}
|
||||
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#40c040;" title="Up"></span>
|
||||
{% else %}
|
||||
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#c04040;" title="Down"></span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td style="padding:0.75rem 1rem;text-align:center;font-size:0.9rem;">
|
||||
{% if ping and ping.response_time_ms is not none %}
|
||||
{% if ping.response_time_ms < 10 %}
|
||||
<span style="color:#40c040;">{{ ping.response_time_ms | round(1) }} ms</span>
|
||||
{% elif ping.response_time_ms < 100 %}
|
||||
<span style="color:#c0c040;">{{ ping.response_time_ms | round(1) }} ms</span>
|
||||
{% else %}
|
||||
<span style="color:#c08040;">{{ ping.response_time_ms | round(1) }} ms</span>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<span style="color:#404060;">—</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td style="padding:0.75rem 1rem;text-align:center;">
|
||||
{% if not host.dns_enabled %}
|
||||
<span style="color:#404060;font-size:0.8rem;">off</span>
|
||||
{% elif dns is none %}
|
||||
<span style="color:#606080;font-size:0.8rem;">—</span>
|
||||
{% elif dns.status.value == 'resolved' %}
|
||||
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#40c040;" title="{{ dns.resolved_ip }}"></span>
|
||||
{% else %}
|
||||
<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:#c04040;" title="Failed"></span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td style="padding:0.75rem 1rem;text-align:right;">
|
||||
<a href="/hosts/{{ host.id }}/edit" style="color:#a0a0ff;margin-right:1rem;">Edit</a>
|
||||
|
||||
Reference in New Issue
Block a user