Skip to content

Commit

Permalink
edit Readme and log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick-Eichhorn committed Jan 27, 2021
1 parent 99e3007 commit 569cabf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
init:
pip install -r requirements.txt

bundle:
zip kapacitor-teams-handler.zip \
README.md \
Expand Down
53 changes: 21 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
# Python MS Teams API for Kapacitor Alerts
A respective setting already exists in the [kapacitor configuration](https://docs.influxdata.com/kapacitor/v1.5/event_handlers/microsoftteams/).

Please note that kapacitor natively provides an [event handler](https://docs.influxdata.com/kapacitor/v1.5/event_handlers/microsoftteams/) for MS Teams.
This project is for greater flexibility in relation to proxies and was developed for the Alerting UI in the **inspectIT Ocelot - Configuration Server** [Project](https://github.com/inspectIT/inspectit-ocelot/tree/master/components/inspectit-ocelot-configurationserver).

## Installation
Install all dependencies.

```bash
pip install -r requirements.txt
```

## Usage
### Bundle
Create a zip file with all required files:
```bash
make bundle
```
Create a "/teams-api" directory under the kapacitor load directory (default: /etc/kapacitor/load/handler_scripts), copy and unzip the created zip file there.
One or more handlers can be created under the /etc/kapacitor/load/handlers directory.

Optional: Edit the [configurations](configuration)

Example "teams-handler-team1.yml":
* Download the [kapacitor-teams-handler.zip](https://github.com/NovatecConsulting/kapacitor-ms-teams-handler/releases)
from the latest Release or create a zip file with all required files with `make bundle`
* Create a "handler script" directory, copy and unzip the ZIP file there. *For example under the kapacitor load directory:
`/etc/kapacitor/load/handler_scripts/teams-api`.*
This path is required later in the handler-files.
* Navigate to the handler script path and install all dependencies: `pip3 install -r requirements.txt`
* To use the MS Teams handler, create one or more handler-files `<custom_name>.yml` under the `/etc/kapacitor/load/handlers` directory.
* Edit the [configurations](configuration) to your specifications *(log path and proxy settings)*.

### Handler-file example
```yml
id: ms_teams-issue-handler_team_1
topic: ms_teams_team_1
# /etc/kapacitor/load/handlers/teams-handler-team1.yml
id: ms_teams-issue-handler_team_1 # handler id in the Ocelot - Configuration Server UI
topic: ms_teams_team_1 # channel in the Ocelot - Configuration Server UI
kind: exec
options:
prog: '/usr/bin/python3'
prog: '/usr/bin/python3' # path to python3
args:
- '/etc/kapacitor/load/handler_scripts/teams-api/main.py' # python file path (required)
- 'https://outlook.office.com/webhook/xxx' # webhook (required)
- '/etc/kapacitor/load/handler_scripts/teams-api/main.py' # entrypoint path (required)
- 'https://outlook.office.com/webhook/xxx' # webhook (required)
```
This handler will be visible on the inspectIT Ocelot - Configuration Server UI, if the Alerting settings are enabled.
Note that the entrypoint path must be refer to the previous unzipped directory.
The handlers are visible on the inspectIT Ocelot - Configuration Server UI.
### Config
The Log Path can be edited in the [configuration.py](configuration/configuration.py) file.
The log path and proxy URLs can be edited in the [configuration.py](configuration/configuration.py) file.
### Testing
The Script can be tested with the [example.json](tests/example_alert.json) file:
```bash
# root folder:
python3 main.py "https://webhook-url" < tests/example_alert.json
```
The script can be tested with the [example.json](tests/example_alert.json) file: `python3 main.py "https://webhook-url" < tests/example_alert.json`
10 changes: 5 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@

def arg_check():
logger = logging.getLogger('arg_check')
logger.debug('Check sys arguments')
try:
if len(sys.argv) == 2:
webhook = sys.argv[1]
if "https://" not in webhook:
logger.debug('The webhook URL must start with "https://"')
logger.error('The webhook URL must start with "https://" \n'
'Received URL: {}'.format(webhook))
sys.exit()
return webhook
else:
logger.debug('Not enough arguments were found/passed (Argument 1 must be the ms teams webhook)')
logger.error('Not enough or too many arguments were passed. \n'
'Example usage: python3 main.py "https://webhook-url" < tests/example_alert.json')
sys.exit()
except Exception as e:
print(e)
logger.exception("Parsing arguments failed")


def main():
Expand All @@ -29,7 +30,6 @@ def main():
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)
logger.debug('Init main')
webhook = arg_check()
logger.debug('Read in stdin json and call message_to_teams')
message_to_teams(alert_json=json.loads(sys.stdin.readline()), webhook=webhook)
Expand Down
9 changes: 4 additions & 5 deletions src/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@


def message_to_teams(alert_json: dict, webhook: str) -> None:
logger.debug('init core')
try:
proxy_dict = {"http": http_proxy, "https": https_proxy}
json_message = {
"title": alert_json["message"],
"text": alert_json["details"].replace("<br>", " \n") + " \nTime: " + alert_json["time"]
}
logger.debug("Trigger Request")
test = requests.post(url=webhook, proxies=proxy_dict, json=json_message)
logger.debug("Triggering MS-Teams webhook: {}".format(webhook))
response = requests.post(url=webhook, proxies=proxy_dict, json=json_message)
except ConnectionError as connection_error:
logger.error('Connection Error: {}'.format(connection_error))
except Exception as e:
logger.error(e)
except Exception:
logger.exception("Triggering the MS-Teams webhook failed")

0 comments on commit 569cabf

Please sign in to comment.