This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AzPostgresLogRetentionDaysEvent event plugin
AzPostgresLogRetentionDaysEvent plugin evaluates Azure Postgres instance and generates an event of type `postgres_log_retention_days_event` if the `log_retention_days` configuration is set below the desired minimum value. The minimum value for this configuration can be set using the `_min_log_retention_days` argument and defaults to 3 days. Enabling log_retention_days helps PostgreSQL Database to Sets number of days a log file is retained which in turn generates query and error logs.
- Loading branch information
1 parent
3930bbe
commit 5cbc90e
Showing
4 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
"""Microsoft Azure Postgres Log Retention Days event. | ||
This module defines the :class:`AzPostgresLogRetentionDaysEvent` class | ||
that identifies Postgre SQL servers which have log retention days set | ||
below the desired minimum value. This plugin works on the properties | ||
found in the ``com`` bucket of ``postgresql_server`` records. | ||
""" | ||
|
||
|
||
import logging | ||
|
||
from cloudmarker import util | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
|
||
class AzPostgresLogRetentionDaysEvent: | ||
"""Az Postgres log retention days event plugin.""" | ||
|
||
def __init__(self, _min_log_retention_days=3): | ||
"""Create instance of :class:`AzPostgresLogRetentionDaysEvent`.""" | ||
self._min_log_retention_days = _min_log_retention_days | ||
_log.info("Initialized; minimum log retention days: %d", | ||
self._min_log_retention_days) | ||
|
||
def eval(self, record): | ||
"""Evaluate Postgres for log retention days. | ||
Arguments: | ||
record (dict): An RDBMS record. | ||
_min_log_retention_days (int): Minimum required log retention days. | ||
Yields: | ||
dict: An event record representing a Postgres where | ||
log retention days is set below desired minimum | ||
""" | ||
com = record.get('com', {}) | ||
if com is None: | ||
return | ||
|
||
if com.get('cloud_type') != 'azure': | ||
return | ||
|
||
if com.get('record_type') != 'rdbms': | ||
return | ||
|
||
ext = record.get('ext', {}) | ||
if ext is None: | ||
return | ||
|
||
if ext.get('record_type') != 'postgresql_server': | ||
return | ||
|
||
# True, None, missing key or any other value will not | ||
# genarated an event. An event will be generated only if | ||
# the value of `postgresql_server` is False. | ||
log_retention_days = ext.get('log_retention_days') | ||
|
||
if log_retention_days < self._min_log_retention_days: | ||
yield from _get_postgres_log_retention_days_event( | ||
com, ext, self._min_log_retention_days) | ||
|
||
def done(self): | ||
"""Perform cleanup work. | ||
Currently, this method does nothing. This may change in future. | ||
""" | ||
|
||
|
||
def _get_postgres_log_retention_days_event(com, ext, min_log_retention_days): | ||
"""Generate event for Postgres log retention days set below minimum. | ||
Arguments: | ||
com (dict): Postgres record `com` bucket | ||
ext (dict): Postgres record `ext` bucket | ||
min_log_retention_days (int): Minimum log retention days | ||
Returns: | ||
dict: An event record representing Postgres server | ||
with log retention days set below desired minimum. | ||
""" | ||
friendly_cloud_type = util.friendly_string(com.get('cloud_type')) | ||
friendly_rdbms_type = util.friendly_string(ext.get('record_type')) | ||
|
||
reference = com.get('reference') | ||
description = ( | ||
'{} {} {} has log retention days set below desired minimum value.' | ||
.format(friendly_cloud_type, friendly_rdbms_type, reference) | ||
) | ||
recommendation = ( | ||
'Check {} {} {} and set log retention days to minimum of {} days.' | ||
.format(friendly_cloud_type, friendly_rdbms_type, reference, | ||
min_log_retention_days) | ||
) | ||
|
||
event_record = { | ||
# Preserve the extended properties from the RDBMS | ||
# record because they provide useful context to | ||
# locate the RDBMS that led to the event. | ||
'ext': util.merge_dicts(ext, { | ||
'record_type': 'postgres_log_retention_days_event' | ||
}), | ||
'com': { | ||
'cloud_type': com.get('cloud_type'), | ||
'record_type': 'postgres_log_retention_days_event', | ||
'reference': reference, | ||
'description': description, | ||
'recommendation': recommendation, | ||
} | ||
} | ||
|
||
_log.info('Generating postgres_log_retention_days_event; %r', | ||
event_record) | ||
yield event_record |
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