main was 16 days and 259 commits behind the line the product is actually built on, at contract 0.30.0 against 0.35.0, with no per-PC credential code at all. It was neither current nor curated - just stale. This brings it forward and, at the same time, makes it mean something: main is what a receiving site installs, and nothing else. WHAT IT CARRIES. Twelve plugins - backups, computers, endpoints, guides, knowledgebase, machines, network, notifications, printers, slides, tools, warranty - plus applications, which is manifest-less core and always ships. The only manifest dependency inside that set is endpoints on computers, and both are in it, so the closure is self-contained. WHAT IT DOES NOT. employees, geenforce, measuringtools, printedparts and usb stay on the site line. So do the Windows installer tree, the VM rehearsal helpers, the agent tooling config, one site's data loader, the internal audit and findings records, and the planner spreadsheet. 1,135 tracked files against 1,438. A REMOVAL THAT BREAKS THE APP IS NOT A REMOVAL, and two nearly shipped: deploy/site-profile-universal.json still named all seventeen plugins. It is the file docs/DEPLOY.md tells a site to provision from, so a fresh install would have failed at `flask plugin apply-profile` with PluginError - the first thing a receiving site does, broken by the branch meant to serve them. It now names the twelve that exist, and its comment says the two must agree. agent/ was never refreshed. Whoever populated this branch copied shopdb/, plugins/, frontend/ and tests/ from the site line and left the agent tree at its September state: 22 C# and PowerShell sources missing, including AgentCredentials, EnrollmentService, AgentHttpFactory and the fleet-alert client, plus one file the site line had deleted still present. The old tree is internally consistent, so it compiles, and nothing in CI builds it - every gate was green over a server that speaks enrollment and per-PC credentials to a bundled agent source that cannot. Resynced whole. DECISIONS THIS ENCODES, rather than defers: - The plugin-authoring lab builds printedparts, whose manifest depends on employees, so it could not stay. The lab remains on the site line and main ships no build-along tutorial. That is a real loss for an adopting developer and is recorded here rather than discovered later. - Recognition, Training and Recertification notifications are no longer seeded. Each splits one notification into a card per employee and renders a photo, which needs a plugin this branch does not bundle. Seeding them would offer an author a type that cannot resolve anybody. A site that installs employees gets them from that plugin's own seed. - Windows/IIS is documented as the legacy path throughout, and the README leads with Docker, because that is what a receiving site gets. The API description is regenerated for THIS branch - 347 paths and 483 operations, down from 415 and 573 - so the published contract describes what is here rather than what the site line serves. docs/CONFIG.md lost the two measuring-tool label keys the code no longer declares, which two lanes had decided opposite ways. Suite: 3124 passed, 6 skipped. Naming and style gates pass. The docs gates run rather than skip, and every index line count is derived from the file it names.
191 lines
7.2 KiB
Python
191 lines
7.2 KiB
Python
"""Low-toner email/webhook alerting.
|
|
|
|
The report shows live levels; this fires an alert when a toner crosses a
|
|
threshold on its way DOWN. A warning email fires at or below 5 percent, a
|
|
critical email at 0 percent (empty). State lives in printersupplyalerts so an
|
|
alert fires once per crossing and re-arms only after a refill.
|
|
|
|
Recipients follow the usual plugin pattern: a plugin-scoped set of shopdb
|
|
users + roles + a free-text email list, falling back to the site-wide
|
|
alert_recipients; the webhook routes to a chosen support team's webhook,
|
|
falling back to the site-wide alert_webhook_url.
|
|
"""
|
|
|
|
import logging
|
|
|
|
from shopdb.api import db, Setting
|
|
|
|
from ..models import Printer, PrinterSupplyAlert
|
|
from .supply_parts import derivesupplytype, derivecolor, alerttier, TIER_RANK
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def alert_recipients():
|
|
"""Merge selected shopdb users' account emails with the free-text list.
|
|
|
|
Empty result means fall back to the site-wide alert_recipients."""
|
|
from shopdb.api import User, Role
|
|
recipients = []
|
|
userids = (Setting.get('printers_alert_userids') or '').strip()
|
|
for rawid in userids.split(','):
|
|
rawid = rawid.strip()
|
|
if not rawid.isdigit():
|
|
continue
|
|
user = db.session.get(User, int(rawid))
|
|
if user and user.isactive and user.email:
|
|
recipients.append(user.email)
|
|
roleids = (Setting.get('printers_alert_roleids') or '').strip()
|
|
for rawid in roleids.split(','):
|
|
rawid = rawid.strip()
|
|
if not rawid.isdigit():
|
|
continue
|
|
role = db.session.get(Role, int(rawid))
|
|
if role:
|
|
for user in role.users:
|
|
if user.isactive and user.email:
|
|
recipients.append(user.email)
|
|
freetext = Setting.get('printers_alert_email') or ''
|
|
for addr in freetext.replace(';', ',').split(','):
|
|
addr = addr.strip()
|
|
if addr:
|
|
recipients.append(addr)
|
|
# de-dupe, preserve order
|
|
seen = set()
|
|
unique = []
|
|
for addr in recipients:
|
|
low = addr.lower()
|
|
if low not in seen:
|
|
seen.add(low)
|
|
unique.append(addr)
|
|
return unique
|
|
|
|
|
|
def _threshold(key, default):
|
|
"""Read a numeric threshold setting, tolerating blank/garbage values."""
|
|
try:
|
|
raw = Setting.get(key)
|
|
return float(raw) if raw not in (None, '') else float(default)
|
|
except (ValueError, TypeError):
|
|
return float(default)
|
|
|
|
|
|
def site_alert_recipients():
|
|
"""Site-wide alert_recipients setting as a clean list (email fallback)."""
|
|
raw = Setting.get('alert_recipients') or ''
|
|
return [r.strip() for r in raw.replace(';', ',').split(',') if r.strip()]
|
|
|
|
|
|
def alert_team_webhook():
|
|
"""Webhook URL of the support team chosen for toner alerts, or None.
|
|
|
|
printers_alert_supportteamid selects a SupportTeam; alerts route to that
|
|
team's webhook. None -> send_webhook uses the site-wide default."""
|
|
team_id = Setting.get('printers_alert_supportteamid')
|
|
if not team_id:
|
|
return None
|
|
try:
|
|
from shopdb.api import SupportTeam
|
|
team = db.session.get(SupportTeam, int(team_id))
|
|
except (ValueError, TypeError):
|
|
return None
|
|
return (team.webhookurl or None) if team else None
|
|
|
|
|
|
def _printer_ipaddress(printer):
|
|
"""Primary (else any) communication IP for a printer's asset, or None."""
|
|
from shopdb.api import Communication
|
|
asset = printer.asset
|
|
if not asset:
|
|
return None
|
|
comm = Communication.query.filter_by(
|
|
assetid=asset.assetid, isprimary=True).first() \
|
|
or Communication.query.filter_by(assetid=asset.assetid).first()
|
|
return comm.ipaddress if comm else None
|
|
|
|
|
|
def _fire_alert(printer, color, remaining, tier):
|
|
"""Best-effort email + webhook for one toner crossing. Never raises."""
|
|
from shopdb.api import send_email, send_webhook
|
|
name = printer.asset.name or printer.asset.assetnumber if printer.asset \
|
|
else f'Printer {printer.printerid}'
|
|
label = 'CRITICAL - empty' if tier == 'critical' else 'Low'
|
|
subject = f'{label} toner: {name} ({color}) - {remaining:.0f}% remaining'
|
|
base = (Setting.get('site_base_url') or '').rstrip('/')
|
|
link = f'{base}/printers/{printer.printerid}' if base else ''
|
|
link_html = f'<p><a href="{link}">View {name}</a></p>' if link else ''
|
|
html = (f'<p><strong>{name}</strong> {color} toner is at '
|
|
f'<strong>{remaining:.0f}%</strong> ({tier}).</p>{link_html}')
|
|
webhook_text = f'{name} {color} toner at {remaining:.0f}% ({tier})'
|
|
try:
|
|
send_webhook(subject, webhook_text, url=alert_team_webhook())
|
|
recipients = alert_recipients() or site_alert_recipients()
|
|
if recipients:
|
|
send_email(recipients, subject, html)
|
|
except Exception:
|
|
logger.exception('Toner alert failed for printer %s', printer.printerid)
|
|
|
|
|
|
def check_supplies(service=None):
|
|
"""Poll Zabbix for all active printers' toner and alert on crossings.
|
|
|
|
Returns a summary dict. Intended to run on a schedule (a scheduled task or
|
|
cron calling `flask printers check-toner-alerts`)."""
|
|
from shopdb.api import Asset
|
|
from .zabbix_service import ZabbixService
|
|
|
|
service = service or ZabbixService()
|
|
summary = {'printers': 0, 'polled': 0, 'alerts': 0, 'rearmed': 0}
|
|
if not service.isconfigured:
|
|
logger.warning('Zabbix not configured; skipping toner alert poll')
|
|
return summary
|
|
|
|
warning_threshold = _threshold('printers_alert_warning_threshold', 5)
|
|
critical_threshold = _threshold('printers_alert_critical_threshold', 0)
|
|
|
|
printers = db.session.query(Printer).join(Asset).filter(
|
|
Asset.isactive == True).all() # noqa: E712
|
|
for printer in printers:
|
|
summary['printers'] += 1
|
|
ipaddress = _printer_ipaddress(printer)
|
|
if not ipaddress or ipaddress == 'USB':
|
|
continue
|
|
supplies = service.getsuppliesbyip(ipaddress)
|
|
if not supplies:
|
|
continue
|
|
summary['polled'] += 1
|
|
for supply in supplies:
|
|
supplyname = supply.get('name', '')
|
|
if derivesupplytype(supplyname) != 'toner':
|
|
continue
|
|
try:
|
|
remaining = float(supply.get('level', 0))
|
|
except (TypeError, ValueError):
|
|
continue
|
|
color = supply.get('color') or derivecolor(supplyname)
|
|
supplykey = color if color and color != 'none' else supplyname
|
|
supplykey = supplykey[:64]
|
|
tier = alerttier(remaining, warning_threshold, critical_threshold)
|
|
|
|
row = PrinterSupplyAlert.query.filter_by(
|
|
printerid=printer.printerid, supplykey=supplykey).first()
|
|
prior = row.lasttier if row else 'ok'
|
|
if tier == prior:
|
|
continue
|
|
if row is None:
|
|
row = PrinterSupplyAlert(
|
|
printerid=printer.printerid, supplykey=supplykey,
|
|
lasttier=tier)
|
|
db.session.add(row)
|
|
else:
|
|
row.lasttier = tier
|
|
|
|
if TIER_RANK[tier] > TIER_RANK[prior]:
|
|
_fire_alert(printer, color, remaining, tier)
|
|
summary['alerts'] += 1
|
|
else:
|
|
summary['rearmed'] += 1
|
|
|
|
db.session.commit()
|
|
return summary
|