Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dingtalk and aiops alert #3240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

add dingtalk and aiops alert #3240

wants to merge 1 commit into from

Conversation

netkey
Copy link

@netkey netkey commented Mar 9, 2022

add dingtalk and aiops alert

@nsano-rururu
Copy link
Contributor

Alert type not added to README.md and elastalert.rst

@nsano-rururu
Copy link
Contributor

The description of the alerter setting added to ruletypes.rst has not been added.

@nsano-rururu
Copy link
Contributor

nsano-rururu commented Mar 9, 2022

No test code has been added.

testcode example

from elastalert.alerts import DingTalkAlerter
from elastalert.util import EAException
from requests import RequestException


def test_dingtalk():
    rule = {
        'name': 'Test DingTalk Rule',
        'type': 'any',
        'dingtalk_webhook': 'http://xxxxxxxxxx/xxxxxxxx',
        'dingtalk_msgtype': 'text',
        'alert': []
    }
    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = DingTalkAlerter(rule)
    match = {
        '@timestamp': '2021-01-01T00:00:00',
        'somefield': 'foobarbaz'
    }
    with mock.patch('requests.post') as mock_post_request:
        alert.alert([match])
    expected_data = {
        'msgtype': 'text',
        'text': {'content': 'Test DingTalk Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n'},
        'at': {'isAtAll': False}
    }

    mock_post_request.assert_called_once_with(
        'http://xxxxxxxxxx/xxxxxxxx',
        data=mock.ANY,
        headers={
            'Content-Type': 'application/json',
            'Accept': 'application/json;charset=utf-8'
        },
    )

    actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
    assert expected_data == actual_data


def test_dingtalk_ea_exception():
    with pytest.raises(EAException) as ea:
        rule = {
            'name': 'Test DingTalk Rule',
            'type': 'any',
            'dingtalk_webhook': 'http://xxxxxxxxxx/xxxxxxxx',
            'dingtalk_msgtype': 'text',
            'alert': []
        }
        rules_loader = FileRulesLoader({})
        rules_loader.load_modules(rule)
        alert = DingTalkAlerter(rule)
        match = {
            '@timestamp': '2021-01-01T00:00:00',
            'somefield': 'foobarbaz'
        }
        mock_run = mock.MagicMock(side_effect=RequestException)
        with mock.patch('requests.post', mock_run), pytest.raises(RequestException):
            alert.alert([match])
    assert 'Error request to Dingtalk: ' in str(ea)


def test_dingtalk_getinfo():
    rule = {
        'name': 'Test DingTalk Rule',
        'type': 'any',
        'dingtalk_webhook': 'http://xxxxxxxxxx/xxxxxxxx',
        'dingtalk_msgtype': 'text',
        'alert': [],
        'alert_subject': 'Test DingTalk'
    }
    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = DingTalkAlerter(rule)

    expected_data = {
        'type': 'dingtalk',
        "dingtalk_webhook": 'http://xxxxxxxxxx/xxxxxxxx'
    }
    actual_data = alert.get_info()
    assert expected_data == actual_data


@pytest.mark.parametrize('dingtalk_webhook, dingtalk_msgtype, expected_data', [
    ('x', '', 'Missing required option(s): dingtalk_webhook, dingtalk_msgtype'),   
    ('', 'x', 'Missing required option(s): dingtalk_webhook, dingtalk_msgtype'),
])
def test_dingtalk_required_error(dingtalk_webhook, dingtalk_msgtype, expected_data):
    try:
        rule = {
            'name': 'Test DingTalk Rule',
            'type': 'any',
            'alert': [],
            'alert_subject': 'Test DingTalk'
        }

        if dingtalk_webhook:
            rule['dingtalk_webhook'] = dingtalk_webhook
        if dingtalk_msgtype:
            rule['dingtalk_msgtype'] = dingtalk_msgtype

        rules_loader = FileRulesLoader({})
        rules_loader.load_modules(rule)
        alert = DingTalkAlerter(rule)

        actual_data = alert.get_info()
        assert expected_data == actual_data
    except Exception as ea:
        assert expected_data in str(ea)

@nsano-rururu
Copy link
Contributor

lint error

./elastalert/alerts.py:2188:1: E302 expected 2 blank lines, found 1
./elastalert/alerts.py:2214:26: E231 missing whitespace after ':'
./elastalert/alerts.py:2219:25: E128 continuation line under-indented for visual indent
./elastalert/alerts.py:2220:25: E128 continuation line under-indented for visual indent
./elastalert/alerts.py:2232:1: E302 expected 2 blank lines, found 1
./elastalert/alerts.py:2253:1: W191 indentation contains tabs
./elastalert/alerts.py:2253:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2254:1: W191 indentation contains tabs
./elastalert/alerts.py:2254:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2255:1: W191 indentation contains tabs
./elastalert/alerts.py:2255:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2257:1: W191 indentation contains tabs
./elastalert/alerts.py:2257:1: E101 indentation contains mixed spaces and tabs
./elastalert/alerts.py:2261:25: E128 continuation line under-indented for visual indent
./elastalert/alerts.py:2262:25: E128 continuation line under-indented for visual indent
./elastalert/loaders.py:82:37: W291 trailing whitespace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants