-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monitor backups with PagerDuty hook integration (#245).
- Loading branch information
Showing
9 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import datetime | ||
import json | ||
import logging | ||
import platform | ||
|
||
import requests | ||
|
||
from borgmatic.hooks import monitor | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
EVENTS_API_URL = 'https://events.pagerduty.com/v2/enqueue' | ||
|
||
|
||
def ping_monitor(integration_key, config_filename, state, monitoring_log_level, dry_run): | ||
''' | ||
If this is an error state, create a PagerDuty event with the given integration key. Use the | ||
given configuration filename in any log entries. If this is a dry run, then don't actually | ||
create an event. | ||
''' | ||
if state != monitor.State.FAIL: | ||
logger.debug( | ||
'{}: Ignoring unsupported monitoring {} in PagerDuty hook'.format( | ||
config_filename, state.name.lower() | ||
) | ||
) | ||
return | ||
|
||
dry_run_label = ' (dry run; not actually sending)' if dry_run else '' | ||
logger.info('{}: Sending failure event to PagerDuty {}'.format(config_filename, dry_run_label)) | ||
|
||
if dry_run: | ||
return | ||
|
||
hostname = platform.node() | ||
local_timestamp = ( | ||
datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).astimezone().isoformat() | ||
) | ||
payload = json.dumps( | ||
{ | ||
'routing_key': integration_key, | ||
'event_action': 'trigger', | ||
'payload': { | ||
'summary': 'backup failed on {}'.format(hostname), | ||
'severity': 'error', | ||
'source': hostname, | ||
'timestamp': local_timestamp, | ||
'component': 'borgmatic', | ||
'group': 'backups', | ||
'class': 'backup failure', | ||
'custom_details': { | ||
'hostname': hostname, | ||
'configuration filename': config_filename, | ||
'server time': local_timestamp, | ||
}, | ||
}, | ||
} | ||
) | ||
logger.debug('{}: Using PagerDuty payload: {}'.format(config_filename, payload)) | ||
|
||
logging.getLogger('urllib3').setLevel(logging.ERROR) | ||
requests.post(EVENTS_API_URL, data=payload.encode('utf-8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from flexmock import flexmock | ||
|
||
from borgmatic.hooks import pagerduty as module | ||
|
||
|
||
def test_ping_monitor_ignores_start_state(): | ||
flexmock(module.requests).should_receive('post').never() | ||
|
||
module.ping_monitor( | ||
'abc123', 'config.yaml', module.monitor.State.START, monitoring_log_level=1, dry_run=False | ||
) | ||
|
||
|
||
def test_ping_monitor_ignores_finish_state(): | ||
flexmock(module.requests).should_receive('post').never() | ||
|
||
module.ping_monitor( | ||
'abc123', 'config.yaml', module.monitor.State.FINISH, monitoring_log_level=1, dry_run=False | ||
) | ||
|
||
|
||
def test_ping_monitor_calls_api_for_fail_state(): | ||
flexmock(module.requests).should_receive('post') | ||
|
||
module.ping_monitor( | ||
'abc123', 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=False | ||
) | ||
|
||
|
||
def test_ping_monitor_dry_run_does_not_call_api(): | ||
flexmock(module.requests).should_receive('post').never() | ||
|
||
module.ping_monitor( | ||
'abc123', 'config.yaml', module.monitor.State.FAIL, monitoring_log_level=1, dry_run=True | ||
) |