mirror of
https://github.com/zulip/zulip.git
synced 2025-10-24 16:43:57 +00:00
integrations: Add webhook code, API endpoint, and tests for papertrail
This commit is contained in:
BIN
static/images/integrations/logos/papertrail.png
Normal file
BIN
static/images/integrations/logos/papertrail.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 63 KiB |
@@ -1,30 +0,0 @@
|
||||
{
|
||||
"min_id": "740953742001864725",
|
||||
"max_id": "740954160207527942",
|
||||
"counts": [
|
||||
{
|
||||
"source_name": "Jains-Mac-Pro.local",
|
||||
"source_id": 561977853,
|
||||
"timeseries": {
|
||||
"1480594360": 3,
|
||||
"1480594368": 3,
|
||||
"1480594374": 2,
|
||||
"1480594377": 2,
|
||||
"1480594398": 2,
|
||||
"1480594401": 2,
|
||||
"1480594411": 2,
|
||||
"1480594422": 2,
|
||||
"1480594434": 2,
|
||||
"1480594460": 2
|
||||
}
|
||||
}
|
||||
],
|
||||
"saved_search": {
|
||||
"id": 15476333,
|
||||
"name": "Os x",
|
||||
"query": "program:com.apple.usbmuxd",
|
||||
"html_edit_url": "https:\/\/papertrailapp.com\/searches\/15476333\/edit",
|
||||
"html_search_url": "https:\/\/papertrailapp.com\/searches\/15476333"
|
||||
},
|
||||
"min_time_at": "2016-12-01T17:42:27+05:30"
|
||||
}
|
||||
78
zerver/fixtures/papertrail/papertrail_long_post.json
Normal file
78
zerver/fixtures/papertrail/papertrail_long_post.json
Normal file
@@ -0,0 +1,78 @@
|
||||
{
|
||||
"events": [
|
||||
{
|
||||
"id": 7711561783320576,
|
||||
"received_at": "2011-05-18T20:30:02-07:00",
|
||||
"display_received_at": "May 18 20:30:02",
|
||||
"source_ip": "208.75.57.121",
|
||||
"source_name": "abc",
|
||||
"source_id": 2,
|
||||
"hostname": "abc",
|
||||
"program": "CROND",
|
||||
"severity": "Info",
|
||||
"facility": "Cron",
|
||||
"message": "message body 1",
|
||||
},
|
||||
{
|
||||
"id": 7711561783320576,
|
||||
"received_at": "2011-05-18T20:30:02-07:00",
|
||||
"display_received_at": "May 18 20:30:02",
|
||||
"source_ip": "208.75.57.121",
|
||||
"source_name": "abc",
|
||||
"source_id": 2,
|
||||
"hostname": "abc",
|
||||
"program": "CROND",
|
||||
"severity": "Info",
|
||||
"facility": "Cron",
|
||||
"message": "message body 2",
|
||||
},
|
||||
{
|
||||
"id": 7711561783320576,
|
||||
"received_at": "2011-05-18T20:30:02-07:00",
|
||||
"display_received_at": "May 18 20:30:02",
|
||||
"source_ip": "208.75.57.121",
|
||||
"source_name": "abc",
|
||||
"source_id": 2,
|
||||
"hostname": "abc",
|
||||
"program": "CROND",
|
||||
"severity": "Info",
|
||||
"facility": "Cron",
|
||||
"message": "message body 3",
|
||||
},
|
||||
{
|
||||
"id": 7711561783320576,
|
||||
"received_at": "2011-05-18T20:30:02-07:00",
|
||||
"display_received_at": "May 18 20:30:02",
|
||||
"source_ip": "208.75.57.121",
|
||||
"source_name": "abc",
|
||||
"source_id": 2,
|
||||
"hostname": "abc",
|
||||
"program": "CROND",
|
||||
"severity": "Info",
|
||||
"facility": "Cron",
|
||||
"message": "message body 4",
|
||||
},
|
||||
{
|
||||
"id": 7711562567655424,
|
||||
"received_at": "2011-05-18T20:30:02-07:00",
|
||||
"display_received_at": "May 18 20:30:02",
|
||||
"source_ip": "208.75.57.120",
|
||||
"source_name": "server1",
|
||||
"source_id": 19,
|
||||
"hostname": "def",
|
||||
"program": "CROND",
|
||||
"severity": "Info",
|
||||
"facility": "Cron",
|
||||
"message": "A short event",
|
||||
}
|
||||
],
|
||||
"saved_search": {
|
||||
"id": 42,
|
||||
"name": "Important stuff",
|
||||
"query": "cron OR server1",
|
||||
"html_edit_url": "https://papertrailapp.com/searches/42/edit",
|
||||
"html_search_url": "https://papertrailapp.com/searches/42"
|
||||
},
|
||||
"max_id": 7711582041804800,
|
||||
"min_id": 7711561783320576
|
||||
}
|
||||
39
zerver/fixtures/papertrail/papertrail_short_post.json
Normal file
39
zerver/fixtures/papertrail/papertrail_short_post.json
Normal file
@@ -0,0 +1,39 @@
|
||||
{
|
||||
"events": [
|
||||
{
|
||||
"id": 7711561783320576,
|
||||
"received_at": "2011-05-18T20:30:02-07:00",
|
||||
"display_received_at": "May 18 20:30:02",
|
||||
"source_ip": "208.75.57.121",
|
||||
"source_name": "abc",
|
||||
"source_id": 2,
|
||||
"hostname": "abc",
|
||||
"program": "CROND",
|
||||
"severity": "Info",
|
||||
"facility": "Cron",
|
||||
"message": "message body",
|
||||
},
|
||||
{
|
||||
"id": 7711562567655424,
|
||||
"received_at": "2011-05-18T20:30:02-07:00",
|
||||
"display_received_at": "May 18 20:30:02",
|
||||
"source_ip": "208.75.57.120",
|
||||
"source_name": "server1",
|
||||
"source_id": 19,
|
||||
"hostname": "def",
|
||||
"program": "CROND",
|
||||
"severity": "Info",
|
||||
"facility": "Cron",
|
||||
"message": "A short event",
|
||||
}
|
||||
],
|
||||
"saved_search": {
|
||||
"id": 42,
|
||||
"name": "Important stuff",
|
||||
"query": "cron OR server1",
|
||||
"html_edit_url": "https://papertrailapp.com/searches/42/edit",
|
||||
"html_search_url": "https://papertrailapp.com/searches/42"
|
||||
},
|
||||
"max_id": 7711582041804800,
|
||||
"min_id": 7711561783320576
|
||||
}
|
||||
@@ -123,6 +123,7 @@ WEBHOOK_INTEGRATIONS = [
|
||||
WebhookIntegration('mention', display_name='Mention'),
|
||||
WebhookIntegration('newrelic', display_name='New Relic'),
|
||||
WebhookIntegration('pagerduty'),
|
||||
WebhookIntegration('papertrail'),
|
||||
WebhookIntegration('pingdom'),
|
||||
WebhookIntegration('pivotal', display_name='Pivotal Tracker'),
|
||||
WebhookIntegration('semaphore'),
|
||||
|
||||
46
zerver/tests/webhooks/test_papertrail.py
Normal file
46
zerver/tests/webhooks/test_papertrail.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from zerver.lib.test_classes import WebhookTestCase
|
||||
from six import text_type
|
||||
|
||||
class PapertrailHookTests(WebhookTestCase):
|
||||
STREAM_NAME = 'papertrail'
|
||||
URL_TEMPLATE = "/api/v1/external/papertrail?&api_key={api_key}"
|
||||
FIXTURE_DIR_NAME = 'papertrail'
|
||||
|
||||
# Note: Include a test function per each distinct message condition your integration supports
|
||||
def test_short_message(self):
|
||||
# type: () -> None
|
||||
expected_subject = u"logs"
|
||||
expected_message = u'''**"Important stuff"** search found **2** matches - https://papertrailapp.com/searches/42
|
||||
```
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body
|
||||
May 18 20:30:02 server1 cron OR server1:
|
||||
A short event
|
||||
```'''
|
||||
|
||||
# use fixture named papertrail_logs
|
||||
self.send_and_test_stream_message('short_post', expected_subject, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def test_long_message(self):
|
||||
# type: () -> None
|
||||
expected_subject = u"logs"
|
||||
expected_message = u'''**"Important stuff"** search found **5** matches - https://papertrailapp.com/searches/42
|
||||
```
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 1
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 2
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 3
|
||||
May 18 20:30:02 abc cron OR server1:
|
||||
message body 4
|
||||
```
|
||||
[See more](https://papertrailapp.com/searches/42)'''
|
||||
# use fixture named papertrail_logs
|
||||
self.send_and_test_stream_message('long_post', expected_subject, expected_message,
|
||||
content_type="application/x-www-form-urlencoded")
|
||||
|
||||
def get_body(self, fixture_name):
|
||||
# type: (text_type) -> text_type
|
||||
return self.fixture_data("papertrail", fixture_name, file_type="json")
|
||||
47
zerver/views/webhooks/papertrail.py
Normal file
47
zerver/views/webhooks/papertrail.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from __future__ import absolute_import
|
||||
from django.utils.translation import ugettext as _
|
||||
from zerver.lib.actions import check_send_message
|
||||
from zerver.lib.response import json_success, json_error
|
||||
from zerver.decorator import REQ, has_request_variables, api_key_only_webhook_view
|
||||
from zerver.lib.validator import check_dict, check_string
|
||||
|
||||
from zerver.models import Client, UserProfile
|
||||
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from six import text_type
|
||||
from typing import Dict, Any, Iterable, Optional
|
||||
|
||||
@api_key_only_webhook_view('Papertrail')
|
||||
@has_request_variables
|
||||
def api_papertrail_webhook(request, user_profile, client,
|
||||
payload=REQ(argument_type='body'),
|
||||
stream=REQ(default='papertrail'),
|
||||
topic=REQ(default='logs')):
|
||||
# type: (HttpRequest, UserProfile, Client, Dict[str, Any], text_type, text_type) -> HttpResponse
|
||||
|
||||
# construct the message of the message
|
||||
try:
|
||||
message_template = '**"{}"** search found **{}** matches - {}\n```'
|
||||
message = [message_template.format(payload["saved_search"]["name"],
|
||||
str(len(payload["events"])),
|
||||
payload["saved_search"]["html_search_url"])]
|
||||
for i, event in enumerate(payload["events"]):
|
||||
event_text = '{} {} {}:\n {}'.format(event["display_received_at"],
|
||||
event["source_name"],
|
||||
payload["saved_search"]["query"],
|
||||
event["message"])
|
||||
message.append(event_text)
|
||||
if i >= 3:
|
||||
message.append('```\n[See more]({})'.format(payload["saved_search"]["html_search_url"]))
|
||||
break
|
||||
else:
|
||||
message.append('```')
|
||||
post = '\n'.join(message)
|
||||
except KeyError as e:
|
||||
return json_error(_("Missing key {} in JSON").format(str(e)))
|
||||
|
||||
# send the message
|
||||
check_send_message(user_profile, client, 'stream', [stream], topic, post)
|
||||
|
||||
# return json result
|
||||
return json_success()
|
||||
Reference in New Issue
Block a user