# pkanderi-abio/monitoring architecture

> Architecture diagram of the GitHub repository pkanderi-abio/monitoring, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pkanderi-abio/monitoring
- Repository: https://github.com/pkanderi-abio/monitoring

## Overview

#### Purpose and Entry Points
This repository implements a containerized real-time MDR/XDR pipeline: ingest host and AWS CloudTrail activity, stream events through Kafka, detect threats, distribute alerts, and expose operational/dashboard views. The Python service starts through `app/main.py`; HTTP/WebSocket endpoints are defined in `app/api.py`. Deployment topology is defined by `docker-compose.yml`.

#### Event Ingestion and Streaming
`app/monitoring.py` watches the configured local authentication log and collects cloud events using configured AWS credentials. It normalizes incoming records and hands them to Kafka publishing logic in `app/kafka_handler.py`. Kafka is the central decoupling boundary between producers and the detection consumer, allowing event sources and analysis throughput to scale independently.
- `app/monitoring.py`
- `app/kafka_handler.py`
- `docker-compose.yml`

#### Detection Pipeline
The Kafka consumer receives events and invokes the detection orchestration in `app/detection.py`. Detection combines known-pattern/rule checks, Isolation Forest anomaly analysis, and compliance-oriented checks such as privileged-access and policy violations. This module is the primary decision boundary: benign events end processing; detected events become alert records.
- `app/detection.py`
- `app/kafka_handler.py`
- `app/utils.py`

#### Alert Persistence and Notifications
Detected threats flow into alert handling in `app/alerting.py`, which persists alert history to the configured JSON file and sends external notifications. Slack webhook delivery and SMTP email are controlled through environment-backed settings in `app/config.py`. The persisted alert file is a cross-service handoff consumed by Logstash.
- `app/alerting.py`
- `app/config.py`
- `data/alerts.json`

#### Dashboard and Real-Time API
The Python backend serves the monitoring dashboard and exposes alert data/control channels, including WebSocket-based real-time updates. The browser UI is implemented as a server-rendered template, while API behavior and connection management reside in the application layer.
- `app/api.py`
- `app/templates/dashboard.html`
- `app/main.py`

#### Observability and ELK Export
The service emits Prometheus metrics for detection/alert activity; Prometheus scraping is configured in `prometheus.yml`. Logstash tails or reads the persisted alert stream and indexes it into Elasticsearch, with Kibana providing downstream search and visualization. This creates two observability paths: Prometheus for service health/metrics and ELK for alert investigation.
- `prometheus.yml`
- `logstash.conf`
- `docker-compose.yml`

#### Runtime and Infrastructure Boundaries
The primary runtime is a Python application packaged by `Dockerfile`. Docker Compose provisions the application alongside Kafka/Zookeeper, Elasticsearch, Logstash, Kibana, and Prometheus. External dependencies include AWS CloudTrail APIs, Slack webhooks, and an SMTP server; secrets and endpoint configuration originate from `.env` based on `.env.example`.
- `Dockerfile`
- `docker-compose.yml`
- `.env.example`

Main entry points: [`app/alerting.py`](https://github.com/pkanderi-abio/monitoring/blob/master/app/alerting.py) (Alert handling), [`docker-compose.yml`](https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml) (Docker Compose), [`app/kafka_handler.py`](https://github.com/pkanderi-abio/monitoring/blob/master/app/kafka_handler.py) (Kafka producer/consumer), [`app/main.py`](https://github.com/pkanderi-abio/monitoring/blob/master/app/main.py) (Python runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["Python service"]
  node_runtime["Python runtime<br/>service entry point<br/>[main.py]"]
  node_api["HTTP &amp; WebSocket API<br/>application API<br/>[api.py]"]
  node_dashboard["Monitoring dashboard<br/>server-rendered UI<br/>[dashboard.html]"]
  node_alerting["Alert handling<br/>alert service<br/>[alerting.py]"]
  node_settings["Environment settings<br/>configuration boundary<br/>[config.py]"]
  node_alert_store[("Alert history<br/>JSON alert store<br/>[alerts.json]")]
end

subgraph group_stream["Event pipeline"]
  node_monitoring["Host &amp; CloudTrail ingestion<br/>event collector<br/>[monitoring.py]"]
  node_kafka_handler["Kafka producer/consumer<br/>stream adapter<br/>[kafka_handler.py]"]
  node_kafka["Kafka<br/>event broker<br/>[docker-compose.yml]"]
  node_detection["Threat detection<br/>decision engine<br/>[detection.py]"]
  node_detection_utils["Detection utilities<br/>analysis support<br/>[utils.py]"]
end

subgraph group_observability["Observability"]
  node_metrics["Prometheus metrics<br/>metrics endpoint<br/>[main.py]"]
  node_prometheus[("Prometheus<br/>metrics collector<br/>[prometheus.yml]")]
  node_logstash["Logstash<br/>alert export<br/>[logstash.conf]"]
  node_elasticsearch[("Elasticsearch<br/>alert index<br/>[docker-compose.yml]")]
  node_kibana["Kibana<br/>investigation UI<br/>[docker-compose.yml]"]
end

subgraph group_external["External services"]
  node_aws(("AWS CloudTrail APIs<br/>external event source"))
  node_slack(("Slack webhook<br/>external notification"))
  node_smtp(("SMTP server<br/>external notification"))
end

node_compose{{"Docker Compose<br/>deployment topology<br/>[docker-compose.yml]"}}

node_compose -->|"runs"| node_runtime
node_compose -->|"provisions"| node_kafka
node_compose -->|"provisions"| node_elasticsearch
node_compose -->|"provisions"| node_kibana
node_runtime -->|"serves"| node_api
node_api -->|"renders"| node_dashboard
node_api -->|"reads alerts"| node_alert_store
node_aws -->|"CloudTrail events"| node_monitoring
node_monitoring -->|"normalized events"| node_kafka_handler
node_kafka_handler -->|"publishes"| node_kafka
node_kafka -->|"consumes"| node_kafka_handler
node_kafka_handler -->|"events"| node_detection
node_detection -->|"analysis helpers"| node_detection_utils
node_detection -->|"detected threats"| node_alerting
node_alerting -->|"persists"| node_alert_store
node_settings -.->|"notification settings"| node_alerting
node_alerting -->|"webhook alert"| node_slack
node_alerting -->|"email alert"| node_smtp
node_runtime -->|"emits"| node_metrics
node_prometheus -->|"scrapes"| node_metrics
node_alert_store -->|"alert stream"| node_logstash
node_logstash -->|"indexes"| node_elasticsearch
node_kibana -->|"searches"| node_elasticsearch

click node_compose "https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml"
click node_runtime "https://github.com/pkanderi-abio/monitoring/blob/master/app/main.py"
click node_api "https://github.com/pkanderi-abio/monitoring/blob/master/app/api.py"
click node_dashboard "https://github.com/pkanderi-abio/monitoring/blob/master/app/templates/dashboard.html"
click node_monitoring "https://github.com/pkanderi-abio/monitoring/blob/master/app/monitoring.py"
click node_kafka_handler "https://github.com/pkanderi-abio/monitoring/blob/master/app/kafka_handler.py"
click node_kafka "https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml"
click node_detection "https://github.com/pkanderi-abio/monitoring/blob/master/app/detection.py"
click node_detection_utils "https://github.com/pkanderi-abio/monitoring/blob/master/app/utils.py"
click node_alerting "https://github.com/pkanderi-abio/monitoring/blob/master/app/alerting.py"
click node_settings "https://github.com/pkanderi-abio/monitoring/blob/master/app/config.py"
click node_alert_store "https://github.com/pkanderi-abio/monitoring/blob/master/data/alerts.json"
click node_metrics "https://github.com/pkanderi-abio/monitoring/blob/master/app/main.py"
click node_prometheus "https://github.com/pkanderi-abio/monitoring/blob/master/prometheus.yml"
click node_logstash "https://github.com/pkanderi-abio/monitoring/blob/master/logstash.conf"
click node_elasticsearch "https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml"
click node_kibana "https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_runtime,node_api,node_dashboard,node_alerting,node_settings,node_alert_store toneBlue
class node_monitoring,node_kafka_handler,node_kafka,node_detection,node_detection_utils toneAmber
class node_metrics,node_prometheus,node_logstash,node_elasticsearch,node_kibana toneMint
class node_aws,node_slack,node_smtp toneRose
class node_compose toneNeutral
```

## Components

### Python service

Containerized MDR/XDR application.

- [Python runtime](https://github.com/pkanderi-abio/monitoring/blob/master/app/main.py): service entry point · `app/main.py`
- [HTTP & WebSocket API](https://github.com/pkanderi-abio/monitoring/blob/master/app/api.py): application API · `app/api.py`
- [Monitoring dashboard](https://github.com/pkanderi-abio/monitoring/blob/master/app/templates/dashboard.html): server-rendered UI · `app/templates/dashboard.html`
- [Alert handling](https://github.com/pkanderi-abio/monitoring/blob/master/app/alerting.py): alert service · Persists detected threats and sends notifications. · `app/alerting.py`
- [Environment settings](https://github.com/pkanderi-abio/monitoring/blob/master/app/config.py): configuration boundary · Provides notification credentials and endpoints. · `app/config.py`
- [Alert history](https://github.com/pkanderi-abio/monitoring/blob/master/data/alerts.json): JSON alert store · Cross-service handoff for alert investigation. · `data/alerts.json`

### Event pipeline

Decoupled ingestion and threat detection.

- [Host & CloudTrail ingestion](https://github.com/pkanderi-abio/monitoring/blob/master/app/monitoring.py): event collector · Watches authentication logs and collects AWS activity. · `app/monitoring.py`
- [Kafka producer/consumer](https://github.com/pkanderi-abio/monitoring/blob/master/app/kafka_handler.py): stream adapter · `app/kafka_handler.py`
- [Kafka](https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml): event broker · Decouples event producers from detection throughput. · `docker-compose.yml`
- [Threat detection](https://github.com/pkanderi-abio/monitoring/blob/master/app/detection.py): decision engine · Runs rules, anomaly analysis, and compliance checks. · `app/detection.py`
- [Detection utilities](https://github.com/pkanderi-abio/monitoring/blob/master/app/utils.py): analysis support · `app/utils.py`

### Observability

Metrics and alert investigation paths.

- [Prometheus metrics](https://github.com/pkanderi-abio/monitoring/blob/master/app/main.py): metrics endpoint · `app/main.py`
- [Prometheus](https://github.com/pkanderi-abio/monitoring/blob/master/prometheus.yml): metrics collector · `prometheus.yml`
- [Logstash](https://github.com/pkanderi-abio/monitoring/blob/master/logstash.conf): alert export · `logstash.conf`
- [Elasticsearch](https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml): alert index · `docker-compose.yml`
- [Kibana](https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml): investigation UI · `docker-compose.yml`

### External services

- **AWS CloudTrail APIs**: external event source
- **Slack webhook**: external notification
- **SMTP server**: external notification

### Other components

- [Docker Compose](https://github.com/pkanderi-abio/monitoring/blob/master/docker-compose.yml): deployment topology · Provisions the application and supporting services. · `docker-compose.yml`

## Connections

- Docker Compose → Python runtime: runs
- Docker Compose → Kafka: provisions
- Docker Compose → Elasticsearch: provisions
- Docker Compose → Kibana: provisions
- Python runtime → HTTP & WebSocket API: serves
- HTTP & WebSocket API → Monitoring dashboard: renders
- HTTP & WebSocket API → Alert history: reads alerts
- AWS CloudTrail APIs → Host & CloudTrail ingestion: CloudTrail events
- Host & CloudTrail ingestion → Kafka producer/consumer: normalized events
- Kafka producer/consumer → Kafka: publishes
- Kafka → Kafka producer/consumer: consumes
- Kafka producer/consumer → Threat detection: events
- Threat detection → Detection utilities: analysis helpers
- Threat detection → Alert handling: detected threats
- Alert handling → Alert history: persists
- Environment settings → Alert handling: notification settings
- Alert handling → Slack webhook: webhook alert
- Alert handling → SMTP server: email alert
- Python runtime → Prometheus metrics: emits
- Prometheus → Prometheus metrics: scrapes
- Alert history → Logstash: alert stream
- Logstash → Elasticsearch: indexes
- Kibana → Elasticsearch: searches
