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:
@@ -4,7 +4,7 @@ from flask import Blueprint, request
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models import Asset, AssetType, OperatingSystem, Application, AppVersion
|
||||
from shopdb.core.models import Asset, AssetType, OperatingSystem, Application, AppVersion, AuditLog
|
||||
from shopdb.utils.responses import (
|
||||
success_response,
|
||||
error_response,
|
||||
@@ -348,6 +348,12 @@ def create_computer():
|
||||
)
|
||||
|
||||
db.session.add(comp)
|
||||
db.session.flush()
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('created', 'Computer', entityid=comp.computerid,
|
||||
entityname=data.get('hostname') or data['assetnumber'])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
@@ -394,11 +400,18 @@ def update_computer(computer_id: int):
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
|
||||
# Update asset fields
|
||||
asset_fields = ['assetnumber', 'name', 'serialnumber', 'statusid',
|
||||
'locationid', 'businessunitid', 'mapleft', 'maptop', 'notes', 'isactive']
|
||||
for key in asset_fields:
|
||||
if key in data:
|
||||
old_val = getattr(asset, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(asset, key, data[key])
|
||||
|
||||
# Update computer fields
|
||||
@@ -406,8 +419,17 @@ def update_computer(computer_id: int):
|
||||
'lastreporteddate', 'lastboottime', 'isvnc', 'iswinrm', 'isshopfloor']
|
||||
for key in computer_fields:
|
||||
if key in data:
|
||||
old_val = getattr(comp, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(comp, key, data[key])
|
||||
|
||||
# Audit log if there were changes
|
||||
if changes:
|
||||
AuditLog.log('updated', 'Computer', entityid=comp.computerid,
|
||||
entityname=comp.hostname or asset.assetnumber, changes=changes)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
@@ -431,6 +453,11 @@ def delete_computer(computer_id: int):
|
||||
|
||||
# Soft delete the asset
|
||||
comp.asset.isactive = False
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('deleted', 'Computer', entityid=comp.computerid,
|
||||
entityname=comp.hostname or comp.asset.assetnumber)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Computer deleted')
|
||||
|
||||
@@ -4,7 +4,7 @@ from flask import Blueprint, request
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models import Asset, AssetType, Vendor, Model
|
||||
from shopdb.core.models import Asset, AssetType, Vendor, Model, AuditLog
|
||||
from shopdb.utils.responses import (
|
||||
success_response,
|
||||
error_response,
|
||||
@@ -311,6 +311,12 @@ def create_equipment():
|
||||
)
|
||||
|
||||
db.session.add(equip)
|
||||
db.session.flush()
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('created', 'Equipment', entityid=equip.equipmentid,
|
||||
entityname=data['assetnumber'])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
@@ -347,11 +353,18 @@ def update_equipment(equipment_id: int):
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
|
||||
# Update asset fields
|
||||
asset_fields = ['assetnumber', 'name', 'serialnumber', 'statusid',
|
||||
'locationid', 'businessunitid', 'mapleft', 'maptop', 'notes', 'isactive']
|
||||
for key in asset_fields:
|
||||
if key in data:
|
||||
old_val = getattr(asset, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(asset, key, data[key])
|
||||
|
||||
# Update equipment fields
|
||||
@@ -361,8 +374,17 @@ def update_equipment(equipment_id: int):
|
||||
'controllervendorid', 'controllermodelid']
|
||||
for key in equipment_fields:
|
||||
if key in data:
|
||||
old_val = getattr(equip, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(equip, key, data[key])
|
||||
|
||||
# Audit log if there were changes
|
||||
if changes:
|
||||
AuditLog.log('updated', 'Equipment', entityid=equip.equipmentid,
|
||||
entityname=asset.assetnumber, changes=changes)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
@@ -386,6 +408,11 @@ def delete_equipment(equipment_id: int):
|
||||
|
||||
# Soft delete the asset (equipment extension will stay linked)
|
||||
equip.asset.isactive = False
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('deleted', 'Equipment', entityid=equip.equipmentid,
|
||||
entityname=equip.asset.assetnumber)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Equipment deleted')
|
||||
|
||||
@@ -4,7 +4,7 @@ from flask import Blueprint, request
|
||||
from flask_jwt_extended import jwt_required
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models import Asset, AssetType, Vendor
|
||||
from shopdb.core.models import Asset, AssetType, Vendor, AuditLog
|
||||
from shopdb.utils.responses import (
|
||||
success_response,
|
||||
error_response,
|
||||
@@ -350,6 +350,12 @@ def create_network_device():
|
||||
)
|
||||
|
||||
db.session.add(netdev)
|
||||
db.session.flush()
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('created', 'NetworkDevice', entityid=netdev.networkdeviceid,
|
||||
entityname=data.get('hostname') or data['assetnumber'])
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
@@ -396,11 +402,18 @@ def update_network_device(device_id: int):
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
|
||||
# Update asset fields
|
||||
asset_fields = ['assetnumber', 'name', 'serialnumber', 'statusid',
|
||||
'locationid', 'businessunitid', 'mapleft', 'maptop', 'notes', 'isactive']
|
||||
for key in asset_fields:
|
||||
if key in data:
|
||||
old_val = getattr(asset, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(asset, key, data[key])
|
||||
|
||||
# Update network device fields
|
||||
@@ -408,8 +421,17 @@ def update_network_device(device_id: int):
|
||||
'firmwareversion', 'portcount', 'ispoe', 'ismanaged', 'rackunit']
|
||||
for key in netdev_fields:
|
||||
if key in data:
|
||||
old_val = getattr(netdev, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(netdev, key, data[key])
|
||||
|
||||
# Audit log if there were changes
|
||||
if changes:
|
||||
AuditLog.log('updated', 'NetworkDevice', entityid=netdev.networkdeviceid,
|
||||
entityname=netdev.hostname or asset.assetnumber, changes=changes)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
result = asset.to_dict()
|
||||
@@ -433,6 +455,11 @@ def delete_network_device(device_id: int):
|
||||
|
||||
# Soft delete the asset
|
||||
netdev.asset.isactive = False
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('deleted', 'NetworkDevice', entityid=netdev.networkdeviceid,
|
||||
entityname=netdev.hostname or netdev.asset.assetnumber)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Network device deleted')
|
||||
@@ -573,6 +600,12 @@ def create_vlan():
|
||||
)
|
||||
|
||||
db.session.add(vlan)
|
||||
db.session.flush()
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('created', 'VLAN', entityid=vlan.vlanid,
|
||||
entityname=f"VLAN {vlan.vlannumber} - {vlan.name}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(vlan.to_dict(), message='VLAN created', http_code=201)
|
||||
@@ -604,10 +637,21 @@ def update_vlan(vlan_id: int):
|
||||
http_code=409
|
||||
)
|
||||
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
for key in ['vlannumber', 'name', 'description', 'vlantype', 'isactive']:
|
||||
if key in data:
|
||||
old_val = getattr(vlan, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(vlan, key, data[key])
|
||||
|
||||
# Audit log if there were changes
|
||||
if changes:
|
||||
AuditLog.log('updated', 'VLAN', entityid=vlan.vlanid,
|
||||
entityname=f"VLAN {vlan.vlannumber} - {vlan.name}", changes=changes)
|
||||
|
||||
db.session.commit()
|
||||
return success_response(vlan.to_dict(), message='VLAN updated')
|
||||
|
||||
@@ -634,6 +678,11 @@ def delete_vlan(vlan_id: int):
|
||||
)
|
||||
|
||||
vlan.isactive = False
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('deleted', 'VLAN', entityid=vlan.vlanid,
|
||||
entityname=f"VLAN {vlan.vlannumber} - {vlan.name}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='VLAN deleted')
|
||||
@@ -754,6 +803,12 @@ def create_subnet():
|
||||
)
|
||||
|
||||
db.session.add(subnet)
|
||||
db.session.flush()
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('created', 'Subnet', entityid=subnet.subnetid,
|
||||
entityname=f"{subnet.cidr} - {subnet.name}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(subnet.to_dict(), message='Subnet created', http_code=201)
|
||||
@@ -790,10 +845,21 @@ def update_subnet(subnet_id: int):
|
||||
'locationid', 'dhcpenabled', 'dhcprangestart', 'dhcprangeend',
|
||||
'dns1', 'dns2', 'isactive']
|
||||
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
for key in allowed_fields:
|
||||
if key in data:
|
||||
old_val = getattr(subnet, key)
|
||||
new_val = data[key]
|
||||
if old_val != new_val:
|
||||
changes[key] = {'old': old_val, 'new': new_val}
|
||||
setattr(subnet, key, data[key])
|
||||
|
||||
# Audit log if there were changes
|
||||
if changes:
|
||||
AuditLog.log('updated', 'Subnet', entityid=subnet.subnetid,
|
||||
entityname=f"{subnet.cidr} - {subnet.name}", changes=changes)
|
||||
|
||||
db.session.commit()
|
||||
return success_response(subnet.to_dict(), message='Subnet updated')
|
||||
|
||||
@@ -812,6 +878,11 @@ def delete_subnet(subnet_id: int):
|
||||
)
|
||||
|
||||
subnet.isactive = False
|
||||
|
||||
# Audit log
|
||||
AuditLog.log('deleted', 'Subnet', entityid=subnet.subnetid,
|
||||
entityname=f"{subnet.cidr} - {subnet.name}")
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(message='Subnet deleted')
|
||||
|
||||
@@ -285,17 +285,20 @@ def get_active_notifications():
|
||||
"""
|
||||
now = datetime.utcnow()
|
||||
|
||||
from datetime import timedelta
|
||||
lookahead = now + timedelta(days=10)
|
||||
|
||||
notifications = Notification.query.filter(
|
||||
Notification.isactive == True,
|
||||
db.or_(
|
||||
Notification.starttime.is_(None),
|
||||
Notification.starttime <= now
|
||||
Notification.starttime <= lookahead
|
||||
),
|
||||
db.or_(
|
||||
Notification.endtime.is_(None),
|
||||
Notification.endtime >= now
|
||||
)
|
||||
).order_by(Notification.starttime.desc()).all()
|
||||
).order_by(Notification.starttime.asc()).all()
|
||||
|
||||
data = [n.to_dict() for n in notifications]
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -5,6 +5,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||
from datetime import datetime
|
||||
|
||||
from shopdb.extensions import db
|
||||
from shopdb.core.models import AuditLog
|
||||
from shopdb.utils.responses import (
|
||||
success_response,
|
||||
error_response,
|
||||
@@ -140,6 +141,11 @@ def create_usb_device():
|
||||
)
|
||||
|
||||
db.session.add(device)
|
||||
db.session.flush()
|
||||
|
||||
AuditLog.log('created', 'USBDevice', entityid=device.usbdeviceid,
|
||||
entityname=device.label or device.serialnumber)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(device.to_dict(), message='Device created', http_code=201)
|
||||
@@ -184,13 +190,22 @@ def update_usb_device(device_id: int):
|
||||
|
||||
data = request.get_json() or {}
|
||||
|
||||
# Update allowed fields
|
||||
# Track changes for audit log
|
||||
changes = {}
|
||||
for field in ['label', 'assetnumber', 'usbdevicetypeid', 'capacitygb',
|
||||
'vendorid', 'productid', 'manufacturer', 'productname',
|
||||
'storagelocation', 'pin', 'notes']:
|
||||
if field in data:
|
||||
old_val = getattr(device, field)
|
||||
new_val = data[field]
|
||||
if old_val != new_val:
|
||||
changes[field] = {'old': old_val, 'new': new_val}
|
||||
setattr(device, field, data[field])
|
||||
|
||||
if changes:
|
||||
AuditLog.log('updated', 'USBDevice', entityid=device.usbdeviceid,
|
||||
entityname=device.label or device.serialnumber, changes=changes)
|
||||
|
||||
device.modifieddate = datetime.utcnow()
|
||||
db.session.commit()
|
||||
|
||||
@@ -219,6 +234,10 @@ def delete_usb_device(device_id: int):
|
||||
|
||||
device.isactive = False
|
||||
device.modifieddate = datetime.utcnow()
|
||||
|
||||
AuditLog.log('deleted', 'USBDevice', entityid=device.usbdeviceid,
|
||||
entityname=device.label or device.serialnumber)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(None, message='Device deleted')
|
||||
@@ -272,6 +291,11 @@ def checkout_device(device_id: int):
|
||||
device.modifieddate = datetime.utcnow()
|
||||
|
||||
db.session.add(checkout)
|
||||
|
||||
AuditLog.log('checked_out', 'USBDevice', entityid=device.usbdeviceid,
|
||||
entityname=device.label or device.serialnumber,
|
||||
changes={'checked_out_to': data['sso'], 'reason': data.get('checkoutreason')})
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(checkout.to_dict(), message='Device checked out', http_code=201)
|
||||
@@ -311,12 +335,17 @@ def checkin_device(device_id: int):
|
||||
active_checkout.waswiped = data.get('waswiped', False)
|
||||
|
||||
# Update device status
|
||||
previous_user = device.currentuserid
|
||||
device.ischeckedout = False
|
||||
device.currentuserid = None
|
||||
device.currentusername = None
|
||||
device.currentcheckoutdate = None
|
||||
device.modifieddate = datetime.utcnow()
|
||||
|
||||
AuditLog.log('checked_in', 'USBDevice', entityid=device.usbdeviceid,
|
||||
entityname=device.label or device.serialnumber,
|
||||
changes={'returned_by': previous_user, 'wiped': data.get('waswiped', False)})
|
||||
|
||||
db.session.commit()
|
||||
|
||||
return success_response(
|
||||
|
||||
Reference in New Issue
Block a user