Skip to content

Commit

Permalink
feat: adding Zenduty provider (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahargl authored Feb 22, 2023
1 parent 9ab6613 commit d36dec6
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/docs/providers/documentation/pushover.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_label: Pushover Provider

# Pushover
Pushover docs
ען

## Inputs
The Pushover provider gets "message" as an input which will be used as the notification message.
Configuration example:
Expand Down
Binary file added docs/docs/providers/documentation/zenduty.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions docs/docs/providers/documentation/zenduty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
sidebar_label: Zenduty Provider
---

# Zenduty
Zenduty docs

![User key](zenduty.jpg?raw=true)
## Inputs
The Zenduty provider gets "title", "summary" and "service" as an input which will be used for the incident.
The `query` method of the ZendutyProvider` class takes the following inputs:

- `title`: The title of Zenduty incident.
- `summary`: The summary of Zenduty incident.
- `service`: The service of Zenduty incident.

## Outputs
None.

## Authentication Parameters
The Zenduty gets api key as an authentication method.
- `api_key` - Zenduty Api Key
Authentication configuration example:
```
zenduty:
authentication:
api_key: XXXXXXXXXXXXXXXX
```
## Useful Links
- https://docs.zenduty.com/docs/api
47 changes: 47 additions & 0 deletions examples/alerts/db_disk_space_zenduty.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Database disk space is low (<10%)
alert:
id: db-disk-space
description: Check that the DB has enough disk space
owners:
- github-shahargl
- slack-talboren
services:
- db
- api
trigger:
# Run every hour or if the service-is-failing alert is triggered
interval: 1h
event:
- id: service-is-failing
type: alert
steps:
- name: db-no-space
provider:
type: mock
config: "{{ providers.db-server-mock }}"
with:
command: df -h | grep /dev/disk3s1s1 | awk '{ print $5}' # Check the disk space
command_output: 91% # Mock
condition:
- type: threshold
value: "{{ steps.this.results }}"
compare_to: 90% # Trigger if more than 90% full
failure_strategy: # What to do if the SSH connection failed?
- name: ssh-connection-failed
retry: 5 # Retry 5 times
alert: true # Finally, alert
actions:
- name: trigger-zenduty
provider:
type: zenduty
config: " {{ providers.zenduty-test }} "
with:
summary: test incident message
service: 9c6ddc88-16a0-4ce8-85ab-181760d8cb87
title: test incident title


providers:
db-server-mock:
description: Paper DB Server
authentication:
5 changes: 1 addition & 4 deletions keep/providers/pushover_provider/pushover_provider.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
"""
SlackOutput is a class that implements the BaseOutputProvider interface for Slack messages.
"""
import dataclasses

import pydantic
Expand All @@ -13,7 +10,7 @@

@pydantic.dataclasses.dataclass
class PushoverProviderAuthConfig:
"""Slack authentication configuration."""
"""Pushover authentication configuration."""

token: str = dataclasses.field(
metadata={"required": True, "description": "Pushover app token"}
Expand Down
Empty file.
96 changes: 96 additions & 0 deletions keep/providers/zenduty_provider/zenduty_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import dataclasses

import pydantic
import zenduty

from keep.exceptions.provider_exception import ProviderException
from keep.providers.base.base_provider import BaseProvider
from keep.providers.models.provider_config import ProviderConfig


@pydantic.dataclasses.dataclass
class ZendutyProviderAuthConfig:
"""Zenduty authentication configuration."""

api_key: str = dataclasses.field(
metadata={"required": True, "description": "Zenduty api key"}
)


class ZendutyProvider(BaseProvider):
def __init__(self, provider_id: str, config: ProviderConfig):
super().__init__(provider_id, config)
self.zenduty_client = zenduty.IncidentsApi(
zenduty.ApiClient(self.authentication_config.api_key)
)

def validate_config(self):
self.authentication_config = ZendutyProviderAuthConfig(
**self.config.authentication
)

def dispose(self):
"""
No need to dispose of anything, so just do nothing.
"""
pass

def notify(self, **kwargs: dict):
"""
Create incident Zenduty using the Zenduty API
https://github.com/Zenduty/zenduty-python-sdk
Args:
kwargs (dict): The providers with context
"""
self.logger.debug("Notifying incident to Zenduty")
title = kwargs.pop("title", "")
summary = kwargs.pop("summary", "")
user = kwargs.pop("user", None)
service = kwargs.pop("service", "")
policy = kwargs.pop("policy", "")

if not service:
raise ProviderException("Service is required")
if not title or not summary:
raise ProviderException("Title and summary are required")

body = {
"service": service,
"policy": policy,
"user": user,
"title": title,
"summary": summary,
}
resp = self.zenduty_client.create_incident(body)
assert resp.status == 201
self.logger.debug("Alert message notified to Zenduty")


if __name__ == "__main__":
# Output debug messages
import logging

logging.basicConfig(level=logging.DEBUG, handlers=[logging.StreamHandler()])

# Load environment variables
import os

zenduty_key = os.environ.get("ZENDUTY_KEY")
assert zenduty_key

# Initalize the provider and provider config
config = ProviderConfig(
description="Zenduty Output Provider",
authentication={"api_key": zenduty_key},
)
provider = ZendutyProvider(provider_id="zenduty-test", config=config)
provider.notify(
message="Simple incident showing context with name: {name}".format(
name="John Doe"
),
title="Simple incident",
summary="Simple incident showing context with name: John Doe",
service="9c6ddc88-16a0-4ce8-85ab-181760d8cb87",
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ astunparse = "^1.6.3"
python-json-logger = "^2.0.6"
boto3 = "^1.26.72"
validators = "^0.20.0"
zenduty-api = "^0.2"


[tool.poetry.group.dev.dependencies]
Expand Down

1 comment on commit d36dec6

@vercel
Copy link

@vercel vercel bot commented on d36dec6 Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.