diff --git a/maintenance_service_http_monitoring/README.rst b/maintenance_service_http_monitoring/README.rst index 98d5171..b765f35 100644 --- a/maintenance_service_http_monitoring/README.rst +++ b/maintenance_service_http_monitoring/README.rst @@ -15,7 +15,43 @@ If the service has a URL, a request is made. If a request fails and a maintenance task has already been created for the same day, no new task is added. -The default values ​​for the cron jobs are located in `data/cron.xml`. +The default values for the cron jobs are located in `data/cron.xml`. + + +Webhook Notification +==================== + +When a new maintenance request is created (HTTP check failure), the module can +send a webhook notification to an external service (e.g., n8n, Rocket.Chat, Slack). + +Configuration +------------- + +Go to **Settings > Technical > Parameters > System Parameters** and configure: + ++--------------------------------------------------------+----------------------------------------+ +| Key | Description | ++========================================================+========================================+ +| ``maintenance_service_http_monitoring.webhook_url`` | Webhook URL (POST endpoint) | ++--------------------------------------------------------+----------------------------------------+ +| ``maintenance_service_http_monitoring.webhook_user`` | Basic Auth username (optional) | ++--------------------------------------------------------+----------------------------------------+ +| ``maintenance_service_http_monitoring.webhook_password``| Basic Auth password (optional) | ++--------------------------------------------------------+----------------------------------------+ + +Webhook Payload +--------------- + +The webhook sends a JSON POST with the following structure:: + + { + "id": 42, + "name": "[HTTP KO] Server Name", + "priority": "2", + "description": "Service KO: https://example.com", + "equipment": "Server Name", + "link": "https://odoo.example.com/web#id=42&model=maintenance.request&view_type=form" + } Installation diff --git a/maintenance_service_http_monitoring/__manifest__.py b/maintenance_service_http_monitoring/__manifest__.py index 37a8ca3..24aa49a 100644 --- a/maintenance_service_http_monitoring/__manifest__.py +++ b/maintenance_service_http_monitoring/__manifest__.py @@ -8,6 +8,7 @@ "depends": ["base", "maintenance", "maintenance_server_data"], "external_dependencies": {"python": ["requests"]}, "data": [ + "data/ir_config_parameter.xml", "data/cron.xml", "views/service_instance_views.xml", "views/maintenance_equipment_views.xml" diff --git a/maintenance_service_http_monitoring/data/ir_config_parameter.xml b/maintenance_service_http_monitoring/data/ir_config_parameter.xml new file mode 100644 index 0000000..5c40ae5 --- /dev/null +++ b/maintenance_service_http_monitoring/data/ir_config_parameter.xml @@ -0,0 +1,15 @@ + + + + maintenance_service_http_monitoring.webhook_url + + + + maintenance_service_http_monitoring.webhook_user + + + + maintenance_service_http_monitoring.webhook_password + + + diff --git a/maintenance_service_http_monitoring/models/maintenance_equipment.py b/maintenance_service_http_monitoring/models/maintenance_equipment.py index 3a86cb3..cb42a1b 100644 --- a/maintenance_service_http_monitoring/models/maintenance_equipment.py +++ b/maintenance_service_http_monitoring/models/maintenance_equipment.py @@ -1,6 +1,16 @@ +import logging from datetime import timedelta from odoo import models, fields, api +try: + import requests as http_requests +except ImportError: + http_requests = None + +_logger = logging.getLogger(__name__) + +WEBHOOK_TIMEOUT = 10 # seconds + class MaintenanceEquipment(models.Model): _inherit = 'maintenance.equipment' @@ -90,8 +100,51 @@ class MaintenanceEquipment(models.Model): vals['maintenance_team_id'] = team.id request = self.env['maintenance.request'].create(vals) self.http_maintenance_request = request.id + self._notify_webhook(request, ko_services) return request + def _notify_webhook(self, request, ko_services): + """Send a webhook notification when a new maintenance request is created.""" + ICP = self.env['ir.config_parameter'].sudo() + webhook_url = ICP.get_param( + 'maintenance_service_http_monitoring.webhook_url', '' + ) + if not webhook_url: + return + webhook_user = ICP.get_param( + 'maintenance_service_http_monitoring.webhook_user', '' + ) + webhook_password = ICP.get_param( + 'maintenance_service_http_monitoring.webhook_password', '' + ) + base_url = ICP.get_param('web.base.url', '') + link = ( + f"{base_url}/web#id={request.id}" + f"&model=maintenance.request&view_type=form" + ) + payload = { + 'id': request.id, + 'name': request.name, + 'description': request.description or '', + 'equipment': self.name, + 'link': link, + } + auth = None + if webhook_user and webhook_password: + auth = (webhook_user, webhook_password) + try: + http_requests.post( + webhook_url, + json=payload, + auth=auth, + timeout=WEBHOOK_TIMEOUT, + ) + except Exception as e: + _logger.warning( + "Webhook notification failed for maintenance request %s: %s", + request.id, e, + ) + def _build_ko_services_description(self, ko_services): lines = [f"Service KO: {s.service_url or s.name}" for s in ko_services] return '\n'.join(lines)