|
| 1 | +""" |
| 2 | +Nagios Provider is a class that provides a way to receive alerts from Nagios |
| 3 | +using webhook notifications. |
| 4 | +""" |
| 5 | + |
| 6 | +from keep.api.models.alert import AlertDto, AlertSeverity, AlertStatus |
| 7 | +from keep.contextmanager.contextmanager import ContextManager |
| 8 | +from keep.providers.base.base_provider import BaseProvider |
| 9 | +from keep.providers.models.provider_config import ProviderConfig |
| 10 | + |
| 11 | + |
| 12 | +class NagiosProvider(BaseProvider): |
| 13 | + """ |
| 14 | + Receive Nagios host and service notifications into Keep via webhooks. |
| 15 | + """ |
| 16 | + |
| 17 | + webhook_documentation_here_differs_from_general_documentation = True |
| 18 | + webhook_description = "" |
| 19 | + webhook_template = "" |
| 20 | + webhook_markdown = """ |
| 21 | +To send alerts from Nagios to Keep, configure a custom notification command: |
| 22 | +
|
| 23 | +1. Create a notification command that POSTs JSON to `{keep_webhook_api_url}`. |
| 24 | +2. Add header `X-API-KEY` with your Keep API key (webhook role). |
| 25 | +3. Use Nagios macros such as `$NOTIFICATIONTYPE$`, `$HOSTNAME$`, `$HOSTSTATE$`, |
| 26 | + `$HOSTOUTPUT$`, `$SERVICEDESC$`, `$SERVICESTATE$`, and `$SERVICEOUTPUT$` to |
| 27 | + build the JSON payload. |
| 28 | +4. Attach the command to your host and/or service notification definitions. |
| 29 | +5. For a complete example, see the [Keep documentation](https://docs.keephq.dev/providers/documentation/nagios-provider). |
| 30 | + """ |
| 31 | + |
| 32 | + PROVIDER_DISPLAY_NAME = "Nagios" |
| 33 | + PROVIDER_TAGS = ["alert"] |
| 34 | + PROVIDER_CATEGORY = ["Monitoring"] |
| 35 | + WEBHOOK_INSTALLATION_REQUIRED = True |
| 36 | + |
| 37 | + HOST_STATUS_MAP = { |
| 38 | + "UP": AlertStatus.RESOLVED, |
| 39 | + "DOWN": AlertStatus.FIRING, |
| 40 | + "UNREACHABLE": AlertStatus.FIRING, |
| 41 | + } |
| 42 | + |
| 43 | + HOST_SEVERITY_MAP = { |
| 44 | + "UP": AlertSeverity.INFO, |
| 45 | + "DOWN": AlertSeverity.CRITICAL, |
| 46 | + "UNREACHABLE": AlertSeverity.CRITICAL, |
| 47 | + } |
| 48 | + |
| 49 | + SERVICE_STATUS_MAP = { |
| 50 | + "OK": AlertStatus.RESOLVED, |
| 51 | + "WARNING": AlertStatus.FIRING, |
| 52 | + "CRITICAL": AlertStatus.FIRING, |
| 53 | + "UNKNOWN": AlertStatus.FIRING, |
| 54 | + } |
| 55 | + |
| 56 | + SERVICE_SEVERITY_MAP = { |
| 57 | + "OK": AlertSeverity.INFO, |
| 58 | + "WARNING": AlertSeverity.WARNING, |
| 59 | + "CRITICAL": AlertSeverity.CRITICAL, |
| 60 | + "UNKNOWN": AlertSeverity.INFO, |
| 61 | + } |
| 62 | + |
| 63 | + def __init__( |
| 64 | + self, context_manager: ContextManager, provider_id: str, config: ProviderConfig |
| 65 | + ): |
| 66 | + super().__init__(context_manager, provider_id, config) |
| 67 | + |
| 68 | + def dispose(self): |
| 69 | + """ |
| 70 | + Dispose of the provider. |
| 71 | + """ |
| 72 | + pass |
| 73 | + |
| 74 | + def validate_config(self): |
| 75 | + """ |
| 76 | + Nagios webhook integration does not require provider-side configuration. |
| 77 | + """ |
| 78 | + pass |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def _format_alert( |
| 82 | + event: dict, provider_instance: "BaseProvider" = None |
| 83 | + ) -> AlertDto | list[AlertDto]: |
| 84 | + """ |
| 85 | + Format Nagios notification payload into Keep alert format. |
| 86 | +
|
| 87 | + Supported payloads are intentionally simple and map closely to Nagios |
| 88 | + host/service notification macros configured in the webhook command. |
| 89 | + """ |
| 90 | + alert_type = (event.get("alert_type") or "service").lower() |
| 91 | + |
| 92 | + if alert_type == "host": |
| 93 | + state = event.get("host_state", "DOWN") |
| 94 | + output = event.get("host_output", "No output provided") |
| 95 | + return AlertDto( |
| 96 | + id=event.get("host_name"), |
| 97 | + name=event.get("host_display_name") or event.get("host_name"), |
| 98 | + status=NagiosProvider.HOST_STATUS_MAP.get(state, AlertStatus.FIRING), |
| 99 | + severity=NagiosProvider.HOST_SEVERITY_MAP.get( |
| 100 | + state, AlertSeverity.CRITICAL |
| 101 | + ), |
| 102 | + description=output, |
| 103 | + source=["nagios"], |
| 104 | + hostname=event.get("host_name"), |
| 105 | + state=state, |
| 106 | + lastReceived=event.get("timestamp"), |
| 107 | + timestamp=event.get("timestamp"), |
| 108 | + notification_type=event.get("notification_type"), |
| 109 | + raw_output=output, |
| 110 | + host_address=event.get("host_address"), |
| 111 | + ) |
| 112 | + |
| 113 | + state = event.get("service_state", "CRITICAL") |
| 114 | + output = event.get("service_output", "No output provided") |
| 115 | + return AlertDto( |
| 116 | + id=event.get("service_name") or event.get("host_name"), |
| 117 | + name=event.get("service_display_name") or event.get("service_name"), |
| 118 | + status=NagiosProvider.SERVICE_STATUS_MAP.get(state, AlertStatus.FIRING), |
| 119 | + severity=NagiosProvider.SERVICE_SEVERITY_MAP.get( |
| 120 | + state, AlertSeverity.CRITICAL |
| 121 | + ), |
| 122 | + description=output, |
| 123 | + source=["nagios"], |
| 124 | + hostname=event.get("host_name"), |
| 125 | + host_address=event.get("host_address"), |
| 126 | + service_name=event.get("service_name"), |
| 127 | + check_command=event.get("service_check_command"), |
| 128 | + state=state, |
| 129 | + lastReceived=event.get("timestamp"), |
| 130 | + timestamp=event.get("timestamp"), |
| 131 | + notification_type=event.get("notification_type"), |
| 132 | + raw_output=output, |
| 133 | + current_attempt=event.get("service_attempt"), |
| 134 | + state_type=event.get("service_state_type"), |
| 135 | + performance_data=event.get("service_perfdata"), |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + pass |
0 commit comments