Skip to content

Commit

Permalink
feat: checkmk provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ezhil56x committed Oct 29, 2024
1 parent e887984 commit cc3e64e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
10 changes: 5 additions & 5 deletions docs/providers/documentation/checkmk-provider.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ To connect Checkmk to Keep, you need to configure it as a webhook from Checkmk.
2. You can download the Keep webhook script using the following command:

```bash
wget -O webhook-keephq.py https://github.com/keephq/keep/blob/main/keep/providers/checkmk_provider/webhook-keephq.py?raw=true
wget -O webhook-keep.py https://github.com/keephq/keep/blob/main/keep/providers/checkmk_provider/webhook-keep.py?raw=true
```

3. Copy the downloaded script to the following path on the Checkmk server:

If you are using Checkmk Docker container, then copy it to the following path according to your docker volume mapping:

```bash
cp webhook-keephq.py /omd/sites/<site_name>/local/share/check_mk/notifications/webhook-keephq.py
cp webhook-keep.py /omd/sites/<site_name>/local/share/check_mk/notifications/webhook-keep.py
cd /omd/sites/<site_name>/local/share/check_mk/notifications
```

If you are using Checkmk installed on the server, then copy it to the following path:

```bash
cp webhook-keephq.py ~/local/share/check_mk/notifications/webhook-keephq.py
cp webhook-keep.py ~/local/share/check_mk/notifications/webhook-keep.py
cd ~/local/share/check_mk/notifications
```

4. Make the script executable:

```bash
chmod +x webhook-keephq.py
chmod +x webhook-keep.py
```

5. Now go to the Checkmk web interface and navigate to Setup
Expand All @@ -66,7 +66,7 @@ chmod +x webhook-keephq.py
<img height="10" src="/images/checkmk-provider_3.png" />
</Frame>

7. In the Notifications method method, select "Webhook - KeepHQ" as the notification method.
7. In the Notifications method method, select "webhook-keep" as the notification method.

<Frame
width="100"
Expand Down
13 changes: 10 additions & 3 deletions keep/providers/checkmk_provider/checkmk_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class CheckmkProvider(BaseProvider):

PROVIDER_DISPLAY_NAME = "Checkmk"
PROVIDER_TAGS = ["alert"]

FINGERPRINT_FIELDS = ["id"]

def __init__(
self, context_manager: ContextManager, provider_id: str, config: ProviderConfig
Expand All @@ -55,12 +57,17 @@ def validate_config():

@staticmethod
def _format_alert(event: dict, provider_instance: BaseProvider = None) -> AlertDto | list[AlertDto]:
"""
Service alerts and Host alerts have different fields, so we are mapping the fields based on the event type.
"""
def _check_values(value):
if value not in event or event[value] == '':
if value not in event or event.get(value) == '':
return None
return event[value]
return event.get(value)

# based on the status of the event, set the severity
"""
Service alerts don't have a status field, so we are mapping the status based on the severity.
"""
def _set_severity(status):
if status == "UP":
return AlertSeverity.INFO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#!/usr/bin/env python3
# Webhook - KeepHQ
# webhook-keep

"""
This script needs to be copied to the Checkmk server to send notifications to keep.
For more details on how to configure Checkmk to send alerts to Keep, see https://docs.keephq.dev/providers/documentation/checkmk-provider.
"""

import os
import sys
import requests


# Get KeepHQ Webhook URL and API Key from environment variables
# Get keep Webhook URL and API Key from environment variables
def GetPluginParams():
env_vars = os.environ

Expand All @@ -15,7 +20,7 @@ def GetPluginParams():

# "None", if not in the environment variables
if (WebHookURL == "None" or API_KEY == "None"):
print("KeepHQ-plugin: Missing Webhook URL or API Key")
print("keep-plugin: Missing Webhook URL or API Key")
return 2, "" # https://docs.checkmk.com/latest/en/notifications.html#_traceable_notifications

return 0, WebHookURL
Expand Down Expand Up @@ -123,8 +128,8 @@ def GetNotificationDetails():
return notify


# Start KeepHQ workflow
def StartKeepHQWorkflow(WebHookURL, data):
# Start Keep workflow
def StartKeepWorkflow(WebHookURL, data):
return_code = 0

API_KEY = str(os.environ.get("NOTIFY_PARAMETER_2"))
Expand All @@ -139,32 +144,32 @@ def StartKeepHQWorkflow(WebHookURL, data):
response = requests.post(WebHookURL, headers=headers, json=data)

if response.status_code == 200:
print("KeepHQ-plugin: Workflow started successfully.")
print("keep-plugin: Workflow started successfully.")
else:
print(
f"KeepHQ-plugin: Failed to start the workflow. Status code: {response.status_code}")
f"keep-plugin: Failed to start the workflow. Status code: {response.status_code}")
print(response.text)
return_code = 2
except Exception as e:
print(f"KeepHQ-plugin: An error occurred: {e}")
print(f"keep-plugin: An error occurred: {e}")
return_code = 2

return return_code


def main():
print("KeepHQ-plugin: Starting...")
print("keep-plugin: Starting...")
return_code, WebHookURL = GetPluginParams()

if return_code != 0:
return return_code # Abort, if parameter for the webhook is missing

print("KeepHQ-plugin: Getting notification details...")
print("keep-plugin: Getting notification details...")
data = GetNotificationDetails()

print("KeepHQ-plugin: Starting KeepHQ workflow...")
return_code = StartKeepHQWorkflow(WebHookURL, data)
print("KeepHQ-plugin: Finished.")
print("keep-plugin: Starting Keep workflow...")
return_code = StartKeepWorkflow(WebHookURL, data)
print("keep-plugin: Finished.")
return return_code


Expand Down

0 comments on commit cc3e64e

Please sign in to comment.