Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
Add AzPostgresLogRetentionDaysEvent event plugin
Browse files Browse the repository at this point in the history
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
mitprasoon committed Feb 29, 2020
1 parent 3930bbe commit 5cbc90e
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cloudmarker/baseconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
azpostgresconnectionthrottlingevent:
plugin: cloudmarker.events.azpostgresconnectionthrottlingevent.AzPostgresConnectionThrottlingEvent
azpostgreslogretentiondaysevent:
plugin: cloudmarker.events.azpostgreslogretentiondaysevent.AzPostgresLogRetentionDaysEvent
mockevent:
plugin: cloudmarker.events.mockevent.MockEvent
Expand Down
115 changes: 115 additions & 0 deletions cloudmarker/events/azpostgreslogretentiondaysevent.py
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
8 changes: 8 additions & 0 deletions docs/api/cloudmarker.events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ cloudmarker.events.azpostgresconnectionthrottlingevent module
:undoc-members:
:show-inheritance:

cloudmarker.events.azpostgreslogretentiondaysevent module
---------------------------------------------------------

.. automodule:: cloudmarker.events.azpostgreslogretentiondaysevent
:members:
:undoc-members:
:show-inheritance:

cloudmarker.events.azsqldatabasetdeevent module
-----------------------------------------------

Expand Down
5 changes: 5 additions & 0 deletions pylama.ini
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,8 @@ ignore = R0201
ignore = R0201

# R0201 Method could be a function [pylint]

[pylama:cloudmarker/events/azpostgreslogretentiondaysevent.py]
ignore = R0201

# R0201 Method could be a function [pylint]

0 comments on commit 5cbc90e

Please sign in to comment.