[ADD] maintenance_service_http_monitoring: webhook_rocketchat_via_n8n + parameters
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 6s
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 6s
This commit is contained in:
@@ -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,
|
If a request fails and a maintenance task has already been created for the same day,
|
||||||
no new task is added.
|
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
|
Installation
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
"depends": ["base", "maintenance", "maintenance_server_data"],
|
"depends": ["base", "maintenance", "maintenance_server_data"],
|
||||||
"external_dependencies": {"python": ["requests"]},
|
"external_dependencies": {"python": ["requests"]},
|
||||||
"data": [
|
"data": [
|
||||||
|
"data/ir_config_parameter.xml",
|
||||||
"data/cron.xml",
|
"data/cron.xml",
|
||||||
"views/service_instance_views.xml",
|
"views/service_instance_views.xml",
|
||||||
"views/maintenance_equipment_views.xml"
|
"views/maintenance_equipment_views.xml"
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<odoo noupdate="1">
|
||||||
|
<record id="config_param_webhook_url" model="ir.config_parameter">
|
||||||
|
<field name="key">maintenance_service_http_monitoring.webhook_url</field>
|
||||||
|
<field name="value"></field>
|
||||||
|
</record>
|
||||||
|
<record id="config_param_webhook_user" model="ir.config_parameter">
|
||||||
|
<field name="key">maintenance_service_http_monitoring.webhook_user</field>
|
||||||
|
<field name="value"></field>
|
||||||
|
</record>
|
||||||
|
<record id="config_param_webhook_password" model="ir.config_parameter">
|
||||||
|
<field name="key">maintenance_service_http_monitoring.webhook_password</field>
|
||||||
|
<field name="value"></field>
|
||||||
|
</record>
|
||||||
|
</odoo>
|
||||||
@@ -1,6 +1,16 @@
|
|||||||
|
import logging
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from odoo import models, fields, api
|
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):
|
class MaintenanceEquipment(models.Model):
|
||||||
_inherit = 'maintenance.equipment'
|
_inherit = 'maintenance.equipment'
|
||||||
|
|
||||||
@@ -90,8 +100,51 @@ class MaintenanceEquipment(models.Model):
|
|||||||
vals['maintenance_team_id'] = team.id
|
vals['maintenance_team_id'] = team.id
|
||||||
request = self.env['maintenance.request'].create(vals)
|
request = self.env['maintenance.request'].create(vals)
|
||||||
self.http_maintenance_request = request.id
|
self.http_maintenance_request = request.id
|
||||||
|
self._notify_webhook(request, ko_services)
|
||||||
return request
|
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):
|
def _build_ko_services_description(self, ko_services):
|
||||||
lines = [f"Service KO: {s.service_url or s.name}" for s in ko_services]
|
lines = [f"Service KO: {s.service_url or s.name}" for s in ko_services]
|
||||||
return '\n'.join(lines)
|
return '\n'.join(lines)
|
||||||
|
|||||||
Reference in New Issue
Block a user