Add system settings, audit logging, user management, and dark mode fixes
System Settings: - Add SystemSettings.vue with Zabbix integration, SMTP/email config, SAML SSO settings - Add Setting model with key-value storage and typed values - Add settings API with caching Audit Logging: - Add AuditLog model tracking user, IP, action, entity changes - Add comprehensive audit logging to all CRUD operations: - Machines, Computers, Equipment, Network devices, VLANs, Subnets - Printers, USB devices (including checkout/checkin) - Applications, Settings, Users/Roles - Track old/new values for all field changes - Mask sensitive values (passwords, tokens) in logs User Management: - Add UsersList.vue with full user CRUD - Add Role management with granular permissions - Add 41 predefined permissions across 10 categories - Add users API with roles and permissions endpoints Reports: - Add TonerReport.vue for printer supply monitoring Dark Mode Fixes: - Fix map position section in PCForm, PrinterForm - Fix alert-warning in KnowledgeBaseDetail - All components now use CSS variables for theming CLI Commands: - Add flask seed permissions - Add flask seed settings Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
"""Printers API routes - new Asset-based architecture."""
|
||||
|
||||
import logging
|
||||
|
||||
from flask import Blueprint, request
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.extensions import db, cache
|
||||
from shopdb.core.models import Asset, AssetType, Vendor, Model, Communication, CommunicationType
|
||||
from shopdb.utils.responses import (
|
||||
success_response,
|
||||
@@ -16,6 +18,8 @@ from shopdb.utils.pagination import get_pagination_params, paginate_query
|
||||
from ..models import Printer, PrinterType
|
||||
from ..services import ZabbixService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
printers_asset_bp = Blueprint('printers_asset', __name__)
|
||||
|
||||
|
||||
@@ -421,8 +425,12 @@ def get_printer_supplies(printer_id: int):
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'Printer has no IP address')
|
||||
|
||||
service = ZabbixService()
|
||||
if not service.isconfigured:
|
||||
return error_response(ErrorCodes.BAD_REQUEST, 'Zabbix not configured')
|
||||
if not service.isconfigured or not service.isreachable:
|
||||
# Return empty supplies if Zabbix not available (fail gracefully)
|
||||
return success_response({
|
||||
'ipaddress': comm.ipaddress,
|
||||
'supplies': []
|
||||
})
|
||||
|
||||
supplies = service.getsuppliesbyip(comm.ipaddress)
|
||||
|
||||
@@ -432,6 +440,117 @@ def get_printer_supplies(printer_id: int):
|
||||
})
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Low Supplies
|
||||
# =============================================================================
|
||||
|
||||
def _get_low_supplies_data():
|
||||
"""Build low supplies data (cached for 10 minutes)."""
|
||||
cached = cache.get('printers_low_supplies')
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
service = ZabbixService()
|
||||
if not service.isconfigured or not service.isreachable:
|
||||
return {'printers': [], 'summary': {'total_checked': 0, 'low': 0, 'critical': 0}}
|
||||
|
||||
# All active printers with an IP address
|
||||
printers = (
|
||||
db.session.query(Printer, Asset, Communication)
|
||||
.join(Asset, Asset.assetid == Printer.assetid)
|
||||
.join(Communication, Communication.assetid == Asset.assetid)
|
||||
.filter(Asset.isactive == True)
|
||||
.filter(Communication.ipaddress.isnot(None))
|
||||
.filter(Communication.ipaddress != '')
|
||||
.all()
|
||||
)
|
||||
|
||||
# Dedupe by printer id (may have multiple comms)
|
||||
seen = set()
|
||||
unique_printers = []
|
||||
for printer, asset, comm in printers:
|
||||
if printer.printerid not in seen:
|
||||
seen.add(printer.printerid)
|
||||
unique_printers.append((printer, asset, comm))
|
||||
|
||||
results = []
|
||||
total_checked = 0
|
||||
|
||||
for printer, asset, comm in unique_printers:
|
||||
supplies = service.getsuppliesbyip_cached(comm.ipaddress)
|
||||
if supplies is None:
|
||||
continue
|
||||
|
||||
total_checked += 1
|
||||
|
||||
# Annotate each supply with status
|
||||
annotated = []
|
||||
has_low = False
|
||||
for s in supplies:
|
||||
level = s.get('level', 0)
|
||||
if level <= 5:
|
||||
status = 'critical'
|
||||
has_low = True
|
||||
elif level <= 10:
|
||||
status = 'low'
|
||||
has_low = True
|
||||
else:
|
||||
status = 'ok'
|
||||
annotated.append({
|
||||
'name': s.get('name', 'Unknown'),
|
||||
'level': level,
|
||||
'status': status
|
||||
})
|
||||
|
||||
if has_low:
|
||||
# Get location name
|
||||
location_name = None
|
||||
if asset.locationid:
|
||||
from shopdb.core.models import Location
|
||||
loc = Location.query.get(asset.locationid)
|
||||
if loc:
|
||||
location_name = loc.location
|
||||
|
||||
results.append({
|
||||
'printerid': printer.printerid,
|
||||
'printername': asset.name or printer.hostname or '',
|
||||
'assetnumber': asset.assetnumber or '',
|
||||
'ipaddress': comm.ipaddress,
|
||||
'location': location_name,
|
||||
'supplies': annotated
|
||||
})
|
||||
|
||||
low_count = 0
|
||||
critical_count = 0
|
||||
for p in results:
|
||||
has_critical = any(s['status'] == 'critical' for s in p['supplies'])
|
||||
has_low = any(s['status'] == 'low' for s in p['supplies'])
|
||||
if has_critical:
|
||||
critical_count += 1
|
||||
elif has_low:
|
||||
low_count += 1
|
||||
|
||||
data = {
|
||||
'printers': results,
|
||||
'summary': {
|
||||
'total_checked': total_checked,
|
||||
'low': low_count,
|
||||
'critical': critical_count
|
||||
}
|
||||
}
|
||||
|
||||
cache.set('printers_low_supplies', data, timeout=600)
|
||||
return data
|
||||
|
||||
|
||||
@printers_asset_bp.route('/lowsupplies', methods=['GET'])
|
||||
@jwt_required(optional=True)
|
||||
def low_supplies():
|
||||
"""Get printers with low or critical supply levels."""
|
||||
data = _get_low_supplies_data()
|
||||
return success_response(data)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Dashboard
|
||||
# =============================================================================
|
||||
@@ -465,12 +584,24 @@ def dashboard_summary():
|
||||
).group_by(Vendor.vendor
|
||||
).all()
|
||||
|
||||
# Get real low/critical supply counts (skip if Zabbix not reachable)
|
||||
low_count = 0
|
||||
critical_count = 0
|
||||
service = ZabbixService()
|
||||
if service.isconfigured and service.isreachable:
|
||||
try:
|
||||
supply_data = _get_low_supplies_data()
|
||||
low_count = supply_data['summary']['low']
|
||||
critical_count = supply_data['summary']['critical']
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not fetch supply data for dashboard: {e}")
|
||||
|
||||
return success_response({
|
||||
'total': total,
|
||||
'totalprinters': total, # For dashboard compatibility
|
||||
'online': total, # Placeholder - would need monitoring integration
|
||||
'lowsupplies': 0, # Placeholder - would need Zabbix integration
|
||||
'criticalsupplies': 0, # Placeholder - would need Zabbix integration
|
||||
'totalprinters': total,
|
||||
'online': total,
|
||||
'lowsupplies': low_count,
|
||||
'criticalsupplies': critical_count,
|
||||
'bytype': [{'type': t, 'count': c} for t, c in by_type],
|
||||
'byvendor': [{'vendor': v, 'count': c} for v, c in by_vendor],
|
||||
})
|
||||
|
||||
@@ -8,6 +8,7 @@ from shopdb.utils.responses import success_response, error_response, paginated_r
|
||||
from shopdb.utils.pagination import get_pagination_params, paginate_query
|
||||
from shopdb.core.models.machine import Machine, MachineType
|
||||
from shopdb.core.models.communication import Communication, CommunicationType
|
||||
from shopdb.core.models import AuditLog
|
||||
|
||||
from ..models import PrinterData
|
||||
from ..services import ZabbixService
|
||||
@@ -132,10 +133,21 @@ def update_printer_data(machine_id: int):
|
||||
pd = PrinterData(machineid=machine_id)
|
||||
db.session.add(pd)
|
||||
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
for key in ['windowsname', 'sharename', 'iscsf', 'installpath', 'pin']:
|
||||
if key in data:
|
||||
old_val = getattr(pd, key, None)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(pd, key, data[key])
|
||||
|
||||
# Audit log if there were changes
|
||||
if changes:
|
||||
AuditLog.log('updated', 'Printer', entityid=machine_id,
|
||||
entityname=machine.machinenumber or machine.hostname, changes=changes)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response({
|
||||
@@ -176,14 +188,28 @@ def update_printer_communication(machine_id: int):
|
||||
comm = Communication(machineid=machine_id, comtypeid=ip_comtype.comtypeid)
|
||||
db.session.add(comm)
|
||||
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
|
||||
# Update fields
|
||||
if 'ipaddress' in data:
|
||||
if comm.ipaddress != data['ipaddress']:
|
||||
changes['ipaddress'] = {'old': comm.ipaddress, 'new': data['ipaddress']}
|
||||
comm.ipaddress = data['ipaddress']
|
||||
if 'isprimary' in data:
|
||||
if comm.isprimary != data['isprimary']:
|
||||
changes['isprimary'] = {'old': comm.isprimary, 'new': data['isprimary']}
|
||||
comm.isprimary = data['isprimary']
|
||||
if 'macaddress' in data:
|
||||
if comm.macaddress != data['macaddress']:
|
||||
changes['macaddress'] = {'old': comm.macaddress, 'new': data['macaddress']}
|
||||
comm.macaddress = data['macaddress']
|
||||
|
||||
# Audit log if there were changes
|
||||
if changes:
|
||||
AuditLog.log('updated', 'Printer', entityid=machine_id,
|
||||
entityname=machine.machinenumber or machine.hostname, changes=changes)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response({
|
||||
@@ -211,8 +237,12 @@ def get_printer_supplies(machine_id: int):
|
||||
return error_response(ErrorCodes.VALIDATION_ERROR, 'Printer has no IP address')
|
||||
|
||||
service = ZabbixService()
|
||||
if not service.isconfigured:
|
||||
return error_response(ErrorCodes.SERVICE_UNAVAILABLE, 'Zabbix not configured')
|
||||
if not service.isconfigured or not service.isreachable:
|
||||
# Return empty supplies if Zabbix not available (fail gracefully)
|
||||
return success_response({
|
||||
'ipaddress': primary_comm.ipaddress,
|
||||
'supplies': []
|
||||
})
|
||||
|
||||
supplies = service.getsuppliesbyip(primary_comm.ipaddress)
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ from typing import Dict, List, Optional
|
||||
import requests
|
||||
from flask import current_app
|
||||
|
||||
from shopdb.extensions import cache
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -14,20 +16,70 @@ class ZabbixService:
|
||||
Zabbix API service for real-time printer supply lookups.
|
||||
|
||||
Queries Zabbix by IP address to get current supply levels.
|
||||
No caching - always returns live data.
|
||||
Use getsuppliesbyip_cached() for cached lookups or
|
||||
getsuppliesbyip() for live data.
|
||||
|
||||
Configuration:
|
||||
ZABBIX_ENABLED: Set to True to enable Zabbix integration (default: False)
|
||||
ZABBIX_URL: Zabbix API URL (e.g., http://zabbix.example.com:8080)
|
||||
ZABBIX_TOKEN: Zabbix API authentication token
|
||||
"""
|
||||
|
||||
CACHE_TTL = 600 # 10 minutes
|
||||
REACHABLE_CHECK_TTL = 60 # Check reachability every 60 seconds
|
||||
|
||||
def __init__(self):
|
||||
self._url = None
|
||||
self._token = None
|
||||
self._enabled = None
|
||||
|
||||
@property
|
||||
def isenabled(self) -> bool:
|
||||
"""Check if Zabbix integration is enabled."""
|
||||
# Check database setting first, fall back to env var
|
||||
from shopdb.core.models import Setting
|
||||
db_enabled = Setting.get('zabbix_enabled')
|
||||
if db_enabled is not None:
|
||||
return bool(db_enabled)
|
||||
# Fall back to env var for backwards compatibility
|
||||
return current_app.config.get('ZABBIX_ENABLED', False)
|
||||
|
||||
@property
|
||||
def isconfigured(self) -> bool:
|
||||
"""Check if Zabbix is configured."""
|
||||
self._url = current_app.config.get('ZABBIX_URL')
|
||||
self._token = current_app.config.get('ZABBIX_TOKEN')
|
||||
"""Check if Zabbix is enabled and configured."""
|
||||
if not self.isenabled:
|
||||
return False
|
||||
# Check database settings first, fall back to env vars
|
||||
from shopdb.core.models import Setting
|
||||
self._url = Setting.get('zabbix_url') or current_app.config.get('ZABBIX_URL')
|
||||
self._token = Setting.get('zabbix_token') or current_app.config.get('ZABBIX_TOKEN')
|
||||
return bool(self._url and self._token)
|
||||
|
||||
@property
|
||||
def isreachable(self) -> bool:
|
||||
"""Check if Zabbix is reachable (cached for 60 seconds)."""
|
||||
if not self.isenabled or not self.isconfigured:
|
||||
return False
|
||||
|
||||
cache_key = 'zabbix_reachable'
|
||||
cached = cache.get(cache_key)
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
# Quick connectivity check with 500ms timeout
|
||||
try:
|
||||
response = requests.get(
|
||||
f"{self._url}/api_jsonrpc.php",
|
||||
timeout=0.5
|
||||
)
|
||||
reachable = response.status_code in (200, 401, 403, 405)
|
||||
except requests.RequestException:
|
||||
reachable = False
|
||||
|
||||
cache.set(cache_key, reachable, timeout=self.REACHABLE_CHECK_TTL)
|
||||
logger.debug(f"Zabbix reachability check: {reachable}")
|
||||
return reachable
|
||||
|
||||
def _apicall(self, method: str, params: Dict) -> Optional[Dict]:
|
||||
"""Make a Zabbix API call."""
|
||||
if not self.isconfigured:
|
||||
@@ -46,7 +98,7 @@ class ZabbixService:
|
||||
f"{self._url}/api_jsonrpc.php",
|
||||
json=payload,
|
||||
headers={'Content-Type': 'application/json'},
|
||||
timeout=10
|
||||
timeout=0.5 # 500ms timeout - fail fast if Zabbix is slow/unreachable
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
@@ -131,3 +183,21 @@ class ZabbixService:
|
||||
"""Get Zabbix host ID for an IP address."""
|
||||
host = self.gethostbyip(ip)
|
||||
return host['hostid'] if host else None
|
||||
|
||||
def getsuppliesbyip_cached(self, ip: str) -> Optional[List[Dict]]:
|
||||
"""Get printer supply levels with caching (10-minute TTL)."""
|
||||
cache_key = f'zabbix_supplies_{ip}'
|
||||
result = cache.get(cache_key)
|
||||
if result is not None:
|
||||
return result
|
||||
|
||||
result = self.getsuppliesbyip(ip)
|
||||
if result is not None:
|
||||
cache.set(cache_key, result, timeout=self.CACHE_TTL)
|
||||
return result
|
||||
|
||||
def clearcache(self, ip: str = None):
|
||||
"""Clear cached supply data for one IP or all."""
|
||||
if ip:
|
||||
cache.delete(f'zabbix_supplies_{ip}')
|
||||
cache.delete('printers_low_supplies')
|
||||
|
||||
Reference in New Issue
Block a user