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

Periodically check when a node last checked in and submitted results #113

Merged
merged 5 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
undo #112 and reuse our existing alerting infrastructure
This commit undoes a lot of the work done in #112, and allows us to
reuse our existing rule manager and alerting infrastructure to also
alert when a new node is enrolled, rather than having to call a
specific method to get this functionality.
  • Loading branch information
mwielgoszewski committed Aug 17, 2017
commit e5d6bdc276da42f4325d9d1e8c5a011b30385c7b
4 changes: 0 additions & 4 deletions doorman/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ def handle_log_entry(self, entry, node):
for alerter, match in to_trigger:
self.alerters[alerter].handle_alert(node, match)

def handle_enroll(self, node):
for alerter in self.alerters.values():
alerter.handle_enroll(node)


def make_celery(app, celery):
""" From http://flask.pocoo.org/docs/0.10/patterns/celery/ """
Expand Down
4 changes: 0 additions & 4 deletions doorman/plugins/alerters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ class AbstractAlerterPlugin(with_metaclass(ABCMeta)):
@abstractmethod
def handle_alert(self, node, match):
raise NotImplementedError()

@abstractmethod
def handle_enroll(self, node):
raise NotImplementedError()
3 changes: 0 additions & 3 deletions doorman/plugins/alerters/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,3 @@ def __init__(self, config):
def handle_alert(self, node, match):
# TODO(andrew-d): better message?
current_app.logger.log(self.level, 'Triggered alert: {0!r}'.format(match))

def handle_enroll(self, node):
current_app.logger.log(self.level, 'Node enrolled: {0!r}'.format(node))
33 changes: 0 additions & 33 deletions doorman/plugins/alerters/emailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,3 @@ def handle_alert(self, node, match):
)

return mail.send(message)

def handle_enroll(self, node):
subject_template = self.config.setdefault(
'enroll_subject_template', 'email/enroll.subject.txt'
)
message_template = self.config.setdefault(
'enroll_message_template', 'email/enroll.body.txt'
)
subject_prefix = self.config.setdefault(
'enroll_subject_prefix', '[Doorman]'
)

subject = render_template(
subject_template,
prefix=subject_prefix,
timestamp=dt.datetime.utcnow(),
node=node
)

body = render_template(
message_template,
timestamp=dt.datetime.utcnow(),
node=node
)

message = Message(
subject.strip(),
recipients=self.recipients,
body=body,
charset='utf-8',
)

return mail.send(message)
3 changes: 0 additions & 3 deletions doorman/plugins/alerters/pagerduty.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,3 @@ def handle_alert(self, node, match):
self.logger.warn('Could not trigger PagerDuty alert!')

self.logger.debug('Response from PagerDuty: %r', resp.content)

def handle_enroll(self, node):
pass
3 changes: 0 additions & 3 deletions doorman/plugins/alerters/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,3 @@ def handle_alert(self, node, match):
'query': query,
},
)

def handle_enroll(self, node):
pass
35 changes: 29 additions & 6 deletions doorman/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,41 @@ def learn_from_result(result, node):
return


@celery.task()
def notify_of_node_enrollment(node):
current_app.rule_manager.handle_enroll(node)
return


@celery.task()
def example_task(one, two):
print('Adding {0} and {1}'.format(one, two))
return one + two


@celery.task()
def notify_of_node_enrollment(node):
'''
Create a result that gets run through our Rule Manager whenever a new
node is enrolled so that we may alert on this action.

A rule can be created within Doorman's rule manager to alert on
any of the following conditions:
- query name: doorman/tasks/node_enrolled
- action: triggered
- columns:
- enrolled_on
- last_ip
- node_id
'''
entry = {
'name': 'doorman/tasks/node_enrolled',
'calendarTime': dt.datetime.utcnow().strftime('%a %b %d %H:%M:%S %Y UTC'),
'action': 'triggered',
}
columns = entry['columns'] = {}
columns['enrolled_on'] = node.get('enrolled_on')
columns['last_ip'] = node.get('last_ip')
columns['node_id'] = node.get('id')
result = {'data': [entry]}
current_app.rule_manager.handle_log_entry(result, node)
return


@celery.task()
def alert_when_node_goes_offline():
'''
Expand Down
9 changes: 0 additions & 9 deletions doorman/templates/email/enroll.body.txt

This file was deleted.

1 change: 0 additions & 1 deletion doorman/templates/email/enroll.subject.txt

This file was deleted.

16 changes: 0 additions & 16 deletions tests/test_alerters.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,6 @@ def verify(message, app):
alerter = EmailAlerter(self.config)
alerter.handle_alert(node.to_dict(), match)

def test_will_email_on_node_enrollment(self, node, testapp):
from flask_mail import email_dispatched

expected_subject = '[Doorman Test] New node enrolled: {display_name}'.format(
display_name=node.display_name,
)

@email_dispatched.connect
def verify(message, app):
assert message.subject == expected_subject
assert self.recipients == message.recipients
assert 'A new node was enrolled:' in message.body

alerter = EmailAlerter(self.config)
alerter.handle_enroll(node.to_dict())


class TestSentryAlerter:

Expand Down
3 changes: 0 additions & 3 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,9 +1362,6 @@ def __init__(self, *args, **kwargs):
def handle_alert(self, node, match):
self.calls.append((node, match))

def handle_enroll(self, node):
self.calls.append((node, None))

dummy_alerter = DummyAlerter()

# This patches the appropriate config to create the 'dummy' alerter. This is a bit ugly :-(
Expand Down