Skip to content

Commit

Permalink
feat: implement stale host notification
Browse files Browse the repository at this point in the history
  • Loading branch information
jpramos123 committed Dec 17, 2024
1 parent 532b677 commit a83a41d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,8 @@ def log_patch_staleness_succeeded(logger, staleness_id):

def log_create_staleness_failed(logger, org_id):
logger.info("Failed to create staleness for account with org_id %s", org_id)


# stale host notification
def log_host_stale_notification_succeeded(logger, host_id, control_rule):
logger.info("Sent Notification for stale host: %s", host_id, extra={"access_rule": control_rule})
61 changes: 61 additions & 0 deletions stale_host_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import sys
from functools import partial

from sqlalchemy import or_

from app.auth.identity import create_mock_identity_with_org_id
from app.auth.identity import to_auth_header
from app.instrumentation import log_host_stale_notification_succeeded
from app.logging import get_logger
from app.logging import threadctx
from app.models import Host
from app.queue.host_mq import OperationResult
from app.queue.notifications import NotificationType
from app.queue.notifications import send_notification
from jobs.common import excepthook
from jobs.common import find_hosts_in_state
from jobs.common import main

LOGGER_NAME = "stale_host_notification"
PROMETHEUS_JOB = "inventory-stale-host-notification"
COLLECTED_METRICS = (
# add metric
)


def _create_host_operation_result(host, identity, logger):
return OperationResult(
host,
{"b64_identity": to_auth_header(identity)} if identity else None,
None,
None,
None,
partial(log_host_stale_notification_succeeded, logger, host.id, control_rule="HOST_STALE_NOTIFICATION"),
)


def run(config, logger, session, event_producer, notification_event_producer, shutdown_handler, application):
with application.app.app_context():
filter_stale_hosts = find_hosts_in_state(logger, session, ["stale_in"], config)

query = session.query(Host).filter(or_(False, *filter_stale_hosts))
stale_hosts = query.all()
if len(stale_hosts) > 0:
for host in stale_hosts:
identity = create_mock_identity_with_org_id(host.org_id)
result = _create_host_operation_result(host, identity, logger)
send_notification(
notification_event_producer, NotificationType.system_became_stale, vars(result.host_row)
)


if __name__ == "__main__":
logger = get_logger(LOGGER_NAME)
job_type = "Stale host notification"
sys.excepthook = partial(excepthook, logger, job_type)

threadctx.request_id = None
config, logger, session, event_producer, notification_event_producer, shutdown_handler, application = main(
logger, COLLECTED_METRICS, PROMETHEUS_JOB
)
run(config, logger, session, event_producer, notification_event_producer, shutdown_handler, application)

0 comments on commit a83a41d

Please sign in to comment.